   Bug in GIF Decoder
  I guess I posed this in the wrong forum the first time,so here goes:I am using GIF/LZ decoding software located at http://www.cplusplus.com/src/ (GIFView).The code is quite fast, but in some rare cases itintroduces some artifacts into the decoded bmp.In my case it is a bitmap font (just some letters in a wide bitmapwith alot of transparent space), and some letters have extrablack pixels attached or are missing pixels.Is there any way to post pictures here?(there is a discussion about this over at The Code Project):http://tinyurl.com/fyrobI'm stuck using GIFs and don't really want to recode it.But I cannot seems to narrow down where the problem is.Any assistance would be greatly appreciated!thanks--------------------------------------------------------------int LZWDecoder (unsigned char * bufIn, unsigned char * bufOut,short InitCodeSize, int AlignedWidth,int Width, int Height, const int Interlace){int n;int row=0,col=0;// used to point output if Interlacedint nPixels, maxPixels;// Output pixel countershort CodeSize;// Current CodeSize (size in bits of codes)short ClearCode;// Clear code : resets decompressorshort EndCode;// End code : marks end of informationlong whichBit;// Index of next bit in bufInlong LongCode;// Temp. var. from which Code is retrievedshort Code;// Code extractedshort PrevCode;// Previous Codeshort OutCode;// Code to output// Translation Table:int Prefix[4097];// Prefix: index of another Codeint Suffix[4097];// Suffix: terminating charactershort FirstEntry;// Index of first free entry in tableshort NextEntry;// Index of next free entry in tableint OutStack[4097];// Output bufferint OutIndex;// Characters in OutStackint RowOffset;// Offset in output buffer for current row// Set up values that depend on InitCodeSize Parameter.CodeSize = InitCodeSize+1;ClearCode = (1 &lt;&lt; InitCodeSize);EndCode = ClearCode + 1;NextEntry = FirstEntry = ClearCode + 2;whichBit=0;nPixels = 0;maxPixels = Width*Height;RowOffset =0;while (nPixels&lt;maxPixels) {OutIndex = 0;// Reset Output Stack// GET NEXT CODE FROM bufIn:// LZW compression uses code items longer than a single byte.// For GIF Files, code sizes are variable between 9 and 12 bits // That's why we must read data (Code) this way:LongCode=*((long*)(bufIn+(whichBit/8)));// Get some bytes from bufInLongCode&gt;&gt;=(whichBit&7);// Discard too low bitsCode =(LongCode & ((1&lt;&lt;CodeSize)-1) );// Discard too high bitswhichBit += CodeSize;// Increase Bit Offset// SWITCH, DIFFERENT POSIBILITIES FOR CODE:if (Code == EndCode)// END CODEbreak;// Exit LZW Decompression loopif (Code == ClearCode) {// CLEAR CODE:CodeSize = InitCodeSize+1;// Reset CodeSizeNextEntry = FirstEntry;// Reset Translation TablePrevCode=Code;// Prevent next to be added to table.continue;// restart, to get another code}if (Code &lt; NextEntry)// CODE IS IN TABLEOutCode = Code;// Set code to output.else {// CODE IS NOT IN TABLE:OutIndex++;// Keep "first" character of previous output.OutCode = PrevCode;// Set PrevCode to be output}// EXPAND OutCode IN OutStack// - Elements up to FirstEntry are Raw-Codes and are not expanded// - Table Prefices contain indexes to other codes// - Table Suffices contain the raw codes to be outputwhile (OutCode &gt;= FirstEntry) {if (OutIndex &gt;= 4096 || OutCode &gt;=4096) {return 0;}OutStack[OutIndex++] = Suffix[OutCode];// Add suffix to Output StackOutCode = Prefix[OutCode];// Loop with preffix}// NOW OutCode IS A RAW CODE, ADD IT TO OUTPUT STACK.if (OutIndex &gt;= 4096) {return 0;}OutStack[OutIndex++] = OutCode;// ADD NEW ENTRY TO TABLE (PrevCode + OutCode)// (EXCEPT IF PREVIOUS CODE WAS A CLEARCODE)if (PrevCode!=ClearCode) {Prefix[NextEntry] = PrevCode;Suffix[NextEntry] = OutCode;NextEntry++;// Prevent Translation table overflow:if (NextEntry&gt;4096) {return 0;} // INCREASE CodeSize IF NextEntry IS INVALID WITH CURRENT CodeSizeif (NextEntry &gt;= (1&lt;&lt;CodeSize)) {if (CodeSize &lt; 12) {CodeSize++;}else {// Do nothing. Maybe next is Clear Code.int iTemp = 9999;}}}PrevCode = Code;// Avoid the possibility of overflow on 'bufOut'.if (nPixels + OutIndex &gt; maxPixels) OutIndex = maxPixels-nPixels;// OUTPUT OutStack (LAST-IN FIRST-OUT ORDER)for (n=OutIndex-1; n&gt;=0; n--) {if (col==Width)// Check if new row.{if (Interlace) {// If interlaced::if ((row&7)==0) {row+=8; if (row&gt;=Height) row=4;}else if ((row&3)==0) {row+=8; if (row&gt;=Height) row=2;}else if ((row&1)==0) {row+=4; if (row&gt;=Height) row=1;}else row+=2;}else// If not interlaced:row++;RowOffset=row*AlignedWidth;// Set new row offsetcol=0;}bufOut[RowOffset+col]=OutStack[n];// Write outputcol++;nPixels++;// Increase counters.}}// while (main decompressor loop)return whichBit;}
  My first guess is that there might be alignment, padding, or endian issues.;Quote:Original post by JohnBoltonMy first guess is that there might be alignment, padding, or endian issues.Thanks for the reply,On a whim I saved the same gif as interlaced and it decodes perfectly.Still, I don't see anything problematic in that part of the code.   