[PROPSYS]
[reactos.git] / reactos / dll / 3rdparty / libtiff / tif_predict.c
1 /* $Id: tif_predict.c,v 1.32 2010-03-10 18:56:49 bfriesen 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 /*
28 * TIFF Library.
29 *
30 * Predictor Tag Support (used by multiple codecs).
31 */
32
33 #include <precomp.h>
34 #include "tif_predict.h"
35
36 #define PredictorState(tif) ((TIFFPredictorState*) (tif)->tif_data)
37
38 static void horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc);
39 static void horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
40 static void horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
41 static void swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
42 static void swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
43 static void horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc);
44 static void horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc);
45 static void horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc);
46 static void fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc);
47 static void fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc);
48 static int PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
49 static int PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
50 static int PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
51 static int PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s);
52
53 static int
54 PredictorSetup(TIFF* tif)
55 {
56 static const char module[] = "PredictorSetup";
57
58 TIFFPredictorState* sp = PredictorState(tif);
59 TIFFDirectory* td = &tif->tif_dir;
60
61 switch (sp->predictor) /* no differencing */
62 {
63 case PREDICTOR_NONE:
64 return 1;
65 case PREDICTOR_HORIZONTAL:
66 if (td->td_bitspersample != 8
67 && td->td_bitspersample != 16
68 && td->td_bitspersample != 32) {
69 TIFFErrorExt(tif->tif_clientdata, module,
70 "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
71 td->td_bitspersample);
72 return 0;
73 }
74 break;
75 case PREDICTOR_FLOATINGPOINT:
76 if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) {
77 TIFFErrorExt(tif->tif_clientdata, module,
78 "Floating point \"Predictor\" not supported with %d data format",
79 td->td_sampleformat);
80 return 0;
81 }
82 break;
83 default:
84 TIFFErrorExt(tif->tif_clientdata, module,
85 "\"Predictor\" value %d not supported",
86 sp->predictor);
87 return 0;
88 }
89 sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
90 td->td_samplesperpixel : 1);
91 /*
92 * Calculate the scanline/tile-width size in bytes.
93 */
94 if (isTiled(tif))
95 sp->rowsize = TIFFTileRowSize(tif);
96 else
97 sp->rowsize = TIFFScanlineSize(tif);
98 if (sp->rowsize == 0)
99 return 0;
100
101 return 1;
102 }
103
104 static int
105 PredictorSetupDecode(TIFF* tif)
106 {
107 TIFFPredictorState* sp = PredictorState(tif);
108 TIFFDirectory* td = &tif->tif_dir;
109
110 if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
111 return 0;
112
113 if (sp->predictor == 2) {
114 switch (td->td_bitspersample) {
115 case 8: sp->decodepfunc = horAcc8; break;
116 case 16: sp->decodepfunc = horAcc16; break;
117 case 32: sp->decodepfunc = horAcc32; break;
118 }
119 /*
120 * Override default decoding method with one that does the
121 * predictor stuff.
122 */
123 if( tif->tif_decoderow != PredictorDecodeRow )
124 {
125 sp->decoderow = tif->tif_decoderow;
126 tif->tif_decoderow = PredictorDecodeRow;
127 sp->decodestrip = tif->tif_decodestrip;
128 tif->tif_decodestrip = PredictorDecodeTile;
129 sp->decodetile = tif->tif_decodetile;
130 tif->tif_decodetile = PredictorDecodeTile;
131 }
132
133 /*
134 * If the data is horizontally differenced 16-bit data that
135 * requires byte-swapping, then it must be byte swapped before
136 * the accumulation step. We do this with a special-purpose
137 * routine and override the normal post decoding logic that
138 * the library setup when the directory was read.
139 */
140 if (tif->tif_flags & TIFF_SWAB) {
141 if (sp->decodepfunc == horAcc16) {
142 sp->decodepfunc = swabHorAcc16;
143 tif->tif_postdecode = _TIFFNoPostDecode;
144 } else if (sp->decodepfunc == horAcc32) {
145 sp->decodepfunc = swabHorAcc32;
146 tif->tif_postdecode = _TIFFNoPostDecode;
147 }
148 }
149 }
150
151 else if (sp->predictor == 3) {
152 sp->decodepfunc = fpAcc;
153 /*
154 * Override default decoding method with one that does the
155 * predictor stuff.
156 */
157 if( tif->tif_decoderow != PredictorDecodeRow )
158 {
159 sp->decoderow = tif->tif_decoderow;
160 tif->tif_decoderow = PredictorDecodeRow;
161 sp->decodestrip = tif->tif_decodestrip;
162 tif->tif_decodestrip = PredictorDecodeTile;
163 sp->decodetile = tif->tif_decodetile;
164 tif->tif_decodetile = PredictorDecodeTile;
165 }
166 /*
167 * The data should not be swapped outside of the floating
168 * point predictor, the accumulation routine should return
169 * byres in the native order.
170 */
171 if (tif->tif_flags & TIFF_SWAB) {
172 tif->tif_postdecode = _TIFFNoPostDecode;
173 }
174 /*
175 * Allocate buffer to keep the decoded bytes before
176 * rearranging in the ight order
177 */
178 }
179
180 return 1;
181 }
182
183 static int
184 PredictorSetupEncode(TIFF* tif)
185 {
186 TIFFPredictorState* sp = PredictorState(tif);
187 TIFFDirectory* td = &tif->tif_dir;
188
189 if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
190 return 0;
191
192 if (sp->predictor == 2) {
193 switch (td->td_bitspersample) {
194 case 8: sp->encodepfunc = horDiff8; break;
195 case 16: sp->encodepfunc = horDiff16; break;
196 case 32: sp->encodepfunc = horDiff32; break;
197 }
198 /*
199 * Override default encoding method with one that does the
200 * predictor stuff.
201 */
202 if( tif->tif_encoderow != PredictorEncodeRow )
203 {
204 sp->encoderow = tif->tif_encoderow;
205 tif->tif_encoderow = PredictorEncodeRow;
206 sp->encodestrip = tif->tif_encodestrip;
207 tif->tif_encodestrip = PredictorEncodeTile;
208 sp->encodetile = tif->tif_encodetile;
209 tif->tif_encodetile = PredictorEncodeTile;
210 }
211 }
212
213 else if (sp->predictor == 3) {
214 sp->encodepfunc = fpDiff;
215 /*
216 * Override default encoding method with one that does the
217 * predictor stuff.
218 */
219 if( tif->tif_encoderow != PredictorEncodeRow )
220 {
221 sp->encoderow = tif->tif_encoderow;
222 tif->tif_encoderow = PredictorEncodeRow;
223 sp->encodestrip = tif->tif_encodestrip;
224 tif->tif_encodestrip = PredictorEncodeTile;
225 sp->encodetile = tif->tif_encodetile;
226 tif->tif_encodetile = PredictorEncodeTile;
227 }
228 }
229
230 return 1;
231 }
232
233 #define REPEAT4(n, op) \
234 switch (n) { \
235 default: { tmsize_t i; for (i = n-4; i > 0; i--) { op; } } \
236 case 4: op; \
237 case 3: op; \
238 case 2: op; \
239 case 1: op; \
240 case 0: ; \
241 }
242
243 static void
244 horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc)
245 {
246 tmsize_t stride = PredictorState(tif)->stride;
247
248 char* cp = (char*) cp0;
249 assert((cc%stride)==0);
250 if (cc > stride) {
251 /*
252 * Pipeline the most common cases.
253 */
254 if (stride == 3) {
255 unsigned int cr = cp[0];
256 unsigned int cg = cp[1];
257 unsigned int cb = cp[2];
258 cc -= 3;
259 cp += 3;
260 while (cc>0) {
261 cp[0] = (char) (cr += cp[0]);
262 cp[1] = (char) (cg += cp[1]);
263 cp[2] = (char) (cb += cp[2]);
264 cc -= 3;
265 cp += 3;
266 }
267 } else if (stride == 4) {
268 unsigned int cr = cp[0];
269 unsigned int cg = cp[1];
270 unsigned int cb = cp[2];
271 unsigned int ca = cp[3];
272 cc -= 4;
273 cp += 4;
274 while (cc>0) {
275 cp[0] = (char) (cr += cp[0]);
276 cp[1] = (char) (cg += cp[1]);
277 cp[2] = (char) (cb += cp[2]);
278 cp[3] = (char) (ca += cp[3]);
279 cc -= 4;
280 cp += 4;
281 }
282 } else {
283 cc -= stride;
284 do {
285 REPEAT4(stride, cp[stride] =
286 (char) (cp[stride] + *cp); cp++)
287 cc -= stride;
288 } while (cc>0);
289 }
290 }
291 }
292
293 static void
294 swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
295 {
296 tmsize_t stride = PredictorState(tif)->stride;
297 uint16* wp = (uint16*) cp0;
298 tmsize_t wc = cc / 2;
299
300 assert((cc%(2*stride))==0);
301
302 if (wc > stride) {
303 TIFFSwabArrayOfShort(wp, wc);
304 wc -= stride;
305 do {
306 REPEAT4(stride, wp[stride] += wp[0]; wp++)
307 wc -= stride;
308 } while (wc > 0);
309 }
310 }
311
312 static void
313 horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
314 {
315 tmsize_t stride = PredictorState(tif)->stride;
316 uint16* wp = (uint16*) cp0;
317 tmsize_t wc = cc / 2;
318
319 assert((cc%(2*stride))==0);
320
321 if (wc > stride) {
322 wc -= stride;
323 do {
324 REPEAT4(stride, wp[stride] += wp[0]; wp++)
325 wc -= stride;
326 } while (wc > 0);
327 }
328 }
329
330 static void
331 swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
332 {
333 tmsize_t stride = PredictorState(tif)->stride;
334 uint32* wp = (uint32*) cp0;
335 tmsize_t wc = cc / 4;
336
337 assert((cc%(4*stride))==0);
338
339 if (wc > stride) {
340 TIFFSwabArrayOfLong(wp, wc);
341 wc -= stride;
342 do {
343 REPEAT4(stride, wp[stride] += wp[0]; wp++)
344 wc -= stride;
345 } while (wc > 0);
346 }
347 }
348
349 static void
350 horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
351 {
352 tmsize_t stride = PredictorState(tif)->stride;
353 uint32* wp = (uint32*) cp0;
354 tmsize_t wc = cc / 4;
355
356 assert((cc%(4*stride))==0);
357
358 if (wc > stride) {
359 wc -= stride;
360 do {
361 REPEAT4(stride, wp[stride] += wp[0]; wp++)
362 wc -= stride;
363 } while (wc > 0);
364 }
365 }
366
367 /*
368 * Floating point predictor accumulation routine.
369 */
370 static void
371 fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc)
372 {
373 tmsize_t stride = PredictorState(tif)->stride;
374 uint32 bps = tif->tif_dir.td_bitspersample / 8;
375 tmsize_t wc = cc / bps;
376 tmsize_t count = cc;
377 uint8 *cp = (uint8 *) cp0;
378 uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
379
380 assert((cc%(bps*stride))==0);
381
382 if (!tmp)
383 return;
384
385 while (count > stride) {
386 REPEAT4(stride, cp[stride] += cp[0]; cp++)
387 count -= stride;
388 }
389
390 _TIFFmemcpy(tmp, cp0, cc);
391 cp = (uint8 *) cp0;
392 for (count = 0; count < wc; count++) {
393 uint32 byte;
394 for (byte = 0; byte < bps; byte++) {
395 #if WORDS_BIGENDIAN
396 cp[bps * count + byte] = tmp[byte * wc + count];
397 #else
398 cp[bps * count + byte] =
399 tmp[(bps - byte - 1) * wc + count];
400 #endif
401 }
402 }
403 _TIFFfree(tmp);
404 }
405
406 /*
407 * Decode a scanline and apply the predictor routine.
408 */
409 static int
410 PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
411 {
412 TIFFPredictorState *sp = PredictorState(tif);
413
414 assert(sp != NULL);
415 assert(sp->decoderow != NULL);
416 assert(sp->decodepfunc != NULL);
417
418 if ((*sp->decoderow)(tif, op0, occ0, s)) {
419 (*sp->decodepfunc)(tif, op0, occ0);
420 return 1;
421 } else
422 return 0;
423 }
424
425 /*
426 * Decode a tile/strip and apply the predictor routine.
427 * Note that horizontal differencing must be done on a
428 * row-by-row basis. The width of a "row" has already
429 * been calculated at pre-decode time according to the
430 * strip/tile dimensions.
431 */
432 static int
433 PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
434 {
435 TIFFPredictorState *sp = PredictorState(tif);
436
437 assert(sp != NULL);
438 assert(sp->decodetile != NULL);
439
440 if ((*sp->decodetile)(tif, op0, occ0, s)) {
441 tmsize_t rowsize = sp->rowsize;
442 assert(rowsize > 0);
443 assert((occ0%rowsize)==0);
444 assert(sp->decodepfunc != NULL);
445 while (occ0 > 0) {
446 (*sp->decodepfunc)(tif, op0, rowsize);
447 occ0 -= rowsize;
448 op0 += rowsize;
449 }
450 return 1;
451 } else
452 return 0;
453 }
454
455 static void
456 horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc)
457 {
458 TIFFPredictorState* sp = PredictorState(tif);
459 tmsize_t stride = sp->stride;
460 char* cp = (char*) cp0;
461
462 assert((cc%stride)==0);
463
464 if (cc > stride) {
465 cc -= stride;
466 /*
467 * Pipeline the most common cases.
468 */
469 if (stride == 3) {
470 int r1, g1, b1;
471 int r2 = cp[0];
472 int g2 = cp[1];
473 int b2 = cp[2];
474 do {
475 r1 = cp[3]; cp[3] = r1-r2; r2 = r1;
476 g1 = cp[4]; cp[4] = g1-g2; g2 = g1;
477 b1 = cp[5]; cp[5] = b1-b2; b2 = b1;
478 cp += 3;
479 } while ((cc -= 3) > 0);
480 } else if (stride == 4) {
481 int r1, g1, b1, a1;
482 int r2 = cp[0];
483 int g2 = cp[1];
484 int b2 = cp[2];
485 int a2 = cp[3];
486 do {
487 r1 = cp[4]; cp[4] = r1-r2; r2 = r1;
488 g1 = cp[5]; cp[5] = g1-g2; g2 = g1;
489 b1 = cp[6]; cp[6] = b1-b2; b2 = b1;
490 a1 = cp[7]; cp[7] = a1-a2; a2 = a1;
491 cp += 4;
492 } while ((cc -= 4) > 0);
493 } else {
494 cp += cc - 1;
495 do {
496 REPEAT4(stride, cp[stride] -= cp[0]; cp--)
497 } while ((cc -= stride) > 0);
498 }
499 }
500 }
501
502 static void
503 horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc)
504 {
505 TIFFPredictorState* sp = PredictorState(tif);
506 tmsize_t stride = sp->stride;
507 int16 *wp = (int16*) cp0;
508 tmsize_t wc = cc/2;
509
510 assert((cc%(2*stride))==0);
511
512 if (wc > stride) {
513 wc -= stride;
514 wp += wc - 1;
515 do {
516 REPEAT4(stride, wp[stride] -= wp[0]; wp--)
517 wc -= stride;
518 } while (wc > 0);
519 }
520 }
521
522 static void
523 horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc)
524 {
525 TIFFPredictorState* sp = PredictorState(tif);
526 tmsize_t stride = sp->stride;
527 int32 *wp = (int32*) cp0;
528 tmsize_t wc = cc/4;
529
530 assert((cc%(4*stride))==0);
531
532 if (wc > stride) {
533 wc -= stride;
534 wp += wc - 1;
535 do {
536 REPEAT4(stride, wp[stride] -= wp[0]; wp--)
537 wc -= stride;
538 } while (wc > 0);
539 }
540 }
541
542 /*
543 * Floating point predictor differencing routine.
544 */
545 static void
546 fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc)
547 {
548 tmsize_t stride = PredictorState(tif)->stride;
549 uint32 bps = tif->tif_dir.td_bitspersample / 8;
550 tmsize_t wc = cc / bps;
551 tmsize_t count;
552 uint8 *cp = (uint8 *) cp0;
553 uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
554
555 assert((cc%(bps*stride))==0);
556
557 if (!tmp)
558 return;
559
560 _TIFFmemcpy(tmp, cp0, cc);
561 for (count = 0; count < wc; count++) {
562 uint32 byte;
563 for (byte = 0; byte < bps; byte++) {
564 #if WORDS_BIGENDIAN
565 cp[byte * wc + count] = tmp[bps * count + byte];
566 #else
567 cp[(bps - byte - 1) * wc + count] =
568 tmp[bps * count + byte];
569 #endif
570 }
571 }
572 _TIFFfree(tmp);
573
574 cp = (uint8 *) cp0;
575 cp += cc - stride - 1;
576 for (count = cc; count > stride; count -= stride)
577 REPEAT4(stride, cp[stride] -= cp[0]; cp--)
578 }
579
580 static int
581 PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
582 {
583 TIFFPredictorState *sp = PredictorState(tif);
584
585 assert(sp != NULL);
586 assert(sp->encodepfunc != NULL);
587 assert(sp->encoderow != NULL);
588
589 /* XXX horizontal differencing alters user's data XXX */
590 (*sp->encodepfunc)(tif, bp, cc);
591 return (*sp->encoderow)(tif, bp, cc, s);
592 }
593
594 static int
595 PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s)
596 {
597 static const char module[] = "PredictorEncodeTile";
598 TIFFPredictorState *sp = PredictorState(tif);
599 uint8 *working_copy;
600 tmsize_t cc = cc0, rowsize;
601 unsigned char* bp;
602 int result_code;
603
604 assert(sp != NULL);
605 assert(sp->encodepfunc != NULL);
606 assert(sp->encodetile != NULL);
607
608 /*
609 * Do predictor manipulation in a working buffer to avoid altering
610 * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
611 */
612 working_copy = (uint8*) _TIFFmalloc(cc0);
613 if( working_copy == NULL )
614 {
615 TIFFErrorExt(tif->tif_clientdata, module,
616 "Out of memory allocating " TIFF_SSIZE_FORMAT " byte temp buffer.",
617 cc0 );
618 return 0;
619 }
620 memcpy( working_copy, bp0, cc0 );
621 bp = working_copy;
622
623 rowsize = sp->rowsize;
624 assert(rowsize > 0);
625 assert((cc0%rowsize)==0);
626 while (cc > 0) {
627 (*sp->encodepfunc)(tif, bp, rowsize);
628 cc -= rowsize;
629 bp += rowsize;
630 }
631 result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
632
633 _TIFFfree( working_copy );
634
635 return result_code;
636 }
637
638 #define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */
639
640 static const TIFFField predictFields[] = {
641 { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL },
642 };
643
644 static int
645 PredictorVSetField(TIFF* tif, uint32 tag, va_list ap)
646 {
647 TIFFPredictorState *sp = PredictorState(tif);
648
649 assert(sp != NULL);
650 assert(sp->vsetparent != NULL);
651
652 switch (tag) {
653 case TIFFTAG_PREDICTOR:
654 sp->predictor = (uint16) va_arg(ap, uint16_vap);
655 TIFFSetFieldBit(tif, FIELD_PREDICTOR);
656 break;
657 default:
658 return (*sp->vsetparent)(tif, tag, ap);
659 }
660 tif->tif_flags |= TIFF_DIRTYDIRECT;
661 return 1;
662 }
663
664 static int
665 PredictorVGetField(TIFF* tif, uint32 tag, va_list ap)
666 {
667 TIFFPredictorState *sp = PredictorState(tif);
668
669 assert(sp != NULL);
670 assert(sp->vgetparent != NULL);
671
672 switch (tag) {
673 case TIFFTAG_PREDICTOR:
674 *va_arg(ap, uint16*) = sp->predictor;
675 break;
676 default:
677 return (*sp->vgetparent)(tif, tag, ap);
678 }
679 return 1;
680 }
681
682 static void
683 PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
684 {
685 TIFFPredictorState* sp = PredictorState(tif);
686
687 (void) flags;
688 if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
689 fprintf(fd, " Predictor: ");
690 switch (sp->predictor) {
691 case 1: fprintf(fd, "none "); break;
692 case 2: fprintf(fd, "horizontal differencing "); break;
693 case 3: fprintf(fd, "floating point predictor "); break;
694 }
695 fprintf(fd, "%u (0x%x)\n", sp->predictor, sp->predictor);
696 }
697 if (sp->printdir)
698 (*sp->printdir)(tif, fd, flags);
699 }
700
701 int
702 TIFFPredictorInit(TIFF* tif)
703 {
704 TIFFPredictorState* sp = PredictorState(tif);
705
706 assert(sp != 0);
707
708 /*
709 * Merge codec-specific tag information.
710 */
711 if (!_TIFFMergeFields(tif, predictFields,
712 TIFFArrayCount(predictFields))) {
713 TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit",
714 "Merging Predictor codec-specific tags failed");
715 return 0;
716 }
717
718 /*
719 * Override parent get/set field methods.
720 */
721 sp->vgetparent = tif->tif_tagmethods.vgetfield;
722 tif->tif_tagmethods.vgetfield =
723 PredictorVGetField;/* hook for predictor tag */
724 sp->vsetparent = tif->tif_tagmethods.vsetfield;
725 tif->tif_tagmethods.vsetfield =
726 PredictorVSetField;/* hook for predictor tag */
727 sp->printdir = tif->tif_tagmethods.printdir;
728 tif->tif_tagmethods.printdir =
729 PredictorPrintDir; /* hook for predictor tag */
730
731 sp->setupdecode = tif->tif_setupdecode;
732 tif->tif_setupdecode = PredictorSetupDecode;
733 sp->setupencode = tif->tif_setupencode;
734 tif->tif_setupencode = PredictorSetupEncode;
735
736 sp->predictor = 1; /* default value */
737 sp->encodepfunc = NULL; /* no predictor routine */
738 sp->decodepfunc = NULL; /* no predictor routine */
739 return 1;
740 }
741
742 int
743 TIFFPredictorCleanup(TIFF* tif)
744 {
745 TIFFPredictorState* sp = PredictorState(tif);
746
747 assert(sp != 0);
748
749 tif->tif_tagmethods.vgetfield = sp->vgetparent;
750 tif->tif_tagmethods.vsetfield = sp->vsetparent;
751 tif->tif_tagmethods.printdir = sp->printdir;
752 tif->tif_setupdecode = sp->setupdecode;
753 tif->tif_setupencode = sp->setupencode;
754
755 return 1;
756 }
757
758 /* vim: set ts=8 sts=8 sw=8 noet: */
759 /*
760 * Local Variables:
761 * mode: c
762 * c-basic-offset: 8
763 * fill-column: 78
764 * End:
765 */