/src/FreeImage/Source/LibTIFF/tif_predict.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 736 lines · 552 code · 79 blank · 105 comment · 114 complexity · 3cba2d1b611f420f7191f53de7506dce MD5 · raw file

  1. /* $Id: tif_predict.c,v 1.37 2011/04/10 17:14:09 drolon Exp $ */
  2. /*
  3. * Copyright (c) 1988-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. *
  28. * Predictor Tag Support (used by multiple codecs).
  29. */
  30. #include "tiffiop.h"
  31. #include "tif_predict.h"
  32. #define PredictorState(tif) ((TIFFPredictorState*) (tif)->tif_data)
  33. static void horAcc8(TIFF*, tidata_t, tsize_t);
  34. static void horAcc16(TIFF*, tidata_t, tsize_t);
  35. static void horAcc32(TIFF*, tidata_t, tsize_t);
  36. static void swabHorAcc16(TIFF*, tidata_t, tsize_t);
  37. static void swabHorAcc32(TIFF*, tidata_t, tsize_t);
  38. static void horDiff8(TIFF*, tidata_t, tsize_t);
  39. static void horDiff16(TIFF*, tidata_t, tsize_t);
  40. static void horDiff32(TIFF*, tidata_t, tsize_t);
  41. static void fpAcc(TIFF*, tidata_t, tsize_t);
  42. static void fpDiff(TIFF*, tidata_t, tsize_t);
  43. static int PredictorDecodeRow(TIFF*, tidata_t, tsize_t, tsample_t);
  44. static int PredictorDecodeTile(TIFF*, tidata_t, tsize_t, tsample_t);
  45. static int PredictorEncodeRow(TIFF*, tidata_t, tsize_t, tsample_t);
  46. static int PredictorEncodeTile(TIFF*, tidata_t, tsize_t, tsample_t);
  47. static int
  48. PredictorSetup(TIFF* tif)
  49. {
  50. static const char module[] = "PredictorSetup";
  51. TIFFPredictorState* sp = PredictorState(tif);
  52. TIFFDirectory* td = &tif->tif_dir;
  53. switch (sp->predictor) /* no differencing */
  54. {
  55. case PREDICTOR_NONE:
  56. return 1;
  57. case PREDICTOR_HORIZONTAL:
  58. if (td->td_bitspersample != 8
  59. && td->td_bitspersample != 16
  60. && td->td_bitspersample != 32) {
  61. TIFFErrorExt(tif->tif_clientdata, module,
  62. "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
  63. td->td_bitspersample);
  64. return 0;
  65. }
  66. break;
  67. case PREDICTOR_FLOATINGPOINT:
  68. if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) {
  69. TIFFErrorExt(tif->tif_clientdata, module,
  70. "Floating point \"Predictor\" not supported with %d data format",
  71. td->td_sampleformat);
  72. return 0;
  73. }
  74. break;
  75. default:
  76. TIFFErrorExt(tif->tif_clientdata, module,
  77. "\"Predictor\" value %d not supported",
  78. sp->predictor);
  79. return 0;
  80. }
  81. sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
  82. td->td_samplesperpixel : 1);
  83. /*
  84. * Calculate the scanline/tile-width size in bytes.
  85. */
  86. if (isTiled(tif))
  87. sp->rowsize = TIFFTileRowSize(tif);
  88. else
  89. sp->rowsize = TIFFScanlineSize(tif);
  90. return 1;
  91. }
  92. static int
  93. PredictorSetupDecode(TIFF* tif)
  94. {
  95. TIFFPredictorState* sp = PredictorState(tif);
  96. TIFFDirectory* td = &tif->tif_dir;
  97. if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
  98. return 0;
  99. if (sp->predictor == 2) {
  100. switch (td->td_bitspersample) {
  101. case 8: sp->decodepfunc = horAcc8; break;
  102. case 16: sp->decodepfunc = horAcc16; break;
  103. case 32: sp->decodepfunc = horAcc32; break;
  104. }
  105. /*
  106. * Override default decoding method with one that does the
  107. * predictor stuff.
  108. */
  109. if( tif->tif_decoderow != PredictorDecodeRow )
  110. {
  111. sp->decoderow = tif->tif_decoderow;
  112. tif->tif_decoderow = PredictorDecodeRow;
  113. sp->decodestrip = tif->tif_decodestrip;
  114. tif->tif_decodestrip = PredictorDecodeTile;
  115. sp->decodetile = tif->tif_decodetile;
  116. tif->tif_decodetile = PredictorDecodeTile;
  117. }
  118. /*
  119. * If the data is horizontally differenced 16-bit data that
  120. * requires byte-swapping, then it must be byte swapped before
  121. * the accumulation step. We do this with a special-purpose
  122. * routine and override the normal post decoding logic that
  123. * the library setup when the directory was read.
  124. */
  125. if (tif->tif_flags & TIFF_SWAB) {
  126. if (sp->decodepfunc == horAcc16) {
  127. sp->decodepfunc = swabHorAcc16;
  128. tif->tif_postdecode = _TIFFNoPostDecode;
  129. } else if (sp->decodepfunc == horAcc32) {
  130. sp->decodepfunc = swabHorAcc32;
  131. tif->tif_postdecode = _TIFFNoPostDecode;
  132. }
  133. }
  134. }
  135. else if (sp->predictor == 3) {
  136. sp->decodepfunc = fpAcc;
  137. /*
  138. * Override default decoding method with one that does the
  139. * predictor stuff.
  140. */
  141. if( tif->tif_decoderow != PredictorDecodeRow )
  142. {
  143. sp->decoderow = tif->tif_decoderow;
  144. tif->tif_decoderow = PredictorDecodeRow;
  145. sp->decodestrip = tif->tif_decodestrip;
  146. tif->tif_decodestrip = PredictorDecodeTile;
  147. sp->decodetile = tif->tif_decodetile;
  148. tif->tif_decodetile = PredictorDecodeTile;
  149. }
  150. /*
  151. * The data should not be swapped outside of the floating
  152. * point predictor, the accumulation routine should return
  153. * byres in the native order.
  154. */
  155. if (tif->tif_flags & TIFF_SWAB) {
  156. tif->tif_postdecode = _TIFFNoPostDecode;
  157. }
  158. /*
  159. * Allocate buffer to keep the decoded bytes before
  160. * rearranging in the ight order
  161. */
  162. }
  163. return 1;
  164. }
  165. static int
  166. PredictorSetupEncode(TIFF* tif)
  167. {
  168. TIFFPredictorState* sp = PredictorState(tif);
  169. TIFFDirectory* td = &tif->tif_dir;
  170. if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
  171. return 0;
  172. if (sp->predictor == 2) {
  173. switch (td->td_bitspersample) {
  174. case 8: sp->encodepfunc = horDiff8; break;
  175. case 16: sp->encodepfunc = horDiff16; break;
  176. case 32: sp->encodepfunc = horDiff32; break;
  177. }
  178. /*
  179. * Override default encoding method with one that does the
  180. * predictor stuff.
  181. */
  182. if( tif->tif_encoderow != PredictorEncodeRow )
  183. {
  184. sp->encoderow = tif->tif_encoderow;
  185. tif->tif_encoderow = PredictorEncodeRow;
  186. sp->encodestrip = tif->tif_encodestrip;
  187. tif->tif_encodestrip = PredictorEncodeTile;
  188. sp->encodetile = tif->tif_encodetile;
  189. tif->tif_encodetile = PredictorEncodeTile;
  190. }
  191. }
  192. else if (sp->predictor == 3) {
  193. sp->encodepfunc = fpDiff;
  194. /*
  195. * Override default encoding method with one that does the
  196. * predictor stuff.
  197. */
  198. if( tif->tif_encoderow != PredictorEncodeRow )
  199. {
  200. sp->encoderow = tif->tif_encoderow;
  201. tif->tif_encoderow = PredictorEncodeRow;
  202. sp->encodestrip = tif->tif_encodestrip;
  203. tif->tif_encodestrip = PredictorEncodeTile;
  204. sp->encodetile = tif->tif_encodetile;
  205. tif->tif_encodetile = PredictorEncodeTile;
  206. }
  207. }
  208. return 1;
  209. }
  210. #define REPEAT4(n, op) \
  211. switch (n) { \
  212. default: { int i; for (i = n-4; i > 0; i--) { op; } } \
  213. case 4: op; \
  214. case 3: op; \
  215. case 2: op; \
  216. case 1: op; \
  217. case 0: ; \
  218. }
  219. static void
  220. horAcc8(TIFF* tif, tidata_t cp0, tsize_t cc)
  221. {
  222. tsize_t stride = PredictorState(tif)->stride;
  223. char* cp = (char*) cp0;
  224. if (cc > stride) {
  225. cc -= stride;
  226. /*
  227. * Pipeline the most common cases.
  228. */
  229. if (stride == 3) {
  230. unsigned int cr = cp[0];
  231. unsigned int cg = cp[1];
  232. unsigned int cb = cp[2];
  233. do {
  234. cc -= 3, cp += 3;
  235. cp[0] = (char) (cr += cp[0]);
  236. cp[1] = (char) (cg += cp[1]);
  237. cp[2] = (char) (cb += cp[2]);
  238. } while ((int32) cc > 0);
  239. } else if (stride == 4) {
  240. unsigned int cr = cp[0];
  241. unsigned int cg = cp[1];
  242. unsigned int cb = cp[2];
  243. unsigned int ca = cp[3];
  244. do {
  245. cc -= 4, cp += 4;
  246. cp[0] = (char) (cr += cp[0]);
  247. cp[1] = (char) (cg += cp[1]);
  248. cp[2] = (char) (cb += cp[2]);
  249. cp[3] = (char) (ca += cp[3]);
  250. } while ((int32) cc > 0);
  251. } else {
  252. do {
  253. REPEAT4(stride, cp[stride] =
  254. (char) (cp[stride] + *cp); cp++)
  255. cc -= stride;
  256. } while ((int32) cc > 0);
  257. }
  258. }
  259. }
  260. static void
  261. swabHorAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
  262. {
  263. tsize_t stride = PredictorState(tif)->stride;
  264. uint16* wp = (uint16*) cp0;
  265. tsize_t wc = cc / 2;
  266. if (wc > stride) {
  267. TIFFSwabArrayOfShort(wp, wc);
  268. wc -= stride;
  269. do {
  270. REPEAT4(stride, wp[stride] += wp[0]; wp++)
  271. wc -= stride;
  272. } while ((int32) wc > 0);
  273. }
  274. }
  275. static void
  276. horAcc16(TIFF* tif, tidata_t cp0, tsize_t cc)
  277. {
  278. tsize_t stride = PredictorState(tif)->stride;
  279. uint16* wp = (uint16*) cp0;
  280. tsize_t wc = cc / 2;
  281. if (wc > stride) {
  282. wc -= stride;
  283. do {
  284. REPEAT4(stride, wp[stride] += wp[0]; wp++)
  285. wc -= stride;
  286. } while ((int32) wc > 0);
  287. }
  288. }
  289. static void
  290. swabHorAcc32(TIFF* tif, tidata_t cp0, tsize_t cc)
  291. {
  292. tsize_t stride = PredictorState(tif)->stride;
  293. uint32* wp = (uint32*) cp0;
  294. tsize_t wc = cc / 4;
  295. if (wc > stride) {
  296. TIFFSwabArrayOfLong(wp, wc);
  297. wc -= stride;
  298. do {
  299. REPEAT4(stride, wp[stride] += wp[0]; wp++)
  300. wc -= stride;
  301. } while ((int32) wc > 0);
  302. }
  303. }
  304. static void
  305. horAcc32(TIFF* tif, tidata_t cp0, tsize_t cc)
  306. {
  307. tsize_t stride = PredictorState(tif)->stride;
  308. uint32* wp = (uint32*) cp0;
  309. tsize_t wc = cc / 4;
  310. if (wc > stride) {
  311. wc -= stride;
  312. do {
  313. REPEAT4(stride, wp[stride] += wp[0]; wp++)
  314. wc -= stride;
  315. } while ((int32) wc > 0);
  316. }
  317. }
  318. /*
  319. * Floating point predictor accumulation routine.
  320. */
  321. static void
  322. fpAcc(TIFF* tif, tidata_t cp0, tsize_t cc)
  323. {
  324. tsize_t stride = PredictorState(tif)->stride;
  325. uint32 bps = tif->tif_dir.td_bitspersample / 8;
  326. tsize_t wc = cc / bps;
  327. tsize_t count = cc;
  328. uint8 *cp = (uint8 *) cp0;
  329. uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
  330. if (!tmp)
  331. return;
  332. while (count > stride) {
  333. REPEAT4(stride, cp[stride] += cp[0]; cp++)
  334. count -= stride;
  335. }
  336. _TIFFmemcpy(tmp, cp0, cc);
  337. cp = (uint8 *) cp0;
  338. for (count = 0; count < wc; count++) {
  339. uint32 byte;
  340. for (byte = 0; byte < bps; byte++) {
  341. #if WORDS_BIGENDIAN
  342. cp[bps * count + byte] = tmp[byte * wc + count];
  343. #else
  344. cp[bps * count + byte] =
  345. tmp[(bps - byte - 1) * wc + count];
  346. #endif
  347. }
  348. }
  349. _TIFFfree(tmp);
  350. }
  351. /*
  352. * Decode a scanline and apply the predictor routine.
  353. */
  354. static int
  355. PredictorDecodeRow(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
  356. {
  357. TIFFPredictorState *sp = PredictorState(tif);
  358. assert(sp != NULL);
  359. assert(sp->decoderow != NULL);
  360. assert(sp->decodepfunc != NULL);
  361. if ((*sp->decoderow)(tif, op0, occ0, s)) {
  362. (*sp->decodepfunc)(tif, op0, occ0);
  363. return 1;
  364. } else
  365. return 0;
  366. }
  367. /*
  368. * Decode a tile/strip and apply the predictor routine.
  369. * Note that horizontal differencing must be done on a
  370. * row-by-row basis. The width of a "row" has already
  371. * been calculated at pre-decode time according to the
  372. * strip/tile dimensions.
  373. */
  374. static int
  375. PredictorDecodeTile(TIFF* tif, tidata_t op0, tsize_t occ0, tsample_t s)
  376. {
  377. TIFFPredictorState *sp = PredictorState(tif);
  378. assert(sp != NULL);
  379. assert(sp->decodetile != NULL);
  380. if ((*sp->decodetile)(tif, op0, occ0, s)) {
  381. tsize_t rowsize = sp->rowsize;
  382. assert(rowsize > 0);
  383. assert(sp->decodepfunc != NULL);
  384. while ((long)occ0 > 0) {
  385. (*sp->decodepfunc)(tif, op0, (tsize_t) rowsize);
  386. occ0 -= rowsize;
  387. op0 += rowsize;
  388. }
  389. return 1;
  390. } else
  391. return 0;
  392. }
  393. static void
  394. horDiff8(TIFF* tif, tidata_t cp0, tsize_t cc)
  395. {
  396. TIFFPredictorState* sp = PredictorState(tif);
  397. tsize_t stride = sp->stride;
  398. char* cp = (char*) cp0;
  399. if (cc > stride) {
  400. cc -= stride;
  401. /*
  402. * Pipeline the most common cases.
  403. */
  404. if (stride == 3) {
  405. int r1, g1, b1;
  406. int r2 = cp[0];
  407. int g2 = cp[1];
  408. int b2 = cp[2];
  409. do {
  410. r1 = cp[3]; cp[3] = r1-r2; r2 = r1;
  411. g1 = cp[4]; cp[4] = g1-g2; g2 = g1;
  412. b1 = cp[5]; cp[5] = b1-b2; b2 = b1;
  413. cp += 3;
  414. } while ((int32)(cc -= 3) > 0);
  415. } else if (stride == 4) {
  416. int r1, g1, b1, a1;
  417. int r2 = cp[0];
  418. int g2 = cp[1];
  419. int b2 = cp[2];
  420. int a2 = cp[3];
  421. do {
  422. r1 = cp[4]; cp[4] = r1-r2; r2 = r1;
  423. g1 = cp[5]; cp[5] = g1-g2; g2 = g1;
  424. b1 = cp[6]; cp[6] = b1-b2; b2 = b1;
  425. a1 = cp[7]; cp[7] = a1-a2; a2 = a1;
  426. cp += 4;
  427. } while ((int32)(cc -= 4) > 0);
  428. } else {
  429. cp += cc - 1;
  430. do {
  431. REPEAT4(stride, cp[stride] -= cp[0]; cp--)
  432. } while ((int32)(cc -= stride) > 0);
  433. }
  434. }
  435. }
  436. static void
  437. horDiff16(TIFF* tif, tidata_t cp0, tsize_t cc)
  438. {
  439. TIFFPredictorState* sp = PredictorState(tif);
  440. tsize_t stride = sp->stride;
  441. int16 *wp = (int16*) cp0;
  442. tsize_t wc = cc/2;
  443. if (wc > stride) {
  444. wc -= stride;
  445. wp += wc - 1;
  446. do {
  447. REPEAT4(stride, wp[stride] -= wp[0]; wp--)
  448. wc -= stride;
  449. } while ((int32) wc > 0);
  450. }
  451. }
  452. static void
  453. horDiff32(TIFF* tif, tidata_t cp0, tsize_t cc)
  454. {
  455. TIFFPredictorState* sp = PredictorState(tif);
  456. tsize_t stride = sp->stride;
  457. int32 *wp = (int32*) cp0;
  458. tsize_t wc = cc/4;
  459. if (wc > stride) {
  460. wc -= stride;
  461. wp += wc - 1;
  462. do {
  463. REPEAT4(stride, wp[stride] -= wp[0]; wp--)
  464. wc -= stride;
  465. } while ((int32) wc > 0);
  466. }
  467. }
  468. /*
  469. * Floating point predictor differencing routine.
  470. */
  471. static void
  472. fpDiff(TIFF* tif, tidata_t cp0, tsize_t cc)
  473. {
  474. tsize_t stride = PredictorState(tif)->stride;
  475. uint32 bps = tif->tif_dir.td_bitspersample / 8;
  476. tsize_t wc = cc / bps;
  477. tsize_t count;
  478. uint8 *cp = (uint8 *) cp0;
  479. uint8 *tmp = (uint8 *)_TIFFmalloc(cc);
  480. if (!tmp)
  481. return;
  482. _TIFFmemcpy(tmp, cp0, cc);
  483. for (count = 0; count < wc; count++) {
  484. uint32 byte;
  485. for (byte = 0; byte < bps; byte++) {
  486. #if WORDS_BIGENDIAN
  487. cp[byte * wc + count] = tmp[bps * count + byte];
  488. #else
  489. cp[(bps - byte - 1) * wc + count] =
  490. tmp[bps * count + byte];
  491. #endif
  492. }
  493. }
  494. _TIFFfree(tmp);
  495. cp = (uint8 *) cp0;
  496. cp += cc - stride - 1;
  497. for (count = cc; count > stride; count -= stride)
  498. REPEAT4(stride, cp[stride] -= cp[0]; cp--)
  499. }
  500. static int
  501. PredictorEncodeRow(TIFF* tif, tidata_t bp, tsize_t cc, tsample_t s)
  502. {
  503. TIFFPredictorState *sp = PredictorState(tif);
  504. assert(sp != NULL);
  505. assert(sp->encodepfunc != NULL);
  506. assert(sp->encoderow != NULL);
  507. /* XXX horizontal differencing alters user's data XXX */
  508. (*sp->encodepfunc)(tif, bp, cc);
  509. return (*sp->encoderow)(tif, bp, cc, s);
  510. }
  511. static int
  512. PredictorEncodeTile(TIFF* tif, tidata_t bp0, tsize_t cc0, tsample_t s)
  513. {
  514. static const char module[] = "PredictorEncodeTile";
  515. TIFFPredictorState *sp = PredictorState(tif);
  516. uint8 *working_copy;
  517. tsize_t cc = cc0, rowsize;
  518. unsigned char* bp;
  519. int result_code;
  520. assert(sp != NULL);
  521. assert(sp->encodepfunc != NULL);
  522. assert(sp->encodetile != NULL);
  523. /*
  524. * Do predictor manipulation in a working buffer to avoid altering
  525. * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
  526. */
  527. working_copy = (uint8*) _TIFFmalloc(cc0);
  528. if( working_copy == NULL )
  529. {
  530. TIFFErrorExt(tif->tif_clientdata, module,
  531. "Out of memory allocating %d byte temp buffer.",
  532. cc0 );
  533. return 0;
  534. }
  535. memcpy( working_copy, bp0, cc0 );
  536. bp = working_copy;
  537. rowsize = sp->rowsize;
  538. assert(rowsize > 0);
  539. assert((cc0%rowsize)==0);
  540. while (cc > 0) {
  541. (*sp->encodepfunc)(tif, bp, rowsize);
  542. cc -= rowsize;
  543. bp += rowsize;
  544. }
  545. result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
  546. _TIFFfree( working_copy );
  547. return result_code;
  548. }
  549. #define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */
  550. static const TIFFFieldInfo predictFieldInfo[] = {
  551. { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, FIELD_PREDICTOR,
  552. FALSE, FALSE, "Predictor" },
  553. };
  554. static int
  555. PredictorVSetField(TIFF* tif, ttag_t tag, va_list ap)
  556. {
  557. TIFFPredictorState *sp = PredictorState(tif);
  558. assert(sp != NULL);
  559. assert(sp->vsetparent != NULL);
  560. switch (tag) {
  561. case TIFFTAG_PREDICTOR:
  562. sp->predictor = (uint16) va_arg(ap, int);
  563. TIFFSetFieldBit(tif, FIELD_PREDICTOR);
  564. break;
  565. default:
  566. return (*sp->vsetparent)(tif, tag, ap);
  567. }
  568. tif->tif_flags |= TIFF_DIRTYDIRECT;
  569. return 1;
  570. }
  571. static int
  572. PredictorVGetField(TIFF* tif, ttag_t tag, va_list ap)
  573. {
  574. TIFFPredictorState *sp = PredictorState(tif);
  575. assert(sp != NULL);
  576. assert(sp->vgetparent != NULL);
  577. switch (tag) {
  578. case TIFFTAG_PREDICTOR:
  579. *va_arg(ap, uint16*) = sp->predictor;
  580. break;
  581. default:
  582. return (*sp->vgetparent)(tif, tag, ap);
  583. }
  584. return 1;
  585. }
  586. static void
  587. PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
  588. {
  589. TIFFPredictorState* sp = PredictorState(tif);
  590. (void) flags;
  591. if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
  592. fprintf(fd, " Predictor: ");
  593. switch (sp->predictor) {
  594. case 1: fprintf(fd, "none "); break;
  595. case 2: fprintf(fd, "horizontal differencing "); break;
  596. case 3: fprintf(fd, "floating point predictor "); break;
  597. }
  598. fprintf(fd, "%u (0x%x)\n", sp->predictor, sp->predictor);
  599. }
  600. if (sp->printdir)
  601. (*sp->printdir)(tif, fd, flags);
  602. }
  603. int
  604. TIFFPredictorInit(TIFF* tif)
  605. {
  606. TIFFPredictorState* sp = PredictorState(tif);
  607. assert(sp != 0);
  608. /*
  609. * Merge codec-specific tag information.
  610. */
  611. if (!_TIFFMergeFieldInfo(tif, predictFieldInfo,
  612. TIFFArrayCount(predictFieldInfo))) {
  613. TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit",
  614. "Merging Predictor codec-specific tags failed");
  615. return 0;
  616. }
  617. /*
  618. * Override parent get/set field methods.
  619. */
  620. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  621. tif->tif_tagmethods.vgetfield =
  622. PredictorVGetField;/* hook for predictor tag */
  623. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  624. tif->tif_tagmethods.vsetfield =
  625. PredictorVSetField;/* hook for predictor tag */
  626. sp->printdir = tif->tif_tagmethods.printdir;
  627. tif->tif_tagmethods.printdir =
  628. PredictorPrintDir; /* hook for predictor tag */
  629. sp->setupdecode = tif->tif_setupdecode;
  630. tif->tif_setupdecode = PredictorSetupDecode;
  631. sp->setupencode = tif->tif_setupencode;
  632. tif->tif_setupencode = PredictorSetupEncode;
  633. sp->predictor = 1; /* default value */
  634. sp->encodepfunc = NULL; /* no predictor routine */
  635. sp->decodepfunc = NULL; /* no predictor routine */
  636. return 1;
  637. }
  638. int
  639. TIFFPredictorCleanup(TIFF* tif)
  640. {
  641. TIFFPredictorState* sp = PredictorState(tif);
  642. assert(sp != 0);
  643. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  644. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  645. tif->tif_tagmethods.printdir = sp->printdir;
  646. tif->tif_setupdecode = sp->setupdecode;
  647. tif->tif_setupencode = sp->setupencode;
  648. return 1;
  649. }
  650. /* vim: set ts=8 sts=8 sw=8 noet: */
  651. /*
  652. * Local Variables:
  653. * mode: c
  654. * c-basic-offset: 8
  655. * fill-column: 78
  656. * End:
  657. */