LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_thunder.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
30#include "tiffiop.h"
31#include <assert.h>
32#ifdef THUNDER_SUPPORT
33/*
34 * TIFF Library.
35 *
36 * ThunderScan 4-bit Compression Algorithm Support
37 */
38
39/*
40 * ThunderScan uses an encoding scheme designed for
41 * 4-bit pixel values. Data is encoded in bytes, with
42 * each byte split into a 2-bit code word and a 6-bit
43 * data value. The encoding gives raw data, runs of
44 * pixels, or pixel values encoded as a delta from the
45 * previous pixel value. For the latter, either 2-bit
46 * or 3-bit delta values are used, with the deltas packed
47 * into a single byte.
48 */
49// #define THUNDER_DATA 0x3f /* mask for 6-bit data */
57#define THUNDER_CODE 0xc0 /* mask for 2-bit code word */
58/* code values */
65#define THUNDER_RUN 0x00 /* run of pixels w/ encoded count */
72#define THUNDER_2BITDELTAS 0x40 /* 3 pixels w/ encoded 2-bit deltas */
77#define DELTA2_SKIP 2 /* skip code for 2-bit deltas */
84#define THUNDER_3BITDELTAS 0x80 /* 2 pixels w/ encoded 3-bit deltas */
89#define DELTA3_SKIP 4 /* skip code for 3-bit deltas */
96#define THUNDER_RAW 0xc0 /* raw data encoded */
97
104static const int twobitdeltas[4] = {0, 1, 0, -1};
111static const int threebitdeltas[8] = {0, 1, 2, 3, 0, -3, -2, -1};
112
131#define SETPIXEL(op, v) \
132 { \
133 lastpixel = (v) & 0xf; \
134 if (npixels < maxpixels) \
135 { \
136 if (npixels++ & 1) \
137 *op++ |= (uint8_t)lastpixel; \
138 else \
139 op[0] = (uint8_t)(lastpixel << 4); \
140 } \
141 }
142
155static int ThunderSetupDecode(TIFF *tif)
156{
157 static const char module[] = "ThunderSetupDecode";
158
159 if (tif->tif_dir.td_bitspersample != 4)
160 {
161 TIFFErrorExtR(tif, module,
162 "Wrong bitspersample value (%d), Thunder decoder only "
163 "supports 4bits per sample.",
164 (int)tif->tif_dir.td_bitspersample);
165 return 0;
166 }
167 if (tif->tif_dir.td_samplesperpixel != 1 &&
169 {
170 TIFFErrorExtR(tif, module,
171 "Samples/pixel shall be 1 for ThunderScan decoding, "
172 "or PlanarConfiguration must be set to Separate.");
173 return 0;
174 }
175
176 return (1);
177}
178
198static int ThunderDecode(TIFF *tif, uint8_t *op0, tmsize_t maxpixels)
199{
200 static const char module[] = "ThunderDecode";
201 unsigned char *bp;
202 tmsize_t cc;
203 unsigned int lastpixel;
204 tmsize_t npixels;
205 uint8_t *op = op0;
206
207 bp = (unsigned char *)tif->tif_rawcp;
208 cc = tif->tif_rawcc;
209 lastpixel = 0;
210 npixels = 0;
211 while (cc > 0 && npixels < maxpixels)
212 {
213 int n, delta;
214
215 n = *bp++;
216 cc--;
217 switch (n & THUNDER_CODE)
218 {
219 case THUNDER_RUN: /* pixel run */
220 /*
221 * Replicate the last pixel n times,
222 * where n is the lower-order 6 bits.
223 */
224 if (n == 0)
225 break;
226 if (npixels & 1)
227 {
228 op[0] |= (uint8_t)lastpixel;
229 lastpixel = *op++;
230 npixels++;
231 n--;
232 }
233 else
234 lastpixel |= lastpixel << 4;
235 npixels += n;
236 if (npixels > maxpixels)
237 break;
238 for (; n > 0; n -= 2)
239 *op++ = (uint8_t)lastpixel;
240 if (n == -1)
241 *--op &= 0xf0;
242 lastpixel &= 0xf;
243 break;
244 case THUNDER_2BITDELTAS: /* 2-bit deltas */
245 if ((delta = ((n >> 4) & 3)) != DELTA2_SKIP)
246 SETPIXEL(op,
247 (unsigned)((int)lastpixel + twobitdeltas[delta]));
248 if ((delta = ((n >> 2) & 3)) != DELTA2_SKIP)
249 SETPIXEL(op,
250 (unsigned)((int)lastpixel + twobitdeltas[delta]));
251 if ((delta = (n & 3)) != DELTA2_SKIP)
252 SETPIXEL(op,
253 (unsigned)((int)lastpixel + twobitdeltas[delta]));
254 break;
255 case THUNDER_3BITDELTAS: /* 3-bit deltas */
256 if ((delta = ((n >> 3) & 7)) != DELTA3_SKIP)
257 SETPIXEL(
258 op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
259 if ((delta = (n & 7)) != DELTA3_SKIP)
260 SETPIXEL(
261 op, (unsigned)((int)lastpixel + threebitdeltas[delta]));
262 break;
263 case THUNDER_RAW: /* raw data */
264 SETPIXEL(op, n);
265 break;
266 default:
267 break;
268 }
269 }
270 tif->tif_rawcp = (uint8_t *)bp;
271 tif->tif_rawcc = cc;
272 if (npixels != maxpixels)
273 {
274 uint8_t *op_end = op0 + (maxpixels + 1) / 2;
275 memset(op, 0, (size_t)(op_end - op));
276 TIFFErrorExtR(tif, module,
277 "%s data at scanline %lu (%" PRIu64 " != %" PRIu64 ")",
278 npixels < maxpixels ? "Not enough" : "Too much",
279 (unsigned long)tif->tif_dir.td_row, (uint64_t)npixels,
280 (uint64_t)maxpixels);
281 return (0);
282 }
283
284 return (1);
285}
286
305static int ThunderDecodeRow(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
306{
307 static const char module[] = "ThunderDecodeRow";
308 uint8_t *row = buf;
309
310 (void)s;
311 if (occ % tif->tif_dir.td_scanlinesize)
312 {
313 TIFFErrorExtR(tif, module, "Fractional scanlines cannot be read");
314 return (0);
315 }
316 while (occ > 0)
317 {
318 if (!ThunderDecode(tif, row, tif->tif_dir.td_imagewidth))
319 {
320 /*
321 * ThunderDecode only clears the tail of the scanline it was
322 * working on. Clear this scanline and every row after it, so a
323 * failed decode cannot hand the caller whatever its buffer
324 * happened to contain.
325 */
326 memset(row, 0, (size_t)occ);
327 return (0);
328 }
329 occ -= tif->tif_dir.td_scanlinesize;
330 row += tif->tif_dir.td_scanlinesize;
331 }
332 return (1);
333}
334
335int TIFFInitThunderScan(TIFF *tif, int scheme)
336{
337 (void)scheme;
338
342 return (1);
343}
344#endif /* THUNDER_SUPPORT */
int TIFFInitThunderScan(TIFF *tif, int scheme)
Install ThunderScan transfer callbacks.
static int ThunderDecodeRow(TIFF *tif, uint8_t *buf, tmsize_t occ, uint16_t s)
Decode complete ThunderScan scanlines into caller storage.
#define THUNDER_2BITDELTAS
ThunderScan code word for three packed 2-bit pixel deltas.
Definition tif_thunder.c:72
static int ThunderSetupDecode(TIFF *tif)
Validate four-bit single-plane ThunderScan decoding.
#define THUNDER_CODE
Mask selecting the 2-bit code word in the top bits of a ThunderScan byte.
Definition tif_thunder.c:57
static int ThunderDecode(TIFF *tif, uint8_t *op0, tmsize_t maxpixels)
Expand one ThunderScan row into packed four-bit samples.
#define DELTA3_SKIP
3-bit delta value meaning "no pixel" in a THUNDER_3BITDELTAS byte.
Definition tif_thunder.c:89
#define THUNDER_3BITDELTAS
ThunderScan code word for two packed 3-bit pixel deltas.
Definition tif_thunder.c:84
#define THUNDER_RAW
ThunderScan code word for a literal 4-bit pixel.
Definition tif_thunder.c:96
#define DELTA2_SKIP
2-bit delta value meaning "no pixel" in a THUNDER_2BITDELTAS byte.
Definition tif_thunder.c:77
#define SETPIXEL(op, v)
Store a 4-bit pixel into the packed output if maxpixels has not been reached.
#define THUNDER_RUN
ThunderScan code word for a run of the previous pixel.
Definition tif_thunder.c:65
static const int twobitdeltas[4]
Map two-bit delta codes to signed pixel increments.
static const int threebitdeltas[8]
Map three-bit delta codes to signed pixel increments.
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
#define PLANARCONFIG_SEPARATE
Separate planes of data.
Definition tiff.h:474
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
uint32_t td_imagewidth
Image width in pixels.
Definition tif_dir.h:154
tmsize_t td_scanlinesize
Cached decoded scanline size in bytes; zero when not computed.
Definition tif_dir.h:245
uint16_t td_bitspersample
Bits in each sample.
Definition tif_dir.h:168
uint32_t td_row
Current scanline index; UINT32_MAX before traversal starts.
Definition tif_dir.h:240
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
Definition tif_dir.h:200
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Definition tif_dir.h:182
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
tmsize_t tif_rawcc
Raw-buffer byte count interpreted according to read/write state.
Definition tiffiop.h:696
TIFFCodeMethod tif_decodestrip
Codec callback decoding strip data.
Definition tiffiop.h:647
TIFFCodeMethod tif_decoderow
Codec callback decoding a scanline.
Definition tiffiop.h:643
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
uint8_t * tif_rawcp
Borrowed current-position pointer into the raw buffer.
Definition tiffiop.h:692
TIFFBoolMethod tif_setupdecode
Codec setup callback run before predecode.
Definition tiffiop.h:628
Declare private library state, callbacks, and implementation helpers.