HI! I create a function to get a grayscale version of an image, but i have problem trying to pass by reference the destination of the bit(s) generated by this function:
void grayscale (const unsigned char *source, unsigned char **dest, int data_size) {
for (int i=0; i < data_size; i= i+4) {
int gray = (source[i] + source[i+1] + source[i+2]) / 3;
gray = 255 - (int)cos(source[i])*255;
*d开发者_JS百科est[i] = (char)gray;
*dest[i + 1] = (char)gray;
*dest[i + 2] = (char)gray; //HERE AN ERROR
*dest[i + 3] = (char)255;
}
}
I call this function with:
grayscale(source, &destination, width*height*4 );
Is there something wrong with pointers ? (i'm working on objective C and i obtain a EXC_BAD_ACCESS).
Thank you
It's not clear why you're using an additional level of indirection for dest
, since *dest
is not being modified but most likely you need to do the following - change:
*dest[i] = (char)source[i];
*dest[i + 1] = (char)gray;
*dest[i + 2] = (char)gray;
*dest[i + 3] = (char)255;
to:
(*dest)[i] = (char)source[i];
(*dest)[i + 1] = (char)gray;
(*dest)[i + 2] = (char)gray;
(*dest)[i + 3] = (char)255;
The reason for this is operator precedence/associativity.
Also it's hard to tell without seeing the calling code, but I'm guessing you may need to change:
(*dest)[i] = (char)source[i];
to:
(*dest)[i] = (char)source[i / 4];
if you're trying to do something like convert a single plane image to RGBA.
Your call is fine, you have an illegal memory access. Debug your indices and check the size of the array your passing in, also check that it is still in scope. Most likely i+2 is out of bounds.
精彩评论