使用CGContextDrawImage时的海报化效果,float CGBitmapContext
为了从 CGImage 检索像素值,我使用 CGContextDrawImage (如下所述: 如何获取像素来自 UIImage (Cocoa Touch) 或 CGImage (Core Graphics) 的数据?)。唯一的区别是我创建了 128 bpp 浮动组件上下文,而不是通常的 32 bpp 上下文。从使用选项 kCGImageSourceShouldAllowFloat 创建的 CGImageSource 获取的源 CGImage。这样我希望能够访问与我的位图上下文的颜色空间颜色匹配的浮点像素值,并将它们用于进一步的图像处理。问题是生成的图像数据似乎失去了动态范围。它可以在阴影、纯蓝天区域看到。它们变得轮廓分明且缺乏细节。一些调查显示问题出现在 CGContextDrawImage 中(源 CGImage 包含完整的动态范围,通过 CGImageDestination 保存它证明了这一点)并且在 CGContextDrawImage 上下文内容变得色调分离之后。
经过更多调查后我发现: http://lists.apple.com/archives/quartz- dev/2007/mar/msg00026.html 这使我得出结论,问题不在于我的代码,而在于核心图形或预期行为。
我的问题是:使用核心显卡从图像中获取浮点数据的正确方法是什么?
To retrieve pixel values from CGImage I use CGContextDrawImage (like described here:
How to get pixel data from a UIImage (Cocoa Touch) or CGImage (Core Graphics)?). The only difference is that I create 128 bpp float components context, not usual 32 bpp context. The source CGImage obtained from CGImageSource created with option kCGImageSourceShouldAllowFloat. That way I hoped to get access to float pixel values color matched with my bitmap context's color space and use them in further image processing. The problem is that resulting image data seems to be loosing dynamic range. It can be seen in shadow, plain blue sky areas. They become contoured and lacking detail. Some investigation showed the problem occurs in CGContextDrawImage (Source CGImage contains full dynamic range, saving it through CGImageDestination proves it) and after CGContextDrawImage context contents become posterized.
After some more investigation I found this:
http://lists.apple.com/archives/quartz-dev/2007/mar/msg00026.html
That led me to conclusion that problem is not in my code but in core graphics or that is intended behaviour.
My question is: what is correct way to obtain floating point data from image using core graphics?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
经过更多调查后,问题缩小到以下范围:当 8 位图像绘制到使用线性颜色空间 (kCGColorSpaceGenericRGBLinear) 创建的 128 bpp 浮动上下文时,会发生色调分离。如果我将相同的图像绘制到使用 kCGColorSpaceGenericRGB 创建的上下文,然后从该上下文中检索 CGImage 并将第二个图像绘制到线性颜色空间上下文,则一切正常。
其他解决方案(解决方法?)是使用 Core Image:从源 CGImage 创建 CIImage 并将其绘制到使用相应的 kCGColorSpaceGenericRGBLinear CGContext 创建的 CIContext。但这是 OS X 上的唯一选项(iOS 上不是)。
After some more investigation the problem is narrowed down to the following: posterization occurs when 8 bit image is drawn to the 128 bpp float context created with linear color space (kCGColorSpaceGenericRGBLinear). If I draw the same image to context created with kCGColorSpaceGenericRGB then retrieve CGImage from that context and draw that second image to linear color space context everything is ok.
Other solution (workaround ?) is to use Core Image: create CIImage from source CGImage and draw it to CIContext created with corresponding kCGColorSpaceGenericRGBLinear CGContext. But that only option on OS X (not on iOS).