annotate lzw.c @ 4726:ecb663d00b6b libavcodec

factorize
author michael
date Sun, 25 Mar 2007 14:02:54 +0000
parents 74caca70e2b3
children 23f8f6efc870
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
1 /*
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
2 * LZW decoder
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
3 * Copyright (c) 2003 Fabrice Bellard.
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
4 * Copyright (c) 2006 Konstantin Shishkov.
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
5 *
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
6 * This file is part of FFmpeg.
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
7 *
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
8 * FFmpeg is free software; you can redistribute it and/or
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
9 * modify it under the terms of the GNU Lesser General Public
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
10 * License as published by the Free Software Foundation; either
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
11 * version 2.1 of the License, or (at your option) any later version.
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
12 *
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
13 * FFmpeg is distributed in the hope that it will be useful,
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
16 * Lesser General Public License for more details.
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
17 *
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
18 * You should have received a copy of the GNU Lesser General Public
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
19 * License along with FFmpeg; if not, write to the Free Software
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
21 */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
22
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
23 /**
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
24 * @file lzw.c
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
25 * @brief LZW decoding routines
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
26 * @author Fabrice Bellard
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
27 * Modified for use in TIFF by Konstantin Shishkov
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
28 */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
29
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
30 #include "avcodec.h"
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
31 #include "lzw.h"
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
32
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
33 #define LZW_MAXBITS 12
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
34 #define LZW_SIZTABLE (1<<LZW_MAXBITS)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
35
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
36 static const uint16_t mask[17] =
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
37 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
38 0x0000, 0x0001, 0x0003, 0x0007,
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
39 0x000F, 0x001F, 0x003F, 0x007F,
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
40 0x00FF, 0x01FF, 0x03FF, 0x07FF,
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
41 0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
42 };
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
43
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
44 struct LZWState {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
45 int eob_reached;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
46 uint8_t *pbuf, *ebuf;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
47 int bbits;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
48 unsigned int bbuf;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
49
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
50 int mode; ///< Decoder mode
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
51 int cursize; ///< The current code size
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
52 int curmask;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
53 int codesize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
54 int clear_code;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
55 int end_code;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
56 int newcodes; ///< First available code
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
57 int top_slot; ///< Highest code for current size
4725
74caca70e2b3 simplify
michael
parents: 4716
diff changeset
58 int extra_slot;
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
59 int slot; ///< Last read code
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
60 int fc, oc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
61 uint8_t *sp;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
62 uint8_t stack[LZW_SIZTABLE];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
63 uint8_t suffix[LZW_SIZTABLE];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
64 uint16_t prefix[LZW_SIZTABLE];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
65 int bs; ///< current buffer size for GIF
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
66 };
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
67
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
68 /* get one code from stream */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
69 static int lzw_get_code(struct LZWState * s)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
70 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
71 int c, sizbuf;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
72
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
73 if(s->mode == FF_LZW_GIF) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
74 while (s->bbits < s->cursize) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
75 if (!s->bs) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
76 sizbuf = *s->pbuf++;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
77 s->bs = sizbuf;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
78 if(!sizbuf) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
79 s->eob_reached = 1;
4715
fdbf89c6e2a7 break if eob is reached to avoid reading one too much byte
bcoudurier
parents: 4080
diff changeset
80 break;
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
81 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
82 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
83 s->bbuf |= (*s->pbuf++) << s->bbits;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
84 s->bbits += 8;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
85 s->bs--;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
86 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
87 c = s->bbuf & s->curmask;
4716
8c00d22d45a0 fix indentation
bcoudurier
parents: 4715
diff changeset
88 s->bbuf >>= s->cursize;
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
89 } else { // TIFF
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
90 while (s->bbits < s->cursize) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
91 if (s->pbuf >= s->ebuf) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
92 s->eob_reached = 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
93 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
94 s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
95 s->bbits += 8;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
96 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
97 c = (s->bbuf >> (s->bbits - s->cursize)) & s->curmask;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
98 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
99 s->bbits -= s->cursize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
100 return c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
101 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
102
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
103 uint8_t* ff_lzw_cur_ptr(LZWState *p)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
104 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
105 return ((struct LZWState*)p)->pbuf;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
106 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
107
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
108 void ff_lzw_decode_tail(LZWState *p)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
109 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
110 struct LZWState *s = (struct LZWState *)p;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
111 while(!s->eob_reached)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
112 lzw_get_code(s);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
113 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
114
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
115 void ff_lzw_decode_open(LZWState **p)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
116 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
117 *p = av_mallocz(sizeof(struct LZWState));
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
118 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
119
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
120 void ff_lzw_decode_close(LZWState **p)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
121 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
122 av_freep(p);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
123 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
124
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
125 /**
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
126 * Initialize LZW decoder
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
127 * @param s LZW context
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
128 * @param csize initial code size in bits
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
129 * @param buf input data
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
130 * @param buf_size input data size
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
131 * @param mode decoder working mode - either GIF or TIFF
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
132 */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
133 int ff_lzw_decode_init(LZWState *p, int csize, uint8_t *buf, int buf_size, int mode)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
134 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
135 struct LZWState *s = (struct LZWState *)p;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
136
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
137 if(csize < 1 || csize > LZW_MAXBITS)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
138 return -1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
139 /* read buffer */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
140 s->eob_reached = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
141 s->pbuf = buf;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
142 s->ebuf = s->pbuf + buf_size;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
143 s->bbuf = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
144 s->bbits = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
145 s->bs = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
146
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
147 /* decoder */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
148 s->codesize = csize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
149 s->cursize = s->codesize + 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
150 s->curmask = mask[s->cursize];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
151 s->top_slot = 1 << s->cursize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
152 s->clear_code = 1 << s->codesize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
153 s->end_code = s->clear_code + 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
154 s->slot = s->newcodes = s->clear_code + 2;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
155 s->oc = s->fc = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
156 s->sp = s->stack;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
157
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
158 s->mode = mode;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
159 switch(s->mode){
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
160 case FF_LZW_GIF:
4725
74caca70e2b3 simplify
michael
parents: 4716
diff changeset
161 s->extra_slot= 0;
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
162 break;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
163 case FF_LZW_TIFF:
4725
74caca70e2b3 simplify
michael
parents: 4716
diff changeset
164 s->extra_slot= 1;
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
165 break;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
166 default:
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
167 return -1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
168 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
169 return 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
170 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
171
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
172 /**
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
173 * Decode given number of bytes
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
174 * NOTE: the algorithm here is inspired from the LZW GIF decoder
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
175 * written by Steven A. Bennett in 1987.
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
176 *
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
177 * @param s LZW context
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
178 * @param buf output buffer
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
179 * @param len number of bytes to decode
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
180 * @return number of bytes decoded
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
181 */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
182 int ff_lzw_decode(LZWState *p, uint8_t *buf, int len){
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
183 int l, c, code, oc, fc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
184 uint8_t *sp;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
185 struct LZWState *s = (struct LZWState *)p;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
186
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
187 if (s->end_code < 0)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
188 return 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
189
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
190 l = len;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
191 sp = s->sp;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
192 oc = s->oc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
193 fc = s->fc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
194
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
195 for (;;) {
4726
ecb663d00b6b factorize
michael
parents: 4725
diff changeset
196 while (sp > s->stack) {
ecb663d00b6b factorize
michael
parents: 4725
diff changeset
197 *buf++ = *(--sp);
ecb663d00b6b factorize
michael
parents: 4725
diff changeset
198 if ((--l) == 0)
ecb663d00b6b factorize
michael
parents: 4725
diff changeset
199 goto the_end;
ecb663d00b6b factorize
michael
parents: 4725
diff changeset
200 }
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
201 c = lzw_get_code(s);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
202 if (c == s->end_code) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
203 s->end_code = -1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
204 break;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
205 } else if (c == s->clear_code) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
206 s->cursize = s->codesize + 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
207 s->curmask = mask[s->cursize];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
208 s->slot = s->newcodes;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
209 s->top_slot = 1 << s->cursize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
210 while ((c = lzw_get_code(s)) == s->clear_code);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
211 if (c == s->end_code) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
212 s->end_code = -1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
213 break;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
214 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
215 /* test error */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
216 if (c >= s->slot)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
217 c = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
218 fc = oc = c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
219 *buf++ = c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
220 if ((--l) == 0)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
221 break;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
222 } else {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
223 code = c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
224 if (code >= s->slot) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
225 *sp++ = fc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
226 code = oc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
227 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
228 while (code >= s->newcodes) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
229 *sp++ = s->suffix[code];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
230 code = s->prefix[code];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
231 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
232 *sp++ = code;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
233 if (s->slot < s->top_slot) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
234 s->suffix[s->slot] = fc = code;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
235 s->prefix[s->slot++] = oc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
236 oc = c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
237 }
4725
74caca70e2b3 simplify
michael
parents: 4716
diff changeset
238 if (s->slot >= s->top_slot - s->extra_slot) {
4080
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
239 if (s->cursize < LZW_MAXBITS) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
240 s->top_slot <<= 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
241 s->curmask = mask[++s->cursize];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
242 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
243 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
244 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
245 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
246 the_end:
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
247 s->sp = sp;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
248 s->oc = oc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
249 s->fc = fc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
250 return len - l;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
251 }