Tk Source Code

View Ticket
Login
Ticket UUID: 1d8b7124b62218beb4961ab4247b6c15718ffa58
Title: X11: Photo images not drawn correctly for 32-bit visuals
Type: Patch Version: 8.6.13
Submitter: chrstphrchvz Created on: 2023-11-19 13:15:04
Subsystem: 41. Photo Images Assigned To: fvogel
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2023-12-09 10:27:55
Resolution: Fixed Closed By: fvogel
    Closed on: 2023-12-09 10:27:55
Description:

On X11, photo image instance drawing involves TkImgPhotoDisplay() getting an XImage from XGetImage(), updating it with BlendComplexAlpha(), and drawing the result with XPutImage(). But if the instance’s toplevel uses a visual with 32-bit depth, then the pixel colors used by the XImage may support premultiplied alpha, which BlendComplexAlpha() is unaware of (and so was I until now). When XPutPixel() is called with with RGB(r, g, b), this puts 0 in the alpha channel. The resulting pixel color is the photo image color added to the color appearing behind the toplevel and clamped to 255 for each RGB channel. In this screenshot, a toplevel using a 32-bit visual which should show an image of Earth instead shows the white xterm background with the Earth image colors appearing through the black xterm text:

I believe https://github.com/python/cpython/issues/112151 is for the same issue, since the reporter’s system uses 32-bit visuals. Given how long this issue has possibly been around, I would think there are other reports of it, but I have not found any. I have not spotted the same issue elsewhere in core Tk.

See attached patch for how I would avoid the issue: do not use XPutPixel() without first using XGetPixel() and preserving any transparency information.

User Comments: fvogel added on 2023-12-09 10:27:55:
Merged in core-8-6-branch and trunk. Thanks!

fvogel added on 2023-12-02 11:53:44:

Thanks! Christian's patch now committed in our bugfix branch, replacing the original patch proposal from Christopher.


chrstphrchvz added on 2023-12-02 06:06:06:

Christian’s approach works for me.


chw added on 2023-12-02 05:02:09:
Christopher, Francois,

I believe that I've found another solution by using the GC's
plane_mask when depth is greater than 24. The reason was that
Christopher's approach did not work on all my various X setups.
Most notably, it produced strange behavior on GNOME on Xorg,
where the effect of the background shining through did go away
only in GNOME shell's minimized view during circulating
through the applications.

See this proof-of-concept change:

  https://www.androwish.org/home/info/7aeac931688d2525

chrstphrchvz added on 2023-11-25 18:21:13:

I have not measured the performance impact of my original suggested change. But concerns about this code’s performance do not seem as relevant now compared to 20 years ago.

If X11 is expecting premultiplied alpha pixel color values, then my suggested fix of OR-ing current transparency is only correct if the pixel was already completely opaque, since Tk is not premultiplying its color. I am not aware of anything that might make those pixels partially transparent, but I am inclined to find a different approach.


fvogel added on 2023-11-25 14:48:52:
Thinking again about my item 2. below I'm no so convinced soemthing should be done after all. I plan to merge as is. Is there any different opinion around here?

fvogel added on 2023-11-22 20:58:19:
Sure, it's one way of repairing the build.

chrstphrchvz added on 2023-11-22 13:23:55:

1. It does not build on Windows ('other_mask': unknown identifier)

Does this help?

--- generic/tkImgPhInstance.c
+++ generic/tkImgPhInstance.c
@@ -574,8 +574,10 @@ BlendComplexAlpha(
                    b = ALPHA_BLEND(ba, b, alpha, unalpha);
                }
                newPixel = RGB(r, g, b);
+#ifndef _WIN32
                /* Bug 1d8b7124b622: preserve alpha if present. */
                newPixel |= pixel & other_mask;
+#endif
                XPutPixel(bgImg, x, y, newPixel);
            }
        }


fvogel added on 2023-11-22 00:20:34:
Oops. Forget about 3. I didn't read the code correctly.  Sorry.

fvogel added on 2023-11-21 21:43:11:

Thanks, I have committed your proposed patch as [182ef922]. I have a few concerns:

  1. It does not build on Windows ('other_mask': unknown identifier)
  1. BlendComplexAlpha() takes quite many precautions to preserve performance. Putting pixel = XGetPixel(bgImg, x, y); out of the if (alpha != 255) clause has an impact on performance for images that do not have transparency (most common case I think). Moreover this impact could be avoided when dealing with visuals other than 32 bits depth, which should be the common case nowadays. We should perhaps specialize this part of the code to impact performance only when needed (i.e when dealing with 32-bits visuals and images having some transparency).
  1. In the following snippet, the 'newPixel' passed to XPutPixel() is computed from 'pixel' and 'other_mask' (one line above) only. Again one line above, NewPixel is computed from RGB(r, g, b), but this value is not used. It seems your patch, while indeed apparently visually fixing the reported issue, unintentionally suppresses the alpha blending.
		newPixel = RGB(r, g, b);
		/* Bug 1d8b7124b622: preserve alpha if present. */
		newPixel |= pixel & other_mask;
		XPutPixel(bgImg, x, y, newPixel);

For reference I will attach the script with which I'm testing this patch.


Attachments: