【问题标题】:Get pixel RGB wrong by CGBitmapContextCreate通过 CGBitmapContextCreate 获取像素 RGB 错误
【发布时间】:2020-03-16 15:43:36
【问题描述】:

我遇到了一个奇怪的问题。我正在使用 CGBitmapContextCreate 从图像中获取原始像素数据,然后修改指定位置的像素颜色。但是,我发现当我得到一个像素的 RGBA 时,alpha 等于 1,一切正常,但如果 alpha 小于 1,则 RGB 是错误的,它是一种较暗的颜色。谁能告诉我为什么,这让我抓狂......

这是我的代码,你可以在这里获取测试图片

#define Mask8(x) ( (x) & 0xFF )
#define R(x) ( Mask8(x) )
#define G(x) ( Mask8(x >> 8 ) )
#define B(x) ( Mask8(x >> 16) )
#define A(x) ( Mask8(x >> 24) )
#define RGBAMake(r, g, b, a) ( Mask8(r) | Mask8(g) << 8 | Mask8(b) << 16 | Mask8(a) << 24 )

- (void)getPixelRGBA
{
    UIImage *image = [UIImage imageNamed:@"testpicture.png"];
    NSUInteger bytesPerRow = 4*image.size.width;
    UInt32 *pixels = (UInt32 *)calloc(image.size.width*image.size.height, sizeof(UInt32));

    CGContextRef context = CGBitmapContextCreate(pixels, image.size.width, image.size.height, 8, bytesPerRow, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
    CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);

    // pixel with alpha < 1, color gets dark
    int offset1 = 75 * (int)image.size.width + 75;
    UInt32 *pixelValue1 = pixels + offset1;
    UInt32 pixelColor1 = *pixelValue1;
    UInt32 r1 = R(pixelColor1);
    UInt32 g1 = G(pixelColor1);
    UInt32 b1 = B(pixelColor1);
    CGFloat a1 = A(pixelColor1)/255.0f;

    // pixel with alpha == 1, color is right
    int offset2 = 75 * (int)image.size.width + 80;
    UInt32 *pixelValue2 = pixels + offset2;
    UInt32 pixelColor2 = *pixelValue2;
    UInt32 r2 = R(pixelColor2);
    UInt32 g2 = G(pixelColor2);
    UInt32 b2 = B(pixelColor2);
    CGFloat a2 = A(pixelColor2)/255.0f;
}

非常感谢你们!

【问题讨论】:

    标签: ios image-processing cgbitmapcontextcreate


    【解决方案1】:

    iOS 使用premultiplied alpha。 (这是kCGImageAlphaPremultipliedLastPremultiplied 部分。)。

    这表示一个像素的R、G、B通道的范围不在0 ... 255。它们的范围在0 ... A。如果R通道是127,但A通道也是127 ,则 R 处于最大强度。

    【讨论】:

    • 非常感谢!就是这个原因!
    猜你喜欢
    • 2018-04-25
    • 1970-01-01
    • 2011-08-29
    • 2012-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    相关资源
    最近更新 更多