在组合jpg和alpha以在iOS中获得png图像后,移除蒙版(alpha出血)

在组合jpg和alpha以在iOS中获得png图像后,移除蒙版(alpha出血),ios,png,quartz-graphics,alpha,jpeg,Ios,Png,Quartz Graphics,Alpha,Jpeg,为了减小应用程序的大小,我使用jpg和alpha over png图像。我能够合并jpg和alpha图像以获得png,但问题是它会留下一个alpha bleedmatte,其中边缘有点尖锐。请帮我做这个 下面的代码帮助我从jpg和alpha图像中获取png图像。请帮我把alpha bleedmatte处理掉。谢谢 + (UIImage*)pngImageWithJPEG:(UIImage*)jpegImage compressedAlphaFile:(UIImage*)alphaImage {

为了减小应用程序的大小,我使用jpg和alpha over png图像。我能够合并jpg和alpha图像以获得png,但问题是它会留下一个alpha bleedmatte,其中边缘有点尖锐。请帮我做这个

下面的代码帮助我从jpg和alpha图像中获取png图像。请帮我把alpha bleedmatte处理掉。谢谢

+ (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;
}

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

r = r*a;
g = g*a;
b = b*a;
要获取原始值,通常在读取图像后将其反转,如果a>0,则将RGB值除以a,但由于iOS本机以预乘alpha颜色工作,因此您甚至不必这样做

还可以尝试使用格式,它有时可能比单独的JPG+掩码小