From 95f2f06d4a33893fc8c582fc3eae6577f3a442f8 Mon Sep 17 00:00:00 2001
From: Christopher Chavez <chrischavez@gmx.us>
Date: Thu, 13 Aug 2020 20:10:52 -0500
Subject: [PATCH 4/6] Rename TkMacOSXBitmapRepFromDrawableRect()
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

…to TkMacOSXCreateCGImageFromDrawableRect() and return a CGImageRef
instead.
---
 macosx/tkMacOSXDraw.c    | 32 +++++++++++++-------------------
 macosx/tkMacOSXImage.c   |  9 +++++----
 macosx/tkMacOSXPrivate.h |  2 +-
 3 files changed, 19 insertions(+), 24 deletions(-)

diff --git macosx/tkMacOSXDraw.c macosx/tkMacOSXDraw.c
index 9d9dcadc6..2f0d3d458 100644
--- macosx/tkMacOSXDraw.c
+++ macosx/tkMacOSXDraw.c
@@ -108,29 +108,29 @@ TkMacOSXInitCGDrawing(
 /*
  *----------------------------------------------------------------------
  *
- * TkMacOSXBitmapRepFromDrawableRect
+ * TkMacOSXCreateCGImageFromDrawableRect
  *
- *	Extract bitmap data from a MacOSX drawable as an NSBitmapImageRep.
+ *	Extract bitmap data from a MacOSX drawable as a CGImage.
  *
  *      Currently only used by XGetImage and XCopyArea.
  *
  * Results:
- *	Returns an NSBitmapRep representing the image of the given rectangle of
- *      the given drawable. This object is retained. The caller is responsible
- *      for releasing it.
+ *	Returns a CGImageRef representing the image of the given rectangle of
+ *	the given drawable. The caller is responsible for releasing it.
+ *	If the CGImage couldn't be created, then NULL is returned.
  *
  *      NOTE: The x,y coordinates should be relative to a coordinate system
  *      with origin at the top left, as used by XImage and CGImage, not bottom
  *      left as used by NSView.
  *
  * Side effects:
- *     None
+ *	If successful, allocates a new CGImage.
  *
  *----------------------------------------------------------------------
  */
 
-NSBitmapImageRep *
-TkMacOSXBitmapRepFromDrawableRect(
+CGImageRef
+TkMacOSXCreateCGImageFromDrawableRect(
     Drawable drawable,
     int x,
     int y,
@@ -152,10 +152,6 @@ TkMacOSXBitmapRepFromDrawableRect(
 	cg_context = TkMacOSXGetCGContextForDrawable(drawable);
 	cg_image = CGBitmapContextCreateImage((CGContextRef) cg_context);
 	sub_cg_image = CGImageCreateWithImageInRect(cg_image, image_rect);
-	if (sub_cg_image) {
-	    bitmap_rep = [NSBitmapImageRep alloc];
-	    [bitmap_rep initWithCGImage:sub_cg_image];
-	}
 	if (cg_image) {
 	    CGImageRelease(cg_image);
 	}
@@ -189,15 +185,17 @@ TkMacOSXBitmapRepFromDrawableRect(
 	     */
 	    bitmap_rep = [[NSBitmapImageRep alloc]
 		    initWithFocusedViewRect:view_rect];
-	    [bitmap_rep retain];
 	}
 	if (needsToUnlockFocus) {
 	    [view unlockFocus];
 	}
+	sub_cg_image = [bitmap_rep CGImage];
+	CGImageRetain(sub_cg_image);
+	[bitmap_rep release];
     } else {
 	TkMacOSXDbgMsg("Invalid source drawable");
     }
-    return bitmap_rep;
+    return sub_cg_image;
 }
 
 /*
@@ -273,7 +271,6 @@ XCopyArea(
 {
     TkMacOSXDrawingContext dc;
     MacDrawable *srcDraw = (MacDrawable *) src;
-    NSBitmapImageRep *bitmap_rep = NULL;
     CGImageRef img = NULL;
     CGRect bounds, srcRect, dstRect;
 
@@ -295,11 +292,8 @@ XCopyArea(
     if (srcDraw->flags & TK_IS_PIXMAP) {
 	img = TkMacOSXCreateCGImageWithDrawable(src);
     } else if (TkMacOSXDrawableWindow(src)) {
-	bitmap_rep = TkMacOSXBitmapRepFromDrawableRect(src,
+	img = TkMacOSXCreateCGImageFromDrawableRect(src,
 		src_x, src_y, width, height);
-	if (bitmap_rep) {
-	    img = [bitmap_rep CGImage];
-	}
     } else {
 	TkMacOSXDbgMsg("Invalid source drawable - neither window nor pixmap.");
     }
diff --git macosx/tkMacOSXImage.c macosx/tkMacOSXImage.c
index 3604841ee..3c8530505 100644
--- macosx/tkMacOSXImage.c
+++ macosx/tkMacOSXImage.c
@@ -177,12 +177,13 @@ XGetImage(
 	    return NULL;
 	}
 
-	bitmap_rep = TkMacOSXBitmapRepFromDrawableRect(drawable,
-		x, y, width, height);
-	if (!bitmap_rep) {
-	    TkMacOSXDbgMsg("XGetImage: Failed to construct NSBitmapRep");
+	CGImageRef cg_image = TkMacOSXCreateCGImageFromDrawableRect(
+		drawable, x, y, width, height);
+	if (!cg_image) {
+	    TkMacOSXDbgMsg("XGetImage: Failed to get CGImage");
 	    return NULL;
 	}
+	bitmap_rep = [[NSBitmapImageRep alloc] initWithCGImage:cg_image];
 	bitmap_fmt = [bitmap_rep bitmapFormat];
 	size = [bitmap_rep bytesPerPlane];
 	bytes_per_row = [bitmap_rep bytesPerRow];
diff --git macosx/tkMacOSXPrivate.h macosx/tkMacOSXPrivate.h
index d8372c1e4..f3900ee33 100644
--- macosx/tkMacOSXPrivate.h
+++ macosx/tkMacOSXPrivate.h
@@ -267,7 +267,7 @@ MODULE_SCOPE WindowClass TkMacOSXWindowClass(TkWindow *winPtr);
 MODULE_SCOPE int	TkMacOSXIsWindowZoomed(TkWindow *winPtr);
 MODULE_SCOPE int	TkGenerateButtonEventForXPointer(Window window);
 MODULE_SCOPE EventModifiers TkMacOSXModifierState(void);
-MODULE_SCOPE NSBitmapImageRep* TkMacOSXBitmapRepFromDrawableRect(Drawable drawable,
+MODULE_SCOPE CGImageRef	TkMacOSXCreateCGImageFromDrawableRect(Drawable drawable,
 			    int x, int y, unsigned int width, unsigned int height);
 MODULE_SCOPE CGImageRef TkMacOSXCreateCGImageWithXImage(XImage *image);
 MODULE_SCOPE void       TkMacOSXDrawCGImage(Drawable d, GC gc, CGContextRef context,
-- 
2.28.0

