Tk Source Code

Artifact [bc710886]
Login

Artifact bc710886f3501a323978d29a2b2a5241c1d23258c21ed57e345eb5e203716b18:

Attachment "1d8b7124b622.diff" to ticket [1d8b7124] added by chrstphrchvz 2023-11-19 13:31:56.
diff --git generic/tkImgPhInstance.c generic/tkImgPhInstance.c
index 05fa40cba..3936de24f 100644
--- generic/tkImgPhInstance.c
+++ generic/tkImgPhInstance.c
@@ -461,13 +461,14 @@ BlendComplexAlpha(
      */
 
 #ifndef _WIN32
-    unsigned long red_mask, green_mask, blue_mask;
+    unsigned long red_mask, green_mask, blue_mask, other_mask;
     unsigned long red_shift, green_shift, blue_shift;
     Visual *visual = iPtr->visualInfo.visual;
 
     red_mask = visual->red_mask;
     green_mask = visual->green_mask;
     blue_mask = visual->blue_mask;
+    other_mask = ~(red_mask|green_mask|blue_mask);
     red_shift = 0;
     green_shift = 0;
     blue_shift = 0;
@@ -549,6 +550,8 @@ BlendComplexAlpha(
 	     */
 
 	    if (alpha) {
+		unsigned long newPixel;
+
 		/*
 		 * We could perhaps be more efficient than XGetPixel for 24
 		 * and 32 bit displays, but this seems "fast enough".
@@ -557,6 +560,7 @@ BlendComplexAlpha(
 		r = modelPtr[0];
 		g = modelPtr[1];
 		b = modelPtr[2];
+		pixel = XGetPixel(bgImg, x, y);
 		if (alpha != 255) {
 		    /*
 		     * Only blend pixels that have some transparency
@@ -564,7 +568,6 @@ BlendComplexAlpha(
 
 		    unsigned char ra, ga, ba;
 
-		    pixel = XGetPixel(bgImg, x, y);
 		    ra = GetRValue(pixel);
 		    ga = GetGValue(pixel);
 		    ba = GetBValue(pixel);
@@ -573,7 +576,10 @@ BlendComplexAlpha(
 		    g = ALPHA_BLEND(ga, g, alpha, unalpha);
 		    b = ALPHA_BLEND(ba, b, alpha, unalpha);
 		}
-		XPutPixel(bgImg, x, y, RGB(r, g, b));
+		newPixel = RGB(r, g, b);
+		/* Bug 1d8b7124b622: preserve alpha if present. */
+		newPixel |= pixel & other_mask;
+		XPutPixel(bgImg, x, y, newPixel);
 	    }
 	}
     }