tif_predict.c 19 KB

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