1 /* $Id: tif_predict.c,v 1.35 2015-08-31 15:05:57 erouault 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* tif, uint8* cp0, tmsize_t cc);
38 static void horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
39 static void horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
40 static void swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
41 static void swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
42 static void horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc);
43 static void horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc);
44 static void horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc);
45 static void swabHorDiff16(TIFF* tif, uint8* cp0, tmsize_t cc);
46 static void swabHorDiff32(TIFF* tif, uint8* cp0, tmsize_t cc);
47 static void fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc);
48 static void fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc);
49 static int PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
50 static int PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
51 static int PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
52 static int PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s);
55 PredictorSetup(TIFF* tif)
57 static const char module[] = "PredictorSetup";
59 TIFFPredictorState* sp = PredictorState(tif);
60 TIFFDirectory* td = &tif->tif_dir;
62 switch (sp->predictor) /* no differencing */
66 case PREDICTOR_HORIZONTAL:
67 if (td->td_bitspersample != 8
68 && td->td_bitspersample != 16
69 && td->td_bitspersample != 32) {
70 TIFFErrorExt(tif->tif_clientdata, module,
71 "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
72 td->td_bitspersample);
76 case PREDICTOR_FLOATINGPOINT:
77 if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) {
78 TIFFErrorExt(tif->tif_clientdata, module,
79 "Floating point \"Predictor\" not supported with %d data format",
85 TIFFErrorExt(tif->tif_clientdata, module,
86 "\"Predictor\" value %d not supported",
90 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
91 td->td_samplesperpixel : 1);
93 * Calculate the scanline/tile-width size in bytes.
96 sp->rowsize = TIFFTileRowSize(tif);
98 sp->rowsize = TIFFScanlineSize(tif);
106 PredictorSetupDecode(TIFF* tif)
108 TIFFPredictorState* sp = PredictorState(tif);
109 TIFFDirectory* td = &tif->tif_dir;
111 if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
114 if (sp->predictor == 2) {
115 switch (td->td_bitspersample) {
116 case 8: sp->decodepfunc = horAcc8; break;
117 case 16: sp->decodepfunc = horAcc16; break;
118 case 32: sp->decodepfunc = horAcc32; break;
121 * Override default decoding method with one that does the
124 if( tif->tif_decoderow != PredictorDecodeRow )
126 sp->decoderow = tif->tif_decoderow;
127 tif->tif_decoderow = PredictorDecodeRow;
128 sp->decodestrip = tif->tif_decodestrip;
129 tif->tif_decodestrip = PredictorDecodeTile;
130 sp->decodetile = tif->tif_decodetile;
131 tif->tif_decodetile = PredictorDecodeTile;
135 * If the data is horizontally differenced 16-bit data that
136 * requires byte-swapping, then it must be byte swapped before
137 * the accumulation step. We do this with a special-purpose
138 * routine and override the normal post decoding logic that
139 * the library setup when the directory was read.
141 if (tif->tif_flags & TIFF_SWAB) {
142 if (sp->decodepfunc == horAcc16) {
143 sp->decodepfunc = swabHorAcc16;
144 tif->tif_postdecode = _TIFFNoPostDecode;
145 } else if (sp->decodepfunc == horAcc32) {
146 sp->decodepfunc = swabHorAcc32;
147 tif->tif_postdecode = _TIFFNoPostDecode;
152 else if (sp->predictor == 3) {
153 sp->decodepfunc = fpAcc;
155 * Override default decoding method with one that does the
158 if( tif->tif_decoderow != PredictorDecodeRow )
160 sp->decoderow = tif->tif_decoderow;
161 tif->tif_decoderow = PredictorDecodeRow;
162 sp->decodestrip = tif->tif_decodestrip;
163 tif->tif_decodestrip = PredictorDecodeTile;
164 sp->decodetile = tif->tif_decodetile;
165 tif->tif_decodetile = PredictorDecodeTile;
168 * The data should not be swapped outside of the floating
169 * point predictor, the accumulation routine should return
170 * byres in the native order.
172 if (tif->tif_flags & TIFF_SWAB) {
173 tif->tif_postdecode = _TIFFNoPostDecode;
176 * Allocate buffer to keep the decoded bytes before
177 * rearranging in the ight order
185 PredictorSetupEncode(TIFF* tif)
187 TIFFPredictorState* sp = PredictorState(tif);
188 TIFFDirectory* td = &tif->tif_dir;
190 if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
193 if (sp->predictor == 2) {
194 switch (td->td_bitspersample) {
195 case 8: sp->encodepfunc = horDiff8; break;
196 case 16: sp->encodepfunc = horDiff16; break;
197 case 32: sp->encodepfunc = horDiff32; break;
200 * Override default encoding method with one that does the
203 if( tif->tif_encoderow != PredictorEncodeRow )
205 sp->encoderow = tif->tif_encoderow;
206 tif->tif_encoderow = PredictorEncodeRow;
207 sp->encodestrip = tif->tif_encodestrip;
208 tif->tif_encodestrip = PredictorEncodeTile;
209 sp->encodetile = tif->tif_encodetile;
210 tif->tif_encodetile = PredictorEncodeTile;
214 * If the data is horizontally differenced 16-bit data that
215 * requires byte-swapping, then it must be byte swapped after
216 * the differenciation step. We do this with a special-purpose
217 * routine and override the normal post decoding logic that
218 * the library setup when the directory was read.
220 if (tif->tif_flags & TIFF_SWAB) {
221 if (sp->encodepfunc == horDiff16) {
222 sp->encodepfunc = swabHorDiff16;
223 tif->tif_postdecode = _TIFFNoPostDecode;
224 } else if (sp->encodepfunc == horDiff32) {
225 sp->encodepfunc = swabHorDiff32;
226 tif->tif_postdecode = _TIFFNoPostDecode;
231 else if (sp->predictor == 3) {
232 sp->encodepfunc = fpDiff;
234 * Override default encoding method with one that does the
237 if( tif->tif_encoderow != PredictorEncodeRow )
239 sp->encoderow = tif->tif_encoderow;
240 tif->tif_encoderow = PredictorEncodeRow;
241 sp->encodestrip = tif->tif_encodestrip;
242 tif->tif_encodestrip = PredictorEncodeTile;
243 sp->encodetile = tif->tif_encodetile;
244 tif->tif_encodetile = PredictorEncodeTile;
251 #define REPEAT4(n, op) \
253 default: { tmsize_t i; for (i = n-4; i > 0; i--) { op; } } \
261 /* Remarks related to C standard compliance in all below functions : */
262 /* - to avoid any undefined behaviour, we only operate on unsigned types */
263 /* since the behaviour of "overflows" is defined (wrap over) */
264 /* - when storing into the byte stream, we explicitly mask with 0xff so */
265 /* as to make icc -check=conversions happy (not necessary by the standard) */
268 horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc)
270 tmsize_t stride = PredictorState(tif)->stride;
272 unsigned char* cp = (unsigned char*) cp0;
273 assert((cc%stride)==0);
276 * Pipeline the most common cases.
279 unsigned int cr = cp[0];
280 unsigned int cg = cp[1];
281 unsigned int cb = cp[2];
285 cp[0] = (unsigned char) ((cr += cp[0]) & 0xff);
286 cp[1] = (unsigned char) ((cg += cp[1]) & 0xff);
287 cp[2] = (unsigned char) ((cb += cp[2]) & 0xff);
291 } else if (stride == 4) {
292 unsigned int cr = cp[0];
293 unsigned int cg = cp[1];
294 unsigned int cb = cp[2];
295 unsigned int ca = cp[3];
299 cp[0] = (unsigned char) ((cr += cp[0]) & 0xff);
300 cp[1] = (unsigned char) ((cg += cp[1]) & 0xff);
301 cp[2] = (unsigned char) ((cb += cp[2]) & 0xff);
302 cp[3] = (unsigned char) ((ca += cp[3]) & 0xff);
309 REPEAT4(stride, cp[stride] =
310 (unsigned char) ((cp[stride] + *cp) & 0xff); cp++)
318 swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
320 uint16* wp = (uint16*) cp0;
321 tmsize_t wc = cc / 2;
323 TIFFSwabArrayOfShort(wp, wc);
324 horAcc16(tif, cp0, cc);
328 horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
330 tmsize_t stride = PredictorState(tif)->stride;
331 uint16* wp = (uint16*) cp0;
332 tmsize_t wc = cc / 2;
334 assert((cc%(2*stride))==0);
339 REPEAT4(stride, wp[stride] = (uint16)(((unsigned int)wp[stride] + (unsigned int)wp[0]) & 0xffff); wp++)
346 swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
348 uint32* wp = (uint32*) cp0;
349 tmsize_t wc = cc / 4;
351 TIFFSwabArrayOfLong(wp, wc);
352 horAcc32(tif, cp0, cc);
356 horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
358 tmsize_t stride = PredictorState(tif)->stride;
359 uint32* wp = (uint32*) cp0;
360 tmsize_t wc = cc / 4;
362 assert((cc%(4*stride))==0);
367 REPEAT4(stride, wp[stride] += wp[0]; wp++)
374 * Floating point predictor accumulation routine.
377 fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc)
379 tmsize_t stride = PredictorState(tif)->stride;
380 uint32 bps = tif->tif_dir.td_bitspersample / 8;
381 tmsize_t wc = cc / bps;
383 uint8 *cp = (uint8 *) cp0;
384 uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
386 assert((cc%(bps*stride))==0);
391 while (count > stride) {
392 REPEAT4(stride, cp[stride] =
393 (unsigned char) ((cp[stride] + cp[0]) & 0xff); cp++)
397 _TIFFmemcpy(tmp, cp0, cc);
399 for (count = 0; count < wc; count++) {
401 for (byte = 0; byte < bps; byte++) {
403 cp[bps * count + byte] = tmp[byte * wc + count];
405 cp[bps * count + byte] =
406 tmp[(bps - byte - 1) * wc + count];
414 * Decode a scanline and apply the predictor routine.
417 PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
419 TIFFPredictorState *sp = PredictorState(tif);
422 assert(sp->decoderow != NULL);
423 assert(sp->decodepfunc != NULL);
425 if ((*sp->decoderow)(tif, op0, occ0, s)) {
426 (*sp->decodepfunc)(tif, op0, occ0);
433 * Decode a tile/strip and apply the predictor routine.
434 * Note that horizontal differencing must be done on a
435 * row-by-row basis. The width of a "row" has already
436 * been calculated at pre-decode time according to the
437 * strip/tile dimensions.
440 PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
442 TIFFPredictorState *sp = PredictorState(tif);
445 assert(sp->decodetile != NULL);
447 if ((*sp->decodetile)(tif, op0, occ0, s)) {
448 tmsize_t rowsize = sp->rowsize;
450 assert((occ0%rowsize)==0);
451 assert(sp->decodepfunc != NULL);
453 (*sp->decodepfunc)(tif, op0, rowsize);
463 horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc)
465 TIFFPredictorState* sp = PredictorState(tif);
466 tmsize_t stride = sp->stride;
467 unsigned char* cp = (unsigned char*) cp0;
469 assert((cc%stride)==0);
474 * Pipeline the most common cases.
477 unsigned int r1, g1, b1;
478 unsigned int r2 = cp[0];
479 unsigned int g2 = cp[1];
480 unsigned int b2 = cp[2];
482 r1 = cp[3]; cp[3] = (unsigned char)((r1-r2)&0xff); r2 = r1;
483 g1 = cp[4]; cp[4] = (unsigned char)((g1-g2)&0xff); g2 = g1;
484 b1 = cp[5]; cp[5] = (unsigned char)((b1-b2)&0xff); b2 = b1;
486 } while ((cc -= 3) > 0);
487 } else if (stride == 4) {
488 unsigned int r1, g1, b1, a1;
489 unsigned int r2 = cp[0];
490 unsigned int g2 = cp[1];
491 unsigned int b2 = cp[2];
492 unsigned int a2 = cp[3];
494 r1 = cp[4]; cp[4] = (unsigned char)((r1-r2)&0xff); r2 = r1;
495 g1 = cp[5]; cp[5] = (unsigned char)((g1-g2)&0xff); g2 = g1;
496 b1 = cp[6]; cp[6] = (unsigned char)((b1-b2)&0xff); b2 = b1;
497 a1 = cp[7]; cp[7] = (unsigned char)((a1-a2)&0xff); a2 = a1;
499 } while ((cc -= 4) > 0);
503 REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--)
504 } while ((cc -= stride) > 0);
510 horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc)
512 TIFFPredictorState* sp = PredictorState(tif);
513 tmsize_t stride = sp->stride;
514 uint16 *wp = (uint16*) cp0;
517 assert((cc%(2*stride))==0);
523 REPEAT4(stride, wp[stride] = (uint16)(((unsigned int)wp[stride] - (unsigned int)wp[0]) & 0xffff); wp--)
530 swabHorDiff16(TIFF* tif, uint8* cp0, tmsize_t cc)
532 uint16* wp = (uint16*) cp0;
533 tmsize_t wc = cc / 2;
535 horDiff16(tif, cp0, cc);
537 TIFFSwabArrayOfShort(wp, wc);
541 horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc)
543 TIFFPredictorState* sp = PredictorState(tif);
544 tmsize_t stride = sp->stride;
545 uint32 *wp = (uint32*) cp0;
548 assert((cc%(4*stride))==0);
554 REPEAT4(stride, wp[stride] -= wp[0]; wp--)
561 swabHorDiff32(TIFF* tif, uint8* cp0, tmsize_t cc)
563 uint32* wp = (uint32*) cp0;
564 tmsize_t wc = cc / 4;
566 horDiff32(tif, cp0, cc);
568 TIFFSwabArrayOfLong(wp, wc);
572 * Floating point predictor differencing routine.
575 fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc)
577 tmsize_t stride = PredictorState(tif)->stride;
578 uint32 bps = tif->tif_dir.td_bitspersample / 8;
579 tmsize_t wc = cc / bps;
581 uint8 *cp = (uint8 *) cp0;
582 uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
584 assert((cc%(bps*stride))==0);
589 _TIFFmemcpy(tmp, cp0, cc);
590 for (count = 0; count < wc; count++) {
592 for (byte = 0; byte < bps; byte++) {
594 cp[byte * wc + count] = tmp[bps * count + byte];
596 cp[(bps - byte - 1) * wc + count] =
597 tmp[bps * count + byte];
604 cp += cc - stride - 1;
605 for (count = cc; count > stride; count -= stride)
606 REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--)
610 PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
612 TIFFPredictorState *sp = PredictorState(tif);
615 assert(sp->encodepfunc != NULL);
616 assert(sp->encoderow != NULL);
618 /* XXX horizontal differencing alters user's data XXX */
619 (*sp->encodepfunc)(tif, bp, cc);
620 return (*sp->encoderow)(tif, bp, cc, s);
624 PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s)
626 static const char module[] = "PredictorEncodeTile";
627 TIFFPredictorState *sp = PredictorState(tif);
629 tmsize_t cc = cc0, rowsize;
634 assert(sp->encodepfunc != NULL);
635 assert(sp->encodetile != NULL);
638 * Do predictor manipulation in a working buffer to avoid altering
639 * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
641 working_copy = (uint8*) _TIFFmalloc(cc0);
642 if( working_copy == NULL )
644 TIFFErrorExt(tif->tif_clientdata, module,
645 "Out of memory allocating " TIFF_SSIZE_FORMAT " byte temp buffer.",
649 memcpy( working_copy, bp0, cc0 );
652 rowsize = sp->rowsize;
654 assert((cc0%rowsize)==0);
656 (*sp->encodepfunc)(tif, bp, rowsize);
660 result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
662 _TIFFfree( working_copy );
667 #define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */
669 static const TIFFField predictFields[] = {
670 { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL },
674 PredictorVSetField(TIFF* tif, uint32 tag, va_list ap)
676 TIFFPredictorState *sp = PredictorState(tif);
679 assert(sp->vsetparent != NULL);
682 case TIFFTAG_PREDICTOR:
683 sp->predictor = (uint16) va_arg(ap, uint16_vap);
684 TIFFSetFieldBit(tif, FIELD_PREDICTOR);
687 return (*sp->vsetparent)(tif, tag, ap);
689 tif->tif_flags |= TIFF_DIRTYDIRECT;
694 PredictorVGetField(TIFF* tif, uint32 tag, va_list ap)
696 TIFFPredictorState *sp = PredictorState(tif);
699 assert(sp->vgetparent != NULL);
702 case TIFFTAG_PREDICTOR:
703 *va_arg(ap, uint16*) = sp->predictor;
706 return (*sp->vgetparent)(tif, tag, ap);
712 PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
714 TIFFPredictorState* sp = PredictorState(tif);
717 if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
718 fprintf(fd, " Predictor: ");
719 switch (sp->predictor) {
720 case 1: fprintf(fd, "none "); break;
721 case 2: fprintf(fd, "horizontal differencing "); break;
722 case 3: fprintf(fd, "floating point predictor "); break;
724 fprintf(fd, "%u (0x%x)\n", sp->predictor, sp->predictor);
727 (*sp->printdir)(tif, fd, flags);
731 TIFFPredictorInit(TIFF* tif)
733 TIFFPredictorState* sp = PredictorState(tif);
738 * Merge codec-specific tag information.
740 if (!_TIFFMergeFields(tif, predictFields,
741 TIFFArrayCount(predictFields))) {
742 TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit",
743 "Merging Predictor codec-specific tags failed");
748 * Override parent get/set field methods.
750 sp->vgetparent = tif->tif_tagmethods.vgetfield;
751 tif->tif_tagmethods.vgetfield =
752 PredictorVGetField;/* hook for predictor tag */
753 sp->vsetparent = tif->tif_tagmethods.vsetfield;
754 tif->tif_tagmethods.vsetfield =
755 PredictorVSetField;/* hook for predictor tag */
756 sp->printdir = tif->tif_tagmethods.printdir;
757 tif->tif_tagmethods.printdir =
758 PredictorPrintDir; /* hook for predictor tag */
760 sp->setupdecode = tif->tif_setupdecode;
761 tif->tif_setupdecode = PredictorSetupDecode;
762 sp->setupencode = tif->tif_setupencode;
763 tif->tif_setupencode = PredictorSetupEncode;
765 sp->predictor = 1; /* default value */
766 sp->encodepfunc = NULL; /* no predictor routine */
767 sp->decodepfunc = NULL; /* no predictor routine */
772 TIFFPredictorCleanup(TIFF* tif)
774 TIFFPredictorState* sp = PredictorState(tif);
778 tif->tif_tagmethods.vgetfield = sp->vgetparent;
779 tif->tif_tagmethods.vsetfield = sp->vsetparent;
780 tif->tif_tagmethods.printdir = sp->printdir;
781 tif->tif_setupdecode = sp->setupdecode;
782 tif->tif_setupencode = sp->setupencode;
787 /* vim: set ts=8 sts=8 sw=8 noet: */