@@ -354,7 +354,7 @@ def _set_argtypes(self):
354
354
self .xlib .XGetImage .argtypes = [POINTER (Display ), POINTER (Display ),
355
355
c_int , c_int , c_uint , c_uint , c_ulong ,
356
356
c_int ]
357
- #self.xlib.XGetPixel.argtypes = [POINTER(XImage), c_int, c_int]
357
+ # self.xlib.XGetPixel.argtypes = [POINTER(XImage), c_int, c_int]
358
358
self .xlib .XDestroyImage .argtypes = [POINTER (XImage )]
359
359
self .xlib .XCloseDisplay .argtypes = [POINTER (Display )]
360
360
self .xrandr .XRRGetScreenResources .argtypes = [POINTER (Display ),
@@ -380,7 +380,7 @@ def _set_restypes(self):
380
380
self .xlib .XGetWindowAttributes .restype = c_int
381
381
self .xlib .XAllPlanes .restype = c_ulong
382
382
self .xlib .XGetImage .restype = POINTER (XImage )
383
- #self.xlib.XGetPixel.restype = c_ulong
383
+ # self.xlib.XGetPixel.restype = c_ulong
384
384
self .xlib .XDestroyImage .restype = c_void_p
385
385
self .xlib .XCloseDisplay .restype = c_void_p
386
386
self .xlib .XDefaultRootWindow .restype = POINTER (XWindowAttributes )
@@ -421,7 +421,11 @@ def enum_display_monitors(self, screen=0):
421
421
self .xrandr .XRRFreeScreenResources (mon )
422
422
423
423
def get_pixels (self , monitor ):
424
- ''' Retrieve all pixels from a monitor. Pixels have to be RGB. '''
424
+ ''' Retrieve all pixels from a monitor. Pixels have to be RGB.
425
+
426
+ The XGetPixel() C code can be found at this URL:
427
+ http://cgit.freedesktop.org/xorg/lib/libX11/tree/src/ImUtil.c#n444
428
+ '''
425
429
426
430
width , height = monitor [b'width' ], monitor [b'height' ]
427
431
left , top = monitor [b'left' ], monitor [b'top' ]
@@ -438,6 +442,8 @@ def get_pixels(self, monitor):
438
442
raise ScreenshotError ('MSS: XGetImage() failed.' )
439
443
440
444
'''
445
+ C code as quick as XGetPixel() to translate into ctypes:
446
+
441
447
pixels = malloc(sizeof(unsigned char) * width * height * 3);
442
448
443
449
for ( x = 0; x < width; ++x )
@@ -450,13 +456,17 @@ def get_pixels(self, monitor):
450
456
pixels[x * 3 + offset + 1] = (pixel & ximage->green_mask) >> 8;
451
457
pixels[x * 3 + offset + 2] = pixel & ximage->blue_mask;
452
458
'''
453
- """from ctypes import create_string_buffer, c_char, sizeof
459
+ '''
460
+ # @TODO: see if it is quicker than using XGetPixel().
461
+ from ctypes import create_string_buffer, c_char, sizeof
454
462
rmask = ximage.contents.red_mask
455
463
gmask = ximage.contents.green_mask
456
464
bmask = ximage.contents.blue_mask
457
465
bpl = ximage.contents.bytes_per_line
458
- data = cast(ximage.contents.data, POINTER(c_char * width * height * 3)).contents
459
- self.image = create_string_buffer(sizeof(c_char) * width * height * 3)
466
+ buffer_len = width * height * 3
467
+ xdata = ximage.contents.data
468
+ data = cast(xdata, POINTER(c_char * buffer_len)).contents
469
+ self.image = create_string_buffer(sizeof(c_char) * buffer_len)
460
470
xrange = getattr(__builtins__, 'xrange', range)
461
471
for x in xrange(width):
462
472
for y in xrange(height):
@@ -466,10 +476,9 @@ def get_pixels(self, monitor):
466
476
self.image[x * 3 + offset] = (pixel & rmask) >> 16
467
477
self.image[x * 3 + offset + 1] = (pixel & gmask) >> 8
468
478
self.image[x * 3 + offset + 2] = pixel & bmask
469
- #~ self.image[x * 3 + offset:x * 3 + offset + 2] = \
470
- #~ (pixel & rmask) >> 16, (pixel & gmask) >> 8, pixel & bmask
479
+ return self.image
480
+ '''
471
481
472
- """
473
482
# @TODO: this part takes most of the time. Need a better solution.
474
483
def pix (pixel , _resultats = {}, b = pack ):
475
484
''' Apply shifts to a pixel to get the RGB values.
@@ -480,7 +489,6 @@ def pix(pixel, _resultats={}, b=pack):
480
489
b (b'<B' , (pixel & gmask ) >> 8 ) + b (b'<B' , pixel & bmask )
481
490
return _resultats [pixel ]
482
491
483
- # http://cgit.freedesktop.org/xorg/lib/libX11/tree/src/ImUtil.c#n444
484
492
rmask = ximage .contents .red_mask
485
493
gmask = ximage .contents .green_mask
486
494
bmask = ximage .contents .blue_mask
@@ -489,7 +497,6 @@ def pix(pixel, _resultats={}, b=pack):
489
497
pixels = [pix (get_pix (ximage , x , y ))
490
498
for y in xrange (height ) for x in xrange (width )]
491
499
self .image = b'' .join (pixels )
492
- #"""
493
500
494
501
self .xlib .XDestroyImage (ximage )
495
502
return self .image
0 commit comments