Question

In my program I am holding the pixel data of a UIImage in a basic C array:

// It can be UInt8 as it is greyscaled, values can only be 0-255
@property (nonatomic, readwrite) UInt8 *imageData;

Now in my greyscaling algorithm I initialize it:

self.imageData = malloc(sizeof(UInt8) * width * height);

then I fill in the data as I greyscale. This works. However, in a later algorithm I go through and make values either 0 or 255 through some magic. I thought that was working fine... but a later algorithm looking for these values was getting 1's and 3's. So I went and threw this in right after I change the value from 0-255 to be either 0 or 255:

// Magic loop. Starts with current value at pixelIndex, gets curVal. 
self.imageData[pixelIndex] = curVal;
NSAssert(self.imageData[pixelIndex] != curVal, @"Whattt");

And lo and behold it isn't actually changing the value of my array. Is there something I'm missing? Are these arrays a one time set and then they become readonly? I know NSArrays are like that, but this is just a simple C pointer-based array... or are they the same?

Whatever the reason, how can I make it so I can properly alter these values?

Was it helpful?

Solution

This should work fine. I think your problem is just that you have your assertion backwards — the condition is supposed to be an expression that you want to be true, so currently you are asserting "After I set self.imageData[pixelIndex] to be curVal, it should not be equal to curVal." Instead, that should be:

NSAssert(self.imageData[pixelIndex] == curVal, @"Whattt");

BTW, when doing this sort of assertion, I always think it's helpful to include the expected and actual values so you can see what's going on. In this case, you could do:

NSAssert3(self.imageData[pixelIndex] == curVal, @"Image data at pixel index %d should have been %d, but instead was %d", pixelIndex, curVal, self.imageData[pixelIndex]);

This would make it more obvious that the values actually are equal and something is going wrong in your equality check.

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