Question

I am trying to modify pixel values (8 bits per channel RGBA) by numerically increasing/decreasing the values by a certain amount. How can I do this in Objective-C or C? The following code generates a "Error: EXC_BAD_ACCESS" everytime.

// Try to Increase RED by 50
    for(int i = 0; i < myLength; i += 4) {

        //NSLog prints the values FINE as integers
            NSLog(@"(%i/%i/%i)", rawData[i], rawData[i+1], rawData[i+2]);

            //But for some reason I cannot do this
        rawData[i]+=50;

}

and even

// Try to set RED to 50
    for(int i = 0; i < myLength; i += 4) {

            //I cannot even do this...
        unsigned char newVal = 50;
        rawData[i] = 50;

}

Sidenote: rawData is a data buffer of type unsigned char

Was it helpful?

Solution

It's possible that you're overrunning the end of your allocated buffer, and that's why you're getting the access violation. That most likely means that your math is wrong in the allocation, or your rawData pointer is of the wrong type.

If you are accessing the raw data of a loaded UIImage, it might be mapped into memory read-only. You'd need to copy the data into a buffer that you allocated, most likely.

OTHER TIPS

Hmm... What's rawdata? Maybe it's a const type which you can not modify?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top