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