1 /* $Id: tif_packbits.c,v 1.22 2012-06-20 05:25:33 fwarmerdam Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 #ifdef PACKBITS_SUPPORT
32 * PackBits Compression Algorithm Support
37 PackBitsPreEncode(TIFF* tif, uint16 s)
41 if (!(tif->tif_data = (uint8*)_TIFFmalloc(sizeof(tmsize_t))))
44 * Calculate the scanline/tile-width size in bytes.
47 *(tmsize_t*)tif->tif_data = TIFFTileRowSize(tif);
49 *(tmsize_t*)tif->tif_data = TIFFScanlineSize(tif);
54 PackBitsPostEncode(TIFF* tif)
57 _TIFFfree(tif->tif_data);
62 * Encode a run of pixels.
65 PackBitsEncode(TIFF* tif, uint8* buf, tmsize_t cc, uint16 s)
67 unsigned char* bp = (unsigned char*) buf;
73 enum { BASE, LITERAL, RUN, LITERAL_RUN } state;
77 ep = tif->tif_rawdata + tif->tif_rawdatasize;
82 * Find the longest string of identical bytes.
84 b = *bp++, cc--, n = 1;
85 for (; cc > 0 && b == *bp; cc--, bp++)
88 if (op + 2 >= ep) { /* insure space for new data */
90 * Be careful about writing the last
91 * literal. Must write up to that point
92 * and then copy the remainder to the
93 * front of the buffer.
95 if (state == LITERAL || state == LITERAL_RUN) {
96 slop = (long)(op - lastliteral);
97 tif->tif_rawcc += (tmsize_t)(lastliteral - tif->tif_rawcp);
98 if (!TIFFFlushData1(tif))
102 *op++ = *lastliteral++;
103 lastliteral = tif->tif_rawcp;
105 tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
106 if (!TIFFFlushData1(tif))
112 case BASE: /* initial state, set run/literal */
116 *op++ = (uint8) -127;
121 *op++ = (uint8)(-(n-1));
130 case LITERAL: /* last object was literal string */
134 *op++ = (uint8) -127;
139 *op++ = (uint8)(-(n-1)); /* encode run */
141 } else { /* extend literal */
142 if (++(*lastliteral) == 127)
147 case RUN: /* last object was run */
150 *op++ = (uint8) -127;
155 *op++ = (uint8)(-(n-1));
164 case LITERAL_RUN: /* literal followed by a run */
166 * Check to see if previous run should
167 * be converted to a literal, in which
168 * case we convert literal-run-literal
169 * to a single literal.
171 if (n == 1 && op[-2] == (uint8) -1 &&
172 *lastliteral < 126) {
173 state = (((*lastliteral) += 2) == 127 ?
175 op[-2] = op[-1]; /* replicate */
181 tif->tif_rawcc += (tmsize_t)(op - tif->tif_rawcp);
187 * Encode a rectangular chunk of pixels. We break it up
188 * into row-sized pieces to insure that encoded runs do
189 * not span rows. Otherwise, there can be problems with
190 * the decoder if data is read, for example, by scanlines
191 * when it was encoded by strips.
194 PackBitsEncodeChunk(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
196 tmsize_t rowsize = *(tmsize_t*)tif->tif_data;
199 tmsize_t chunk = rowsize;
204 if (PackBitsEncode(tif, bp, chunk, s) < 0)
213 PackBitsDecode(TIFF* tif, uint8* op, tmsize_t occ, uint16 s)
215 static const char module[] = "PackBitsDecode";
222 bp = (char*) tif->tif_rawcp;
224 while (cc > 0 && occ > 0) {
225 n = (long) *bp++, cc--;
227 * Watch out for compilers that
228 * don't sign extend chars...
232 if (n < 0) { /* replicate next byte -n+1 times */
233 if (n == -128) /* nop */
236 if( occ < (tmsize_t)n )
238 TIFFWarningExt(tif->tif_clientdata, module,
239 "Discarding %lu bytes to avoid buffer overrun",
240 (unsigned long) ((tmsize_t)n - occ));
247 } else { /* copy next n+1 bytes literally */
248 if (occ < (tmsize_t)(n + 1))
250 TIFFWarningExt(tif->tif_clientdata, module,
251 "Discarding %lu bytes to avoid buffer overrun",
252 (unsigned long) ((tmsize_t)n - occ + 1));
255 if (cc < (tmsize_t) (n+1))
257 TIFFWarningExt(tif->tif_clientdata, module,
258 "Terminating PackBitsDecode due to lack of data.");
261 _TIFFmemcpy(op, bp, ++n);
266 tif->tif_rawcp = (uint8*) bp;
269 TIFFErrorExt(tif->tif_clientdata, module,
270 "Not enough data for scanline %lu",
271 (unsigned long) tif->tif_row);
278 TIFFInitPackBits(TIFF* tif, int scheme)
281 tif->tif_decoderow = PackBitsDecode;
282 tif->tif_decodestrip = PackBitsDecode;
283 tif->tif_decodetile = PackBitsDecode;
284 tif->tif_preencode = PackBitsPreEncode;
285 tif->tif_postencode = PackBitsPostEncode;
286 tif->tif_encoderow = PackBitsEncode;
287 tif->tif_encodestrip = PackBitsEncodeChunk;
288 tif->tif_encodetile = PackBitsEncodeChunk;
291 #endif /* PACKBITS_SUPPORT */
293 /* vim: set ts=8 sts=8 sw=8 noet: */