ReactOS 0.4.16-dev-2574-g474348f
tif_packbits.c
Go to the documentation of this file.
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#ifndef PACKBITS_READ_ONLY
35
36static int PackBitsPreEncode(TIFF *tif, uint16_t s)
37{
38 (void)s;
39
40 tif->tif_data = (uint8_t *)_TIFFmallocExt(tif, sizeof(tmsize_t));
41 if (tif->tif_data == NULL)
42 return (0);
43 /*
44 * Calculate the scanline/tile-width size in bytes.
45 */
46 if (isTiled(tif))
47 *(tmsize_t *)tif->tif_data = TIFFTileRowSize(tif);
48 else
49 *(tmsize_t *)tif->tif_data = TIFFScanlineSize(tif);
50 return (1);
51}
52
53static int PackBitsPostEncode(TIFF *tif)
54{
55 if (tif->tif_data)
56 _TIFFfreeExt(tif, tif->tif_data);
57 return (1);
58}
59
60/*
61 * Encode a run of pixels.
62 */
63static int PackBitsEncode(TIFF *tif, uint8_t *buf, tmsize_t cc, uint16_t s)
64{
65 unsigned char *bp = (unsigned char *)buf;
66 uint8_t *op;
67 uint8_t *ep;
68 uint8_t *lastliteral;
69 long n, slop;
70 int b;
71 enum
72 {
73 BASE,
74 LITERAL,
75 RUN,
76 LITERAL_RUN
77 } state;
78
79 (void)s;
80 op = tif->tif_rawcp;
81 ep = tif->tif_rawdata + tif->tif_rawdatasize;
82 state = BASE;
83 lastliteral = NULL;
84 while (cc > 0)
85 {
86 /*
87 * Find the longest string of identical bytes.
88 */
89 b = *bp++;
90 cc--;
91 n = 1;
92 for (; cc > 0 && b == *bp; cc--, bp++)
93 n++;
94 again:
95 if (op + 2 >= ep)
96 { /* insure space for new data */
97 /*
98 * Be careful about writing the last
99 * literal. Must write up to that point
100 * and then copy the remainder to the
101 * front of the buffer.
102 */
103 if (state == LITERAL || state == LITERAL_RUN)
104 {
105 slop = (long)(op - lastliteral);
106 tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp);
107 if (!TIFFFlushData1(tif))
108 return (0);
109 op = tif->tif_rawcp;
110 while (slop-- > 0)
111 *op++ = *lastliteral++;
112 lastliteral = tif->tif_rawcp;
113 }
114 else
115 {
116 tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
117 if (!TIFFFlushData1(tif))
118 return (0);
119 op = tif->tif_rawcp;
120 }
121 }
122 switch (state)
123 {
124 case BASE: /* initial state, set run/literal */
125 if (n > 1)
126 {
127 state = RUN;
128 if (n > 128)
129 {
130 *op++ = (uint8_t)-127;
131 *op++ = (uint8_t)b;
132 n -= 128;
133 goto again;
134 }
135 *op++ = (uint8_t)(-(n - 1));
136 *op++ = (uint8_t)b;
137 }
138 else
139 {
140 lastliteral = op;
141 *op++ = 0;
142 *op++ = (uint8_t)b;
143 state = LITERAL;
144 }
145 break;
146 case LITERAL: /* last object was literal string */
147 if (n > 1)
148 {
149 state = LITERAL_RUN;
150 if (n > 128)
151 {
152 *op++ = (uint8_t)-127;
153 *op++ = (uint8_t)b;
154 n -= 128;
155 goto again;
156 }
157 *op++ = (uint8_t)(-(n - 1)); /* encode run */
158 *op++ = (uint8_t)b;
159 }
160 else
161 { /* extend literal */
162 if (++(*lastliteral) == 127)
163 state = BASE;
164 *op++ = (uint8_t)b;
165 }
166 break;
167 case RUN: /* last object was run */
168 if (n > 1)
169 {
170 if (n > 128)
171 {
172 *op++ = (uint8_t)-127;
173 *op++ = (uint8_t)b;
174 n -= 128;
175 goto again;
176 }
177 *op++ = (uint8_t)(-(n - 1));
178 *op++ = (uint8_t)b;
179 }
180 else
181 {
182 lastliteral = op;
183 *op++ = 0;
184 *op++ = (uint8_t)b;
185 state = LITERAL;
186 }
187 break;
188 case LITERAL_RUN: /* literal followed by a run */
189 /*
190 * Check to see if previous run should
191 * be converted to a literal, in which
192 * case we convert literal-run-literal
193 * to a single literal.
194 */
195 if (n == 1 && op[-2] == (uint8_t)-1 && *lastliteral < 126)
196 {
197 state = (((*lastliteral) += 2) == 127 ? BASE : LITERAL);
198 op[-2] = op[-1]; /* replicate */
199 }
200 else
201 state = RUN;
202 goto again;
203 }
204 }
205 tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
206 tif->tif_rawcp = op;
207 return (1);
208}
209
210/*
211 * Encode a rectangular chunk of pixels. We break it up
212 * into row-sized pieces to insure that encoded runs do
213 * not span rows. Otherwise, there can be problems with
214 * the decoder if data is read, for example, by scanlines
215 * when it was encoded by strips.
216 */
217static int PackBitsEncodeChunk(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
218{
219 tmsize_t rowsize = *(tmsize_t *)tif->tif_data;
220
221 while (cc > 0)
222 {
223 tmsize_t chunk = rowsize;
224
225 if (cc < chunk)
226 chunk = cc;
227
228 if (PackBitsEncode(tif, bp, chunk, s) < 0)
229 return (-1);
230 bp += chunk;
231 cc -= chunk;
232 }
233 return (1);
234}
235
236#endif
237
238static int PackBitsDecode(TIFF *tif, uint8_t *op, tmsize_t occ, uint16_t s)
239{
240 static const char module[] = "PackBitsDecode";
241 int8_t *bp;
242 tmsize_t cc;
243 long n;
244 int b;
245
246 (void)s;
247 bp = (int8_t *)tif->tif_rawcp;
248 cc = tif->tif_rawcc;
249 while (cc > 0 && occ > 0)
250 {
251 n = (long)*bp++;
252 cc--;
253 if (n < 0)
254 { /* replicate next byte -n+1 times */
255 if (n == -128) /* nop */
256 continue;
257 n = -n + 1;
258 if (occ < (tmsize_t)n)
259 {
261 "Discarding %" TIFF_SSIZE_FORMAT
262 " bytes to avoid buffer overrun",
263 (tmsize_t)n - occ);
264 n = (long)occ;
265 }
266 if (cc == 0)
267 {
269 tif, module,
270 "Terminating PackBitsDecode due to lack of data.");
271 break;
272 }
273 occ -= n;
274 b = *bp++;
275 cc--;
276 while (n-- > 0)
277 *op++ = (uint8_t)b;
278 }
279 else
280 { /* copy next n+1 bytes literally */
281 if (occ < (tmsize_t)(n + 1))
282 {
284 "Discarding %" TIFF_SSIZE_FORMAT
285 " bytes to avoid buffer overrun",
286 (tmsize_t)n - occ + 1);
287 n = (long)occ - 1;
288 }
289 if (cc < (tmsize_t)(n + 1))
290 {
292 tif, module,
293 "Terminating PackBitsDecode due to lack of data.");
294 break;
295 }
296 _TIFFmemcpy(op, bp, ++n);
297 op += n;
298 occ -= n;
299 bp += n;
300 cc -= n;
301 }
302 }
303 tif->tif_rawcp = (uint8_t *)bp;
304 tif->tif_rawcc = cc;
305 if (occ > 0)
306 {
307 memset(op, 0, (size_t)occ);
308 TIFFErrorExtR(tif, module, "Not enough data for scanline %" PRIu32,
309 tif->tif_row);
310 return (0);
311 }
312 return (1);
313}
314
315int TIFFInitPackBits(TIFF *tif, int scheme)
316{
317 (void)scheme;
318 tif->tif_decoderow = PackBitsDecode;
319 tif->tif_decodestrip = PackBitsDecode;
320 tif->tif_decodetile = PackBitsDecode;
321#ifndef PACKBITS_READ_ONLY
322 tif->tif_preencode = PackBitsPreEncode;
323 tif->tif_postencode = PackBitsPostEncode;
324 tif->tif_encoderow = PackBitsEncode;
325 tif->tif_encodestrip = PackBitsEncodeChunk;
326 tif->tif_encodetile = PackBitsEncodeChunk;
327#endif
328 return (1);
329}
330#endif /* PACKBITS_SUPPORT */
static int state
Definition: maze.c:121
while(CdLookupNextInitialFileDirent(IrpContext, Fcb, FileContext))
#define NULL
Definition: types.h:112
UINT op
Definition: effect.c:236
#define BASE
Definition: inflate.c:58
#define PRIu32
Definition: inttypes.h:84
unsigned short uint16_t
Definition: stdint.h:35
unsigned char uint8_t
Definition: stdint.h:33
signed char int8_t
Definition: stdint.h:32
GLdouble s
Definition: gl.h:2039
GLdouble n
Definition: glext.h:7729
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
uint32_t cc
Definition: isohybrid.c:75
#define b
Definition: ke_i.h:79
#define LITERAL(a)
#define uint8_t
Definition: nsiface.idl:59
#define long
Definition: qsort.c:33
DWORD scheme
#define memset(x, y, z)
Definition: compat.h:39
Definition: tiffiop.h:113
TIFFCodeMethod tif_encodestrip
Definition: tiffiop.h:208
TIFFCodeMethod tif_encodetile
Definition: tiffiop.h:210
TIFFPreMethod tif_preencode
Definition: tiffiop.h:203
tmsize_t tif_rawcc
Definition: tiffiop.h:225
TIFFCodeMethod tif_decodestrip
Definition: tiffiop.h:207
TIFFCodeMethod tif_decoderow
Definition: tiffiop.h:205
TIFFBoolMethod tif_postencode
Definition: tiffiop.h:204
TIFFCodeMethod tif_encoderow
Definition: tiffiop.h:206
uint8_t * tif_data
Definition: tiffiop.h:216
uint8_t * tif_rawcp
Definition: tiffiop.h:224
uint8_t * tif_rawdata
Definition: tiffiop.h:220
tmsize_t tif_rawdatasize
Definition: tiffiop.h:221
uint32_t tif_row
Definition: tiffiop.h:162
TIFFCodeMethod tif_decodetile
Definition: tiffiop.h:209
#define TIFFInitPackBits
Definition: tif_codec.c:38
#define TIFF_SSIZE_FORMAT
Definition: tif_config.h:176
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_error.c:107
void _TIFFfreeExt(TIFF *tif, void *p)
Definition: tif_open.c:275
void * _TIFFmallocExt(TIFF *tif, tmsize_t s)
Definition: tif_open.c:173
tmsize_t TIFFScanlineSize(TIFF *tif)
Definition: tif_strip.c:343
tmsize_t TIFFTileRowSize(TIFF *tif)
Definition: tif_tile.c:177
void _TIFFmemcpy(void *d, const void *s, tmsize_t c)
Definition: tif_unix.c:355
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Definition: tif_warning.c:80
int TIFFFlushData1(TIFF *tif)
Definition: tif_write.c:931
TIFF_SSIZE_T tmsize_t
Definition: tiffio.h:67
#define isTiled(tif)
Definition: tiffiop.h:274
RUN
Definition: userinit.h:41