0

Well, the title is pretty self-explanatory. I have tested this code thoroughly, and what I have found is that the code is somehow resetting "y" when getPixel is called. It shouldn't be possible you'd think, because the method doesn't take a pointer, but somehow that is what's happening. If I remove the line of code that calls getPixel, the code completes, although obviously nothing useful is produced. If anyone can figure out what's going on here, I would be most appreciative. (getPixel is in NSBitmapImageRep, FYI)

NSInteger pixWide = theCAPTCHA.pixelsWide;
NSInteger pixHigh = theCAPTCHA.pixelsHigh;
for (NSInteger x = 0; x<pixWide;x++)
{
    for (NSInteger y = 0; y<pixHigh;y++)
    {
        unsigned int thePixel[3];
        [theCAPTCHA getPixel:(unsigned long *)thePixel atX:x y:y];
        for (int i = 0; i<3; i++) {
            theColors[i+(y*3)+(x*pixHigh)] = thePixel[i];
        }
    }
}
  • thePixel is an unsigned int but passed in as unsigned long. The function might be overflowing your integer and changing y. Try declaring thePixel as unsigned long – Jack Mar 27 '14 at 20:33
  • Thanks! That worked! I don't understand why y was being effected though. Obviously I missed something in computer science... Is it because that the overflow was scribbling over y's value? – William T Froggard Mar 27 '14 at 20:52
  • yes, it assumes it has 64-bits of space to write, but in fact it only has 32. Thus it writes 32 extra bits into memory which happens (by coincidence) to overwite y – Jack Mar 27 '14 at 20:58

1 Answers1

1

thePixel is an unsigned int but passed in as unsigned long. The function might be overflowing your integer and changing y. Try declaring thePixel as unsigned long

In memory, the variables are just laid out one after another. The function getPixel:at:y: assumes it has 64 bits to write to in the input, because it takes an unsigned long *. However your own variable thePixel is an unsigned int * and writing an unsigned long to it will cause an overflow in memory

Jack
  • 16,677
  • 8
  • 47
  • 51
  • Thanks. I'm not even sure why I was typecasting it in this manner. I can find no line of code where I need thePixel to be an unsigned int... Ah well, a lesson for the future! – William T Froggard Mar 27 '14 at 21:06