http://giflib.sourceforge.net/whatsinagif/lzw_image_data.html
我正在阅读此页面,以了解Giff的LZW压缩。它从其示例图像显示编码的代码:
#4#1#6#6#2#9#9 ..
可变长度压缩到字节中后,它变为:
8c 2d 99 ..
这意味着:
#4-3位
#1-3位
#6-3位
#6-3位
#2-4位
#9-4位
当我使用Photoshop和验证的二进制内容生成Giff示例图像时,此压缩图像数据是正确的。
清楚地表明,当输出代码#2
但是,这就是页面谈论位大小增加的方式: When you are encoding the data, you increase your code size as soon as your write out the code equal to 2^(current code size)-1
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.
但是在其编码表中,步骤6是将条目#7添加到lzw字典中的位置,但是添加的输出代码是第一个#6。根据该算法,两个#6应该为4位,但是它们实际上是3位?
根据此页面https://www.eecis.udel.edu/~amer/cisc651/lzw.and.gif.explaining.html
它对位尺寸If you're encoding, you start with a compression size of (N+1) bits, and, whenever you output the code (2**(compression size)-1), you bump the compression size up one bit
那怎么了?
查看您再次链接到的页面上给出的示例:图像仅具有4种颜色,即LZW压缩机以3位代码开始。词典到目前为止包含6个条目:文字0..3,清除代码4和EOI代码5。前两个代码输出是清除代码4和文字1。这就是所有GIF LZW流的方式。现在开始压缩,因为图像始于1的运行。在将两个代码写入流后,将8个字典插槽耗尽,并且代码大小增加到4。因此,下一个代码 - 字面2-写为4位编号。
如您所见,没有什么错。这就是GIF的工作方式。