xref: /reactos/dll/3rdparty/libtiff/tif_thunder.c (revision c2c66aff)
1 /* $Id: tif_thunder.c,v 1.13 2016-09-04 21:32:56 erouault Exp $ */
2 
3 /*
4  * Copyright (c) 1988-1997 Sam Leffler
5  * Copyright (c) 1991-1997 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 #include <precomp.h>
28 //#include <assert.h>
29 
30 #ifdef THUNDER_SUPPORT
31 /*
32  * TIFF Library.
33  *
34  * ThunderScan 4-bit Compression Algorithm Support
35  */
36 
37 /*
38  * ThunderScan uses an encoding scheme designed for
39  * 4-bit pixel values.  Data is encoded in bytes, with
40  * each byte split into a 2-bit code word and a 6-bit
41  * data value.  The encoding gives raw data, runs of
42  * pixels, or pixel values encoded as a delta from the
43  * previous pixel value.  For the latter, either 2-bit
44  * or 3-bit delta values are used, with the deltas packed
45  * into a single byte.
46  */
47 #define	THUNDER_DATA		0x3f	/* mask for 6-bit data */
48 #define	THUNDER_CODE		0xc0	/* mask for 2-bit code word */
49 /* code values */
50 #define	THUNDER_RUN		0x00	/* run of pixels w/ encoded count */
51 #define	THUNDER_2BITDELTAS	0x40	/* 3 pixels w/ encoded 2-bit deltas */
52 #define	    DELTA2_SKIP		2	/* skip code for 2-bit deltas */
53 #define	THUNDER_3BITDELTAS	0x80	/* 2 pixels w/ encoded 3-bit deltas */
54 #define	    DELTA3_SKIP		4	/* skip code for 3-bit deltas */
55 #define	THUNDER_RAW		0xc0	/* raw data encoded */
56 
57 static const int twobitdeltas[4] = { 0, 1, 0, -1 };
58 static const int threebitdeltas[8] = { 0, 1, 2, 3, 0, -3, -2, -1 };
59 
60 #define	SETPIXEL(op, v) {                     \
61 	lastpixel = (v) & 0xf;                \
62         if ( npixels < maxpixels )         \
63         {                                     \
64 	  if (npixels++ & 1)                  \
65 	    *op++ |= lastpixel;               \
66 	  else                                \
67 	    op[0] = (uint8) (lastpixel << 4); \
68         }                                     \
69 }
70 
71 static int
72 ThunderSetupDecode(TIFF* tif)
73 {
74 	static const char module[] = "ThunderSetupDecode";
75 
76         if( tif->tif_dir.td_bitspersample != 4 )
77         {
78                 TIFFErrorExt(tif->tif_clientdata, module,
79                              "Wrong bitspersample value (%d), Thunder decoder only supports 4bits per sample.",
80                              (int) tif->tif_dir.td_bitspersample );
81                 return 0;
82         }
83 
84 
85 	return (1);
86 }
87 
88 static int
89 ThunderDecode(TIFF* tif, uint8* op, tmsize_t maxpixels)
90 {
91 	static const char module[] = "ThunderDecode";
92 	register unsigned char *bp;
93 	register tmsize_t cc;
94 	unsigned int lastpixel;
95 	tmsize_t npixels;
96 
97 	bp = (unsigned char *)tif->tif_rawcp;
98 	cc = tif->tif_rawcc;
99 	lastpixel = 0;
100 	npixels = 0;
101 	while (cc > 0 && npixels < maxpixels) {
102 		int n, delta;
103 
104 		n = *bp++;
105 		cc--;
106 		switch (n & THUNDER_CODE) {
107 		case THUNDER_RUN:		/* pixel run */
108 			/*
109 			 * Replicate the last pixel n times,
110 			 * where n is the lower-order 6 bits.
111 			 */
112 			if (npixels & 1) {
113 				op[0] |= lastpixel;
114 				lastpixel = *op++; npixels++; n--;
115 			} else
116 				lastpixel |= lastpixel << 4;
117 			npixels += n;
118 			if (npixels < maxpixels) {
119 				for (; n > 0; n -= 2)
120 					*op++ = (uint8) lastpixel;
121 			}
122 			if (n == -1)
123 				*--op &= 0xf0;
124 			lastpixel &= 0xf;
125 			break;
126 		case THUNDER_2BITDELTAS:	/* 2-bit deltas */
127 			if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
128 				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
129 			if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
130 				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
131 			if ((delta = (n & 3)) != DELTA2_SKIP)
132 				SETPIXEL(op, lastpixel + twobitdeltas[delta]);
133 			break;
134 		case THUNDER_3BITDELTAS:	/* 3-bit deltas */
135 			if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
136 				SETPIXEL(op, lastpixel + threebitdeltas[delta]);
137 			if ((delta = (n & 7)) != DELTA3_SKIP)
138 				SETPIXEL(op, lastpixel + threebitdeltas[delta]);
139 			break;
140 		case THUNDER_RAW:		/* raw data */
141 			SETPIXEL(op, n);
142 			break;
143 		}
144 	}
145 	tif->tif_rawcp = (uint8*) bp;
146 	tif->tif_rawcc = cc;
147 	if (npixels != maxpixels) {
148 #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
149 		TIFFErrorExt(tif->tif_clientdata, module,
150 			     "%s data at scanline %lu (%I64u != %I64u)",
151 			     npixels < maxpixels ? "Not enough" : "Too much",
152 			     (unsigned long) tif->tif_row,
153 			     (unsigned __int64) npixels,
154 			     (unsigned __int64) maxpixels);
155 #else
156 		TIFFErrorExt(tif->tif_clientdata, module,
157 			     "%s data at scanline %lu (%llu != %llu)",
158 			     npixels < maxpixels ? "Not enough" : "Too much",
159 			     (unsigned long) tif->tif_row,
160 			     (unsigned long long) npixels,
161 			     (unsigned long long) maxpixels);
162 #endif
163 		return (0);
164 	}
165 
166         return (1);
167 }
168 
169 static int
170 ThunderDecodeRow(TIFF* tif, uint8* buf, tmsize_t occ, uint16 s)
171 {
172 	static const char module[] = "ThunderDecodeRow";
173 	uint8* row = buf;
174 
175 	(void) s;
176 	if (occ % tif->tif_scanlinesize)
177 	{
178 		TIFFErrorExt(tif->tif_clientdata, module, "Fractional scanlines cannot be read");
179 		return (0);
180 	}
181 	while (occ > 0) {
182 		if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
183 			return (0);
184 		occ -= tif->tif_scanlinesize;
185 		row += tif->tif_scanlinesize;
186 	}
187 	return (1);
188 }
189 
190 int
191 TIFFInitThunderScan(TIFF* tif, int scheme)
192 {
193 	(void) scheme;
194 
195         tif->tif_setupdecode = ThunderSetupDecode;
196 	tif->tif_decoderow = ThunderDecodeRow;
197 	tif->tif_decodestrip = ThunderDecodeRow;
198 	return (1);
199 }
200 #endif /* THUNDER_SUPPORT */
201 
202 /* vim: set ts=8 sts=8 sw=8 noet: */
203 /*
204  * Local Variables:
205  * mode: c
206  * c-basic-offset: 8
207  * fill-column: 78
208  * End:
209  */
210