forked from techy101/LED-Tophat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LZWFunctions.cpp
177 lines (160 loc) · 4.8 KB
/
LZWFunctions.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Animated GIFs Display Code for SmartMatrix and 32x32 RGB LED Panels
*
* This file contains code to decompress the LZW encoded animated GIF data
*
* Written by: Craig A. Lindley, Fabrice Bellard and Steven A. Bennett
* See my book, "Practical Image Processing in C", John Wiley & Sons, Inc.
*
* Copyright (c) 2014 Craig A. Lindley
* Minor modifications by Louis Beaudoin (pixelmatix)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define DEBUG 0
#include <Arduino.h>
#include "GIFDecoder.h"
// LZW constants
// NOTE: LZW_MAXBITS set to 11 (initially 10) to support more GIFs with 6k RAM increase
#define LZW_MAXBITS 11
#define LZW_SIZTABLE (1 << LZW_MAXBITS)
// Masks for 0 .. 16 bits
unsigned int mask[17] = {
0x0000, 0x0001, 0x0003, 0x0007,
0x000F, 0x001F, 0x003F, 0x007F,
0x00FF, 0x01FF, 0x03FF, 0x07FF,
0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF,
0xFFFF
};
// LZW variables
byte *pbuf;
int bbits;
int bbuf;
int cursize; // The current code size
int curmask;
int codesize;
int clear_code;
int end_code;
int newcodes; // First available code
int top_slot; // Highest code for current size
int extra_slot;
int slot; // Last read code
int fc, oc;
int bs; // Current buffer size for GIF
byte *sp;
byte stack [LZW_SIZTABLE];
byte suffix [LZW_SIZTABLE];
unsigned int prefix [LZW_SIZTABLE];
// Initialize LZW decoder
// csize initial code size in bits
// buf input data
void lzw_decode_init (int csize, byte *buf) {
// Initialize read buffer variables
pbuf = buf;
bbuf = 0;
bbits = 0;
bs = 0;
// Initialize decoder variables
codesize = csize;
cursize = codesize + 1;
curmask = mask[cursize];
top_slot = 1 << cursize;
clear_code = 1 << codesize;
end_code = clear_code + 1;
slot = newcodes = clear_code + 2;
oc = fc = -1;
sp = stack;
}
// Get one code of given number of bits from stream
int lzw_get_code() {
while (bbits < cursize) {
if (!bs) {
bs = *pbuf++;
}
bbuf |= (*pbuf++) << bbits;
bbits += 8;
bs--;
}
int c = bbuf;
bbuf >>= cursize;
bbits -= cursize;
return c & curmask;
}
// Decode given number of bytes
// buf 8 bit output buffer
// len number of pixels to decode
// returns the number of bytes decoded
int lzw_decode(byte *buf, int len) {
int l, c, code;
if (end_code < 0) {
return 0;
}
l = len;
for (;;) {
while (sp > stack) {
*buf++ = *(--sp);
if ((--l) == 0) {
return len;
}
}
c = lzw_get_code();
if (c == end_code) {
break;
}
else if (c == clear_code) {
cursize = codesize + 1;
curmask = mask[cursize];
slot = newcodes;
top_slot = 1 << cursize;
fc= oc= -1;
}
else {
code = c;
if ((code == slot) && (fc >= 0)) {
*sp++ = fc;
code = oc;
}
else if (code >= slot) {
break;
}
while (code >= newcodes) {
*sp++ = suffix[code];
code = prefix[code];
}
*sp++ = code;
if ((slot < top_slot) && (oc >= 0)) {
suffix[slot] = code;
prefix[slot++] = oc;
}
fc = code;
oc = c;
if (slot >= top_slot) {
if (cursize < LZW_MAXBITS) {
top_slot <<= 1;
curmask = mask[++cursize];
} else {
#if DEBUG == 1
Serial.println("****** cursize >= MAXBITS *******");
#endif
}
}
}
}
end_code = -1;
return len - l;
}