2
0

tif_predict.c 24 KB

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