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