fix screen capture for Mac

This commit is contained in:
vCaesar 2016-10-20 17:41:25 +08:00
parent 815c7f0050
commit 6d3a330d9b

View File

@ -21,54 +21,41 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
{ {
#if defined(IS_MACOSX) #if defined(IS_MACOSX)
size_t bytewidth; MMBitmapRef bitmap = NULL;
uint8_t bitsPerPixel, bytesPerPixel; uint8_t *buffer = NULL;
//uint8_t *buffer; size_t bufferSize = 0;
CGDirectDisplayID displayID = CGMainDisplayID(); CGDirectDisplayID displayID = CGMainDisplayID();
//Replacement for CGDisplayBitsPerPixel. CGImageRef image = CGDisplayCreateImageForRect(displayID,
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayID); CGRectMake(rect.origin.x,
size_t depth = 0; rect.origin.y,
rect.size.width,
rect.size.height));
CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(mode); if (!image) { return NULL; }
if(CFStringCompare(pixEnc, CFSTR(IO32BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
depth = 32;
else if(CFStringCompare(pixEnc, CFSTR(IO16BitDirectPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
depth = 16;
else if(CFStringCompare(pixEnc, CFSTR(IO8BitIndexedPixels), kCFCompareCaseInsensitive) == kCFCompareEqualTo)
depth = 8;
CGDisplayModeRelease(mode); CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image));
CFRelease(pixEnc);
bitsPerPixel = (uint8_t) depth; if (!imageData) { return NULL; }
bytesPerPixel = bitsPerPixel / 8;
/* Align width to padding. */
//bytewidth = ADD_PADDING(rect.size.width * bytesPerPixel);
bytewidth = rect.size.width * bytesPerPixel;
/* Convert Quartz point to postscript point. */ bufferSize = CFDataGetLength(imageData);
//rect.origin.y = CGDisplayPixelsHigh(displayID) - rect.origin.y - rect.size.height; buffer = malloc(bufferSize);
CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height)); CFDataGetBytes(imageData, CFRangeMake(0,bufferSize), buffer);
// Request access to the raw pixel data via the image's DataProvider. bitmap = createMMBitmap(buffer,
CGDataProviderRef provider = CGImageGetDataProvider(image); CGImageGetWidth(image),
CFDataRef data = CGDataProviderCopyData(provider); CGImageGetHeight(image),
CGImageGetBytesPerRow(image),
CGImageGetBitsPerPixel(image),
CGImageGetBitsPerPixel(image) / 8);
size_t width, height; CFRelease(imageData);
width = CGImageGetWidth(image);
height = CGImageGetHeight(image);
size_t bpp = CGImageGetBitsPerPixel(image) / 8;
uint8 *pixels = malloc(width * height * bpp); CGImageRelease(image);
memcpy(pixels, CFDataGetBytePtr(data), width * height * bpp);
CFRelease(data);
CGImageRelease(image);
return createMMBitmap(pixels, rect.size.width, rect.size.height, bytewidth, return bitmap;
bitsPerPixel, bytesPerPixel);
#elif defined(USE_X11) #elif defined(USE_X11)
MMBitmapRef bitmap; MMBitmapRef bitmap;