0

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?

malat
  • 12,152
  • 13
  • 89
  • 158
Jean-Denis Muys
  • 6,772
  • 7
  • 45
  • 71

1 Answers1

1

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)
};
Community
  • 1
  • 1
JWWalker
  • 22,385
  • 6
  • 55
  • 76