1 /* $Id: tif_predict.c,v 1.1 2005/06/17 13:54:52 vp153 Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
30 * Predictor Tag Support (used by multiple codecs).
33 #include "tif_predict.h"
35 #define PredictorState(tif) ((TIFFPredictorState*) (tif)->tif_data)
37 static void horAcc8(TIFF*, tidata_t, tsize_t);
38 static void horAcc16(TIFF*, tidata_t, tsize_t);
39 static void swabHorAcc16(TIFF*, tidata_t, tsize_t);
40 static void horDiff8(TIFF*, tidata_t, tsize_t);
41 static void horDiff16(TIFF*, tidata_t, tsize_t);
42 static int PredictorDecodeRow(TIFF*, tidata_t, tsize_t, tsample_t);
43 static int PredictorDecodeTile(TIFF*, tidata_t, tsize_t, tsample_t);
44 static int PredictorEncodeRow(TIFF*, tidata_t, tsize_t, tsample_t);
45 static int PredictorEncodeTile(TIFF*, tidata_t, tsize_t, tsample_t);
48 PredictorSetup(TIFF* tif)
50 TIFFPredictorState* sp = PredictorState(tif);
51 TIFFDirectory* td = &tif->tif_dir;
53 if (sp->predictor == 1) /* no differencing */
55 if (sp->predictor != 2) {
56 TIFFError(tif->tif_name, "\"Predictor\" value %d not supported",
60 if (td->td_bitspersample != 8 && td->td_bitspersample != 16) {
61 TIFFError(tif->tif_name,
62 "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
63 td->td_bitspersample);
66 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
67 td->td_samplesperpixel : 1);
69 * Calculate the scanline/tile-width size in bytes.
72 sp->rowsize = TIFFTileRowSize(tif);
74 sp->rowsize = TIFFScanlineSize(tif);
79 PredictorSetupDecode(TIFF* tif)
81 TIFFPredictorState* sp = PredictorState(tif);
82 TIFFDirectory* td = &tif->tif_dir;
84 if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
86 if (sp->predictor == 2) {
87 switch (td->td_bitspersample) {
88 case 8: sp->pfunc = horAcc8; break;
89 case 16: sp->pfunc = horAcc16; break;
92 * Override default decoding method with
93 * one that does the predictor stuff.
95 sp->coderow = tif->tif_decoderow;
96 tif->tif_decoderow = PredictorDecodeRow;
97 sp->codestrip = tif->tif_decodestrip;
98 tif->tif_decodestrip = PredictorDecodeTile;
99 sp->codetile = tif->tif_decodetile;
100 tif->tif_decodetile = PredictorDecodeTile;
102 * If the data is horizontally differenced
103 * 16-bit data that requires byte-swapping,
104 * then it must be byte swapped before the
105 * accumulation step. We do this with a
106 * special-purpose routine and override the
107 * normal post decoding logic that the library
108 * setup when the directory was read.
110 if (tif->tif_flags&TIFF_SWAB) {
111 if (sp->pfunc == horAcc16) {
112 sp->pfunc = swabHorAcc16;
113 tif->tif_postdecode = _TIFFNoPostDecode;
114 } /* else handle 32-bit case... */
121 PredictorSetupEncode(TIFF* tif)
123 TIFFPredictorState* sp = PredictorState(tif);
124 TIFFDirectory* td = &tif->tif_dir;
126 if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
128 if (sp->predictor == 2) {
129 switch (td->td_bitspersample) {
130 case 8: sp->pfunc = horDiff8; break;
131 case 16: sp->pfunc = horDiff16; break;
134 * Override default encoding method with
135 * one that does the predictor stuff.
137 sp->coderow = tif->tif_encoderow;
138 tif->tif_encoderow = PredictorEncodeRow;
139 sp->codestrip = tif->tif_encodestrip;
140 tif->tif_encodestrip = PredictorEncodeTile;
141 sp->codetile = tif->tif_encodetile;
142 tif->tif_encodetile = PredictorEncodeTile;
147 #define REPEAT4(n, op) \
149 default: { int i; for (i = n-4; i > 0; i--) { op; } } \
158 horAcc8(TIFF* tif, tidata_t cp0, tsize_t cc)
160 TIFFPredictorState* sp = PredictorState(tif);
161 tsize_t stride = sp->stride;
163 char* cp = (char*) cp0;
167 * Pipeline the most common cases.
170 unsigned int cr = cp[0];
171 unsigned int cg = cp[1];
172 unsigned int cb = cp[2];
175 cp[0] = (char) (cr += cp[0]);
176 cp[1] = (char) (cg += cp[1]);
177 cp[2] = (char) (cb += cp[2]);
178 } while ((int32) cc > 0);
179 } else if (stride == 4) {
180 unsigned int cr = cp[0];
181 unsigned int cg = cp[1];
182 unsigned int cb = cp[2];
183 unsigned int ca = cp[3];
186 cp[0] = (char) (cr += cp[0]);
187 cp[1] = (char) (cg += cp[1]);
188 cp[2] = (char) (cb += cp[2]);
189 cp[3] = (char) (ca += cp[3]);
190 } while ((int32) cc > 0);
193 REPEAT4(stride, cp[stride] = (char) (cp[stride] + *cp); cp++)
195 } while ((int32) cc > 0);
201 swabHorAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
203 TIFFPredictorState* sp = PredictorState(tif);
204 tsize_t stride = sp->stride;
205 uint16* wp = (uint16*) cp0;
209 TIFFSwabArrayOfShort(wp, wc);
212 REPEAT4(stride, wp[stride] += wp[0]; wp++)
214 } while ((int32) wc > 0);
219 horAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
221 tsize_t stride = PredictorState(tif)->stride;
222 uint16* wp = (uint16*) cp0;
228 REPEAT4(stride, wp[stride] += wp[0]; wp++)
230 } while ((int32) wc > 0);
235 * Decode a scanline and apply the predictor routine.
238 PredictorDecodeRow(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
240 TIFFPredictorState *sp = PredictorState(tif);
243 assert(sp->coderow != NULL);
244 assert(sp->pfunc != NULL);
245 if ((*sp->coderow)(tif, op0, occ0, s)) {
246 (*sp->pfunc)(tif, op0, occ0);
253 * Decode a tile/strip and apply the predictor routine.
254 * Note that horizontal differencing must be done on a
255 * row-by-row basis. The width of a "row" has already
256 * been calculated at pre-decode time according to the
257 * strip/tile dimensions.
260 PredictorDecodeTile(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
262 TIFFPredictorState *sp = PredictorState(tif);
265 assert(sp->codetile != NULL);
266 if ((*sp->codetile)(tif, op0, occ0, s)) {
267 tsize_t rowsize = sp->rowsize;
269 assert(sp->pfunc != NULL);
270 while ((long)occ0 > 0) {
271 (*sp->pfunc)(tif, op0, (tsize_t) rowsize);
281 horDiff8(TIFF* tif, tidata_t cp0, tsize_t cc)
283 TIFFPredictorState* sp = PredictorState(tif);
284 tsize_t stride = sp->stride;
285 char* cp = (char*) cp0;
290 * Pipeline the most common cases.
298 r1 = cp[3]; cp[3] = r1-r2; r2 = r1;
299 g1 = cp[4]; cp[4] = g1-g2; g2 = g1;
300 b1 = cp[5]; cp[5] = b1-b2; b2 = b1;
302 } while ((int32)(cc -= 3) > 0);
303 } else if (stride == 4) {
310 r1 = cp[4]; cp[4] = r1-r2; r2 = r1;
311 g1 = cp[5]; cp[5] = g1-g2; g2 = g1;
312 b1 = cp[6]; cp[6] = b1-b2; b2 = b1;
313 a1 = cp[7]; cp[7] = a1-a2; a2 = a1;
315 } while ((int32)(cc -= 4) > 0);
319 REPEAT4(stride, cp[stride] -= cp[0]; cp--)
320 } while ((int32)(cc -= stride) > 0);
326 horDiff16(TIFF* tif, tidata_t cp0, tsize_t cc)
328 TIFFPredictorState* sp = PredictorState(tif);
329 tsize_t stride = sp->stride;
330 int16 *wp = (int16*) cp0;
337 REPEAT4(stride, wp[stride] -= wp[0]; wp--)
339 } while ((int32) wc > 0);
344 PredictorEncodeRow(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
346 TIFFPredictorState *sp = PredictorState(tif);
349 assert(sp->pfunc != NULL);
350 assert(sp->coderow != NULL);
351 /* XXX horizontal differencing alters user's data XXX */
352 (*sp->pfunc)(tif, bp, cc);
353 return ((*sp->coderow)(tif, bp, cc, s));
357 PredictorEncodeTile(TIFF* tif, tidata_t bp0, tsize_t cc0, tsample_t s)
359 TIFFPredictorState *sp = PredictorState(tif);
360 tsize_t cc = cc0, rowsize;
361 unsigned char* bp = bp0;
364 assert(sp->pfunc != NULL);
365 assert(sp->codetile != NULL);
366 rowsize = sp->rowsize;
368 while ((long)cc > 0) {
369 (*sp->pfunc)(tif, bp, (tsize_t) rowsize);
373 return ((*sp->codetile)(tif, bp0, cc0, s));
376 #define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */
378 static const TIFFFieldInfo predictFieldInfo[] = {
379 { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, FIELD_PREDICTOR,
380 FALSE, FALSE, "Predictor" },
382 #define N(a) (sizeof (a) / sizeof (a[0]))
385 PredictorVSetField(TIFF* tif, ttag_t tag, va_list ap)
387 TIFFPredictorState *sp = PredictorState(tif);
390 case TIFFTAG_PREDICTOR:
391 sp->predictor = (uint16) va_arg(ap, int);
392 TIFFSetFieldBit(tif, FIELD_PREDICTOR);
395 return (*sp->vsetparent)(tif, tag, ap);
397 tif->tif_flags |= TIFF_DIRTYDIRECT;
402 PredictorVGetField(TIFF* tif, ttag_t tag, va_list ap)
404 TIFFPredictorState *sp = PredictorState(tif);
407 case TIFFTAG_PREDICTOR:
408 *va_arg(ap, uint16*) = sp->predictor;
411 return (*sp->vgetparent)(tif, tag, ap);
417 PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
419 TIFFPredictorState* sp = PredictorState(tif);
422 if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
423 fprintf(fd, " Predictor: ");
424 switch (sp->predictor) {
425 case 1: fprintf(fd, "none "); break;
426 case 2: fprintf(fd, "horizontal differencing "); break;
428 fprintf(fd, "%u (0x%x)\n", sp->predictor, sp->predictor);
431 (*sp->printdir)(tif, fd, flags);
435 TIFFPredictorInit(TIFF* tif)
437 TIFFPredictorState* sp = PredictorState(tif);
440 * Merge codec-specific tag information and
441 * override parent get/set field methods.
443 _TIFFMergeFieldInfo(tif, predictFieldInfo, N(predictFieldInfo));
444 sp->vgetparent = tif->tif_tagmethods.vgetfield;
445 tif->tif_tagmethods.vgetfield =
446 PredictorVGetField;/* hook for predictor tag */
447 sp->vsetparent = tif->tif_tagmethods.vsetfield;
448 tif->tif_tagmethods.vsetfield =
449 PredictorVSetField;/* hook for predictor tag */
450 sp->printdir = tif->tif_tagmethods.printdir;
451 tif->tif_tagmethods.printdir =
452 PredictorPrintDir; /* hook for predictor tag */
454 sp->setupdecode = tif->tif_setupdecode;
455 tif->tif_setupdecode = PredictorSetupDecode;
456 sp->setupencode = tif->tif_setupencode;
457 tif->tif_setupencode = PredictorSetupEncode;
459 sp->predictor = 1; /* default value */
460 sp->pfunc = NULL; /* no predictor routine */
464 /* vim: set ts=8 sts=8 sw=8 noet: */