算法 – 解析GIF栅格数据 – LZW

我一直在尝试用 PHP解压缩GIF,似乎除了LZW解压缩之外,还有其他一切.我保存了显示的图像:

这张照片是3 x 5像这样:

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

我决定手动通过二进制文件并解析这个文件.手动解析的结果如下.我仍然坚持如何在这里解码栅格数据.有人会分解光栅数据如何变成图像?我已经能够分解一个图像,但没有别的(不是这个图像).我已经发表了我的理解,这应该如何分解,但我显然是错误的.

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

初始码大小= 3
一次读2位

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

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

资源我一直在使用:

http://www.daubnet.com/en/file-format-gif
http://en.wikipedia.org/wiki/Graphics_Interchange_Format
http://www.w3.org/Graphics/GIF/spec-gif87.txt

解决方法

GIF解析器

你说你想编写自己的GIF解析器,以了解它的工作原理.我建议您查看包含GIF阅读器的任何库的源代码,例如事件参考实现GIFLIB.相关的源文件dgif_lib.c;启动at slurp进行解码,或跳到LZW decompression implementation.

以下是您的图像如何解码.

我认为问题是您将输入字节分成LZW代码错误.

颜色数量为(0b001 1)* 2 = 4.

代码大小从2 1 = 3位开始.

所以初始的字典是

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

现在,GIF packs LZW codes into bytes in LSB-first order.因此,第一个代码被存储为第一个字节的3个最低有效位;第二个代码作为下一个3位;等等.在您的示例(第一个字节:0x84 = 10000100)中,前2个代码为100(清除)和000(蓝色).整个东西

01011101 11000001 00100111 01101110 10000100

被分成代码(在读取最高3位代码之后切换到4位组,111)为

0101 1101 1100 0001 0010 0111 0110 111 010 000 100

这解码为:

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列):

blue  black black
black blue  black
black black ...

这是你想要的.

相关文章

vue阻止冒泡事件 阻止点击事件的执行 <div @click=&a...
尝试过使用网友说的API接口获取 找到的都是失效了 暂时就使用...
后台我拿的数据是这样的格式: [ {id:1 , parentId: 0, name:...
JAVA下载文件防重复点击,防止多次下载请求,Cookie方式快速简...
Mip是什么意思以及作用有哪些