diff options
author | culler <culler> | 2021-04-25 22:48:28 (GMT) |
---|---|---|
committer | culler <culler> | 2021-04-25 22:48:28 (GMT) |
commit | 45e129155eee3c174598146404e28b0cbdc02e5c (patch) | |
tree | 9722563957bdddae66c294bb892aa3d9db68b893 /macosx/tkMacOSXImage.c | |
parent | 312349dd711ba2bb4341ec16cc1b476351d6cbb9 (diff) | |
download | tk-45e129155eee3c174598146404e28b0cbdc02e5c.zip tk-45e129155eee3c174598146404e28b0cbdc02e5c.tar.gz tk-45e129155eee3c174598146404e28b0cbdc02e5c.tar.bz2 |
Code cleanup. Thanks to Christopher Chavez for the careful review.
Diffstat (limited to 'macosx/tkMacOSXImage.c')
-rw-r--r-- | macosx/tkMacOSXImage.c | 46 |
1 files changed, 22 insertions, 24 deletions
diff --git a/macosx/tkMacOSXImage.c b/macosx/tkMacOSXImage.c index 8160303..6f55171 100644 --- a/macosx/tkMacOSXImage.c +++ b/macosx/tkMacOSXImage.c @@ -529,44 +529,42 @@ CreateCGImageFromDrawableRect( MacDrawable *mac_drawable = (MacDrawable *)drawable; CGContextRef cg_context = NULL; CGRect image_rect = CGRectMake(x, y, width, height); - CGImageRef cg_image = nil, result = nil; + CGImageRef cg_image = NULL, result = NULL; + unsigned char *imageData = NULL; if (mac_drawable->flags & TK_IS_PIXMAP) { cg_context = TkMacOSXGetCGContextForDrawable(drawable); - cg_image = CGBitmapContextCreateImage((CGContextRef) cg_context); - if (cg_image) { - result = CGImageCreateWithImageInRect(cg_image, image_rect); - CGImageRelease(cg_image); + if (cg_context) { + cg_image = CGBitmapContextCreateImage((CGContextRef) cg_context); + CFRelease(cg_context); } } else { NSView *view = TkMacOSXGetNSViewForDrawable(mac_drawable); if (view == nil) { TkMacOSXDbgMsg("Invalid source drawable"); - return nil; + return NULL; } NSSize size = view.frame.size; NSUInteger width = size.width, height = size.height; NSUInteger bytesPerPixel = 4, bytesPerRow = bytesPerPixel * width, bitsPerComponent = 8; - unsigned char *rawData = malloc(height * bytesPerRow); - if (rawData) { - CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); - cg_context = CGBitmapContextCreate(rawData, width, height, - bitsPerComponent, bytesPerRow, colorSpace, - kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); - CFRelease(colorSpace); - if (cg_context) { - [view.layer renderInContext:cg_context]; - cg_image = CGBitmapContextCreateImage(cg_context); - CGContextRelease(cg_context); - } - free(rawData); - } - if (cg_image) { - result = CGImageCreateWithImageInRect(cg_image, image_rect); - CGImageRelease(cg_image); - } + imageData = ckalloc(height * bytesPerRow); + CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); + cg_context = CGBitmapContextCreate(imageData, width, height, + bitsPerComponent, bytesPerRow, colorSpace, + kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); + CFRelease(colorSpace); + [view.layer renderInContext:cg_context]; + } + if (cg_context) { + cg_image = CGBitmapContextCreateImage(cg_context); + CGContextRelease(cg_context); + } + if (cg_image) { + result = CGImageCreateWithImageInRect(cg_image, image_rect); + CGImageRelease(cg_image); } + ckfree(imageData); return result; } |