I want to convert a planar greyscale image where each pixel is stored in 32-bit floats into a XRGB image on Mac OS X. vImage seems to be the most appropriate tool. I wrote a short function that does that, but it crashes in the vImage call with a EXC_BAD_ACCESS crash. Here is my code:
- (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;
}
I tried a few variations on the same theme, with no success. What did I do wrong?