转换为 JPEG 时我应该如何考虑 CGImageAlphaInfo?

发布于 2024-10-08 00:23:14 字数 1444 浏览 7 评论 0原文

我正在将其交给此方法,并使用生成的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野心澎湃 2024-10-15 00:23:14

这并不重要,因为 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 or kCGImageAlphaNoneSkipLast. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文