Skip to content

Commit 672a5b9

Browse files
author
BoboTiG
committed
Production ready
1 parent 0aefa5d commit 672a5b9

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

mss.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ def _set_argtypes(self):
354354
self.xlib.XGetImage.argtypes = [POINTER(Display), POINTER(Display),
355355
c_int, c_int, c_uint, c_uint, c_ulong,
356356
c_int]
357-
#self.xlib.XGetPixel.argtypes = [POINTER(XImage), c_int, c_int]
357+
# self.xlib.XGetPixel.argtypes = [POINTER(XImage), c_int, c_int]
358358
self.xlib.XDestroyImage.argtypes = [POINTER(XImage)]
359359
self.xlib.XCloseDisplay.argtypes = [POINTER(Display)]
360360
self.xrandr.XRRGetScreenResources.argtypes = [POINTER(Display),
@@ -380,7 +380,7 @@ def _set_restypes(self):
380380
self.xlib.XGetWindowAttributes.restype = c_int
381381
self.xlib.XAllPlanes.restype = c_ulong
382382
self.xlib.XGetImage.restype = POINTER(XImage)
383-
#self.xlib.XGetPixel.restype = c_ulong
383+
# self.xlib.XGetPixel.restype = c_ulong
384384
self.xlib.XDestroyImage.restype = c_void_p
385385
self.xlib.XCloseDisplay.restype = c_void_p
386386
self.xlib.XDefaultRootWindow.restype = POINTER(XWindowAttributes)
@@ -421,7 +421,11 @@ def enum_display_monitors(self, screen=0):
421421
self.xrandr.XRRFreeScreenResources(mon)
422422

423423
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+
'''
425429

426430
width, height = monitor[b'width'], monitor[b'height']
427431
left, top = monitor[b'left'], monitor[b'top']
@@ -438,6 +442,8 @@ def get_pixels(self, monitor):
438442
raise ScreenshotError('MSS: XGetImage() failed.')
439443

440444
'''
445+
C code as quick as XGetPixel() to translate into ctypes:
446+
441447
pixels = malloc(sizeof(unsigned char) * width * height * 3);
442448
443449
for ( x = 0; x < width; ++x )
@@ -450,13 +456,17 @@ def get_pixels(self, monitor):
450456
pixels[x * 3 + offset + 1] = (pixel & ximage->green_mask) >> 8;
451457
pixels[x * 3 + offset + 2] = pixel & ximage->blue_mask;
452458
'''
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
454462
rmask = ximage.contents.red_mask
455463
gmask = ximage.contents.green_mask
456464
bmask = ximage.contents.blue_mask
457465
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)
460470
xrange = getattr(__builtins__, 'xrange', range)
461471
for x in xrange(width):
462472
for y in xrange(height):
@@ -466,10 +476,9 @@ def get_pixels(self, monitor):
466476
self.image[x * 3 + offset] = (pixel & rmask) >> 16
467477
self.image[x * 3 + offset + 1] = (pixel & gmask) >> 8
468478
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+
'''
471481

472-
"""
473482
# @TODO: this part takes most of the time. Need a better solution.
474483
def pix(pixel, _resultats={}, b=pack):
475484
''' Apply shifts to a pixel to get the RGB values.
@@ -480,7 +489,6 @@ def pix(pixel, _resultats={}, b=pack):
480489
b(b'<B', (pixel & gmask) >> 8) + b(b'<B', pixel & bmask)
481490
return _resultats[pixel]
482491

483-
# http://cgit.freedesktop.org/xorg/lib/libX11/tree/src/ImUtil.c#n444
484492
rmask = ximage.contents.red_mask
485493
gmask = ximage.contents.green_mask
486494
bmask = ximage.contents.blue_mask
@@ -489,7 +497,6 @@ def pix(pixel, _resultats={}, b=pack):
489497
pixels = [pix(get_pix(ximage, x, y))
490498
for y in xrange(height) for x in xrange(width)]
491499
self.image = b''.join(pixels)
492-
#"""
493500

494501
self.xlib.XDestroyImage(ximage)
495502
return self.image

0 commit comments

Comments
 (0)