Tk Source Code

View Ticket
Login
2026-11-13
17:14
Fix [de6ef827]: photo -gamma and -palette had no effect on macOS check-in: 6503452b user: jan.nijtmans tags: core-8-6-branch
16:25 • Ticket [b17dcd71] Windows: imgPhInstance-7.* test failures (wayland branch) status still Open with 3 other changes artifact: c63c6560 user: serhiy.storchaka
2026-11-06
23:22
Fix [de6ef827]: photo -gamma and -palette had no effect on macOS check-in: b8fdf645 user: jan.nijtmans tags: core-9-0-branch
2026-10-20
03:02 • Ticket [de6ef827] imageName configure -gamma - has no effect on macOS status still Open with 4 other changes artifact: 9f7d976d user: marc_culler
2026-10-19
20:06 • Ticket [de6ef827]: 3 changes artifact: 2b44f17a user: chrstphrchvz
19:58
Fix [de6ef827]: photo -gamma and -palette had no effect on macOS. Adapted from [05a0704a] closed check-in: 80429846 user: chrstphrchvz tags: bug-de6ef827-c90b
16:18 • Ticket [de6ef827] imageName configure -gamma - has no effect on macOS status still Open with 3 other changes artifact: 65464b45 user: kevin_walzer
14:06 • Ticket [de6ef827]: 4 changes artifact: 2ba088a8 user: marc_culler
09:22 • Ticket [8beb2a08] photo -gamma and -palette: apply them in software on platforms which render RGBA images status still Open with 6 other changes artifact: ca558b91 user: chrstphrchvz
07:12 • Ticket [de6ef827] imageName configure -gamma - has no effect on macOS status still Open with 3 other changes artifact: 8fb66603 user: chrstphrchvz
2026-10-18
20:06 • Ticket [de6ef827]: 3 changes artifact: 130b2c98 user: serhiy.storchaka
18:31 • Open ticket [de6ef827]. artifact: b5a42c46 user: chrstphrchvz
14:38 • Closed ticket [de6ef827]. artifact: 1f3499ee user: serhiy.storchaka
14:38 • Ticket [de6ef827]: 3 changes artifact: f292e4b2 user: serhiy.storchaka
14:37
Fix [de6ef8276c]: photo -gamma and -palette had no effect on macOS. check-in: dee06853 user: serhiy.storchaka tags: trunk, main
14:19 • Ticket [de6ef827] imageName configure -gamma - has no effect on macOS status still Open with 4 other changes artifact: 4ba71ef4 user: marc_culler
14:16 • Add attachment Screenshot 2026-10-18 at 9.07.42 AM.png to ticket [de6ef827] artifact: 2f4f3b78 user: marc_culler
07:17 • Ticket [de6ef827] imageName configure -gamma - has no effect on macOS status still Open with 3 other changes artifact: ded017d7 user: serhiy.storchaka
05:41
Fix [de6ef8276c]: photo -gamma and -palette had no effect on macOS. Apply the fix for [32f2e19de8] to all platforms which render RGBA images, not only Windows: images without partial transparency and with a non-default -gamma or -palette are drawn from the pixmap of the instance. Run tests imgPhInstance-7.1 and 7.2 on Aqua too. closed check-in: 8316cf93 user: serhiy.storchaka tags: aqua-photo-gamma-palette
2026-10-17
10:13 • New ticket [ff3965a5] Photo gamma and palette options ignored in some cases on X11. artifact: 677a11b2 user: chrstphrchvz
10:12 • Ticket [de6ef827] imageName configure -gamma - has no effect on macOS status still Open with 3 other changes artifact: c7e29925 user: chrstphrchvz
2026-10-14
02:33 • Ticket [32f2e19d] Windows: photo -gamma and -palette are ignored since RGBA rendering status still Closed with 5 other changes artifact: 1aa3ad4a user: chrstphrchvz
2026-09-29
19:32 • Closed ticket [32f2e19d]. artifact: 53048d6f user: oehhar
2025-08-11
17:38 • Ticket [f215a9fd] Revisiting [df4b603950] status still Open with 3 other changes artifact: 06dbcc4f user: chrstphrchvz
2023-11-20
17:43 • New ticket [fe942372] Aqua: XPutImage() swaps red and blue channels. artifact: 2da4af8a user: chrstphrchvz
2023-07-21
20:29 • Ticket [919066ff] Slow processing irregular transparencies status still Open with 3 other changes artifact: 4f5228bb user: chrstphrchvz
2022-09-01
01:09 • Ticket [de6ef827] imageName configure -gamma - has no effect on macOS status still Open with 3 other changes artifact: 2080a586 user: chrstphrchvz
2022-02-25
07:15 • Ticket [de6ef827]: 3 changes artifact: 0cea8d18 user: chrstphrchvz
2022-02-14
19:46 • New ticket [de6ef827]. artifact: a405a7a0 user: anonymous

Ticket UUID: de6ef8276cee0bbb27985cfe4a5bebe8ae3a895a
Title: imageName configure -gamma - has no effect on macOS
Type: Bug Version: 8.6.12
Submitter: anonymous Created on: 2022-02-14 19:46:17
Subsystem: 41. Photo Images Assigned To: nobody
Priority: 5 Medium Severity: Minor
Status: Open Last Modified: 2026-10-20 03:02:43
Resolution: Fixed Closed By: nobody
    Closed on:
Description:
The following code creates two rectangles from the same data. The lower one is brighter than the upper one because of the higher gamma value.

---
package require Tk

set img1 [image create photo -width 200 -height 100]
$img1 put #ffaa00 -to 0 0 200 100

set img2 [image create photo]
$img2 copy $img1
# The following line has now effect on macOS
$img2 configure -gamma 2.0

canvas .cv -width 200 -height 200
.cv create image {100 50} -image $img1
.cv create image {100 150} -image $img2
pack .cv
---

This works on Windows and Linux and up to 8.6.8 it worked under macOS (Catalina) too.
It does not work since 8.6.9 or 8.6.10 (I have only the latter available), including 8.6.12, on any macOS version from Catalina to Monterey
User Comments: marc_culler (claiming to be Marc Culler) added on 2026-10-20 03:02:43:
I think you are correct that CGContextDrawImage may not use the GPU.
And I also think you are correct that its code for alpha blending is
likely to be much more highly optimized than BlendComplexAlpha().

The Wayland port does use the GPU, and having the TK_CAN_RENDER_RGBA
flag available was a big help there.

I tried to think of a use for the -palette option.  The only use I could
come up with was converting a color image to a grayscale image.  I asked
chatGPT to search for software that uses the option, and the only thing
it found was TKMC, a 2002 clone of the MC file manager whose full source
code is on the Tk Wiki.  It used -palette to create some image buttons
from strings embedded in the source code. I am pretty sure the code could
be easily modified to avoid using the option.  

I am also skeptical of the utility of the -gamma option.  Of course we
do have this bug report about it, so at least one person wants to use
it.  But applying a gamma correction to an image which will be displayed
on a system that already applies a gamma correction based on the monitor
specifications or user preferences seems pretty unlikely to be very useful.
Also, for any static image that needs a different gamma correction one
could just use image processing software to fix it.  Why would one want
to apply the correction as part of rendering the image? It is true that
TKMC uses -gamma too, but it sets gamma to 1, so it was using the option
to tell Tk NOT to apply a gamma correction.  

This is not to say that the options should be eliminated. Having special
code to support them in the rare case where they actually get used seems
fine.  But making any aspect of Tk worse solely for the purpose of
supporting those arcane options does not make sense to me.

chrstphrchvz added on 2026-10-19 20:06:19:

What I have read elsewhere is that while Apple has other frameworks which are more GPU-focused, Core Graphics mostly uses CPU. On Aqua, alpha blending in TkpPutRGBAImage() is done by CGContextDrawImage(), which reportedly only uses CPU. It is likely just far more optimized (possibly using CPU SIMD) than Tk's BlendComplexAlpha().

If using BlendComplexAlpha() on Aqua is unacceptable, then an alternative which does not use it would be to copy some of the code introduced by [05a0704a], so that TkImgPhotoDisplay() more resembles what is currently in 9.1, but without affected behavior for other platforms: please see branch bug-de6ef827-c90b.


kevin_walzer added on 2026-10-19 16:18:53:
I agree with Marc. The flag TK_CAN_RENDER_RGBA is a key part of current Tk on macOS, and Wayland borrows this design.

marc_culler (claiming to be Marc Culler) added on 2026-10-19 14:06:00:
The original reason for the flag TK_CAN_RENDER_RGBA was to allow macOS
to use the GPU to blend an RGBA image instead of doing that with slow
code on the CPU.  Are we proposing to go back to doing all blending with
inefficient CPU code?

chrstphrchvz added on 2026-10-19 07:12:03:

Rather than backporting changes prompted by TIP 754, my suggestion for 9.0 remains to disable the code in TkImgPhotoDisplay() currently being enabled by TK_CAN_RENDER_RGBA; probably the simplest way would be to disable the definition in tkMacOSXPort.h and leave TkpPutRGBAImage() unused. Is this acceptable?

--- macosx/tkMacOSXPort.h
+++ macosx/tkMacOSXPort.h
@@ -138,12 +138,14 @@
 #define TK_HAS_DYNAMIC_COLORS 1
 #define TK_DYNAMIC_COLORMAP 0x0fffffff
 
+#if 0 // Disabled for 9.0.x only, to allow -gamma/-palette to work (bug de6ef827)
 /*
  * Inform tkImgPhInstance.c that we implement TkpPutRGBAImage to render RGBA
  * images directly into a window.
  */
 
 #define TK_CAN_RENDER_RGBA
+#endif
 
 MODULE_SCOPE int TkpPutRGBAImage(
 		     Display* display, Drawable drawable, GC gc,XImage* image,

serhiy.storchaka added on 2026-10-18 20:06:37:

I do not know. The other fixes for -gamma and -palette ([32f2e19de8] and [481255]) were not backported to 9.0 either.


chrstphrchvz added on 2026-10-18 18:31:39:

Reopening to ask: will this be fixed for 9.0?


serhiy.storchaka added on 2026-10-18 14:38:01:

Thank you for testing, Marc! Merged into main in [dee0685394]. I will send an email next time.


marc_culler (claiming to be Marc Culler) added on 2026-10-18 14:19:57:
Yes, the fix works.  I attached a screenshot which shows different colors
in the two rectangles, and the imgPhInstance tests all pass.

Serhiy, it would be better to send me an email.  It was more or less an
accident that I noticed your request.

serhiy.storchaka added on 2026-10-18 07:17:27:

Proposed fix in branch aqua-photo-gamma-palette, as suggested by Christopher: the fix for [32f2e19de8] now applies to macOS too, not only Windows, so images without partial transparency and with a non-default -gamma or -palette are drawn from the pixmap of the instance. Tests imgPhInstance-7.1 and 7.2 now run on Aqua too. Marc, could you check them on macOS?


chrstphrchvz added on 2026-10-17 10:12:49:

[fe942372] and [f215a9fd] were resolved: color channel order assumptions have been corrected for some time, and XGetImage()/XCopyArea() should now work again on Tk Aqua.

I thought the remaining step to fix this ticket's issue would simply be to remove #define TK_CAN_RENDER_RGBA from tkMacOSXPort.h, which would make Aqua go back to drawing instance pixmaps, rather than drawing directly from model data (which I previously said was erroneous). That should still work for 9.0. But it likely will not be accepted for 9.1: TIP 754 appeared a few months ago and prompted Tk to draw directly from model data (and break -gamma and -palette) on other platforms as well. So now the likely fix for 9.1 will be to adapt the fix for [32f2e19d] so that it also applies to Aqua.


chrstphrchvz added on 2022-09-01 01:09:40:

I have been interested in getting XGetImage() working again (which would easily fix photo instance gamma), but the ideas I have for doing so involve more general/radical changes to all drawing on Aqua.

The approach proposed in [3e9e82bcfe] does allow fixing this, but it is not as easy as disabling TK_CAN_RENDER_RGBA in tkMacOSXPort.h; assumptions throughout the Aqua code regarding the order of color channels must also be rectified.


chrstphrchvz added on 2022-02-25 07:15:54:

Gamma is applied when creating a color table for photo instances (AllocateColors()); the color table is then used when creating an instance’s Pixmap (TkImgDitherInstance()). But as of Tk Aqua 8.6.9, TkImgPhotoDisplay() no longer draws using an instance’s Pixmap, and instead uses the data directly from the photo model. (On Aqua, instance Pixmaps are still being created, and are probably now unused.) The change in TkImgPhotoDisplay() ([4a251d07db]) was prompted by needing an approach for drawing transparent images without relying on XGetImage(), which stopped working for windows (i.e. non-Pixmap drawables) after removing usage of APIs deprecated by the 10.14 Mojave SDK.

I have been interested in getting XGetImage() working again (which would easily fix photo instance gamma), but the ideas I have for doing so involve more general/radical changes to all drawing on Aqua. It’s more likely that if someone fixes this issue, it will be by extending the platform-specific image drawing routines (currently TkpPutRGBAImage()) to accept a gamma value.


Attachments: