转换为 JPEG 时我应该如何考虑 CGImageAlphaInfo?
我正在将其交给此方法,并使用生成的 CGImageRef 将其添加到设置为最终确定为 kUTTypeJPEG 的 CGImageDestinationRef 中:
- (CGImageRef)scaleImage:(CGImageRef)fullImageRef originalSize:(CGSize)originalSize scale:(CGFloat)scale;
{
CGSize imageSize = originalSize;
CGRect scaledRect = CGRectMake(0.0, 0.0, imageSize.width * scale, imageSize.height * scale);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(fullImageRef);
CGContextRef context = CGBitmapContextCreate(NULL, scaledRect.size.width, scaledRect.size.height, 8, 0, colorSpace, CGImageGetAlphaInfo(fullImageRef));
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGImageRef subImage = CGImageCreateWithImageInRect(fullImageRef, scaledRect);
CGContextDrawImage(context, scaledRect, subImage); // offscreen
CGImageRef scaledImage = CGBitmapContextCreateImage(context);
CGImageRelease(subImage);
subImage = NULL;
subImage = scaledImage;
CGImageRelease(scaledImage);
CGContextFlush(context);
CGContextRelease(context);
return subImage;
}
我需要确保生成的 JPEG 与原始图像的颜色最匹配。
我不清楚 CGImageAlphaInfo 常量对于我的目的的实际作用。
我已经阅读了 Quartz 文档,以及优秀的《Programming With Quartz》一书(Gelphman 和 Laden 撰写),但我仍然不确定我的方法。
我已在 jpeg 的属性字典中将属性 kCGImageDestinationLossyCompressionQuality 设置为 1.0,并捕获其他属性。
我应该做更多的事情来保持原件的颜色完整性吗?
这是macos,并且使用gc。
i'm handing off to this method and using the resultant CGImageRef
adding it to a CGImageDestinationRef
that is setup for finalizing as a kUTTypeJPEG
:
- (CGImageRef)scaleImage:(CGImageRef)fullImageRef originalSize:(CGSize)originalSize scale:(CGFloat)scale;
{
CGSize imageSize = originalSize;
CGRect scaledRect = CGRectMake(0.0, 0.0, imageSize.width * scale, imageSize.height * scale);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(fullImageRef);
CGContextRef context = CGBitmapContextCreate(NULL, scaledRect.size.width, scaledRect.size.height, 8, 0, colorSpace, CGImageGetAlphaInfo(fullImageRef));
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGImageRef subImage = CGImageCreateWithImageInRect(fullImageRef, scaledRect);
CGContextDrawImage(context, scaledRect, subImage); // offscreen
CGImageRef scaledImage = CGBitmapContextCreateImage(context);
CGImageRelease(subImage);
subImage = NULL;
subImage = scaledImage;
CGImageRelease(scaledImage);
CGContextFlush(context);
CGContextRelease(context);
return subImage;
}
i need to be sure that the resulting JPEG is the best possible color match for the original.
what i am unclear about is the actual role of CGImageAlphaInfo constant for my purposes.
i've read Quartz docs, and also the excellent Programming With Quartz book (by Gelphman and Laden), but i'm still not sure of my methods.
i've set the property kCGImageDestinationLossyCompressionQuality to 1.0 in the jpeg's property dictionary, along with capturing other properties.
should i be doing something more to maintain the color integrity of the original?
this is macos, and using gc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不重要,因为 JPEG 不支持 alpha(至少不支持 外部遮罩 或 扩展),因此您可以预期 CGImageDestination 会丢弃 alpha出口阶段的渠道。
我会首先尝试原始图像的 Alpha 信息,如果无法以这种方式创建位图上下文,我会使用 kCGImageAlphaNoneSkipFirst 或 kCGImageAlphaNoneSkipLast 。当然,对于其他目标文件格式,我会使用带有预乘颜色的 alpha 像素格式之一。
此页面包含 CGBitmapContext 支持的组合的完整列表。
It doesn't much matter, because JPEG doesn't support alpha (at least, not without external masking or an extension), so you can expect that CGImageDestination will throw away the alpha channel at the export stage.
I would try the original image's alpha info first, and if I can't create the bitmap context that way, I would use either
kCGImageAlphaNoneSkipFirst
orkCGImageAlphaNoneSkipLast
. For other destination file formats, of course, I'd use one of the alpha-with-premultiplied-colors pixel formats.This page has the full list of combinations supported by CGBitmapContext.