Hi freaks. I have a basic Run Length Encoder but it is getting caught in a loop and freezing the system.
/** * inRaw, pointer to raw pixel data, r,g,b - 24bit * inSize size in bytes of the raw image */ void encode_rle(char * inRaw, unsigned int inSize){ struct size24 * outBuffer = safe_malloc(1850000); struct size24 * inBuffer = (struct size24 *)inRaw; struct size24 * inStart = (struct size24 *)inRaw; unsigned int outStart = (unsigned int)outBuffer; struct size24 * pixel1 = NULL, * pixel2 = NULL, * pixel3 = NULL, * pixel = NULL, marker; // arbitrary value - set_value(&marker, 0x101010); unsigned int count = 0; // kick off the comparison algorithm memcpy(pixel1, inBuffer++, 3); memcpy(pixel2, inBuffer++, 3); memcpy(pixel3, inBuffer++, 3); int p = 0; while(((char *)inBuffer - (char *)inStart) - inSize != 0){ // detected a string of three matching pixels, begin to traverse the pixels if they match if(memcmp(pixel1, pixel2, 3) == 0){ if(memcmp(pixel2, pixel3, 3) == 0){ // set the MARKER pixel set_value(outBuffer++, 0x101010); // three bytes match thus far count = 3; // next pixel to compare memcpy(pixel, inBuffer++, 3); // while string of likewise pixels begin counting while(memcmp(pixel3, pixel, 3) == 0){ count++; memcpy(pixel, inBuffer++, 3); printf("\n\rloop %i", count); } // set the repetition count set_value(outBuffer++, count); // set the repetition pixel color memcpy(outBuffer++, pixel3, 3); // load next palette to compare memcpy(pixel, pixel, 3); memcpy(pixel2, inBuffer++, 3); memcpy(pixel3, inBuffer++, 3); } else{ if(memcmp(pixel1, &marker, 3) == 0) memcpy(outBuffer++, pixel1, 3); memcpy(outBuffer++, pixel1, 3); memcpy(pixel1, pixel2, 3); memcpy(pixel2, pixel3, 3); memcpy(pixel3, inBuffer++, 3); } } else{ if(memcmp(pixel1, &marker, 3) == 0) memcpy(outBuffer++, pixel1, 3); memcpy(outBuffer++, pixel1, 3); memcpy(pixel1, pixel2, 3); memcpy(pixel2, pixel3, 3); memcpy(pixel3, inBuffer++, 3); } } printf("\n\rFile Size before compression %i", (unsigned int)inSize); printf("\n\rFile Size after compression %i\n\r\n\r", (unsigned int)outBuffer - outStart); }
it freezes here after say 650 loops:
// while string of likewise pixels begin counting while(memcmp(pixel3, pixel, 3) == 0){ count++; memcpy(pixel, inBuffer++, 3); printf("\n\rloop %i", count); }
out of interest, the printf statement displays the following...
Now, I'm merely counting the number of consecutive pixels and am not writing to any memory location which leads me to believe the problem is somewhere else. Also, it is failing when doing the printf.. yes, without the printf it still fails.
What are your views?