GIF-encoder lazarus / 39-Graphics ---------------------------------------------------------------------- stab · 2013-02-13 07:21 Hi all, I'm trying to make a GIF-encoder in Lazarus - FPC(Object Pascal) and have been reading a very precise article at http://giflib.sourceforge.net/whatsinagif/index.html, but I don't understand the last part on "LZW Image Data - Saving the Code Stream as Bytes". The example code stream is: #4 #1 #6 #6 #2 #9 #9 #7 #8 #10 #2 #12 #1 #14 #15 #6 #0 #21 #0 #10 #7 #22 #23 #18 #26 #7 #10 #29 #13 #24 #12 #18 #16 #36 #12 #5 Jumping back to our sample image, we see that we have a minimum code size value of 2 which means out first code size will be 3 bits long. Out first three codes, #1 #6 and #6, would be coded as 001 110 and 110. If you see at Step 6 of the encoding, we added a code of #7 to our code table. This is our clue to increase our code size because 7 is equal to 2^3-1 (where 3 is our current code size). Thus, the next code we write out, #2, will use the new code size of 4 and therefore look like 0010. In the decoding process, we again would increase our code size when we read the code for #7 and would read the next 4, rather than the next 3 bits, to get the next code. In the sample table above this occurs in Step 2. Why should the codesize be increased at code #2. IMHO 2 fit within 3 bit (or actually in 2 bits)? I my opinion you should increase codesize when you reach code #9. I realize that I've misunderstood something fundamental, but cannot see what.    Very thankful for a clarification Regards stab ---------------------------------------------------------------------- circular · 2013-02-20 12:16 Maybe the code size is increased after a code out of the current range is used. So for example if all codes fit into 2 bits, you need to forsee that a new code could be used, outside of that range. I suppose that if that out-of-range code is used, it means that the last string of values is associated with that new code.