Skip to content

Premultiplied alpha vs raw alpha in pngs

meeloo edited this page Oct 2, 2010 · 1 revision

Premultiplied Alpha vs Raw Alpha in NUI

There are many resources on the web that explain the differences in between premultiplied and non premultiplied alpha. NUI has always been using the later form but we may have to rethink that.

At the end of the application build process, Xcode runs a PNG optimizer on all images stored in the executable. The process renders the PNGs unreadable with the standard libPNG code that we use on all platforms. That is the reason why we have been using the standard OSX CoreGraphics code to read images on the iPhone instead of libPNG and libJPEG. All is good and well, except that this tool also changes the PNG images so that their pixels already are premultiplied in the files. This frees up the device from doing that operation when it loads them but the resulting image we get on the phone is does not have the pixels we expect and it breaks NUI’s blending code.

There is a way to tell Xcode NOT to optimize the PNG (put IPHONE_OPTIMIZE_OPTIONS=-skip-PNGs in the build settings of each application project), however if we use that trick we also have to use libPNG back in the iPhone version of NUI as CoreGraphics will premultiply alpha channels anyway and there is no way to tell it not to (the flags exists but they are not implemented on iPhone and MacOSX). If we have to include libPNG again it will bloat our executable size some more and we don’t want that (particularly on the iPhone…).

Some possible solutions:
- unpremultiply the images at load time. Pro: we don’t need to change our habits. Cons: might be very slow for big images on iPhone and introduce errors in the pixels.
- use our libPNG + IPHONE_OPTIMIZE_OPTIONS=-skip-PNGs. Pro: very easy to do for NUI. Cons: executable size bloat and might be cumbersome for the end user.
- use premultiplied Alpha everywhere in libNUI. Pro: follow some kind of standard for porter duff compositing, may be done transparently (hehe) for the user. Cons: change our habits and maybe prevent some operations.

What’s your take on that?
Let’s talk about it in the forums! http://redmine.libnui.net/projects/libnui/boards