Subject: Fix for bitmap distortion on Mac OS; thanks to Marc Culler for patch

From: Kevin Walzer <[email-masked]>
Date: Fri, 8 Jul 2016 01:21:02 +0000

---
 macosx/tkMacOSXDraw.c | 49 ++++++++++++++++++++++++++-----------------------
 1 file changed, 26 insertions(+), 23 deletions(-)

diff --git a/macosx/tkMacOSXDraw.c b/macosx/tkMacOSXDraw.c
index 6a0b409..d9b909b 100644
--- a/macosx/tkMacOSXDraw.c
+++ b/macosx/tkMacOSXDraw.c
@@ -314,29 +314,35 @@ XCopyPlane(
 		TkpClipMask *clipPtr = (TkpClipMask *) gc->clip_mask;
 		unsigned long imageBackground  = gc->background;
                 if (clipPtr && clipPtr->type == TKP_CLIP_PIXMAP){
-			CGImageRef mask = TkMacOSXCreateCGImageWithDrawable(clipPtr->value.pixmap);
-			CGRect rect = CGRectMake(dest_x, dest_y, width, height);
-			rect = CGRectOffset(rect, dstDraw->xOff, dstDraw->yOff);
-			CGContextSaveGState(context);
-			/* Move the origin of the destination to top left. */
-			CGContextTranslateCTM(context, 0, rect.origin.y + CGRectGetMaxY(rect));
-			CGContextScaleCTM(context, 1, -1);
-			/* Fill with the background color, clipping to the mask. */
-			CGContextClipToMask(context, rect, mask);
-			TkMacOSXSetColorInContext(gc, gc->background, dc.context);
-			CGContextFillRect(dc.context, rect);
-			/* Fill with the foreground color, clipping to the intersection of img and mask. */
-			CGContextClipToMask(context, rect, img);
-			TkMacOSXSetColorInContext(gc, gc->foreground, context);
-			CGContextFillRect(context, rect);
-			CGContextRestoreGState(context);
-			CGImageRelease(mask);
-			CGImageRelease(img);
+		    CGRect srcRect = CGRectMake(src_x, src_y, width, height);
+		    CGImageRef mask = TkMacOSXCreateCGImageWithDrawable(clipPtr->value.pixmap);
+		    CGImageRef submask = CGImageCreateWithImageInRect(img, srcRect);
+		    CGRect rect = CGRectMake(dest_x, dest_y, width, height);
+		    rect = CGRectOffset(rect, dstDraw->xOff, dstDraw->yOff);
+		    CGContextSaveGState(context);
+		    /* Move the origin of the destination to top left. */
+		    CGContextTranslateCTM(context, 0, rect.origin.y + CGRectGetMaxY(rect));
+		    CGContextScaleCTM(context, 1, -1);
+		    /* Fill with the background color, clipping to the mask. */
+		    CGContextClipToMask(context, rect, submask);
+		    TkMacOSXSetColorInContext(gc, gc->background, dc.context);
+		    CGContextFillRect(context, rect);
+		    /* Fill with the foreground color, clipping to the
+		       intersection of img and mask. */
+		    CGImageRef subimage = CGImageCreateWithImageInRect(img, srcRect);
+		    CGContextClipToMask(context, rect, subimage);
+		    TkMacOSXSetColorInContext(gc, gc->foreground, context);
+		    CGContextFillRect(context, rect);
+		    CGContextRestoreGState(context);
+		    CGImageRelease(img);
+		    CGImageRelease(mask);
+		    CGImageRelease(submask);
+		    CGImageRelease(subimage);
 		} else {
 		    DrawCGImage(dst, gc, dc.context, img, gc->foreground, imageBackground,
 				CGRectMake(0, 0, srcDraw->size.width, srcDraw->size.height),
-			    CGRectMake(src_x, src_y, width, height),
-			    CGRectMake(dest_x, dest_y, width, height));
+				CGRectMake(src_x, src_y, width, height),
+				CGRectMake(dest_x, dest_y, width, height));
 		    CGImageRelease(img);
 		}
 	    } else { /* no image */
@@ -786,9 +792,6 @@ DrawCGImage(
 	CGContextDrawImage(context, dstBounds, image);
 	CGContextRestoreGState(context);
 #endif /* TK_MAC_DEBUG_IMAGE_DRAWING */
-	/*if (CGImageIsMask(image)) {
-	    CGContextRestoreGState(context);
-	}*/
 	if (subImage) {
 	    CFRelease(subImage);
 	}
-- 
cgit v0.12