0

I have used vImageHistogramCalculation in my current application for calculating the histogram of image and I am getting EXC_BAD_ACCESS in some cases.I went through the cases as follows-

- (void)histogramForImage:(UIImage *)image {
    vImage_Buffer inBuffer;
    CGImageRef img = image.CGImage;

    CGDataProviderRef inProvider = CGImageGetDataProvider(img);
    CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);

    inBuffer.width = CGImageGetWidth(img);
    inBuffer.height = CGImageGetHeight(img);
    inBuffer.rowBytes = CGImageGetBytesPerRow(img);

    inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);

    vImagePixelCount histogram[4][8] = {{0}};
    vImagePixelCount *histogramPointers[4] = { &histogram[0][0], &histogram[1][0], &histogram[2][0], &histogram[3][0] };
    vImage_Error error = vImageHistogramCalculation_ARGBFFFF(&inBuffer, histogramPointers, 8, 0, 255, kvImageNoFlags);

    if (error) {
        NSLog(@"error %ld", error);
    }

    CGDataProviderRelease(inProvider);
}
  1. I used the image from iPhone Camera Roll in PNG form and manually put in bundle in the above code and it works fine.

  2. I used the same code for the image from iPhone Camera Roll in JPG format then I am getting EXC_BAD_ACCESS error.

  3. I tried to get image from Camera Roll using Photos Framework and then passed to the same code then also I am getting EXC_BAD_ACCESS error.

What I actually need is to find the histogram of all the images of iPhone Camera Roll.So I am unable to find out why the above code is working fine for one image format and failing for other.Is there any other reason for the crash?

EDIT 1- what I came to its not about image format its working fine for some JPG images too but still its crashing in some cases.How should I figure that out?

Reference:

https://developer.apple.com/library/mac/documentation/Performance/Reference/vImage_histogram/#//apple_ref/c/func/vImageHistogramCalculation_ARGBFFFF

Compute the histogram of an image using vImageHistogramCalculation

Community
  • 1
  • 1
Imran
  • 1,715
  • 2
  • 20
  • 42
  • You seem not to allocate memory to hold the computed histogram: [check this answer](http://stackoverflow.com/questions/25633053/compute-the-histogram-of-an-image-using-vimagehistogramcalculation) – Tomas Camin Oct 16 '15 at 09:27

1 Answers1

1

vImageHistogramCalculation_ARGBFFFF is for four-channel floating-point data. Chances are extremely high that the data you are getting out of the data provider is RGB or RGBA 8-bit integer data. Check with the CGImageRef for the storage format of the image data.

If you want a specific data format out of a CGImageRef, you can call vImageBuffer_InitWithCGImage().

Ian Ollmann
  • 1,592
  • 9
  • 16