Skip to content

Commit

Permalink
New screen capture code.
Browse files Browse the repository at this point in the history
  • Loading branch information
octalmage committed May 15, 2016
1 parent b58962f commit fdbbff9
Showing 1 changed file with 30 additions and 42 deletions.
72 changes: 30 additions & 42 deletions src/screengrab.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,39 @@ MMBitmapRef copyMMBitmapFromDisplayInRect(MMRect rect)
{
#if defined(IS_MACOSX)

size_t bytewidth;
uint8_t bitsPerPixel, bytesPerPixel;
//uint8_t *buffer;
MMBitmapRef bitmap = NULL;
uint8_t *buffer = NULL;
size_t bufferSize = 0;

CGDirectDisplayID displayID = CGMainDisplayID();

//Replacement for CGDisplayBitsPerPixel.
CGDisplayModeRef mode = CGDisplayCopyDisplayMode(displayID);
size_t depth = 0;

CFStringRef pixEnc = CGDisplayModeCopyPixelEncoding(mode);
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;

bitsPerPixel = (uint8_t) depth;
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. */
//rect.origin.y = CGDisplayPixelsHigh(displayID) - rect.origin.y - rect.size.height;

CGImageRef image = CGDisplayCreateImageForRect(displayID, CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height));

// Request access to the raw pixel data via the image's DataProvider.
CGDataProviderRef provider = CGImageGetDataProvider(image);
CFDataRef data = CGDataProviderCopyData(provider);

size_t width, height;
width = CGImageGetWidth(image);
height = CGImageGetHeight(image);
size_t bpp = CGImageGetBitsPerPixel(image) / 8;

uint8 *pixels = malloc(width * height * bpp);
memcpy(pixels, CFDataGetBytePtr(data), width * height * bpp);
CFRelease(data);
CGImageRelease(image);

return createMMBitmap(pixels, rect.size.width, rect.size.height, bytewidth,
bitsPerPixel, bytesPerPixel);
CGImageRef image = CGDisplayCreateImageForRect(displayID,
CGRectMake(rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height));

CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(image));

bufferSize = CFDataGetLength(imageData);
buffer = malloc(bufferSize);

CFDataGetBytes(imageData, CFRangeMake(0,bufferSize), buffer);

/* Use image size because it can differ from specified size */
bitmap = createMMBitmap(buffer,
CGImageGetWidth(image),
CGImageGetHeight(image),
CGImageGetBytesPerRow(image),
CGImageGetBitsPerPixel(image),
CGImageGetBitsPerPixel(image) / 8);

CFRelease(imageData);

CGImageRelease(image);

return bitmap;

#elif defined(USE_X11)
MMBitmapRef bitmap;

Expand Down

0 comments on commit fdbbff9

Please sign in to comment.