解码 CGCreateImage 中忽略的值

发布于 2024-11-04 22:11:41 字数 578 浏览 3 评论 0原文

我使用以下代码创建单色图像:

  CGColorSpaceRef cgColorSpace = CGColorSpaceCreateDeviceGray();
  CGImageRef cgImage = CGImageCreate (width, height, 1, 1, rowBytes, colorSpace, 0, dataProvider, decodeValues, NO, kCGRenderingIntentDefault);

其中 decodeValues 是 2 个 CGFloat 的数组,等于 {0,1} 。这给了我一个很好的图像,但显然我的数据(来自 PDF 图像蒙版)是白底黑字,而不是黑底白字。为了反转图像,我尝试将 decodeValues 的值设置为 {1,0},但这根本没有改变任何东西。实际上,无论我在 decodeValues 中输入什么无意义的值,我都会得到相同的图像。

为什么这里忽略 decodeValues ?如何反转黑白?

I am creating a monochrome image with the following code:

  CGColorSpaceRef cgColorSpace = CGColorSpaceCreateDeviceGray();
  CGImageRef cgImage = CGImageCreate (width, height, 1, 1, rowBytes, colorSpace, 0, dataProvider, decodeValues, NO, kCGRenderingIntentDefault);

where decodeValues is an array of 2 CGFloat's, equal to {0,1}. This gives me a fine image, but apparently my data (which comes from a PDF image mask) is black-on-white instead of white-on-black. To invert the image, I tried to set the values of decodeValues to {1,0}, but this did not change anything at all. Actually, whatever nonsensical values I put into decodeValues, I get the same image.

Why is decodeValues ignored here? How do I invert black and white?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

眸中客 2024-11-11 22:11:41

这是一些用于创建和绘制单色图像的代码。它与您的相同,但具有更多上下文(并且无需进行必要的清理):

size_t width = 200;
size_t height = 200;
size_t bitsPerComponent = 1;
size_t componentsPerPixel = 1;
size_t bitsPerPixel = bitsPerComponent * componentsPerPixel;
size_t bytesPerRow = (width * bitsPerPixel + 7)/8;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CGBitmapInfo bitmapInfo = kCGImageAlphaNone;
CGFloat decode[] = {0.0, 1.0};
size_t dataLength = bytesPerRow * height;
UInt32 *bitmap = malloc( dataLength );
memset( bitmap, 255, dataLength );

CGDataProviderRef dataProvider = CGDataProviderCreateWithData( NULL, bitmap, dataLength, NULL);

CGImageRef cgImage = CGImageCreate (
                          width,
                          height,
                          bitsPerComponent,
                          bitsPerPixel,
                          bytesPerRow,
                          colorspace,
                          bitmapInfo,
                          dataProvider,
                          decode,
                          false,
                          kCGRenderingIntentDefault
                          );  

CGRect destRect = CGRectMake(0, 0, width, height);
CGContextDrawImage( context, destRect, cgImage );

如果我将解码数组更改为 CGFloatdecode[] = {0.0, 0.0}; 我总是得到黑色图像。
如果您已经尝试过并且没有任何效果(您说无论您使用什么值,您都会得到相同的图像),或者:您实际上并没有传递这些值,但您认为您是,或者:不知何故,您不是实际检查 CGImageCreate 的输出。

here's some code for creating and drawing a mono image. It's the same as yours but with more context (and without necessary cleanup):

size_t width = 200;
size_t height = 200;
size_t bitsPerComponent = 1;
size_t componentsPerPixel = 1;
size_t bitsPerPixel = bitsPerComponent * componentsPerPixel;
size_t bytesPerRow = (width * bitsPerPixel + 7)/8;
CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceGray();
CGBitmapInfo bitmapInfo = kCGImageAlphaNone;
CGFloat decode[] = {0.0, 1.0};
size_t dataLength = bytesPerRow * height;
UInt32 *bitmap = malloc( dataLength );
memset( bitmap, 255, dataLength );

CGDataProviderRef dataProvider = CGDataProviderCreateWithData( NULL, bitmap, dataLength, NULL);

CGImageRef cgImage = CGImageCreate (
                          width,
                          height,
                          bitsPerComponent,
                          bitsPerPixel,
                          bytesPerRow,
                          colorspace,
                          bitmapInfo,
                          dataProvider,
                          decode,
                          false,
                          kCGRenderingIntentDefault
                          );  

CGRect destRect = CGRectMake(0, 0, width, height);
CGContextDrawImage( context, destRect, cgImage );

If i change the decode array to CGFloat decode[] = {0.0, 0.0}; i always get a black image.
If you have tried that and it didn't have any effect (you say you get the same image whatever values you use), either: you aren't actually passing in those values but you think you are, or: somehow you aren't actually examining the output of CGImageCreate.

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