由 CGContextDrawImage 解压缩的 png 与 xcode 中的原始 png 图像不同
png uncompressed by CGContextDrawImage is different from orign png image in xcode
我有一个颜色为白色的png图像,但是alpha通道是different.That是图像的一个像素由(255, 255, 255, x)组成,x属于[0~255 ].
我用下面的代码读入内存,发现颜色值改成和alpha一样value.That是像素改成(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);
使用lldb内存的值为:(lldb) expr spriteData
(GLubyte *) = 0x17bf7600 "1111222211110000666ƽ\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\xbd666600001111222211113333111166663333\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\xb73333666611113333444400003333\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\xb533330000444444446666\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\xb7666644445555\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\xbd55556666\xb4\xb4\xb4\xb4\xa2\xa2\xa2\xa2\x8e\x8e\x8e\x8e{{{{jjjjZZZZNNNNEEEEAAAAAAAAEEEENNNNZZZZjjjj{{{{\x8e\x8e\x8e\x8e\xa2\xa2\xa2\xa2\xb4\xb4\xb4\xb466660000\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\xad0000\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\xbc0000\xad\xad\xad\xad\x99\x99\x99\x99\x84\x84\x84\x84pppp]]]]MMMM@@@@7777333333337777@@@@MMMM]]]]pppp..."
我需要 opengl 渲染的真实颜色,但是 changed.Can 你告诉我如何解决它。
这是我的演示代码,你可以运行直接使用xcode构建。
code download
Android 和 Ios 始终以预乘 alpha 加载图形 format.Premultiplied alpha 仅意味着所有输入颜色值都已与 alpha 值相乘。即当alpha为128(0.5)时,RGB变为(R*0.5,G*0.5,B*0.5)。你可以从这个网站看到:the dirty secrets of premultiplied alpha
如何处理,可以用RGB除以ALPHA来恢复。
在 GLSL 中,你可以试试这个:
vec4 premultipliedColor = texture2D(sampler, texCoordinate);
vec4 realColor = premultipliedColor;
if(premultipliedColor.a > 0.0)
realColor = vec4(premultipliedColor.rgb/premultipliedColor.a,premultipliedColor.a);
我有一个颜色为白色的png图像,但是alpha通道是different.That是图像的一个像素由(255, 255, 255, x)组成,x属于[0~255 ].
我用下面的代码读入内存,发现颜色值改成和alpha一样value.That是像素改成(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);
使用lldb内存的值为:(lldb) expr spriteData
(GLubyte *) = 0x17bf7600 "1111222211110000666ƽ\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\xbd666600001111222211113333111166663333\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\xb73333666611113333444400003333\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\xb533330000444444446666\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\xb7666644445555\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\xbd55556666\xb4\xb4\xb4\xb4\xa2\xa2\xa2\xa2\x8e\x8e\x8e\x8e{{{{jjjjZZZZNNNNEEEEAAAAAAAAEEEENNNNZZZZjjjj{{{{\x8e\x8e\x8e\x8e\xa2\xa2\xa2\xa2\xb4\xb4\xb4\xb466660000\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\xad0000\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\xbc0000\xad\xad\xad\xad\x99\x99\x99\x99\x84\x84\x84\x84pppp]]]]MMMM@@@@7777333333337777@@@@MMMM]]]]pppp..."
我需要 opengl 渲染的真实颜色,但是 changed.Can 你告诉我如何解决它。
这是我的演示代码,你可以运行直接使用xcode构建。 code download
Android 和 Ios 始终以预乘 alpha 加载图形 format.Premultiplied alpha 仅意味着所有输入颜色值都已与 alpha 值相乘。即当alpha为128(0.5)时,RGB变为(R*0.5,G*0.5,B*0.5)。你可以从这个网站看到:the dirty secrets of premultiplied alpha
如何处理,可以用RGB除以ALPHA来恢复。 在 GLSL 中,你可以试试这个:
vec4 premultipliedColor = texture2D(sampler, texCoordinate);
vec4 realColor = premultipliedColor;
if(premultipliedColor.a > 0.0)
realColor = vec4(premultipliedColor.rgb/premultipliedColor.a,premultipliedColor.a);