| 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
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:
- Screenshot 2026-10-18 at 9.07.42 AM.png [download] added by marc_culler on 2026-10-18 14:16:59. [details]
