/src/opencv/3rdparty/libtiff/tif_packbits.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (c) 1988-1997 Sam Leffler |
3 | | * Copyright (c) 1991-1997 Silicon Graphics, Inc. |
4 | | * |
5 | | * Permission to use, copy, modify, distribute, and sell this software and |
6 | | * its documentation for any purpose is hereby granted without fee, provided |
7 | | * that (i) the above copyright notices and this permission notice appear in |
8 | | * all copies of the software and related documentation, and (ii) the names of |
9 | | * Sam Leffler and Silicon Graphics may not be used in any advertising or |
10 | | * publicity relating to the software without the specific, prior written |
11 | | * permission of Sam Leffler and Silicon Graphics. |
12 | | * |
13 | | * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, |
14 | | * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY |
15 | | * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
16 | | * |
17 | | * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR |
18 | | * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, |
19 | | * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, |
20 | | * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF |
21 | | * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE |
22 | | * OF THIS SOFTWARE. |
23 | | */ |
24 | | |
25 | | #include "tiffiop.h" |
26 | | #ifdef PACKBITS_SUPPORT |
27 | | /* |
28 | | * TIFF Library. |
29 | | * |
30 | | * PackBits Compression Algorithm Support |
31 | | */ |
32 | | #include <stdio.h> |
33 | | |
34 | | static int |
35 | | PackBitsPreEncode(TIFF* tif, uint16 s) |
36 | 0 | { |
37 | 0 | (void) s; |
38 | |
|
39 | 0 | tif->tif_data = (uint8*)_TIFFmalloc(sizeof(tmsize_t)); |
40 | 0 | if (tif->tif_data == NULL) |
41 | 0 | return (0); |
42 | | /* |
43 | | * Calculate the scanline/tile-width size in bytes. |
44 | | */ |
45 | 0 | if (isTiled(tif)) |
46 | 0 | *(tmsize_t*)tif->tif_data = TIFFTileRowSize(tif); |
47 | 0 | else |
48 | 0 | *(tmsize_t*)tif->tif_data = TIFFScanlineSize(tif); |
49 | 0 | return (1); |
50 | 0 | } |
51 | | |
52 | | static int |
53 | | PackBitsPostEncode(TIFF* tif) |
54 | 0 | { |
55 | 0 | if (tif->tif_data) |
56 | 0 | _TIFFfree(tif->tif_data); |
57 | 0 | return (1); |
58 | 0 | } |
59 | | |
60 | | /* |
61 | | * Encode a run of pixels. |
62 | | */ |
63 | | static int |
64 | | PackBitsEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s) |
65 | 0 | { |
66 | 0 | unsigned char* bp = (unsigned char*) buf; |
67 | 0 | uint8* op; |
68 | 0 | uint8* ep; |
69 | 0 | uint8* lastliteral; |
70 | 0 | long n, slop; |
71 | 0 | int b; |
72 | 0 | enum { BASE, LITERAL, RUN, LITERAL_RUN } state; |
73 | |
|
74 | 0 | (void) s; |
75 | 0 | op = tif->tif_rawcp; |
76 | 0 | ep = tif->tif_rawdata + tif->tif_rawdatasize; |
77 | 0 | state = BASE; |
78 | 0 | lastliteral = 0; |
79 | 0 | while (cc > 0) { |
80 | | /* |
81 | | * Find the longest string of identical bytes. |
82 | | */ |
83 | 0 | b = *bp++; |
84 | 0 | cc--; |
85 | 0 | n = 1; |
86 | 0 | for (; cc > 0 && b == *bp; cc--, bp++) |
87 | 0 | n++; |
88 | 0 | again: |
89 | 0 | if (op + 2 >= ep) { /* insure space for new data */ |
90 | | /* |
91 | | * Be careful about writing the last |
92 | | * literal. Must write up to that point |
93 | | * and then copy the remainder to the |
94 | | * front of the buffer. |
95 | | */ |
96 | 0 | if (state == LITERAL || state == LITERAL_RUN) { |
97 | 0 | slop = (long)(op - lastliteral); |
98 | 0 | tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp); |
99 | 0 | if (!TIFFFlushData1(tif)) |
100 | 0 | return (0); |
101 | 0 | op = tif->tif_rawcp; |
102 | 0 | while (slop-- > 0) |
103 | 0 | *op++ = *lastliteral++; |
104 | 0 | lastliteral = tif->tif_rawcp; |
105 | 0 | } else { |
106 | 0 | tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp); |
107 | 0 | if (!TIFFFlushData1(tif)) |
108 | 0 | return (0); |
109 | 0 | op = tif->tif_rawcp; |
110 | 0 | } |
111 | 0 | } |
112 | 0 | switch (state) { |
113 | 0 | case BASE: /* initial state, set run/literal */ |
114 | 0 | if (n > 1) { |
115 | 0 | state = RUN; |
116 | 0 | if (n > 128) { |
117 | 0 | *op++ = (uint8) -127; |
118 | 0 | *op++ = (uint8) b; |
119 | 0 | n -= 128; |
120 | 0 | goto again; |
121 | 0 | } |
122 | 0 | *op++ = (uint8)(-(n-1)); |
123 | 0 | *op++ = (uint8) b; |
124 | 0 | } else { |
125 | 0 | lastliteral = op; |
126 | 0 | *op++ = 0; |
127 | 0 | *op++ = (uint8) b; |
128 | 0 | state = LITERAL; |
129 | 0 | } |
130 | 0 | break; |
131 | 0 | case LITERAL: /* last object was literal string */ |
132 | 0 | if (n > 1) { |
133 | 0 | state = LITERAL_RUN; |
134 | 0 | if (n > 128) { |
135 | 0 | *op++ = (uint8) -127; |
136 | 0 | *op++ = (uint8) b; |
137 | 0 | n -= 128; |
138 | 0 | goto again; |
139 | 0 | } |
140 | 0 | *op++ = (uint8)(-(n-1)); /* encode run */ |
141 | 0 | *op++ = (uint8) b; |
142 | 0 | } else { /* extend literal */ |
143 | 0 | if (++(*lastliteral) == 127) |
144 | 0 | state = BASE; |
145 | 0 | *op++ = (uint8) b; |
146 | 0 | } |
147 | 0 | break; |
148 | 0 | case RUN: /* last object was run */ |
149 | 0 | if (n > 1) { |
150 | 0 | if (n > 128) { |
151 | 0 | *op++ = (uint8) -127; |
152 | 0 | *op++ = (uint8) b; |
153 | 0 | n -= 128; |
154 | 0 | goto again; |
155 | 0 | } |
156 | 0 | *op++ = (uint8)(-(n-1)); |
157 | 0 | *op++ = (uint8) b; |
158 | 0 | } else { |
159 | 0 | lastliteral = op; |
160 | 0 | *op++ = 0; |
161 | 0 | *op++ = (uint8) b; |
162 | 0 | state = LITERAL; |
163 | 0 | } |
164 | 0 | break; |
165 | 0 | case LITERAL_RUN: /* literal followed by a run */ |
166 | | /* |
167 | | * Check to see if previous run should |
168 | | * be converted to a literal, in which |
169 | | * case we convert literal-run-literal |
170 | | * to a single literal. |
171 | | */ |
172 | 0 | if (n == 1 && op[-2] == (uint8) -1 && |
173 | 0 | *lastliteral < 126) { |
174 | 0 | state = (((*lastliteral) += 2) == 127 ? |
175 | 0 | BASE : LITERAL); |
176 | 0 | op[-2] = op[-1]; /* replicate */ |
177 | 0 | } else |
178 | 0 | state = RUN; |
179 | 0 | goto again; |
180 | 0 | } |
181 | 0 | } |
182 | 0 | tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp); |
183 | 0 | tif->tif_rawcp = op; |
184 | 0 | return (1); |
185 | 0 | } |
186 | | |
187 | | /* |
188 | | * Encode a rectangular chunk of pixels. We break it up |
189 | | * into row-sized pieces to insure that encoded runs do |
190 | | * not span rows. Otherwise, there can be problems with |
191 | | * the decoder if data is read, for example, by scanlines |
192 | | * when it was encoded by strips. |
193 | | */ |
194 | | static int |
195 | | PackBitsEncodeChunk(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s) |
196 | 0 | { |
197 | 0 | tmsize_t rowsize = *(tmsize_t*)tif->tif_data; |
198 | |
|
199 | 0 | while (cc > 0) { |
200 | 0 | tmsize_t chunk = rowsize; |
201 | | |
202 | 0 | if( cc < chunk ) |
203 | 0 | chunk = cc; |
204 | |
|
205 | 0 | if (PackBitsEncode(tif, bp, chunk, s) < 0) |
206 | 0 | return (-1); |
207 | 0 | bp += chunk; |
208 | 0 | cc -= chunk; |
209 | 0 | } |
210 | 0 | return (1); |
211 | 0 | } |
212 | | |
213 | | static int |
214 | | PackBitsDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s) |
215 | 6.41k | { |
216 | 6.41k | static const char module[] = "PackBitsDecode"; |
217 | 6.41k | char *bp; |
218 | 6.41k | tmsize_t cc; |
219 | 6.41k | long n; |
220 | 6.41k | int b; |
221 | | |
222 | 6.41k | (void) s; |
223 | 6.41k | bp = (char*) tif->tif_rawcp; |
224 | 6.41k | cc = tif->tif_rawcc; |
225 | 2.59M | while (cc > 0 && occ > 0) { |
226 | 2.59M | n = (long) *bp++; |
227 | 2.59M | cc--; |
228 | | /* |
229 | | * Watch out for compilers that |
230 | | * don't sign extend chars... |
231 | | */ |
232 | 2.59M | if (n >= 128) |
233 | 0 | n -= 256; |
234 | 2.59M | if (n < 0) { /* replicate next byte -n+1 times */ |
235 | 1.01M | if (n == -128) /* nop */ |
236 | 96.5k | continue; |
237 | 920k | n = -n + 1; |
238 | 920k | if( occ < (tmsize_t)n ) |
239 | 3.40k | { |
240 | 3.40k | TIFFWarningExt(tif->tif_clientdata, module, |
241 | 3.40k | "Discarding %lu bytes to avoid buffer overrun", |
242 | 3.40k | (unsigned long) ((tmsize_t)n - occ)); |
243 | 3.40k | n = (long)occ; |
244 | 3.40k | } |
245 | 920k | if( cc == 0 ) |
246 | 143 | { |
247 | 143 | TIFFWarningExt(tif->tif_clientdata, module, |
248 | 143 | "Terminating PackBitsDecode due to lack of data."); |
249 | 143 | break; |
250 | 143 | } |
251 | 919k | occ -= n; |
252 | 919k | b = *bp++; |
253 | 919k | cc--; |
254 | 37.3M | while (n-- > 0) |
255 | 36.4M | *op++ = (uint8) b; |
256 | 1.57M | } else { /* copy next n+1 bytes literally */ |
257 | 1.57M | if (occ < (tmsize_t)(n + 1)) |
258 | 546 | { |
259 | 546 | TIFFWarningExt(tif->tif_clientdata, module, |
260 | 546 | "Discarding %lu bytes to avoid buffer overrun", |
261 | 546 | (unsigned long) ((tmsize_t)n - occ + 1)); |
262 | 546 | n = (long)occ - 1; |
263 | 546 | } |
264 | 1.57M | if (cc < (tmsize_t) (n+1)) |
265 | 2.09k | { |
266 | 2.09k | TIFFWarningExt(tif->tif_clientdata, module, |
267 | 2.09k | "Terminating PackBitsDecode due to lack of data."); |
268 | 2.09k | break; |
269 | 2.09k | } |
270 | 1.57M | _TIFFmemcpy(op, bp, ++n); |
271 | 1.57M | op += n; occ -= n; |
272 | 1.57M | bp += n; cc -= n; |
273 | 1.57M | } |
274 | 2.59M | } |
275 | 6.41k | tif->tif_rawcp = (uint8*) bp; |
276 | 6.41k | tif->tif_rawcc = cc; |
277 | 6.41k | if (occ > 0) { |
278 | 2.49k | TIFFErrorExt(tif->tif_clientdata, module, |
279 | 2.49k | "Not enough data for scanline %lu", |
280 | 2.49k | (unsigned long) tif->tif_row); |
281 | 2.49k | return (0); |
282 | 2.49k | } |
283 | 3.91k | return (1); |
284 | 6.41k | } |
285 | | |
286 | | int |
287 | | TIFFInitPackBits(TIFF* tif, int scheme) |
288 | 117 | { |
289 | 117 | (void) scheme; |
290 | 117 | tif->tif_decoderow = PackBitsDecode; |
291 | 117 | tif->tif_decodestrip = PackBitsDecode; |
292 | 117 | tif->tif_decodetile = PackBitsDecode; |
293 | 117 | tif->tif_preencode = PackBitsPreEncode; |
294 | 117 | tif->tif_postencode = PackBitsPostEncode; |
295 | 117 | tif->tif_encoderow = PackBitsEncode; |
296 | 117 | tif->tif_encodestrip = PackBitsEncodeChunk; |
297 | 117 | tif->tif_encodetile = PackBitsEncodeChunk; |
298 | 117 | return (1); |
299 | 117 | } |
300 | | #endif /* PACKBITS_SUPPORT */ |
301 | | |
302 | | /* vim: set ts=8 sts=8 sw=8 noet: */ |
303 | | /* |
304 | | * Local Variables: |
305 | | * mode: c |
306 | | * c-basic-offset: 8 |
307 | | * fill-column: 78 |
308 | | * End: |
309 | | */ |