For one thing, you are leaking the memory pointed to by grayScaleImageData
:
In grayScaleImageDataFromImage you do
uint8_t *grayScaleImageData = (uint8_t *) malloc(imageWidth * imageHeight);
which is fine, then in createGrayScaleDescription
you set vImage->grayScaleImage
to point to that memory, which is also fine.
But then in your 1 to 100 loop, you free vImage without freeing the memory pointed to by vImage->grayScaleImage
first. That is a memory leak.
I don't think that should be crashing your application, but it is something you need to fix. Nothing else jumps out at me, but I'll take a closer look.
EDIT: On closer inspection, it looks like you are overrunning the buffer you allocated for
grayScaleImageData
.
You malloc it with:
uint8_t *grayScaleImageData = (uint8_t *) malloc(imageWidth * imageHeight);
but then you fill that buffer with:
uint32_t rgbPixel = rgbImageData[y*imageWidth+x];
grayScaleImageData[y*imageWidth + x] = 0.299*((rgbPixel>>24)&255) + 0.587*((rgbPixel>>16)&255) + 0.114*((rgbPixel>>8)&255);
You've allocated (imageWidth * imageHeight) 8-bit uints, but then you fill it with 32-bit uints.
Perhaps that is what is causing you to crash? Maybe try changing that malloc to
uint32_t *grayScaleImageData = (uint32_t *) malloc(imageWidth * imageHeight * sizeof (uint32_t));
as you did above for rgbImageData? That, or cast the result of your bit shifting calculations to a uint8_t before setting it into grayScaleImageData:
uint32_t rgbPixel = rgbImageData[y*imageWidth+x];
grayScaleImageData[y*imageWidth + x] = (uint8_t)(0.299*((rgbPixel>>24)&255) + 0.587*((rgbPixel>>16)&255) + 0.114*((rgbPixel>>8)&255));