From b6eecdc141bb152220b41a6f296e74e74d492135 Mon Sep 17 00:00:00 2001 From: culler Date: Sat, 10 Aug 2024 03:54:01 +0000 Subject: [PATCH] Fix the rescaling code in XGetImage which is needed for Retina displays. --- macosx/tkMacOSXImage.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/macosx/tkMacOSXImage.c b/macosx/tkMacOSXImage.c index 99995288b..564cfff23 100644 --- a/macosx/tkMacOSXImage.c +++ b/macosx/tkMacOSXImage.c @@ -674,8 +674,6 @@ CreateCGImageFromDrawableRect( CGRect rect = CGRectMake(x + mac_drawable->xOff, y + mac_drawable->yOff, width, height); rect = CGRectApplyAffineTransform(rect, CGAffineTransformMakeScale(scaleFactor, scaleFactor)); - result = CGImageCreateWithImageInRect(cg_image, rect); - CGImageRelease(cg_image); if (force_1x_scale && (scaleFactor != 1.0)) { // See https://web.archive.org/web/20200219030756/http://blog.foundry376.com/2008/07/scaling-a-cgimage/#comment-200 // create context, keeping original image properties @@ -688,15 +686,21 @@ CreateCGImageFromDrawableRect( CGImageGetAlphaInfo(cg_image)); CGColorSpaceRelease(colorspace); if (cg_context) { - // draw image to context (resizing it) + // Extract the subimage in the specified rectangle. + CGImageRef subimage = CGImageCreateWithImageInRect(cg_image, rect); + // Draw the subimage in our context (resizing it to fit). CGContextDrawImage(cg_context, CGRectMake(0, 0, width, height), - cg_image); - // extract resulting image from context + subimage); + // We will return the image we just drew. result = CGBitmapContextCreateImage(cg_context); CGContextRelease(cg_context); + CGImageRelease(subimage); } - CGImageRelease(cg_image); + } else { + // No resizing is needed. Just return the subimage + result = CGImageCreateWithImageInRect(cg_image, rect); } + CGImageRelease(cg_image); } return result; }