我想在most上将每个像素都存储在32位浮点中的平面灰度图像转换为XRGB图像。vImage似乎是最合适的工具。我写了一个简短的函数来做这件事,但它在EXC_BAD_ACCESS崩溃的vImage调用中崩溃了。下面是我的代码:
- (NSData *) convertToRGB_vImage {
size_t numRows = self.rows;
size_t numColumns = self.columns;
size_t height = numRows;
size_t width = numColumns;
size_t inRowBytes = width*sizeof(float);
size_t outRowBytes = byteAlign(inRowBytes, 64);
size_t destinationSize = outRowBytes * numRows;
void *outData = malloc(destinationSize);
void *inData = self.sourceData.mutableBytes; // source pixels in an NSMutableData
Pixel_8 alpha = 255; // fully opaque
vImage_Buffer red = { inData, width, height, inRowBytes };
vImage_Buffer green = { inData, width, height, inRowBytes };
vImage_Buffer blue = { inData, width, height, inRowBytes };
vImage_Buffer dest = { outData, width, height, outRowBytes }; // 3
Pixel_FFFF maxFloat = { 1.0, 1.0, 1.0, 1.0};
Pixel_FFFF minFloat = { 0.0, 0.0, 0.0, 0.0};
vImage_Flags flags = kvImageNoFlags;
vImage_Error error = vImageConvert_PlanarFToXRGB8888 (alpha, &red, &green, &blue, &dest, maxFloat, minFloat, flags);
if (error != 0) {
NSLog(@"vImage error %zd", error);
}
NSMutableData *colorData = [[NSMutableData alloc] initWithBytesNoCopy:outData length:destinationSize];
return colorData;
}我尝试了同一主题的几个变体,但都没有成功。我做错什么了?
发布于 2016-03-16 00:35:15
在vImage_Buffer结构中,高度优先于宽度。除非这些数字相等,否则这可能是您的问题。
假设您的编译器允许,使用命名字段会更安全:
vImage_Buffer b = (vImage_Buffer){
.data = my_data,
.width = the_width,
.height = the_height,
.rowBytes = ROUND_SIZE_UP( the_width * pixel_bytes, 64)
};https://stackoverflow.com/questions/36013487
复制相似问题