解析GIF栅格数据 - LZW [英] Parsing GIF Raster Data - LZW

查看:156
本文介绍了解析GIF栅格数据 - LZW的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图DECOM preSS GIF在PHP中,似乎什么都有,除了LZW DECOM pression下来。我已经保存所显示的图像:

I've been trying to decompress GIF's in PHP and seem to have everything except the LZW decompression down. I have saved an image that is shown:

此图片是3×5这样的:

This image is 3 x 5 like this:

Blue  Black Black
Black Blue  Black
Black Black Black
White White White
White White White

我决定去通过手动二进制和分析这个文件。手动解析的结果为如下。我仍然坚持至于如何去$ C C这里的栅格数据$。有人可以打破怎样的栅格数据变得形象?我已经能够打破一个图像,但没有别的(没有这个图片)。我已经张贴了我的这个应该怎么打破的理解,但我明明做错了。

I decided to go through manually in Binary and parse this file. The result of manual parsing is below. I am still stuck as to how to decode the raster data here. Can someone break down how the raster data becomes the image? I've been able to break down one image, but nothing else (not this image). I have posted my understanding of how this should break down, but I am obviously doing it wrong.

01000111 G
01001001 I
01000110 F
00111000 8
00111001 9
01100001 a

Screen Descriptor
WIDTH
00000011 3
00000000

00000101 5
00000000

10010001 GCM (1), CR (001), BPP (001), CD = 2, COLORS = 4

00000000 BGCOLOR Index

00000000 Aspect Ratio

GCM
BLUE
00110101 | 53
00000000 | 0
11000001 | 193

WHITE
11111111 | 255
11111111 | 255
11111111 | 255

BLACK
00000000 | 0
00000000 | 0
00000000 | 0

00000000 | 0
00000000 | 0
00000000 | 0

Extension
00100001 | 21
Function Code
11111001 | F9
Length
00000100 | 4
00000000
00000000
00000000
00000000
Terminator
00000000

Local Descriptor
00101100 Header
XPOS
00000000 | 0
00000000

YPOS
00000000 | 0
00000000

Width
00000011 | 3
00000000

Height
00000101 | 5
00000000

Flags
00000000 (LCM = 0, Interlaced = 0, Sorted = 0, Reserved = 0, Pixel Bits = 0)

RASTER DATA
Initial Code Size
00000010 | 2
Length
00000101 | 5

Data
10000100
01101110
00100111
11000001
01011101

Terminator
00000000

00111011 | ;
00000000

我尝试

10000100
01101110
00100111
11000001
01011101

初​​始code尺寸= 3 读2位的时间

Initial Code Size = 3 Read 2 bits at a time

10
00
Append last bit to first (010)
String becomes 010 or 2. 2 would be color # 3 or BLACK

在这一点上,我已经错了。第一种颜色应该是蓝色的。

At this point, I am already wrong. The first color should be blue.

资源我一直在使用:

http://www.daubnet.com/en/file-format-gif <一href="http://en.wikipedia.org/wiki/Graphics_Interchange_Format">http://en.wikipedia.org/wiki/Graphics_Interchange_Format <一href="http://www.w3.org/Graphics/GIF/spec-gif87.txt">http://www.w3.org/Graphics/GIF/spec-gif87.txt

推荐答案

您说您要编写自己的GIF分析器,以了解它是如何工作的。我建议你​​看一下任何含GIF阅读器,如事实上的参考实现 GIFLIB 。相关的源文件是<一个href="http://sourceforge.net/p/giflib/$c$c/ci/5889d00ac21d92aeea3ded203dfa2b658210a277/tree/lib/dgif_lib.c"><$c$c>dgif_lib.c;启动<一href="http://sourceforge.net/p/giflib/$c$c/ci/5889d00ac21d92aeea3ded203dfa2b658210a277/tree/lib/dgif_lib.c#l1056">at 解码,或跳转到<一个href="http://sourceforge.net/p/giflib/$c$c/ci/5889d00ac21d92aeea3ded203dfa2b658210a277/tree/lib/dgif_lib.c#l740">LZW DECOM pression实施。

GIF parser

You said you want to write your own GIF parser in order to understand how it works. I suggest you look at the source code of any of the libraries containing GIF readers, such as the de-facto reference implementation GIFLIB. The relevant source file is dgif_lib.c; start at slurp for decoding, or jump to the LZW decompression implementation.

我认为这个问题是,你是分裂输入字节到LZW codeS不正确。

I think the issue was that you were splitting the input bytes into LZW codes incorrectly.

色彩数为(项目0B001 + 1)* 2 = 4

code尺寸开始于2 + 1 = 3位。

Code size starts at 2 + 1 = 3 bits.

因此​​,最初的意思是

So the initial dictionary is

000 = color 0 = [blue]
001 = color 1 = [white]
010 = color 2 = [black]
011 = color 3 = [black]
100 = clear dictionary
101 = end of data

现在,<一个href="https://en.wikipedia.org/w/index.php?title=Lempel%E2%80%93Ziv%E2%80%93Welch&oldid=531967504#Packing_order">GIF包LZW codeS到LSB优先序位字节因此,第一code存储作为第一个字节的3最低显著位。第二code作为接下来的3位;等等。在您的例子(第一个字节:的0x84 = 10000100 ),第2 codeS因而 100 (清除)和 000 (蓝色)。整个事情

Now, GIF packs LZW codes into bytes in LSB-first order. Accordingly, the first code is stored as the 3 least-significant bits of the first byte; the second code as the next 3 bits; and so on. In your example (first byte: 0x84 = 10000100), the first 2 codes are thus 100 (clear) and 000 (blue). The whole thing

01011101 11000001 00100111 01101110 10000100

被分成codeS(开关读的最高3位code后4位组, 111 )的

0101 1101 1100 0001 0010 0111 0110 111 010 000 100

本德codeS到:

     last
code code
 100      clear dictionary
 000      output [blue] (1st pixel)
 010  000 new code in table:
              output 010 = [black]
              add 110 = old + 1st byte of new = [blue black] to table
 111  010 new code not in table:
              output last string followed by copy of first byte, [black black]
              add 111 = [black black] to table
              111 is largest possible 3-bit code, so switch to 4 bits
0110 0111 new code in table:
              output 0110 = [blue black]
              add 1000 = old + 1st byte of new = [black black blue] to table
0111 0110 new code in table:
              output 0111 = [black black]
              add 1001 = old + 1st byte of new = [blue black black] to table
...

所以输出开始(包3列):

So the output starts (wrapping to 3 columns):

blue  black black
black blue  black
black black ...

这是你想要的。

which is what you wanted.

这篇关于解析GIF栅格数据 - LZW的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆