2017-04-17 102 views
0

http://giflib.sourceforge.net/whatsinagif/lzw_image_data.html吉夫LZW壓縮

我在閱讀本頁,瞭解吉夫的LZW壓縮。它示出了從它的樣本圖像的編碼代碼:

#4#1#6#6#2#9#9 ..

可變長度壓縮成字節後,就變成:

8C 2D 99 ..

這意味着:

#4 - 3比特

#1 - 3比特

#6 - 3個比特

#6 - 3比特

#2 - 4位

#9 - 4比特

該壓縮圖像數據是正確的,因爲我生成的吉夫樣品圖像使用Photoshop和驗證的二進制內容。

它清楚地表明位大小增加發生在輸出代碼#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.explained.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

這樣有什麼不好?

+0

如果沒有看到實際的代碼表正在構建就很難遵循該示例,但很顯然,只要有機會輸出下一個代碼以溢出當前寬度,代碼寬度就會增加,但不會更早。因此,如果描述所需的寬度不會增加,但是如果解壓縮仍然有效,則它必須是因爲該算法有邏輯來檢測添加的新代碼(這會溢出當前寬度)實際上不可能相當尚未出現在輸出流中。 –

回答

0

查看您再次鏈接到頁面上的示例:圖像僅有4種顏色,即LZW壓縮器以3位代碼開始。該字典到目前爲止包含6個條目:文字0..3,清除代碼4和EOI代碼5.前兩個代碼輸出是清除代碼4和文字1.這就是所有GIF LZW流開始的方式。現在開始壓縮,因爲圖像以1的運行開始。在寫入數據流之後再添加2個代碼,8個字典插槽耗盡,代碼大小增加到4.因此,下一個代碼 - 文字2 - 被寫爲4位數字。

如你所見,沒有什麼是錯的。這就是GIF用4色圖像工作的方式。