diff options
author | culler <culler> | 2021-04-25 14:00:35 (GMT) |
---|---|---|
committer | culler <culler> | 2021-04-25 14:00:35 (GMT) |
commit | 55bde88d25c729979dd328b7084bcb6ee2498255 (patch) | |
tree | 99fcd5393409f7afe366cb304d681b5771bb7ef2 /macosx/tkMacOSXImage.c | |
parent | e3f627385b02a29326439ffe0159930466944f6a (diff) | |
download | tk-55bde88d25c729979dd328b7084bcb6ee2498255.zip tk-55bde88d25c729979dd328b7084bcb6ee2498255.tar.gz tk-55bde88d25c729979dd328b7084bcb6ee2498255.tar.bz2 |
Bonus! Use the new CALayer to make XCopyArea work again!
Diffstat (limited to 'macosx/tkMacOSXImage.c')
-rw-r--r-- | macosx/tkMacOSXImage.c | 66 |
1 files changed, 30 insertions, 36 deletions
diff --git a/macosx/tkMacOSXImage.c b/macosx/tkMacOSXImage.c index 69967af..1dc4613 100644 --- a/macosx/tkMacOSXImage.c +++ b/macosx/tkMacOSXImage.c @@ -528,50 +528,44 @@ CreateCGImageFromDrawableRect( { MacDrawable *mac_drawable = (MacDrawable *)drawable; CGContextRef cg_context = NULL; - CGImageRef cg_image = NULL, result = NULL; - NSBitmapImageRep *bitmapRep = nil; - NSView *view = nil; + CGRect image_rect = CGRectMake(x, y, width, height); + CGImageRef cg_image = nil, result = nil; if (mac_drawable->flags & TK_IS_PIXMAP) { - /* - * This MacDrawable is a bitmap, so its view is NULL. - */ - - CGRect image_rect = CGRectMake(x, y, width, height); - cg_context = TkMacOSXGetCGContextForDrawable(drawable); cg_image = CGBitmapContextCreateImage((CGContextRef) cg_context); if (cg_image) { result = CGImageCreateWithImageInRect(cg_image, image_rect); CGImageRelease(cg_image); } - } else if (TkMacOSXGetNSViewForDrawable(mac_drawable) != nil) { - - /* - * Convert Tk top-left to NSView bottom-left coordinates. - */ - - int view_height = [view bounds].size.height; - NSRect view_rect = NSMakeRect(x + mac_drawable->xOff, - view_height - height - y - mac_drawable->yOff, - width, height); - - /* - * Attempt to copy from the view to a bitmapImageRep. If the view does - * not have a valid CGContext, doing this will silently corrupt memory - * and make a big mess. So, in that case, we just return NULL. - */ - - if (view == [NSView focusView]) { - bitmapRep = [view bitmapImageRepForCachingDisplayInRect: view_rect]; - [view cacheDisplayInRect:view_rect toBitmapImageRep:bitmapRep]; - result = [bitmapRep CGImage]; - CFRelease(bitmapRep); - } else { - TkMacOSXDbgMsg("No CGContext - cannot copy from screen to bitmap."); - result = NULL; - } } else { - TkMacOSXDbgMsg("Invalid source drawable"); + NSView *view = TkMacOSXGetNSViewForDrawable(mac_drawable); + if (view == nil) { + TkMacOSXDbgMsg("Invalid source drawable"); + return nil; + } + 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); + } } return result; } |