1 /*
2 * LZW decoder
3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2006 Konstantin Shishkov
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file
25 * @brief LZW decoding routines
26 * @author Fabrice Bellard
27 * @author modified for use in TIFF by Konstantin Shishkov
28 */
29
34
35 #define LZW_MAXBITS 12
36 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
37
38 static const uint16_t
mask[17] =
39 {
40 0x0000, 0x0001, 0x0003, 0x0007,
41 0x000F, 0x001F, 0x003F, 0x007F,
42 0x00FF, 0x01FF, 0x03FF, 0x07FF,
43 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
44 };
45
50
51 int mode;
///< Decoder mode
58 int top_slot;
///< Highest code for current size
60 int slot;
///< Last read code
66 int bs;
///< current buffer size for GIF
67 };
68
69 /* get one code from stream */
71 {
73
77 s->
bs = bytestream2_get_byte(&s->
gb);
78 }
79 s->
bbuf |= bytestream2_get_byte(&s->
gb) << s->
bbits;
82 }
85 } else { // TIFF
87 s->
bbuf = (s->
bbuf << 8) | bytestream2_get_byte(&s->
gb);
89 }
91 }
94 }
95
97 {
99
103 s->
bs = bytestream2_get_byte(&s->
gb);
104 }
105 }else
107 }
108
110 {
112 }
113
115 {
117 }
118
119 /**
120 * Initialize LZW decoder
121 * @param p LZW context
122 * @param csize initial code size in bits
123 * @param buf input data
124 * @param buf_size input data size
125 * @param mode decoder working mode - either GIF or TIFF
126 */
128 {
130
132 return -1;
133 /* read buffer */
138
139 /* decoder */
149
152 return 0;
153 }
154
155 /**
156 * Decode given number of bytes
157 * NOTE: the algorithm here is inspired from the LZW GIF decoder
158 * written by Steven A. Bennett in 1987.
159 *
160 * @param p LZW context
161 * @param buf output buffer
162 * @param len number of bytes to decode
163 * @return number of bytes decoded
164 */
166 int l,
c, code,
oc,
fc;
169
171 return 0;
172
177
178 for (;;) {
179 while (sp > s->
stack) {
181 if ((--l) == 0)
182 goto the_end;
183 }
186 break;
192 fc= oc= -1;
193 } else {
195 if (code == s->
slot && fc>=0) {
198 }
else if(code >= s->
slot)
199 break;
203 }
204 *sp++ = code;
208 }
209 fc = code;
215 }
216 }
217 }
218 }
220 the_end:
224 return len - l;
225 }