@@ -19,51 +19,39 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
19
19
{
20
20
#if defined(IS_MACOSX )
21
21
22
- size_t bytewidth ;
23
- uint8_t bitsPerPixel , bytesPerPixel ;
24
- //uint8_t *buffer ;
22
+ MMBitmapRef bitmap = NULL ;
23
+ uint8_t * buffer = NULL ;
24
+ size_t bufferSize = 0 ;
25
25
26
26
CGDirectDisplayID displayID = CGMainDisplayID ();
27
27
28
- //Replacement for CGDisplayBitsPerPixel.
29
- CGDisplayModeRef mode = CGDisplayCopyDisplayMode (displayID );
30
- size_t depth = 0 ;
31
-
32
- CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding (mode );
33
- if (CFStringCompare (pixEnc , CFSTR (IO32BitDirectPixels ), kCFCompareCaseInsensitive ) == kCFCompareEqualTo )
34
- depth = 32 ;
35
- else if (CFStringCompare (pixEnc , CFSTR (IO16BitDirectPixels ), kCFCompareCaseInsensitive ) == kCFCompareEqualTo )
36
- depth = 16 ;
37
- else if (CFStringCompare (pixEnc , CFSTR (IO8BitIndexedPixels ), kCFCompareCaseInsensitive ) == kCFCompareEqualTo )
38
- depth = 8 ;
39
-
40
- bitsPerPixel = (uint8_t ) depth ;
41
- bytesPerPixel = bitsPerPixel / 8 ;
42
- /* Align width to padding. */
43
- //bytewidth = ADD_PADDING(rect.size.width * bytesPerPixel);
44
- bytewidth = rect .size .width * bytesPerPixel ;
45
-
46
- /* Convert Quartz point to postscript point. */
47
- //rect.origin.y = CGDisplayPixelsHigh(displayID) - rect.origin.y - rect.size.height;
48
-
49
- CGImageRef image = CGDisplayCreateImageForRect (displayID , CGRectMake (rect .origin .x , rect .origin .y , rect .size .width , rect .size .height ));
50
-
51
- // Request access to the raw pixel data via the image's DataProvider.
52
- CGDataProviderRef provider = CGImageGetDataProvider (image );
53
- CFDataRef data = CGDataProviderCopyData (provider );
54
-
55
- size_t width , height ;
56
- width = CGImageGetWidth (image );
57
- height = CGImageGetHeight (image );
58
- size_t bpp = CGImageGetBitsPerPixel (image ) / 8 ;
59
-
60
- uint8 * pixels = malloc (width * height * bpp );
61
- memcpy (pixels , CFDataGetBytePtr (data ), width * height * bpp );
62
- CFRelease (data );
63
- CGImageRelease (image );
64
-
65
- return createMMBitmap (pixels , rect .size .width , rect .size .height , bytewidth ,
66
- bitsPerPixel , bytesPerPixel );
28
+ CGImageRef image = CGDisplayCreateImageForRect (displayID ,
29
+ CGRectMake (rect .origin .x ,
30
+ rect .origin .y ,
31
+ rect .size .width ,
32
+ rect .size .height ));
33
+
34
+ CFDataRef imageData = CGDataProviderCopyData (CGImageGetDataProvider (image ));
35
+
36
+ bufferSize = CFDataGetLength (imageData );
37
+ buffer = malloc (bufferSize );
38
+
39
+ CFDataGetBytes (imageData , CFRangeMake (0 ,bufferSize ), buffer );
40
+
41
+ /* Use image size because it can differ from specified size */
42
+ bitmap = createMMBitmap (buffer ,
43
+ CGImageGetWidth (image ),
44
+ CGImageGetHeight (image ),
45
+ CGImageGetBytesPerRow (image ),
46
+ CGImageGetBitsPerPixel (image ),
47
+ CGImageGetBitsPerPixel (image ) / 8 );
48
+
49
+ CFRelease (imageData );
50
+
51
+ CGImageRelease (image );
52
+
53
+ return bitmap ;
54
+
67
55
#elif defined(USE_X11 )
68
56
MMBitmapRef bitmap ;
69
57
0 commit comments