0

我想将每个像素以 32 位浮点数存储的平面灰度图像转换为 Mac OS X 上的 XRGB 图像。 vImage 似乎是最合适的工具。我编写了一个简短的函数来执行此操作,但它在 vImage 调用中崩溃并出现 EXC_BAD_ACCESS 崩溃。这是我的代码:

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

我在同一主题上尝试了一些变体,但没有成功。我做错了什么?

4

1 回答 1

1

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)
};
于 2016-03-15T16:35:15.183 回答