The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Premultiplication problem on iOS using SDL_Image
ollika


Joined: 28 Nov 2013
Posts: 18
I'm having trouble getting unmodified RGBA data from a png image through to my OpenGl texture using SDL_Image on iOS. Same stuff works fine on android. First problem was that XCode does premultiplication when packaging png images by default. I already disabled that.

Seems that at some stage when loading the png image, iOS does premultiplication anyway and SDL_Image does the inverse to get the original RGB values. The problem with this is that on parts where my alpha channel is zero or near to zero I'm losing all my color information. Or at least that's what I think is happening by glancing the code @ IMG_ImageIO.m

I want to use the RGB data for color as it is, and alpha for other stuff in the shader. The texture is used in a fragment shader that my program uses a lot, so I'd like to avoid making the alpha channel a separate image (and adding another texture lookup). Could be that it doesn't really affect perf that much, but I still think this should be fixed if possible.

Any ideas?
Premultiplication problem on iOS using SDL_Image
Jeffrey Carpenter
Guest

Hi there,
Ow, your problem sounds a lot awfully like a known issue, see "Perhaps again? Pixel bug in Mac OS X” at https://forums.libsdl.org/viewtopic.php?t=10013&sid=34e8ef7cf74074039495037c1a077025 for the ugly details — applies to iOS, too, AFAIK. Try the workaround that is mentioned in the thread to help confirm if this is indeed the same issue.

If it turns out that it is the same issue, you might consider doing what I have, and rebuild SDL_image with the libpng & friends backends instead.
Cheers,
Jeffrey Carpenter
Premultiplication problem on iOS using SDL_Image
i8degrees


Joined: 22 Nov 2014
Posts: 39
Hello,
Have you figured out the problem yet? I’m curious if the issue turned out to be related to the problem I mentioned. If it was, a patch I wrote concerning the issue might be of interest to you — https://bugzilla.libsdl.org/show_bug.cgi?id=2788

I’d love to know if it resolves your problem ...
Cheers,
Jeffrey Carpenter
ollika


Joined: 28 Nov 2013
Posts: 18
Oh, sorry for not responding earlier. I thought I would be getting notifications for my own posts by default.

However, I think I have a different problem than describe in the issue. Loading works well enough in normal cases when I use premultiplied alpha. I don't need the colors to be perfect, so it doesn't really matter to me if the colors might be off by one bit. However I was getting massive loss of precision with the specific scenario where the image is not premultiplied and have alpha channel has values near to zero.

As I understand SDL_Image is currently "undoing" the premultiply that is done by the Apple API (when loading png images at least) by defining something like rgb = (loaded_RGB * 255 / loaded_alpha).

The problem I was having is that if my RGBA value is e.g. (245, 235, 255, 1), it is being premultiplied to something like (1,1,1,1).
Then when loading the image, SDL tries to undo the premultiply with something like RGB = (1, 1, 1) * 255 / 1 = (255, 255, 255)

-> So my color basically turned to white and I'm losing almost all precision.


Like I said, the first problem was iOS packaging doing the premultiply for my png images, but that can be easily disabled. After hours of investigation, going through apple documentation and poking the SDL_Image source, I concluded that Apple is doing the premultiply anyway on load time if the image is not already premultiplied. Loading nonpremultiplied images using the apple's own image loading API is either not possible or very hard (I could not figure out how to do it. I have very little experience with objective-C or cocoa so I might be wrong).

Finally after so much frustration with a stupid issue like this, I just went with a totally different implementation for the effect that I need that does not need nonpremultiplied images at all.