That code, while it looks a bit odd, is basically doing this:
Firstly, PNG_PREMULTIPLIED_ALPHA_ENABLED
is pointing to a global static which is set via Image::setPNGPremultipliedAlphaEnabled(true | false);
. The default value is true
, so all images being loaded are assumed to be non-PMA.
If PNG_PREMULTIPLIED_ALPHA_ENABLED
is true, then the image being loaded is assumed not be to PMA, and PMA is applied to it via premultiplyAlpha()
, and that function sets _hasPremultipliedAlpha = true
when it’s completed processing.
If PNG_PREMULTIPLIED_ALPHA_ENABLED
is false, then no processing will take place, so we go into the else
condition, being:
else
{
#if CC_ENABLE_PREMULTIPLIED_ALPHA != 0
_hasPremultipliedAlpha = true;
#endif
}
Here it is setting _hasPremultipliedAlpha
based on whether the engine is using PMA textures or non-PMA textures in memory. The default is that it uses PMA textures.
Regardless which path it takes in that conditional check, both result in setting _hasPremultipliedAlpha = true
. One path processes the image to apply PMA, the other does not. It’s useful when you have an image on disk or in memory that already has PMA, and you don’t want it processed again, since it would result in incorrect output.
So, in my earlier code snippet, this is an example of the image on disk being PMA, and I don’t want it to be processed again:
Image::setPNGPremultipliedAlphaEnabled(false); // set PNG_PREMULTIPLIED_ALPHA_ENABLED = false
image->initWithImageFile(pngFilename); // image file already PMA
Image::setPNGPremultipliedAlphaEnabled(true); // set PNG_PREMULTIPLIED_ALPHA_ENABLED = true (default)
…which will end up in the else
block:
if (PNG_PREMULTIPLIED_ALPHA_ENABLED)
{
premultiplyAlpha();
}
else
{
#if CC_ENABLE_PREMULTIPLIED_ALPHA != 0
_hasPremultipliedAlpha = true;
#endif
}