tif_predict.c 21 KB

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