Floating point CGImage浮点格式需要字节交换吗?
我正在做一些需要浮点灰度图像数据的图像处理工作。下面的方法Floating point CGImage浮点格式需要字节交换吗?,floating-point,pixel,cgimage,endianness,Floating Point,Pixel,Cgimage,Endianness,我正在做一些需要浮点灰度图像数据的图像处理工作。下面的方法-imagePlanarFData:是我目前从输入CIImage中提取此数据的方法: - (NSData *)byteswapPlanarFData:(NSData *)data swapInPlace:(BOOL)swapInPlace; { NSData * outputData = swapInPlace ? data : [data mutableCopy]; const i
-imagePlanarFData:
是我目前从输入CIImage
中提取此数据的方法:
- (NSData *)byteswapPlanarFData:(NSData *)data
swapInPlace:(BOOL)swapInPlace;
{
NSData * outputData = swapInPlace ? data : [data mutableCopy];
const int32_t * image = [outputData bytes];
size_t length = [outputData length] / sizeof(*image);
for (int i = 0;
i < length;
i++) {
int32_t * val = (int32_t *)&image[i];
*val = OSSwapBigToHostInt32(*val);
}
return outputData;
}
- (NSData *)imagePlanarFData:(CIImage *)processedImage;
{
NSSize size = [processedImage extent].size;
if (size.width == 0) {
return nil;
}
dispatch_once(&_onceToken, ^ {
_colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericGray);
_bytesPerRow = size.width * sizeof(float);
_cgx = CGBitmapContextCreate(NULL, size.width, size.height, 32, _bytesPerRow, _colorSpace, kCGImageAlphaNone | kCGBitmapFloatComponents);
// Work-around for CIImage drawing EXC_BAD_ACCESS when running with Guard Malloc;
// see <http://stackoverflow.com/questions/11689233/ciimage-drawing-exc-bad-access>
NSDictionary * options = nil;
if (getenv("MallocStackLogging") || getenv("MallocStackLoggingNoCompact")) {
NSLog(@"Forcing CIImageContext to use software rendering; see %@",
@"<http://stackoverflow.com/questions/11689233/ciimage-drawing-exc-bad-access>");
options = @{ kCIContextUseSoftwareRenderer: @YES };
}
_cix = [CIContext contextWithCGContext:_cgx
options:options];
_rect = CGRectMake(0, 0, size.width, size.height);
});
float * data = CGBitmapContextGetData(_cgx);
CGContextClearRect(_cgx, _rect);
[_cix drawImage:processedImage
inRect:_rect
fromRect:_rect];
NSData * pixelData = [NSData dataWithBytesNoCopy:data
length:_bytesPerRow * size.height
freeWhenDone:NO];
// For whatever bizarre reason, CoreGraphics uses big-endian floats (!)
return [self byteswapPlanarFData:pixelData swapInPlace:NO];
}
所以现在我在做这个:
pixelData = [mumble imagePlanarFData:processedImage];
// Swap data back to how it was before...
pixelData = [mumble byteswapPlanarFData:pixelData
swapInPlace:YES];
float * data = (float *)[pixelData bytes];
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data, bytesPerRow * size.height, NULL);
CGImageRef renderedImage = CGImageCreate(size.width, size.height, 32, 32, bytesPerRow, colorSpace, kCGImageAlphaNone | kCGBitmapFloatComponents, provider, NULL, false, kCGRenderingIntentDefault);
CGDataProviderRelease(provider);
NSImage * image = [[NSImage alloc] initWithCGImage:renderedImage
size:size];
CGImageRelease(renderedImage);
上述方法不起作用。相反,我从CoreGraphics得到了一张损坏的图像和一连串的投诉:
Mar 21 05:56:46 aoide.local LabCam[34235] <Error>: CMMConvLut::ConvertFloat 1 input (inf)
Mar 21 05:56:46 aoide.local LabCam[34235] <Error>: ApplySequenceToBitmap failed (-171)
Mar 21 05:56:46 aoide.local LabCam[34235] <Error>: ColorSyncTransformConvert - failed width = 160 height = 199 dstDepth = 7 dstLayout = 0 dstBytesPerRow = 1920 srcDepth = 7 srcLayout = 0 srcBytesPerRow = 640
Mar 21 05:56:46 aoide.local LabCam[34235]:CMMConvLut::ConvertFloat 1输入(inf)
Mar 21 05:56:46 aoide.local LabCam[34235]:ApplySequenceToBitmap失败(-171)
Mar 21 05:56:46 aoide.local LabCam[34235]:ColorSyncTransformConvert-失败的宽度=160高度=199 dstDepth=7 DSTLayment=0 dstBytesPerRow=1920 srcDepth=7 srcLayout=0 srcBytesPerRow=640
我哪里出错了?啊,发现问题了。。。原来在我将字节交换调用添加到
-imagePlanarFData:
之前还有一个剩余的例程,该例程也在原地交换字节
我仍然希望听到一些解释,解释为什么CoreGraphics希望在我们的little endian平台上以big endian字节顺序显示这些值。From:“作为一种历史怪诞,Quartz将不存在的endian标志默认为big endian,而不是host endian。”
Mar 21 05:56:46 aoide.local LabCam[34235] <Error>: CMMConvLut::ConvertFloat 1 input (inf)
Mar 21 05:56:46 aoide.local LabCam[34235] <Error>: ApplySequenceToBitmap failed (-171)
Mar 21 05:56:46 aoide.local LabCam[34235] <Error>: ColorSyncTransformConvert - failed width = 160 height = 199 dstDepth = 7 dstLayout = 0 dstBytesPerRow = 1920 srcDepth = 7 srcLayout = 0 srcBytesPerRow = 640