crash in vImage convert function

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?


In the vImage_Buffer structure, height comes before width. Unless those numbers are equal, that could be your problem.

Assuming your compiler allows it, using named fields is safer:

vImage_Buffer b = (vImage_Buffer){
   .data = my_data,
   .width = the_width,
   .height = the_height,
   .rowBytes = ROUND_SIZE_UP( the_width * pixel_bytes, 64)

Need Your Help

How to make table view load all data at once and not change said data while scrolling?

ios swift uitableview

I'm making a simple table view app to display and play all the iOS System sounds.

How to best integrate Flex and Django?

django flex ria

I'm building a site with a Flex frontend and Django backend.