1 /* PDFlib GmbH cvsid:
2  * $Id: tif_thunder.c,v 1.10 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 THUNDER_SUPPORT
30 /*
31  * TIFF Library.
32  *
33  * ThunderScan 4-bit Compression Algorithm Support
34  */
35 
36 /*
37  * ThunderScan uses an encoding scheme designed for
38  * 4-bit pixel values.  Data is encoded in bytes, with
39  * each byte split into a 2-bit code word and a 6-bit
40  * data value.  The encoding gives raw data, runs of
41  * pixels, or pixel values encoded as a delta from the
42  * previous pixel value.  For the latter, either 2-bit
43  * or 3-bit delta values are used, with the deltas packed
44  * into a single byte.
45  */
46 #define	THUNDER_DATA		0x3f	/* mask for 6-bit data */
47 #define	THUNDER_CODE		0xc0	/* mask for 2-bit code word */
48 /* code values */
49 #define	THUNDER_RUN		0x00	/* run of pixels w/ encoded count */
50 #define	THUNDER_2BITDELTAS	0x40	/* 3 pixels w/ encoded 2-bit deltas */
51 #define	    DELTA2_SKIP		2	/* skip code for 2-bit deltas */
52 #define	THUNDER_3BITDELTAS	0x80	/* 2 pixels w/ encoded 3-bit deltas */
53 #define	    DELTA3_SKIP		4	/* skip code for 3-bit deltas */
54 #define	THUNDER_RAW		0xc0	/* raw data encoded */
55 
56 static const int twobitdeltas[4] = { 0, 1, 0, -1 };
57 static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
58 
59 #define	SETPIXEL(op, v) { \
60 	lastpixel = (v) & 0xf; \
61 	if (npixels++ & 1) \
62 	    *op++ |= lastpixel; \
63 	else \
64 	    op[0] = (tidataval_t) (lastpixel << 4); \
65 }
66 
67 static int
ThunderDecode(TIFF * tif,tidata_t op,tsize_t maxpixels)68 ThunderDecode(TIFF* tif, tidata_t op, tsize_t maxpixels)
69 {
70 	register unsigned char *bp;
71 	register tsize_t cc;
72 	unsigned int lastpixel;
73 	tsize_t npixels;
74 
75 	bp = (unsigned char *)tif->tif_rawcp;
76 	cc = tif->tif_rawcc;
77 	lastpixel = 0;
78 	npixels = 0;
79 	while (cc > 0 && npixels < maxpixels) {
80 		int n, delta;
81 
82 		n = *bp++, cc--;
83 		switch (n & THUNDER_CODE) {
84 		case THUNDER_RUN:		/* pixel run */
85 			/*
86 			 * Replicate the last pixel n times,
87 			 * where n is the lower-order 6 bits.
88 			 */
89 			if (npixels & 1) {
90 				op[0] |= lastpixel;
91 				lastpixel = *op++; npixels++; n--;
92 			} else
93 				lastpixel |= lastpixel << 4;
94 			npixels += n;
95 			if (npixels < maxpixels) {
96 				for (; n > 0; n -= 2)
97 					*op++ = (tidataval_t) lastpixel;
98 			}
99 			if (n == -1)
100 				*--op &= 0xf0;
101 			lastpixel &= 0xf;
102 			break;
103 		case THUNDER_2BITDELTAS:	/* 2-bit deltas */
104 			if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
105 				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
106 			if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
107 				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
108 			if ((delta = (n & 3)) != DELTA2_SKIP)
109 				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
110 			break;
111 		case THUNDER_3BITDELTAS:	/* 3-bit deltas */
112 			if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
113 				SETPIXEL(op, lastpixel + threebitdeltas[delta]);
114 			if ((delta = (n & 7)) != DELTA3_SKIP)
115 				SETPIXEL(op, lastpixel + threebitdeltas[delta]);
116 			break;
117 		case THUNDER_RAW:		/* raw data */
118 			SETPIXEL(op, n);
119 			break;
120 		}
121 	}
122 	tif->tif_rawcp = (tidata_t) bp;
123 	tif->tif_rawcc = cc;
124 	if (npixels != maxpixels) {
125 		_TIFFError(tif, tif->tif_name,
126 		    "ThunderDecode: %s data at scanline %ld (%lu != %lu)",
127 		    npixels < maxpixels ? "Not enough" : "Too much",
128 		    (long) tif->tif_row, (long) npixels, (long) maxpixels);
129 		return (0);
130 	}
131 	return (1);
132 }
133 
134 static int
ThunderDecodeRow(TIFF * tif,tidata_t buf,tsize_t occ,tsample_t s)135 ThunderDecodeRow(TIFF* tif, tidata_t buf, tsize_t occ, tsample_t s)
136 {
137 	tidata_t row = buf;
138 
139 	(void) s;
140 	while ((long)occ > 0) {
141 		if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
142 			return (0);
143 		occ -= tif->tif_scanlinesize;
144 		row += tif->tif_scanlinesize;
145 	}
146 	return (1);
147 }
148 
149 int
TIFFInitThunderScan(TIFF * tif,int scheme)150 TIFFInitThunderScan(TIFF* tif, int scheme)
151 {
152 	(void) scheme;
153 	tif->tif_decoderow = ThunderDecodeRow;
154 	tif->tif_decodestrip = ThunderDecodeRow;
155 	return (1);
156 }
157 #endif /* THUNDER_SUPPORT */
158 
159 /* vim: set ts=8 sts=8 sw=8 noet: */
160