   bug in GIF decoder
  Hello,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!thanksedit: I guess I should just post the code ; )--------------------------------------------------------------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;}[Edited by - zorba990 on June 5, 2006 6:20:57 PM]
  Functions are your friends! syntax highlighting is your friend too.   ;Quote:Original post by doodoFunctions are your friends! syntax highlighting is your friend too.Is there a way to post the syntax highlighted code?None of my tabs, etc showed up either.edit: I figured it out and modified the original post, thanks!   ;  I recommend you use the same LZW decompressor as this guyshttp://www.codeproject.com/staticctrl/gifanimation.asp?forumid=15674&select=1497552&df=100#xx1497552xxHe gives the link. It would be the quickest solution to your problem. Unless your willing to get out a book and read how decompressing gif files and bmp files actualy work.   ;Quote:Original post by doodoI recommend you use the same LZW decompressor as this guyshttp://www.codeproject.com/staticctrl/gifanimation.asp?forumid=15674&select=1497552&df=100#xx1497552xxHe gives the link. It would be the quickest solution to your problem. Unless your willing to get out a book and read how decompressing gif files and bmp files actualy work.That is the same code I am using.  It has an artifact in the decompressionon some gif files.So there is some small issue with the code.To clarify, the code at CodeProject uses code fromJuan Soulie's WinImage.  This is the same as the postedcode above (in terms of the LZ decoder).  But it has somesmall issue that causes a few pixels to sometimes be decoded incorrectly.Perhaps I should post this to a different forum?[Edited by - zorba990 on June 6, 2006 12:40:16 PM]