【问题标题】:Remove matte(alpha bleed) after combining jpg and alpha to get png image in iOS在 iOS 中结合 jpg 和 alpha 以获取 png 图像后删除 matte(alpha bleed)
【发布时间】:2012-10-18 15:51:20
【问题描述】:

为了减小应用程序的大小,我在 png 图像上使用 jpg 和 alpha。我可以合并 jpg 和 alpha 图像以获取 png,但问题是它会留下边缘不清晰的 alpha 出血(哑光)。请帮我解决一下这个。

我编写的以下代码可以帮助我从 jpg 和 alpha 图像中获取 png 图像。请帮助我摆脱 alpha bleed(matte)。谢谢。

+ (UIImage*)pngImageWithJPEG:(UIImage*)jpegImage compressedAlphaFile:(UIImage*)alphaImage
{
CGRect imageRect = CGRectMake(0, 0, jpegImage.size.width, jpegImage.size.height);

//Pixel Buffer
uint32_t* piPixels = (uint32_t*)malloc(imageRect.size.width * imageRect.size.height * sizeof(uint32_t));

memset(piPixels, 0, imageRect.size.width * imageRect.size.height * sizeof(uint32_t));

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(piPixels, imageRect.size.width, imageRect.size.height, 8, sizeof(uint32_t) * imageRect.size.width, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaPremultipliedLast);

//Drawing the alphaImage to the pixel buffer to seperate the alpha values
CGContextDrawImage(context, imageRect, alphaImage.CGImage);

//Buffer to store the alpha values from the alphaImage
uint8_t* piAlpha = (uint8_t*)malloc(sizeof(uint8_t) * imageRect.size.width * imageRect.size.height);

//Copying the alpha values from the alphaImage to the alpha buffer
for (uint32_t y = 0; y < imageRect.size.height; y++) 
{
    for (uint32_t x = 0; x < imageRect.size.width; x++)
    {
        uint8_t* piRGBAPixel = (uint8_t*)&piPixels[y * (uint32_t)imageRect.size.width + x];

        //alpha = 0, red = 1, green = 2, blue = 3.
        piAlpha[y * (uint32_t)imageRect.size.width + x] = piRGBAPixel[1];
    }
}

//Drawing the jpegImage in the pixel buffer.
CGContextDrawImage(context, imageRect, jpegImage.CGImage);

//Setting alpha to the jpegImage
for (uint32_t y = 0; y < imageRect.size.height; y++) 
{
    for (uint32_t x = 0; x < imageRect.size.width; x++)
    {
        uint8_t* piRGBAPixel = (uint8_t*)&piPixels[y * (uint32_t)imageRect.size.width + x];

        float fAlpha0To1 = piAlpha[y * (uint32_t)imageRect.size.width + x] / 255.0f;

        //alpha = 0, red = 1, green = 2, blue = 3.
        piRGBAPixel[0] = piAlpha[y * (uint32_t)imageRect.size.width + x];
        piRGBAPixel[1] *= fAlpha0To1;
        piRGBAPixel[2] *= fAlpha0To1;
        piRGBAPixel[3] *= fAlpha0To1;
    }
}

//Creating image from the pixel buffer
CGImageRef cgImage = CGBitmapContextCreateImage(context);

//Releasing resources
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
free(piPixels);
free(piAlpha);

//Creating the pngImage to return from the cgImage
UIImage* pngImage = [UIImage imageWithCGImage:cgImage];

//Releasing the cgImage.
CGImageRelease(cgImage);

return pngImage;
}

【问题讨论】:

    标签: ios png quartz-graphics alpha jpeg


    【解决方案1】:

    预乘 alpha 颜色空间可以更好地处理颜色渗色。尝试像这样预处理源像素:

    r = r*a;
    g = g*a;
    b = b*a;
    

    要获得原始值,您通常会在读取图像后将其反转(如果 a > 0,则将 RGB 值除以 a),但由于 iOS 本身以预乘 alpha 颜色工作,因此您甚至没有这样做。


    也可以试试PNG8+Alpha 格式,有时可能比单独的 JPG+mask 小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 2011-07-06
      • 2014-11-28
      • 2010-12-30
      • 1970-01-01
      • 2019-09-05
      • 1970-01-01
      相关资源
      最近更新 更多