-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
display.c
915 lines (750 loc) · 23.3 KB
/
display.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/*
* The Python Imaging Library.
*
* display support (and other windows-related stuff)
*
* History:
* 1996-05-13 fl Windows DIB support
* 1996-05-21 fl Added palette stuff
* 1996-05-28 fl Added display_mode stuff
* 1997-09-21 fl Added draw primitive
* 2001-09-17 fl Added ImagingGrabScreen (from _grabscreen.c)
* 2002-05-12 fl Added ImagingListWindows
* 2002-11-19 fl Added clipboard support
* 2002-11-25 fl Added GetDC/ReleaseDC helpers
* 2003-05-21 fl Added create window support (including window callback)
* 2003-09-05 fl Added fromstring/tostring methods
* 2009-03-14 fl Added WMF support (from pilwmf)
*
* Copyright (c) 1997-2003 by Secret Labs AB.
* Copyright (c) 1996-1997 by Fredrik Lundh.
*
* See the README file for information on usage and redistribution.
*/
#define PY_SSIZE_T_CLEAN
#include "Python.h"
#include "libImaging/Imaging.h"
/* -------------------------------------------------------------------- */
/* Windows DIB support */
#ifdef _WIN32
#include "libImaging/ImDib.h"
#if SIZEOF_VOID_P == 8
#define F_HANDLE "K"
#else
#define F_HANDLE "k"
#endif
typedef struct {
PyObject_HEAD ImagingDIB dib;
} ImagingDisplayObject;
static PyTypeObject ImagingDisplayType;
static ImagingDisplayObject *
_new(const char *mode, int xsize, int ysize) {
ImagingDisplayObject *display;
if (PyType_Ready(&ImagingDisplayType) < 0) {
return NULL;
}
display = PyObject_New(ImagingDisplayObject, &ImagingDisplayType);
if (display == NULL) {
return NULL;
}
display->dib = ImagingNewDIB(mode, xsize, ysize);
if (!display->dib) {
Py_DECREF(display);
return NULL;
}
return display;
}
static void
_delete(ImagingDisplayObject *display) {
if (display->dib) {
ImagingDeleteDIB(display->dib);
}
PyObject_Del(display);
}
static PyObject *
_expose(ImagingDisplayObject *display, PyObject *args) {
HDC hdc;
if (!PyArg_ParseTuple(args, F_HANDLE, &hdc)) {
return NULL;
}
ImagingExposeDIB(display->dib, hdc);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_draw(ImagingDisplayObject *display, PyObject *args) {
HDC hdc;
int dst[4];
int src[4];
if (!PyArg_ParseTuple(
args,
F_HANDLE "(iiii)(iiii)",
&hdc,
dst + 0,
dst + 1,
dst + 2,
dst + 3,
src + 0,
src + 1,
src + 2,
src + 3)) {
return NULL;
}
ImagingDrawDIB(display->dib, hdc, dst, src);
Py_INCREF(Py_None);
return Py_None;
}
extern Imaging
PyImaging_AsImaging(PyObject *op);
static PyObject *
_paste(ImagingDisplayObject *display, PyObject *args) {
Imaging im;
PyObject *op;
int xy[4];
xy[0] = xy[1] = xy[2] = xy[3] = 0;
if (!PyArg_ParseTuple(args, "O|(iiii)", &op, xy + 0, xy + 1, xy + 2, xy + 3)) {
return NULL;
}
im = PyImaging_AsImaging(op);
if (!im) {
return NULL;
}
if (xy[2] <= xy[0]) {
xy[2] = xy[0] + im->xsize;
}
if (xy[3] <= xy[1]) {
xy[3] = xy[1] + im->ysize;
}
ImagingPasteDIB(display->dib, im, xy);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_query_palette(ImagingDisplayObject *display, PyObject *args) {
HDC hdc;
int status;
if (!PyArg_ParseTuple(args, F_HANDLE, &hdc)) {
return NULL;
}
status = ImagingQueryPaletteDIB(display->dib, hdc);
return Py_BuildValue("i", status);
}
static PyObject *
_getdc(ImagingDisplayObject *display, PyObject *args) {
HWND window;
HDC dc;
if (!PyArg_ParseTuple(args, F_HANDLE, &window)) {
return NULL;
}
dc = GetDC(window);
if (!dc) {
PyErr_SetString(PyExc_OSError, "cannot create dc");
return NULL;
}
return Py_BuildValue(F_HANDLE, dc);
}
static PyObject *
_releasedc(ImagingDisplayObject *display, PyObject *args) {
HWND window;
HDC dc;
if (!PyArg_ParseTuple(args, F_HANDLE F_HANDLE, &window, &dc)) {
return NULL;
}
ReleaseDC(window, dc);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_frombytes(ImagingDisplayObject *display, PyObject *args) {
Py_buffer buffer;
if (!PyArg_ParseTuple(args, "y*:frombytes", &buffer)) {
return NULL;
}
if (display->dib->ysize * display->dib->linesize != buffer.len) {
PyBuffer_Release(&buffer);
PyErr_SetString(PyExc_ValueError, "wrong size");
return NULL;
}
memcpy(display->dib->bits, buffer.buf, buffer.len);
PyBuffer_Release(&buffer);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
_tobytes(ImagingDisplayObject *display, PyObject *args) {
if (!PyArg_ParseTuple(args, ":tobytes")) {
return NULL;
}
return PyBytes_FromStringAndSize(
display->dib->bits, display->dib->ysize * display->dib->linesize);
}
static struct PyMethodDef methods[] = {
{"draw", (PyCFunction)_draw, METH_VARARGS},
{"expose", (PyCFunction)_expose, METH_VARARGS},
{"paste", (PyCFunction)_paste, METH_VARARGS},
{"query_palette", (PyCFunction)_query_palette, METH_VARARGS},
{"getdc", (PyCFunction)_getdc, METH_VARARGS},
{"releasedc", (PyCFunction)_releasedc, METH_VARARGS},
{"frombytes", (PyCFunction)_frombytes, METH_VARARGS},
{"tobytes", (PyCFunction)_tobytes, METH_VARARGS},
{NULL, NULL} /* sentinel */
};
static PyObject *
_getattr_mode(ImagingDisplayObject *self, void *closure) {
return Py_BuildValue("s", self->dib->mode);
}
static PyObject *
_getattr_size(ImagingDisplayObject *self, void *closure) {
return Py_BuildValue("ii", self->dib->xsize, self->dib->ysize);
}
static struct PyGetSetDef getsetters[] = {
{"mode", (getter)_getattr_mode}, {"size", (getter)_getattr_size}, {NULL}};
static PyTypeObject ImagingDisplayType = {
PyVarObject_HEAD_INIT(NULL, 0) "ImagingDisplay", /*tp_name*/
sizeof(ImagingDisplayObject), /*tp_basicsize*/
0, /*tp_itemsize*/
/* methods */
(destructor)_delete, /*tp_dealloc*/
0, /*tp_vectorcall_offset*/
0, /*tp_getattr*/
0, /*tp_setattr*/
0, /*tp_as_async*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
0, /*tp_hash*/
0, /*tp_call*/
0, /*tp_str*/
0, /*tp_getattro*/
0, /*tp_setattro*/
0, /*tp_as_buffer*/
Py_TPFLAGS_DEFAULT, /*tp_flags*/
0, /*tp_doc*/
0, /*tp_traverse*/
0, /*tp_clear*/
0, /*tp_richcompare*/
0, /*tp_weaklistoffset*/
0, /*tp_iter*/
0, /*tp_iternext*/
methods, /*tp_methods*/
0, /*tp_members*/
getsetters, /*tp_getset*/
};
PyObject *
PyImaging_DisplayWin32(PyObject *self, PyObject *args) {
ImagingDisplayObject *display;
char *mode;
int xsize, ysize;
if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize)) {
return NULL;
}
display = _new(mode, xsize, ysize);
if (display == NULL) {
return NULL;
}
return (PyObject *)display;
}
PyObject *
PyImaging_DisplayModeWin32(PyObject *self, PyObject *args) {
char *mode;
int size[2];
mode = ImagingGetModeDIB(size);
return Py_BuildValue("s(ii)", mode, size[0], size[1]);
}
/* -------------------------------------------------------------------- */
/* Windows screen grabber */
typedef HANDLE(__stdcall *Func_SetThreadDpiAwarenessContext)(HANDLE);
PyObject *
PyImaging_GrabScreenWin32(PyObject *self, PyObject *args) {
int x = 0, y = 0, width, height;
int includeLayeredWindows = 0, all_screens = 0;
HBITMAP bitmap;
BITMAPCOREHEADER core;
HDC screen, screen_copy;
DWORD rop;
PyObject *buffer;
HANDLE dpiAwareness;
HMODULE user32;
Func_SetThreadDpiAwarenessContext SetThreadDpiAwarenessContext_function;
if (!PyArg_ParseTuple(args, "|ii", &includeLayeredWindows, &all_screens)) {
return NULL;
}
/* step 1: create a memory DC large enough to hold the
entire screen */
screen = CreateDC("DISPLAY", NULL, NULL, NULL);
screen_copy = CreateCompatibleDC(screen);
// added in Windows 10 (1607)
// loaded dynamically to avoid link errors
user32 = LoadLibraryA("User32.dll");
SetThreadDpiAwarenessContext_function =
(Func_SetThreadDpiAwarenessContext)GetProcAddress(
user32, "SetThreadDpiAwarenessContext");
if (SetThreadDpiAwarenessContext_function != NULL) {
// DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = ((DPI_CONTEXT_HANDLE)-3)
dpiAwareness = SetThreadDpiAwarenessContext_function((HANDLE)-3);
}
if (all_screens) {
x = GetSystemMetrics(SM_XVIRTUALSCREEN);
y = GetSystemMetrics(SM_YVIRTUALSCREEN);
width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
} else {
width = GetDeviceCaps(screen, HORZRES);
height = GetDeviceCaps(screen, VERTRES);
}
if (SetThreadDpiAwarenessContext_function != NULL) {
SetThreadDpiAwarenessContext_function(dpiAwareness);
}
FreeLibrary(user32);
bitmap = CreateCompatibleBitmap(screen, width, height);
if (!bitmap) {
goto error;
}
if (!SelectObject(screen_copy, bitmap)) {
goto error;
}
/* step 2: copy bits into memory DC bitmap */
rop = SRCCOPY;
if (includeLayeredWindows) {
rop |= CAPTUREBLT;
}
if (!BitBlt(screen_copy, 0, 0, width, height, screen, x, y, rop)) {
goto error;
}
/* step 3: extract bits from bitmap */
buffer = PyBytes_FromStringAndSize(NULL, height * ((width * 3 + 3) & -4));
if (!buffer) {
return NULL;
}
core.bcSize = sizeof(core);
core.bcWidth = width;
core.bcHeight = height;
core.bcPlanes = 1;
core.bcBitCount = 24;
if (!GetDIBits(
screen_copy,
bitmap,
0,
height,
PyBytes_AS_STRING(buffer),
(BITMAPINFO *)&core,
DIB_RGB_COLORS)) {
goto error;
}
DeleteObject(bitmap);
DeleteDC(screen_copy);
DeleteDC(screen);
return Py_BuildValue("(ii)(ii)N", x, y, width, height, buffer);
error:
PyErr_SetString(PyExc_OSError, "screen grab failed");
DeleteDC(screen_copy);
DeleteDC(screen);
return NULL;
}
/* -------------------------------------------------------------------- */
/* Windows clipboard grabber */
PyObject *
PyImaging_GrabClipboardWin32(PyObject *self, PyObject *args) {
HANDLE handle = NULL;
int size;
void *data;
PyObject *result;
UINT format;
UINT formats[] = {CF_DIB, CF_DIBV5, CF_HDROP, RegisterClipboardFormatA("PNG"), 0};
LPCSTR format_names[] = {"DIB", "DIB", "file", "png", NULL};
if (!OpenClipboard(NULL)) {
// Maybe the clipboard is temporarily in use by another process.
// Wait and try again
Sleep(500);
if (!OpenClipboard(NULL)) {
PyErr_SetString(PyExc_OSError, "failed to open clipboard");
return NULL;
}
}
// find best format as set by clipboard owner
format = 0;
while (!handle && (format = EnumClipboardFormats(format))) {
for (UINT i = 0; formats[i] != 0; i++) {
if (format == formats[i]) {
handle = GetClipboardData(format);
format = i;
break;
}
}
}
if (!handle) {
CloseClipboard();
return Py_BuildValue("zO", NULL, Py_None);
}
data = GlobalLock(handle);
size = GlobalSize(handle);
result = PyBytes_FromStringAndSize(data, size);
GlobalUnlock(handle);
CloseClipboard();
return Py_BuildValue("zN", format_names[format], result);
}
/* -------------------------------------------------------------------- */
/* Windows class */
#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL 522
#endif
static int mainloop = 0;
static void
callback_error(const char *handler) {
PyObject *sys_stderr;
sys_stderr = PySys_GetObject("stderr");
if (sys_stderr) {
PyFile_WriteString("*** ImageWin: error in ", sys_stderr);
PyFile_WriteString((char *)handler, sys_stderr);
PyFile_WriteString(":\n", sys_stderr);
}
PyErr_Print();
PyErr_Clear();
}
static LRESULT CALLBACK
windowCallback(HWND wnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
PyObject *callback = NULL;
PyObject *result;
PyThreadState *threadstate;
PyThreadState *current_threadstate;
HDC dc;
RECT rect;
LRESULT status = 0;
/* set up threadstate for messages that calls back into python */
switch (message) {
case WM_CREATE:
mainloop++;
break;
case WM_DESTROY:
mainloop--;
/* fall through... */
case WM_PAINT:
case WM_SIZE:
callback = (PyObject *)GetWindowLongPtr(wnd, 0);
if (callback) {
threadstate =
(PyThreadState *)GetWindowLongPtr(wnd, sizeof(PyObject *));
current_threadstate = PyThreadState_Swap(NULL);
PyEval_RestoreThread(threadstate);
} else {
return DefWindowProc(wnd, message, wParam, lParam);
}
}
/* process message */
switch (message) {
case WM_PAINT:
/* redraw (part of) window. this generates a WCK-style
damage/clear/repair cascade */
BeginPaint(wnd, &ps);
dc = GetDC(wnd);
GetWindowRect(wnd, &rect); /* in screen coordinates */
result = PyObject_CallFunction(
callback,
"siiii",
"damage",
ps.rcPaint.left,
ps.rcPaint.top,
ps.rcPaint.right,
ps.rcPaint.bottom);
if (result) {
Py_DECREF(result);
} else {
callback_error("window damage callback");
}
result = PyObject_CallFunction(
callback,
"s" F_HANDLE "iiii",
"clear",
dc,
0,
0,
rect.right - rect.left,
rect.bottom - rect.top);
if (result) {
Py_DECREF(result);
} else {
callback_error("window clear callback");
}
result = PyObject_CallFunction(
callback,
"s" F_HANDLE "iiii",
"repair",
dc,
0,
0,
rect.right - rect.left,
rect.bottom - rect.top);
if (result) {
Py_DECREF(result);
} else {
callback_error("window repair callback");
}
ReleaseDC(wnd, dc);
EndPaint(wnd, &ps);
break;
case WM_SIZE:
/* resize window */
result = PyObject_CallFunction(
callback, "sii", "resize", LOWORD(lParam), HIWORD(lParam));
if (result) {
InvalidateRect(wnd, NULL, 1);
Py_DECREF(result);
} else {
callback_error("window resize callback");
}
break;
case WM_DESTROY:
/* destroy window */
result = PyObject_CallFunction(callback, "s", "destroy");
if (result) {
Py_DECREF(result);
} else {
callback_error("window destroy callback");
}
Py_DECREF(callback);
break;
default:
status = DefWindowProc(wnd, message, wParam, lParam);
}
if (callback) {
/* restore thread state */
PyEval_SaveThread();
PyThreadState_Swap(threadstate);
}
return status;
}
PyObject *
PyImaging_CreateWindowWin32(PyObject *self, PyObject *args) {
HWND wnd;
WNDCLASS windowClass;
char *title;
PyObject *callback;
int width = 0, height = 0;
if (!PyArg_ParseTuple(args, "sO|ii", &title, &callback, &width, &height)) {
return NULL;
}
if (width <= 0) {
width = CW_USEDEFAULT;
}
if (height <= 0) {
height = CW_USEDEFAULT;
}
/* register toplevel window class */
windowClass.style = CS_CLASSDC;
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra = sizeof(PyObject *) + sizeof(PyThreadState *);
windowClass.hInstance = GetModuleHandle(NULL);
/* windowClass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1); */
windowClass.hbrBackground = NULL;
windowClass.lpszMenuName = NULL;
windowClass.lpszClassName = "pilWindow";
windowClass.lpfnWndProc = windowCallback;
windowClass.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(1));
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); /* CROSS? */
RegisterClass(&windowClass); /* FIXME: check return status */
wnd = CreateWindowEx(
0,
windowClass.lpszClassName,
title,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
width,
height,
HWND_DESKTOP,
NULL,
NULL,
NULL);
if (!wnd) {
PyErr_SetString(PyExc_OSError, "failed to create window");
return NULL;
}
/* register window callback */
Py_INCREF(callback);
SetWindowLongPtr(wnd, 0, (LONG_PTR)callback);
SetWindowLongPtr(wnd, sizeof(callback), (LONG_PTR)PyThreadState_Get());
Py_BEGIN_ALLOW_THREADS ShowWindow(wnd, SW_SHOWNORMAL);
SetForegroundWindow(wnd); /* to make sure it's visible */
Py_END_ALLOW_THREADS
return Py_BuildValue(F_HANDLE, wnd);
}
PyObject *
PyImaging_EventLoopWin32(PyObject *self, PyObject *args) {
MSG msg;
Py_BEGIN_ALLOW_THREADS while (mainloop && GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Py_END_ALLOW_THREADS
Py_INCREF(Py_None);
return Py_None;
}
/* -------------------------------------------------------------------- */
/* windows WMF renderer */
#define GET32(p, o) ((DWORD *)(p + o))[0]
PyObject *
PyImaging_DrawWmf(PyObject *self, PyObject *args) {
HBITMAP bitmap;
HENHMETAFILE meta;
BITMAPCOREHEADER core;
HDC dc;
RECT rect;
PyObject *buffer = NULL;
char *ptr;
char *data;
Py_ssize_t datasize;
int width, height;
int x0, y0, x1, y1;
if (!PyArg_ParseTuple(
args,
"y#(ii)(iiii):_load",
&data,
&datasize,
&width,
&height,
&x0,
&x1,
&y0,
&y1)) {
return NULL;
}
/* step 1: copy metafile contents into METAFILE object */
if (datasize > 22 && GET32(data, 0) == 0x9ac6cdd7) {
/* placeable windows metafile (22-byte aldus header) */
meta = SetWinMetaFileBits(datasize - 22, data + 22, NULL, NULL);
} else if (datasize > 80 && GET32(data, 0) == 1 && GET32(data, 40) == 0x464d4520) {
/* enhanced metafile */
meta = SetEnhMetaFileBits(datasize, data);
} else {
/* unknown meta format */
meta = NULL;
}
if (!meta) {
PyErr_SetString(PyExc_OSError, "cannot load metafile");
return NULL;
}
/* step 2: create bitmap */
core.bcSize = sizeof(core);
core.bcWidth = width;
core.bcHeight = height;
core.bcPlanes = 1;
core.bcBitCount = 24;
dc = CreateCompatibleDC(NULL);
bitmap = CreateDIBSection(dc, (BITMAPINFO *)&core, DIB_RGB_COLORS, &ptr, NULL, 0);
if (!bitmap) {
PyErr_SetString(PyExc_OSError, "cannot create bitmap");
goto error;
}
if (!SelectObject(dc, bitmap)) {
PyErr_SetString(PyExc_OSError, "cannot select bitmap");
goto error;
}
/* step 3: render metafile into bitmap */
rect.left = rect.top = 0;
rect.right = width;
rect.bottom = height;
/* FIXME: make background transparent? configurable? */
FillRect(dc, &rect, GetStockObject(WHITE_BRUSH));
if (!PlayEnhMetaFile(dc, meta, &rect)) {
PyErr_SetString(PyExc_OSError, "cannot render metafile");
goto error;
}
/* step 4: extract bits from bitmap */
GdiFlush();
buffer = PyBytes_FromStringAndSize(ptr, height * ((width * 3 + 3) & -4));
error:
DeleteEnhMetaFile(meta);
if (bitmap) {
DeleteObject(bitmap);
}
DeleteDC(dc);
return buffer;
}
#endif /* _WIN32 */
/* -------------------------------------------------------------------- */
/* X11 support */
#ifdef HAVE_XCB
#include <xcb/xcb.h>
/* -------------------------------------------------------------------- */
/* X11 screen grabber */
PyObject *
PyImaging_GrabScreenX11(PyObject *self, PyObject *args) {
int width, height;
char *display_name;
xcb_connection_t *connection;
int screen_number;
xcb_screen_iterator_t iter;
xcb_screen_t *screen = NULL;
xcb_get_image_reply_t *reply;
xcb_generic_error_t *error;
PyObject *buffer = NULL;
if (!PyArg_ParseTuple(args, "|z", &display_name)) {
return NULL;
}
/* connect to X and get screen data */
connection = xcb_connect(display_name, &screen_number);
if (xcb_connection_has_error(connection)) {
PyErr_Format(
PyExc_OSError,
"X connection failed: error %i",
xcb_connection_has_error(connection));
xcb_disconnect(connection);
return NULL;
}
iter = xcb_setup_roots_iterator(xcb_get_setup(connection));
for (; iter.rem; --screen_number, xcb_screen_next(&iter)) {
if (screen_number == 0) {
screen = iter.data;
break;
}
}
if (screen == NULL || screen->root == 0) {
// this case is usually caught with "X connection failed: error 6" above
xcb_disconnect(connection);
PyErr_SetString(PyExc_OSError, "X screen not found");
return NULL;
}
width = screen->width_in_pixels;
height = screen->height_in_pixels;
/* get image data */
reply = xcb_get_image_reply(
connection,
xcb_get_image(
connection,
XCB_IMAGE_FORMAT_Z_PIXMAP,
screen->root,
0,
0,
width,
height,
0x00ffffff),
&error);
if (reply == NULL) {
PyErr_Format(
PyExc_OSError,
"X get_image failed: error %i (%i, %i, %i)",
error->error_code,
error->major_code,
error->minor_code,
error->resource_id);
free(error);
xcb_disconnect(connection);
return NULL;
}
/* store data in Python buffer */
if (reply->depth == 24) {
buffer = PyBytes_FromStringAndSize(
(char *)xcb_get_image_data(reply), xcb_get_image_data_length(reply));
} else {
PyErr_Format(PyExc_OSError, "unsupported bit depth: %i", reply->depth);
}
free(reply);
xcb_disconnect(connection);
if (!buffer) {
return NULL;
}
return Py_BuildValue("(ii)N", width, height, buffer);
}
#endif /* HAVE_XCB */