2

我有两张图片。其中一个是纯白色的,并且有一些具有 alpha 透明度的区域。它旨在成为使另一个图像透明的蒙版。另一个图像,全彩色和 PNG,在任何地方都没有应用 alpha。

所以我想将蒙版图像的 alpha 值添加到另一个的 alpha 值。两个图像具有完全相同的大小。我猜这只是循环像素的问题。知道这会如何详细吗?

4

4 回答 4

1

查看 Alpha 合成。 http://en.wikipedia.org/wiki/Alpha_compositing 看起来你正在尝试执行 A out B。

于 2009-07-15T14:20:24.973 回答
1

“clemahieu”是对的——在这种情况下,您不必担心预乘 alpha,因为您正在执行高级操作,而实际的像素格式是低级细节。

如果您只是使用正确的合成运算符将白色/alpha 图像合成到彩色/无 alpha 图像(在颜色/alpha 缓冲区中),您将得到您想要的。

-会

于 2009-10-03T05:23:15.117 回答
1

CGImageRef resultImage = CGImageCreateWithMask([imageA CGImage],[imageB CGImage]); UIImage* img= [UIImage imageWithCGImage:resultImage]; CGImageRelease(resultImage); 返回图片;

于 2010-08-12T14:49:06.053 回答
0

我能想到的最快方法是为每个图像获取一个指向缓冲区的指针,并将它们组合在第三个缓冲区中,循环遍历所有像素,就像你提到的那样。

或者,如果源(颜色)图像有一个 alpha 通道,但它设置为 1,只需用第二个图像中的 alpha 替换该通道。

从 Panther Quartz 开始,有一个仅 alpha 的位图上下文,可用于屏蔽其他图像。在 mac os x 中使用石英 2d 和 pdf 图形进行编程的优秀书籍有一个关于仅 alpha 位图上下文的部分。

要从压缩的 PNG 文件中获取 alpha,您可以执行类似以下操作:


    CGImageRef myImage = [self CGImage];
    CGSize newSize = {CGImageGetWidth(myImage), CGImageGetHeight(myImage)};

if(!isPowerOf2(newSize.width))
    newSize.width = nextPowerOf2(newSize.width);
if(!isPowerOf2(newSize.height))
    newSize.height = nextPowerOf2(newSize.height);


const GLint picSize = newSize.height * newSize.width * 4;

// The bitmapinfo provided by the CGImageRef isn't supported by the bitmap context.
// So I'll make a conforming bitmap info
CGBitmapInfo myInfo = kCGImageAlphaPremultipliedLast;

unsigned char * actual_bytes = new unsigned char[picSize];
CGContextRef imageContext = CGBitmapContextCreate(
                                                  actual_bytes,
                                                  newSize.width,
                                                  newSize.height,
                                                  CGImageGetBitsPerComponent(myImage),
                                                  newSize.width * 4,
                                                  CGImageGetColorSpace(myImage),
                                                  myInfo);

CGContextSaveGState(imageContext);
CGContextDrawImage(imageContext, [self bounds], myImage);
CGContextRestoreGState(imageContext);

此时 actual_bytes 有 RGBA 数据。不要忘记删除actual_bytes 的内存。这是 UIImage 上的一个类别,因此 self 是一个已经从 PNG 文件加载的 UIImage。

于 2009-07-15T11:53:08.740 回答