-
Notifications
You must be signed in to change notification settings - Fork 289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
png file is garbled, Mac OS X #1531
Comments
Would be useful to know if this is MacOS only or Allegro in general (guessing 16-bit pngs are not supported?) |
The very same image is displayed properly on both Linux and Windows. |
Alright, allegro5/addons/image/macosx.m Lines 53 to 55 in 8575e37
appears to be wrong; it's assuming 8 bits per channel, so that number of samples per pixel = number of bits per pixel / 8. In the example png, bitsPerPixel is 64, i.e. 4 samples of 16 bits each, will be incorrectly calculated as 8 samples of 8 bits each.There may be other errors subsequent to this. |
At the moment we're loading an
I don't know anything about CoreGraphics but I saw Apple's docs suggesting it should be used if image manipulation was being done. @SiegeLord has there been previous discussion on this, I can't believe this issue has only surfaced in 2024! |
I didn't know 16 bit PNGs even existed, I'm not surprised this hasn't come up before. Regardless of what's done, we should at least gracefully reject formats we can't handle. |
...I think maybe TIFF also supports higher bits per channel. How about retaining the current code (should be fast) but falling back to the drawing context option if the current code can't handle it? At the moment AFAICS there's no I can investigate if no-one else feels motivated to. |
As a workaround - you can disable the native image addon and fall back to libpng (I don't remember if because of this specific bug, but the native addon never worked for my pngs - I just never investigated more). |
I've looked into the render to context option, it works on the png file here and on an 8-bit png. All bitmap loads (bmp, jpg, ...) on MacOS will pass through that function so it's a bit scary to be changing it. I will post some code soon so others can weigh in. I've also started on trying CIImage , currently it is messing up the colours (swapping channels I guess) and I'm not sure why. (CoreImage looks pretty heavy but the only parts we need are "image from data" and "render to bitmap") More later! |
Have pushed some code, please have a look |
Ironically I was hit by this myself yesterday - spent ages trying to debug something when the issue was that ImageMagick's montage command had created a 16 bit-per-channel PNG for me 😠 |
@pedro-w Hey, I've tried your fork under MacOS virtual machine and I'm afraid it still outputs the 16-bit PNG image from OP incorrectly: (and just a small observation, the visual garbage itself is different every time I launch |
Disappointing. It worked for me. What version of MacOS are you using? |
That'd be Monterey. |
One useful thing would be to just |
Oh, so sorry, I forgot to checkout the correct branch 😅 |
Let's not ever mention this again 😉 Thanks very much for testing. I've also got an old MacOS (Catalina) so would be handy if someone with a newer version, especially an Apple Silicon model, could give this code a try out. @justjoheinz would you be able to try it on your M1? |
I can give it a shot. |
LGTM, I tried your issue-1531 branch in your repo. |
OK I'll tidy it up for a PR. I'll look to add a test for it too. |
I have a related-looking bug in my Allegro 5 game: Lix #431: Magic pink won't become transparent on macOS. (My obvious symptom in Lix, the failing conversion from magic pink to transparency, is not the bug itself. Other colors are also slightly wrong. I recolor in-game UI according to pixels from PNGs, and this UI recoloring fails, too.) @Rhys-T has reduced my bug to the interaction between Allegro 5 and Cocoa (Mac's native image loading API). Our findings so far agree with @pedro-w's findings:
How is the PR going? Do you need more help with some of the formats? Can Allegro 5 tell Cocoa to always offer the loaded file as 32-bit, so that Allegro 5's assumptions (always 8 bit per channel) become correct? Should Allegro 5 disable the native Mac image loaders by default? This would fix the bug in future commonly packaged Allegro 5 binaries and make Allegro 5's codebase simpler at the same time. I don't have a Mac to test myself. |
Slight correction: as far as I can tell, it only affects palette-indexed PNGs. I was just saying "don't convert everything to 24-bit, because that loses information from some images that are currently working as-is". I'm not 100% sure what the correct behavior is here. Converting the colors arguably might be more correct in some ways - as far as I can tell, Allegro is exclusively working with colors in the display's native RGB color space, not the sRGB that the images were in, so leaving the colors numerically the same actually ends up changing them visually. But on the other hand, pre-converting the colors means that things like 'magic pink' (#FF00FF) no longer get recognized by In any case, it definitely needs to pay attention to what kind of data the |
There seem to be two semi-related problems with how the macOS native bitmap loader handles the NSBitmapImageRep it gets back:
Summary of the results of different loader/color format/color space combinations as far as I understand at this point:
|
I've figured out why I couldn't get @pedro-w's CIImage-based loader to build under Nix: Nix defaults to targeting a minimum macOS version of 10.12, and CIContextOption first appeared in 10.14. I worked around that and started testing it with Lix. (I'm applying that loader as a patch to the same version of Allegro I've been building Lix against, rather than using that branch as-is, to minimize the number of changes that might affect what I'm seeing. However, it seemed to look the same when I used the actual branch.) Magic pink seems to work correctly with the CIImage loader, but normal colors all look significantly darker than in either of the current loaders, and UI icons don't get remapped from grayscale to the proper light-blue colors.1 I suspect this is a gamma-related issue: magic pink consists of only I'll update the table above with this info. ScreenshotsMain menuWith libpng, or NSImage after converting to PNG32: With CIImage, or libpng with In-gameWith libpng, or NSImage after converting to PNG32: With CIImage, or libpng with Dock iconLeft is CIImage, or libpng with Footnotes
|
This is great work. Sorry I've not kept up with this thread. It seems to me that it would be better to go for the libpng loader, because then any improvements we make would be cross-plaform (ditto any idiosyncrasies that still occur). Not sure about the color space thing though - is there a library to convert color spaces, or is it acceptable for us to say in the docs that only sRGB files will be reproduced faithfully? Also if gamma can be set via the config file, does this mean that programs that rely on image pixels being a certain RGB value can be broken? (e.g. I think I remember an RPG-type game where the character sprites were recolored to give them different clothes) |
One question I have is, what color space is Allegro supposed to be working in - or in other words, what space is an It seems like the ideal setup would be to separate loading from converting to native: First load the pixel values as-is, without any conversion or gamma adjustment or anything, so the game can recognize any special colors it needs to. Then either have a function to convert the final bitmap to native explicitly before drawing, or make it so that an I've never actually developed anything using Allegro myself, nor done much of anything that needed to care about color spaces, so I can't really offer any insight as to what approach would be best from a developer perspective.
Yes - if I set (Lix does have a few other special colors it looks for, but for various reasons, the gamma stuff in particular doesn't affect most of them. Black and magic pink are both used as 'transparent' in various places, but they're at the very ends of the gamma curve and don't get adjusted, so they work. In multiplayer, the various colors in the lix sprites get remapped to shades of each player's color, closer to your RPG example, and those colors do get adjusted - but the colors that the game is looking for are taken from the first row of lixrecol.png, so they've also been adjusted, and still match what's in the sprites. The only other special color I know of that might be getting affected is |
I use allegro 5.2.9.1 (built by brew) on a Mac OS X M1 architecture. Libpng is 1.6.42.
I have a file (attached) which according to file is :
> file a10000.png a10000.png: PNG image data, 64 x 64, 16-bit/color RGBA, non-interlaced
Using https://github.com/liballeg/allegro5/blob/master/examples/ex_bitmap_file.c to render this image the output is garbled.
If I do
the output is ok
The text was updated successfully, but these errors were encountered: