使用 CGBitmapContextCreate 始终为黑色的 32 位灰度图像
我使用以下代码来显示 32 位灰度图像。即使我明确地将每个像素设置为 4294967297(应该是白色),最终结果始终是黑色。我在这里做错了什么?该图像仅为 64x64 像素。
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
ptr = (float*)malloc(4*xDim*yDim);
for(i=0;i<yDim;i++)
{
for(j=0;j<xDim;j++)
{
ptr[i*xDim + j] = 4294967297;
}
}
CGContextRef bitmapContext = CGBitmapContextCreate(
ptr,
xDim,
yDim,
32,
4*xDim,
colorSpace,
kCGImageAlphaNone | kCGBitmapFloatComponents);
//ptr = CGBitmapContextGetData(bitmapContext);
//NSLog(@"%ld",sizeof(float));
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
NSRect drawRect;
drawRect.origin = NSMakePoint(1.0, 1.0);
drawRect.size.width = 64;
drawRect.size.height = 64;
NSImage *greyscale = [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];
[greyscale drawInRect:drawRect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
I'm using the following code to display a 32 bit Grayscale image. Even if I explicitly set every pixel to be 4294967297 (which ought to be white), the end result is always black. What am I doing wrong here? The image is just 64x64 pixels.
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
ptr = (float*)malloc(4*xDim*yDim);
for(i=0;i<yDim;i++)
{
for(j=0;j<xDim;j++)
{
ptr[i*xDim + j] = 4294967297;
}
}
CGContextRef bitmapContext = CGBitmapContextCreate(
ptr,
xDim,
yDim,
32,
4*xDim,
colorSpace,
kCGImageAlphaNone | kCGBitmapFloatComponents);
//ptr = CGBitmapContextGetData(bitmapContext);
//NSLog(@"%ld",sizeof(float));
CGImageRef cgImage = CGBitmapContextCreateImage(bitmapContext);
NSRect drawRect;
drawRect.origin = NSMakePoint(1.0, 1.0);
drawRect.size.width = 64;
drawRect.size.height = 64;
NSImage *greyscale = [[NSImage alloc] initWithCGImage:cgImage size:NSZeroSize];
[greyscale drawInRect:drawRect
fromRect:NSZeroRect
operation:NSCompositeSourceOver
fraction:1.0];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果不指定字节序,Quartz 将默认为大字节序。如果您使用的是 Intel Mac,则这是错误的。您需要显式设置字节顺序,最好的方法是将您的标志更改为:
无论您的 CPU 如何,这都将正常工作(为了将来的兼容性!)。
您可以在这里找到更多详细信息:http://lists.apple。 com/archives/Quartz-dev/2011/Dec/msg00001.html
If you don't specify the endianness, Quartz will default to big-endian. If you are on an Intel Mac, this will be wrong. You will need to explicitly set the endianness, and the best way to do that is to change your flags to:
This will work properly regardless of your CPU (for future compatibility!).
You can find more detail here: http://lists.apple.com/archives/Quartz-dev/2011/Dec/msg00001.html
您正在使用浮动组件图像。确保 ptr 的类型为 float* 并尝试将值设置为 0.5f 而不是 4294967297。
You are using a float component image. Make sure ptr has type float* and try setting values to 0.5f instead of 4294967297.
您是否显示了您正在使用的确切代码?
2^32 - 1 = 4294967295
如果您使用 4294967297 我怀疑您会溢出并且实际值为 2 !
Are you showing the exact code you are using ?
2^32 - 1 = 4294967295
If you are using 4294967297 I suspect you are getting overflow and an actual value of 2 !
CGBitmapContextCreate 不支持 32 位浮点灰度。 CGBitmapContext创建支持的颜色空间
32 bit floating point gray scale is not supported by CGBitmapContextCreate. CGBitmapContextCreate Supported Color Spaces