2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
28 * Predictor Tag Support (used by multiple codecs).
30 #include "tif_predict.h"
33 #define PredictorState(tif) ((TIFFPredictorState *)(tif)->tif_data)
35 static int horAcc8(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
36 static int horAcc16(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
37 static int horAcc32(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
38 static int horAcc64(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
39 static int swabHorAcc16(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
40 static int swabHorAcc32(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
41 static int swabHorAcc64(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
42 static int horDiff8(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
43 static int horDiff16(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
44 static int horDiff32(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
45 static int horDiff64(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
46 static int swabHorDiff16(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
47 static int swabHorDiff32(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
48 static int swabHorDiff64(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
49 static int fpAcc(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
50 static int fpDiff(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
);
51 static int PredictorDecodeRow(TIFF
*tif
, uint8_t *op0
, tmsize_t occ0
,
53 static int PredictorDecodeTile(TIFF
*tif
, uint8_t *op0
, tmsize_t occ0
,
55 static int PredictorEncodeRow(TIFF
*tif
, uint8_t *bp
, tmsize_t cc
, uint16_t s
);
56 static int PredictorEncodeTile(TIFF
*tif
, uint8_t *bp0
, tmsize_t cc0
,
59 static int PredictorSetup(TIFF
*tif
)
61 static const char module
[] = "PredictorSetup";
63 TIFFPredictorState
*sp
= PredictorState(tif
);
64 TIFFDirectory
*td
= &tif
->tif_dir
;
66 switch (sp
->predictor
) /* no differencing */
70 case PREDICTOR_HORIZONTAL
:
71 if (td
->td_bitspersample
!= 8 && td
->td_bitspersample
!= 16 &&
72 td
->td_bitspersample
!= 32 && td
->td_bitspersample
!= 64)
74 TIFFErrorExtR(tif
, module
,
75 "Horizontal differencing \"Predictor\" not "
76 "supported with %" PRIu16
"-bit samples",
77 td
->td_bitspersample
);
81 case PREDICTOR_FLOATINGPOINT
:
82 if (td
->td_sampleformat
!= SAMPLEFORMAT_IEEEFP
)
86 "Floating point \"Predictor\" not supported with %" PRIu16
91 if (td
->td_bitspersample
!= 16 && td
->td_bitspersample
!= 24 &&
92 td
->td_bitspersample
!= 32 && td
->td_bitspersample
!= 64)
93 { /* Should 64 be allowed? */
96 "Floating point \"Predictor\" not supported with %" PRIu16
98 td
->td_bitspersample
);
103 TIFFErrorExtR(tif
, module
, "\"Predictor\" value %d not supported",
108 (td
->td_planarconfig
== PLANARCONFIG_CONTIG
? td
->td_samplesperpixel
111 * Calculate the scanline/tile-width size in bytes.
114 sp
->rowsize
= TIFFTileRowSize(tif
);
116 sp
->rowsize
= TIFFScanlineSize(tif
);
117 if (sp
->rowsize
== 0)
123 static int PredictorSetupDecode(TIFF
*tif
)
125 TIFFPredictorState
*sp
= PredictorState(tif
);
126 TIFFDirectory
*td
= &tif
->tif_dir
;
128 /* Note: when PredictorSetup() fails, the effets of setupdecode() */
129 /* will not be "canceled" so setupdecode() might be robust to */
130 /* be called several times. */
131 if (!(*sp
->setupdecode
)(tif
) || !PredictorSetup(tif
))
134 if (sp
->predictor
== 2)
136 switch (td
->td_bitspersample
)
139 sp
->decodepfunc
= horAcc8
;
142 sp
->decodepfunc
= horAcc16
;
145 sp
->decodepfunc
= horAcc32
;
148 sp
->decodepfunc
= horAcc64
;
152 * Override default decoding method with one that does the
155 if (tif
->tif_decoderow
!= PredictorDecodeRow
)
157 sp
->decoderow
= tif
->tif_decoderow
;
158 tif
->tif_decoderow
= PredictorDecodeRow
;
159 sp
->decodestrip
= tif
->tif_decodestrip
;
160 tif
->tif_decodestrip
= PredictorDecodeTile
;
161 sp
->decodetile
= tif
->tif_decodetile
;
162 tif
->tif_decodetile
= PredictorDecodeTile
;
166 * If the data is horizontally differenced 16-bit data that
167 * requires byte-swapping, then it must be byte swapped before
168 * the accumulation step. We do this with a special-purpose
169 * routine and override the normal post decoding logic that
170 * the library setup when the directory was read.
172 if (tif
->tif_flags
& TIFF_SWAB
)
174 if (sp
->decodepfunc
== horAcc16
)
176 sp
->decodepfunc
= swabHorAcc16
;
177 tif
->tif_postdecode
= _TIFFNoPostDecode
;
179 else if (sp
->decodepfunc
== horAcc32
)
181 sp
->decodepfunc
= swabHorAcc32
;
182 tif
->tif_postdecode
= _TIFFNoPostDecode
;
184 else if (sp
->decodepfunc
== horAcc64
)
186 sp
->decodepfunc
= swabHorAcc64
;
187 tif
->tif_postdecode
= _TIFFNoPostDecode
;
192 else if (sp
->predictor
== 3)
194 sp
->decodepfunc
= fpAcc
;
196 * Override default decoding method with one that does the
199 if (tif
->tif_decoderow
!= PredictorDecodeRow
)
201 sp
->decoderow
= tif
->tif_decoderow
;
202 tif
->tif_decoderow
= PredictorDecodeRow
;
203 sp
->decodestrip
= tif
->tif_decodestrip
;
204 tif
->tif_decodestrip
= PredictorDecodeTile
;
205 sp
->decodetile
= tif
->tif_decodetile
;
206 tif
->tif_decodetile
= PredictorDecodeTile
;
209 * The data should not be swapped outside of the floating
210 * point predictor, the accumulation routine should return
211 * byres in the native order.
213 if (tif
->tif_flags
& TIFF_SWAB
)
215 tif
->tif_postdecode
= _TIFFNoPostDecode
;
218 * Allocate buffer to keep the decoded bytes before
219 * rearranging in the right order
226 static int PredictorSetupEncode(TIFF
*tif
)
228 TIFFPredictorState
*sp
= PredictorState(tif
);
229 TIFFDirectory
*td
= &tif
->tif_dir
;
231 if (!(*sp
->setupencode
)(tif
) || !PredictorSetup(tif
))
234 if (sp
->predictor
== 2)
236 switch (td
->td_bitspersample
)
239 sp
->encodepfunc
= horDiff8
;
242 sp
->encodepfunc
= horDiff16
;
245 sp
->encodepfunc
= horDiff32
;
248 sp
->encodepfunc
= horDiff64
;
252 * Override default encoding method with one that does the
255 if (tif
->tif_encoderow
!= PredictorEncodeRow
)
257 sp
->encoderow
= tif
->tif_encoderow
;
258 tif
->tif_encoderow
= PredictorEncodeRow
;
259 sp
->encodestrip
= tif
->tif_encodestrip
;
260 tif
->tif_encodestrip
= PredictorEncodeTile
;
261 sp
->encodetile
= tif
->tif_encodetile
;
262 tif
->tif_encodetile
= PredictorEncodeTile
;
266 * If the data is horizontally differenced 16-bit data that
267 * requires byte-swapping, then it must be byte swapped after
268 * the differentiation step. We do this with a special-purpose
269 * routine and override the normal post decoding logic that
270 * the library setup when the directory was read.
272 if (tif
->tif_flags
& TIFF_SWAB
)
274 if (sp
->encodepfunc
== horDiff16
)
276 sp
->encodepfunc
= swabHorDiff16
;
277 tif
->tif_postdecode
= _TIFFNoPostDecode
;
279 else if (sp
->encodepfunc
== horDiff32
)
281 sp
->encodepfunc
= swabHorDiff32
;
282 tif
->tif_postdecode
= _TIFFNoPostDecode
;
284 else if (sp
->encodepfunc
== horDiff64
)
286 sp
->encodepfunc
= swabHorDiff64
;
287 tif
->tif_postdecode
= _TIFFNoPostDecode
;
292 else if (sp
->predictor
== 3)
294 sp
->encodepfunc
= fpDiff
;
296 * Override default encoding method with one that does the
299 if (tif
->tif_encoderow
!= PredictorEncodeRow
)
301 sp
->encoderow
= tif
->tif_encoderow
;
302 tif
->tif_encoderow
= PredictorEncodeRow
;
303 sp
->encodestrip
= tif
->tif_encodestrip
;
304 tif
->tif_encodestrip
= PredictorEncodeTile
;
305 sp
->encodetile
= tif
->tif_encodetile
;
306 tif
->tif_encodetile
= PredictorEncodeTile
;
313 #define REPEAT4(n, op) \
319 for (i = n - 4; i > 0; i--) \
325 op; /*-fallthrough*/ \
327 op; /*-fallthrough*/ \
329 op; /*-fallthrough*/ \
331 op; /*-fallthrough*/ \
335 /* Remarks related to C standard compliance in all below functions : */
336 /* - to avoid any undefined behavior, we only operate on unsigned types */
337 /* since the behavior of "overflows" is defined (wrap over) */
338 /* - when storing into the byte stream, we explicitly mask with 0xff so */
339 /* as to make icc -check=conversions happy (not necessary by the standard) */
341 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
342 static int horAcc8(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
344 tmsize_t stride
= PredictorState(tif
)->stride
;
346 unsigned char *cp
= (unsigned char *)cp0
;
347 if ((cc
% stride
) != 0)
349 TIFFErrorExtR(tif
, "horAcc8", "%s", "(cc%stride)!=0");
356 * Pipeline the most common cases.
360 unsigned int cr
= cp
[0];
361 unsigned int cg
= cp
[1];
362 unsigned int cb
= cp
[2];
364 for (; i
< cc
; i
+= stride
)
366 cp
[i
+ 0] = (unsigned char)((cr
+= cp
[i
+ 0]) & 0xff);
367 cp
[i
+ 1] = (unsigned char)((cg
+= cp
[i
+ 1]) & 0xff);
368 cp
[i
+ 2] = (unsigned char)((cb
+= cp
[i
+ 2]) & 0xff);
371 else if (stride
== 4)
373 unsigned int cr
= cp
[0];
374 unsigned int cg
= cp
[1];
375 unsigned int cb
= cp
[2];
376 unsigned int ca
= cp
[3];
378 for (; i
< cc
; i
+= stride
)
380 cp
[i
+ 0] = (unsigned char)((cr
+= cp
[i
+ 0]) & 0xff);
381 cp
[i
+ 1] = (unsigned char)((cg
+= cp
[i
+ 1]) & 0xff);
382 cp
[i
+ 2] = (unsigned char)((cb
+= cp
[i
+ 2]) & 0xff);
383 cp
[i
+ 3] = (unsigned char)((ca
+= cp
[i
+ 3]) & 0xff);
392 cp
[stride
] = (unsigned char)((cp
[stride
] + *cp
) & 0xff);
401 static int swabHorAcc16(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
403 uint16_t *wp
= (uint16_t *)cp0
;
404 tmsize_t wc
= cc
/ 2;
406 TIFFSwabArrayOfShort(wp
, wc
);
407 return horAcc16(tif
, cp0
, cc
);
410 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
411 static int horAcc16(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
413 tmsize_t stride
= PredictorState(tif
)->stride
;
414 uint16_t *wp
= (uint16_t *)cp0
;
415 tmsize_t wc
= cc
/ 2;
417 if ((cc
% (2 * stride
)) != 0)
419 TIFFErrorExtR(tif
, "horAcc16", "%s", "cc%(2*stride))!=0");
428 REPEAT4(stride
, wp
[stride
] = (uint16_t)(((unsigned int)wp
[stride
] +
429 (unsigned int)wp
[0]) &
438 static int swabHorAcc32(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
440 uint32_t *wp
= (uint32_t *)cp0
;
441 tmsize_t wc
= cc
/ 4;
443 TIFFSwabArrayOfLong(wp
, wc
);
444 return horAcc32(tif
, cp0
, cc
);
447 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
448 static int horAcc32(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
450 tmsize_t stride
= PredictorState(tif
)->stride
;
451 uint32_t *wp
= (uint32_t *)cp0
;
452 tmsize_t wc
= cc
/ 4;
454 if ((cc
% (4 * stride
)) != 0)
456 TIFFErrorExtR(tif
, "horAcc32", "%s", "cc%(4*stride))!=0");
465 REPEAT4(stride
, wp
[stride
] += wp
[0]; wp
++)
472 static int swabHorAcc64(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
474 uint64_t *wp
= (uint64_t *)cp0
;
475 tmsize_t wc
= cc
/ 8;
477 TIFFSwabArrayOfLong8(wp
, wc
);
478 return horAcc64(tif
, cp0
, cc
);
481 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
482 static int horAcc64(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
484 tmsize_t stride
= PredictorState(tif
)->stride
;
485 uint64_t *wp
= (uint64_t *)cp0
;
486 tmsize_t wc
= cc
/ 8;
488 if ((cc
% (8 * stride
)) != 0)
490 TIFFErrorExtR(tif
, "horAcc64", "%s", "cc%(8*stride))!=0");
499 REPEAT4(stride
, wp
[stride
] += wp
[0]; wp
++)
507 * Floating point predictor accumulation routine.
509 static int fpAcc(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
511 tmsize_t stride
= PredictorState(tif
)->stride
;
512 uint32_t bps
= tif
->tif_dir
.td_bitspersample
/ 8;
513 tmsize_t wc
= cc
/ bps
;
515 uint8_t *cp
= (uint8_t *)cp0
;
518 if (cc
% (bps
* stride
) != 0)
520 TIFFErrorExtR(tif
, "fpAcc", "%s", "cc%(bps*stride))!=0");
524 tmp
= (uint8_t *)_TIFFmallocExt(tif
, cc
);
528 while (count
> stride
)
531 cp
[stride
] = (unsigned char)((cp
[stride
] + cp
[0]) & 0xff);
536 _TIFFmemcpy(tmp
, cp0
, cc
);
538 for (count
= 0; count
< wc
; count
++)
541 for (byte
= 0; byte
< bps
; byte
++)
544 cp
[bps
* count
+ byte
] = tmp
[byte
* wc
+ count
];
546 cp
[bps
* count
+ byte
] = tmp
[(bps
- byte
- 1) * wc
+ count
];
550 _TIFFfreeExt(tif
, tmp
);
555 * Decode a scanline and apply the predictor routine.
557 static int PredictorDecodeRow(TIFF
*tif
, uint8_t *op0
, tmsize_t occ0
,
560 TIFFPredictorState
*sp
= PredictorState(tif
);
563 assert(sp
->decoderow
!= NULL
);
564 assert(sp
->decodepfunc
!= NULL
);
566 if ((*sp
->decoderow
)(tif
, op0
, occ0
, s
))
568 return (*sp
->decodepfunc
)(tif
, op0
, occ0
);
575 * Decode a tile/strip and apply the predictor routine.
576 * Note that horizontal differencing must be done on a
577 * row-by-row basis. The width of a "row" has already
578 * been calculated at pre-decode time according to the
579 * strip/tile dimensions.
581 static int PredictorDecodeTile(TIFF
*tif
, uint8_t *op0
, tmsize_t occ0
,
584 TIFFPredictorState
*sp
= PredictorState(tif
);
587 assert(sp
->decodetile
!= NULL
);
589 if ((*sp
->decodetile
)(tif
, op0
, occ0
, s
))
591 tmsize_t rowsize
= sp
->rowsize
;
593 if ((occ0
% rowsize
) != 0)
595 TIFFErrorExtR(tif
, "PredictorDecodeTile", "%s",
596 "occ0%rowsize != 0");
599 assert(sp
->decodepfunc
!= NULL
);
602 if (!(*sp
->decodepfunc
)(tif
, op0
, rowsize
))
613 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
614 static int horDiff8(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
616 TIFFPredictorState
*sp
= PredictorState(tif
);
617 tmsize_t stride
= sp
->stride
;
618 unsigned char *cp
= (unsigned char *)cp0
;
620 if ((cc
% stride
) != 0)
622 TIFFErrorExtR(tif
, "horDiff8", "%s", "(cc%stride)!=0");
630 * Pipeline the most common cases.
634 unsigned int r1
, g1
, b1
;
635 unsigned int r2
= cp
[0];
636 unsigned int g2
= cp
[1];
637 unsigned int b2
= cp
[2];
641 cp
[3] = (unsigned char)((r1
- r2
) & 0xff);
644 cp
[4] = (unsigned char)((g1
- g2
) & 0xff);
647 cp
[5] = (unsigned char)((b1
- b2
) & 0xff);
650 } while ((cc
-= 3) > 0);
652 else if (stride
== 4)
654 unsigned int r1
, g1
, b1
, a1
;
655 unsigned int r2
= cp
[0];
656 unsigned int g2
= cp
[1];
657 unsigned int b2
= cp
[2];
658 unsigned int a2
= cp
[3];
662 cp
[4] = (unsigned char)((r1
- r2
) & 0xff);
665 cp
[5] = (unsigned char)((g1
- g2
) & 0xff);
668 cp
[6] = (unsigned char)((b1
- b2
) & 0xff);
671 cp
[7] = (unsigned char)((a1
- a2
) & 0xff);
674 } while ((cc
-= 4) > 0);
683 (unsigned char)((cp
[stride
] - cp
[0]) & 0xff);
685 } while ((cc
-= stride
) > 0);
691 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
692 static int horDiff16(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
694 TIFFPredictorState
*sp
= PredictorState(tif
);
695 tmsize_t stride
= sp
->stride
;
696 uint16_t *wp
= (uint16_t *)cp0
;
697 tmsize_t wc
= cc
/ 2;
699 if ((cc
% (2 * stride
)) != 0)
701 TIFFErrorExtR(tif
, "horDiff8", "%s", "(cc%(2*stride))!=0");
711 REPEAT4(stride
, wp
[stride
] = (uint16_t)(((unsigned int)wp
[stride
] -
712 (unsigned int)wp
[0]) &
721 static int swabHorDiff16(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
723 uint16_t *wp
= (uint16_t *)cp0
;
724 tmsize_t wc
= cc
/ 2;
726 if (!horDiff16(tif
, cp0
, cc
))
729 TIFFSwabArrayOfShort(wp
, wc
);
733 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
734 static int horDiff32(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
736 TIFFPredictorState
*sp
= PredictorState(tif
);
737 tmsize_t stride
= sp
->stride
;
738 uint32_t *wp
= (uint32_t *)cp0
;
739 tmsize_t wc
= cc
/ 4;
741 if ((cc
% (4 * stride
)) != 0)
743 TIFFErrorExtR(tif
, "horDiff32", "%s", "(cc%(4*stride))!=0");
753 REPEAT4(stride
, wp
[stride
] -= wp
[0]; wp
--)
760 static int swabHorDiff32(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
762 uint32_t *wp
= (uint32_t *)cp0
;
763 tmsize_t wc
= cc
/ 4;
765 if (!horDiff32(tif
, cp0
, cc
))
768 TIFFSwabArrayOfLong(wp
, wc
);
772 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
773 static int horDiff64(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
775 TIFFPredictorState
*sp
= PredictorState(tif
);
776 tmsize_t stride
= sp
->stride
;
777 uint64_t *wp
= (uint64_t *)cp0
;
778 tmsize_t wc
= cc
/ 8;
780 if ((cc
% (8 * stride
)) != 0)
782 TIFFErrorExtR(tif
, "horDiff64", "%s", "(cc%(8*stride))!=0");
792 REPEAT4(stride
, wp
[stride
] -= wp
[0]; wp
--)
799 static int swabHorDiff64(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
801 uint64_t *wp
= (uint64_t *)cp0
;
802 tmsize_t wc
= cc
/ 8;
804 if (!horDiff64(tif
, cp0
, cc
))
807 TIFFSwabArrayOfLong8(wp
, wc
);
812 * Floating point predictor differencing routine.
814 TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
815 static int fpDiff(TIFF
*tif
, uint8_t *cp0
, tmsize_t cc
)
817 tmsize_t stride
= PredictorState(tif
)->stride
;
818 uint32_t bps
= tif
->tif_dir
.td_bitspersample
/ 8;
819 tmsize_t wc
= cc
/ bps
;
821 uint8_t *cp
= (uint8_t *)cp0
;
824 if ((cc
% (bps
* stride
)) != 0)
826 TIFFErrorExtR(tif
, "fpDiff", "%s", "(cc%(bps*stride))!=0");
830 tmp
= (uint8_t *)_TIFFmallocExt(tif
, cc
);
834 _TIFFmemcpy(tmp
, cp0
, cc
);
835 for (count
= 0; count
< wc
; count
++)
838 for (byte
= 0; byte
< bps
; byte
++)
841 cp
[byte
* wc
+ count
] = tmp
[bps
* count
+ byte
];
843 cp
[(bps
- byte
- 1) * wc
+ count
] = tmp
[bps
* count
+ byte
];
847 _TIFFfreeExt(tif
, tmp
);
850 cp
+= cc
- stride
- 1;
851 for (count
= cc
; count
> stride
; count
-= stride
)
853 cp
[stride
] = (unsigned char)((cp
[stride
] - cp
[0]) & 0xff);
858 static int PredictorEncodeRow(TIFF
*tif
, uint8_t *bp
, tmsize_t cc
, uint16_t s
)
860 TIFFPredictorState
*sp
= PredictorState(tif
);
863 assert(sp
->encodepfunc
!= NULL
);
864 assert(sp
->encoderow
!= NULL
);
866 /* XXX horizontal differencing alters user's data XXX */
867 if (!(*sp
->encodepfunc
)(tif
, bp
, cc
))
869 return (*sp
->encoderow
)(tif
, bp
, cc
, s
);
872 static int PredictorEncodeTile(TIFF
*tif
, uint8_t *bp0
, tmsize_t cc0
,
875 static const char module
[] = "PredictorEncodeTile";
876 TIFFPredictorState
*sp
= PredictorState(tif
);
877 uint8_t *working_copy
;
878 tmsize_t cc
= cc0
, rowsize
;
883 assert(sp
->encodepfunc
!= NULL
);
884 assert(sp
->encodetile
!= NULL
);
887 * Do predictor manipulation in a working buffer to avoid altering
888 * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
890 working_copy
= (uint8_t *)_TIFFmallocExt(tif
, cc0
);
891 if (working_copy
== NULL
)
893 TIFFErrorExtR(tif
, module
,
894 "Out of memory allocating %" PRId64
" byte temp buffer.",
898 memcpy(working_copy
, bp0
, cc0
);
901 rowsize
= sp
->rowsize
;
903 if ((cc0
% rowsize
) != 0)
905 TIFFErrorExtR(tif
, "PredictorEncodeTile", "%s", "(cc0%rowsize)!=0");
906 _TIFFfreeExt(tif
, working_copy
);
911 (*sp
->encodepfunc
)(tif
, bp
, rowsize
);
915 result_code
= (*sp
->encodetile
)(tif
, working_copy
, cc0
, s
);
917 _TIFFfreeExt(tif
, working_copy
);
922 #define FIELD_PREDICTOR (FIELD_CODEC + 0) /* XXX */
924 static const TIFFField predictFields
[] = {
925 {TIFFTAG_PREDICTOR
, 1, 1, TIFF_SHORT
, 0, TIFF_SETGET_UINT16
,
926 TIFF_SETGET_UINT16
, FIELD_PREDICTOR
, FALSE
, FALSE
, "Predictor", NULL
},
929 static int PredictorVSetField(TIFF
*tif
, uint32_t tag
, va_list ap
)
931 TIFFPredictorState
*sp
= PredictorState(tif
);
934 assert(sp
->vsetparent
!= NULL
);
938 case TIFFTAG_PREDICTOR
:
939 sp
->predictor
= (uint16_t)va_arg(ap
, uint16_vap
);
940 TIFFSetFieldBit(tif
, FIELD_PREDICTOR
);
943 return (*sp
->vsetparent
)(tif
, tag
, ap
);
945 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
949 static int PredictorVGetField(TIFF
*tif
, uint32_t tag
, va_list ap
)
951 TIFFPredictorState
*sp
= PredictorState(tif
);
954 assert(sp
->vgetparent
!= NULL
);
958 case TIFFTAG_PREDICTOR
:
959 *va_arg(ap
, uint16_t *) = (uint16_t)sp
->predictor
;
962 return (*sp
->vgetparent
)(tif
, tag
, ap
);
967 static void PredictorPrintDir(TIFF
*tif
, FILE *fd
, long flags
)
969 TIFFPredictorState
*sp
= PredictorState(tif
);
972 if (TIFFFieldSet(tif
, FIELD_PREDICTOR
))
974 fprintf(fd
, " Predictor: ");
975 switch (sp
->predictor
)
978 fprintf(fd
, "none ");
981 fprintf(fd
, "horizontal differencing ");
984 fprintf(fd
, "floating point predictor ");
987 fprintf(fd
, "%d (0x%x)\n", sp
->predictor
, sp
->predictor
);
990 (*sp
->printdir
)(tif
, fd
, flags
);
993 int TIFFPredictorInit(TIFF
*tif
)
995 TIFFPredictorState
*sp
= PredictorState(tif
);
1000 * Merge codec-specific tag information.
1002 if (!_TIFFMergeFields(tif
, predictFields
, TIFFArrayCount(predictFields
)))
1004 TIFFErrorExtR(tif
, "TIFFPredictorInit",
1005 "Merging Predictor codec-specific tags failed");
1010 * Override parent get/set field methods.
1012 sp
->vgetparent
= tif
->tif_tagmethods
.vgetfield
;
1013 tif
->tif_tagmethods
.vgetfield
=
1014 PredictorVGetField
; /* hook for predictor tag */
1015 sp
->vsetparent
= tif
->tif_tagmethods
.vsetfield
;
1016 tif
->tif_tagmethods
.vsetfield
=
1017 PredictorVSetField
; /* hook for predictor tag */
1018 sp
->printdir
= tif
->tif_tagmethods
.printdir
;
1019 tif
->tif_tagmethods
.printdir
=
1020 PredictorPrintDir
; /* hook for predictor tag */
1022 sp
->setupdecode
= tif
->tif_setupdecode
;
1023 tif
->tif_setupdecode
= PredictorSetupDecode
;
1024 sp
->setupencode
= tif
->tif_setupencode
;
1025 tif
->tif_setupencode
= PredictorSetupEncode
;
1027 sp
->predictor
= 1; /* default value */
1028 sp
->encodepfunc
= NULL
; /* no predictor routine */
1029 sp
->decodepfunc
= NULL
; /* no predictor routine */
1033 int TIFFPredictorCleanup(TIFF
*tif
)
1035 TIFFPredictorState
*sp
= PredictorState(tif
);
1039 tif
->tif_tagmethods
.vgetfield
= sp
->vgetparent
;
1040 tif
->tif_tagmethods
.vsetfield
= sp
->vsetparent
;
1041 tif
->tif_tagmethods
.printdir
= sp
->printdir
;
1042 tif
->tif_setupdecode
= sp
->setupdecode
;
1043 tif
->tif_setupencode
= sp
->setupencode
;