annotate lzw.c @ 4568:ad50768a5b57 libavcodec

Doxygenize the comments for the avpicture_free() function.
author takis
date Thu, 22 Feb 2007 19:06:28 +0000
parents f426c81afc9e
children fdbf89c6e2a7
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
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
58 int top_slot2; ///< Highest possible code for current size (<=top_slot)
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;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
80 }
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 s->bbuf |= (*s->pbuf++) << s->bbits;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
83 s->bbits += 8;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
84 s->bs--;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
85 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
86 c = s->bbuf & s->curmask;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
87 s->bbuf >>= s->cursize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
88 } else { // TIFF
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
89 while (s->bbits < s->cursize) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
90 if (s->pbuf >= s->ebuf) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
91 s->eob_reached = 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
92 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
93 s->bbuf = (s->bbuf << 8) | (*s->pbuf++);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
94 s->bbits += 8;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
95 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
96 c = (s->bbuf >> (s->bbits - s->cursize)) & s->curmask;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
97 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
98 s->bbits -= s->cursize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
99 return c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
100 }
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 uint8_t* ff_lzw_cur_ptr(LZWState *p)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
103 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
104 return ((struct LZWState*)p)->pbuf;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
105 }
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 void ff_lzw_decode_tail(LZWState *p)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
108 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
109 struct LZWState *s = (struct LZWState *)p;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
110 while(!s->eob_reached)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
111 lzw_get_code(s);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
112 }
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 void ff_lzw_decode_open(LZWState **p)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
115 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
116 *p = av_mallocz(sizeof(struct LZWState));
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
117 }
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 void ff_lzw_decode_close(LZWState **p)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
120 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
121 av_freep(p);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
122 }
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 * Initialize LZW decoder
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
126 * @param s LZW context
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
127 * @param csize initial code size in bits
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
128 * @param buf input data
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
129 * @param buf_size input data size
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
130 * @param mode decoder working mode - either GIF or TIFF
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
131 */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
132 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
133 {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
134 struct LZWState *s = (struct LZWState *)p;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
135
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
136 if(csize < 1 || csize > LZW_MAXBITS)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
137 return -1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
138 /* read buffer */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
139 s->eob_reached = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
140 s->pbuf = buf;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
141 s->ebuf = s->pbuf + buf_size;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
142 s->bbuf = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
143 s->bbits = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
144 s->bs = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
145
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
146 /* decoder */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
147 s->codesize = csize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
148 s->cursize = s->codesize + 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
149 s->curmask = mask[s->cursize];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
150 s->top_slot = 1 << s->cursize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
151 s->clear_code = 1 << s->codesize;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
152 s->end_code = s->clear_code + 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
153 s->slot = s->newcodes = s->clear_code + 2;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
154 s->oc = s->fc = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
155 s->sp = s->stack;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
156
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
157 s->mode = mode;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
158 switch(s->mode){
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
159 case FF_LZW_GIF:
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
160 s->top_slot2 = s->top_slot;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
161 break;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
162 case FF_LZW_TIFF:
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
163 s->top_slot2 = s->top_slot - 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
164 break;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
165 default:
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
166 return -1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
167 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
168 return 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
169 }
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 * Decode given number of bytes
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
173 * 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
174 * written by Steven A. Bennett in 1987.
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
175 *
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
176 * @param s LZW context
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
177 * @param buf output buffer
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
178 * @param len number of bytes to decode
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
179 * @return number of bytes decoded
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
180 */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
181 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
182 int l, c, code, oc, fc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
183 uint8_t *sp;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
184 struct LZWState *s = (struct LZWState *)p;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
185
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
186 if (s->end_code < 0)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
187 return 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
188
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
189 l = len;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
190 sp = s->sp;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
191 oc = s->oc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
192 fc = s->fc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
193
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
194 while (sp > s->stack) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
195 *buf++ = *(--sp);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
196 if ((--l) == 0)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
197 goto the_end;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
198 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
199
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
200 for (;;) {
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 s->top_slot2 = s->top_slot;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
211 if(s->mode == FF_LZW_TIFF)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
212 s->top_slot2--;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
213 while ((c = lzw_get_code(s)) == s->clear_code);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
214 if (c == s->end_code) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
215 s->end_code = -1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
216 break;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
217 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
218 /* test error */
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
219 if (c >= s->slot)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
220 c = 0;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
221 fc = oc = c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
222 *buf++ = c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
223 if ((--l) == 0)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
224 break;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
225 } else {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
226 code = c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
227 if (code >= s->slot) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
228 *sp++ = fc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
229 code = oc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
230 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
231 while (code >= s->newcodes) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
232 *sp++ = s->suffix[code];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
233 code = s->prefix[code];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
234 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
235 *sp++ = code;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
236 if (s->slot < s->top_slot) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
237 s->suffix[s->slot] = fc = code;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
238 s->prefix[s->slot++] = oc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
239 oc = c;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
240 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
241 if (s->slot >= s->top_slot2) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
242 if (s->cursize < LZW_MAXBITS) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
243 s->top_slot <<= 1;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
244 s->top_slot2 = s->top_slot;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
245 if(s->mode == FF_LZW_TIFF)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
246 s->top_slot2--;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
247 s->curmask = mask[++s->cursize];
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
248 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
249 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
250 while (sp > s->stack) {
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
251 *buf++ = *(--sp);
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
252 if ((--l) == 0)
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
253 goto the_end;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
254 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
255 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
256 }
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
257 the_end:
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
258 s->sp = sp;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
259 s->oc = oc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
260 s->fc = fc;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
261 return len - l;
f426c81afc9e LZW decoder as separate module plus TIFF LZW support
kostya
parents:
diff changeset
262 }