2016-03-15 59 views
0

我想将在32位浮点数中存储每个像素的平面灰度图像转换为Mac OS X上的XRGB图像。vImage似乎是最合适的工具。我写了一个简短的函数,但它在EXI_BAD_ACCESS崩溃的vImage调用中崩溃。这里是我的代码: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; 
} 

我试了一些相同的主题的变化,没有成功。我做错了什么?

回答

0

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) 
};