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