png uncompressed by CGContextDrawImage is different from orign png image in xcode

108 views Asked by At

I have a png image which color is white, but alpha channel is different.That is a pixel of the image is made of (255, 255, 255, x),x belongs to [0~255].

I use the following code to read it into menory, find color value changed to the same as alpha value.That is pixel changed to (x, x, x, x);

CGImageRef spriteImage = [UIImage imageNamed:fileName].CGImage;
if (!spriteImage) {
    NSLog(@"Failed to load image %@", fileName);
    exit(1);
}

size_t width = CGImageGetWidth(spriteImage);
size_t height = CGImageGetHeight(spriteImage);

GLubyte * spriteData = (GLubyte *) calloc(width*height*4, sizeof(GLubyte));

CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4, CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);

CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);

CGContextRelease(spriteContext);

use lldb the value of memory is :(lldb) expr spriteData (GLubyte *) $5 = 0x17bf7600 "\351\351\351\351\342\342\342\342\331\331\331\331\320\320\320\320\306\306\306ƽ\xbd\xbd\xbd\xb5\xb5\xb5\xb5\xae\xae\xae\xae\xa9\xa9\xa9\xa9\xa7\xa7\xa7\xa7\xa7\xa7\xa7\xa7\xa9\xa9\xa9\xa9\xae\xae\xae\xae\xb5\xb5\xb5\xb5\xbd\xbd\xbd\xbd\306\306\306\306\320\320\320\320\331\331\331\331\342\342\342\342\351\351\351\351\343\343\343\343\331\331\331\331\316\316\316\316\303\303\303\303\xb7\xb7\xb7\xb7\xac\xac\xac\xac\xa2\xa2\xa2\xa2\x9a\x9a\x9a\x9a\x94\x94\x94\x94\x91\x91\x91\x91\x91\x91\x91\x91\x94\x94\x94\x94\x9a\x9a\x9a\x9a\xa2\xa2\xa2\xa2\xac\xac\xac\xac\xb7\xb7\xb7\xb7\303\303\303\303\316\316\316\316\331\331\331\331\343\343\343\343\334\334\334\334\320\320\320\320\303\303\303\303\xb5\xb5\xb5\xb5\xa7\xa7\xa7\xa7\x9a\x9a\x9a\x9a\x8f\x8f\x8f\x8f\x86\x86\x86\x86\x7f\x7f\x7f\x7f{{{{{{{{\x7f\x7f\x7f\x7f\x86\x86\x86\x86\x8f\x8f\x8f\x8f\x9a\x9a\x9a\x9a\xa7\xa7\xa7\xa7\xb5\xb5\xb5\xb5\303\303\303\303\320\320\320\320\334\334\334\334\324\324\324\324\306\306\306\306\xb7\xb7\xb7\xb7\xa7\xa7\xa7\xa7\x98\x98\x98\x98\x89\x89\x89\x89||||qqqqjjjjffffffffjjjjqqqq||||\x89\x89\x89\x89\x98\x98\x98\x98\xa7\xa7\xa7\xa7\xb7\xb7\xb7\xb7\306\306\306\306\324\324\324\324\315\315\315\315\xbd\xbd\xbd\xbd\xac\xac\xac\xac\x9a\x9a\x9a\x9a\x89\x89\x89\x89xxxxjjjj^^^^VVVVRRRRRRRRVVVV^^^^jjjjxxxx\x89\x89\x89\x89\x9a\x9a\x9a\x9a\xac\xac\xac\xac\xbd\xbd\xbd\xbd\315\315\315\315\306\306\306\306\xb4\xb4\xb4\xb4\xa2\xa2\xa2\xa2\x8e\x8e\x8e\x8e{{{{jjjjZZZZNNNNEEEEAAAAAAAAEEEENNNNZZZZjjjj{{{{\x8e\x8e\x8e\x8e\xa2\xa2\xa2\xa2\xb4\xb4\xb4\xb4\306\306\306\306\300\300\300\300\xad\xad\xad\xad\x99\x99\x99\x99\x84\x84\x84\x84pppp]]]]MMMM@@@@7777333333337777@@@@MMMM]]]]pppp\x84\x84\x84\x84\x99\x99\x99\x99\xad\xad\xad\xad\300\300\300\300\xbc\xbc\xbc\xbc\xa8\xa8\xa8\xa8\x92\x92\x92\x92}}}}ggggTTTTCCCC6666----((((((((----6666CCCCTTTTgggg}}}}\x92\x92\x92\x92\xa8\xa8\xa8\xa8\xbc\xbc\xbc\xbc\xb9\xb9\xb9\xb9\xa4\xa4\xa4\xa4\x8e\x8e\x8e\x8exxxxbbbbOOOO====0000''''\"\"\"\"\"\"\"\"''''0000====OOOObbbbxxxx\x8e\x8e\x8e\x8e\xa4\xa4\xa4\xa4\xb9\xb9\xb9\xb9\xb8\xb8\xb8\xb8\xa3\xa3\xa3\xa3\x8d\x8d\x8d\x8dvvvv````MMMM;;;;....$$$$ $$$$....;;;;MMMM````vvvv\x8d\x8d\x8d\x8d\xa3\xa3\xa3\xa3\xb8\xb8\xb8\xb8\xb9\xb9\xb9\xb9\xa4\xa4\xa4\xa4\x8e\x8e\x8e\x8exxxxbbbbOOOO====0000''''\"\"\"\"\"\"\"\"''''0000====OOOObbbbxxxx\x8e\x8e\x8e\x8e\xa4\xa4\xa4\xa4\xb9\xb9\xb9\xb9\xbc\xbc\xbc\xbc\xa8\xa8\xa8\xa8\x92\x92\x92\x92}}}}ggggTTTTCCCC6666----((((((((----6666CCCCTTTTgggg}}}}\x92\x92\x92\x92\xa8\xa8\xa8\xa8\xbc\xbc\xbc\xbc\300\300\300\300\xad\xad\xad\xad\x99\x99\x99\x99\x84\x84\x84\x84pppp]]]]MMMM@@@@7777333333337777@@@@MMMM]]]]pppp..."

I need the real color for opengl rendering, But it is changed.Can you tell me how to fix it.

This is my demo code, U can run it use xcode to build directly. code download

1

There are 1 answers

0
jayzhen On BEST ANSWER

Android and Ios always loads graphics in premultiplied alpha format.Premultiplied alpha just means that all input color values are already multiplied with the alpha value. That is when alpha is 128 (0.5), RGB is changed to (R * 0.5, G * 0.5, B * 0.5). you can see from this web:the dirty secrets of premultiplied alpha

How to deal with it, you can use RGB divided by ALPHA to recover it. In GLSL,you can try this:

vec4 premultipliedColor = texture2D(sampler, texCoordinate);
vec4 realColor = premultipliedColor;
if(premultipliedColor.a > 0.0)
    realColor = vec4(premultipliedColor.rgb/premultipliedColor.a,premultipliedColor.a);