tif_predict.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. /*
  2. * Copyright (c) 1988-1997 Sam Leffler
  3. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  4. *
  5. * Permission to use, copy, modify, distribute, and sell this software and
  6. * its documentation for any purpose is hereby granted without fee, provided
  7. * that (i) the above copyright notices and this permission notice appear in
  8. * all copies of the software and related documentation, and (ii) the names of
  9. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  10. * publicity relating to the software without the specific, prior written
  11. * permission of Sam Leffler and Silicon Graphics.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  14. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  15. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  16. *
  17. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  18. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  19. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  20. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  21. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  22. * OF THIS SOFTWARE.
  23. */
  24. /*
  25. * TIFF Library.
  26. *
  27. * Predictor Tag Support (used by multiple codecs).
  28. */
  29. #include "tiffiop.h"
  30. #include "tif_predict.h"
  31. #define PredictorState(tif) ((TIFFPredictorState*) (tif)->tif_data)
  32. static int horAcc8(TIFF* tif, uint8* cp0, tmsize_t cc);
  33. static int horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
  34. static int horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
  35. static int swabHorAcc16(TIFF* tif, uint8* cp0, tmsize_t cc);
  36. static int swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc);
  37. static int horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc);
  38. static int horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc);
  39. static int horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc);
  40. static int swabHorDiff16(TIFF* tif, uint8* cp0, tmsize_t cc);
  41. static int swabHorDiff32(TIFF* tif, uint8* cp0, tmsize_t cc);
  42. static int fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc);
  43. static int fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc);
  44. static int PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
  45. static int PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s);
  46. static int PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s);
  47. static int PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s);
  48. static int
  49. PredictorSetup(TIFF* tif)
  50. {
  51. static const char module[] = "PredictorSetup";
  52. TIFFPredictorState* sp = PredictorState(tif);
  53. TIFFDirectory* td = &tif->tif_dir;
  54. switch (sp->predictor) /* no differencing */
  55. {
  56. case PREDICTOR_NONE:
  57. return 1;
  58. case PREDICTOR_HORIZONTAL:
  59. if (td->td_bitspersample != 8
  60. && td->td_bitspersample != 16
  61. && td->td_bitspersample != 32) {
  62. TIFFErrorExt(tif->tif_clientdata, module,
  63. "Horizontal differencing \"Predictor\" not supported with %d-bit samples",
  64. td->td_bitspersample);
  65. return 0;
  66. }
  67. break;
  68. case PREDICTOR_FLOATINGPOINT:
  69. if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP) {
  70. TIFFErrorExt(tif->tif_clientdata, module,
  71. "Floating point \"Predictor\" not supported with %d data format",
  72. td->td_sampleformat);
  73. return 0;
  74. }
  75. if (td->td_bitspersample != 16
  76. && td->td_bitspersample != 24
  77. && td->td_bitspersample != 32
  78. && td->td_bitspersample != 64) { /* Should 64 be allowed? */
  79. TIFFErrorExt(tif->tif_clientdata, module,
  80. "Floating point \"Predictor\" not supported with %d-bit samples",
  81. td->td_bitspersample);
  82. return 0;
  83. }
  84. break;
  85. default:
  86. TIFFErrorExt(tif->tif_clientdata, module,
  87. "\"Predictor\" value %d not supported",
  88. sp->predictor);
  89. return 0;
  90. }
  91. sp->stride = (td->td_planarconfig == PLANARCONFIG_CONTIG ?
  92. td->td_samplesperpixel : 1);
  93. /*
  94. * Calculate the scanline/tile-width size in bytes.
  95. */
  96. if (isTiled(tif))
  97. sp->rowsize = TIFFTileRowSize(tif);
  98. else
  99. sp->rowsize = TIFFScanlineSize(tif);
  100. if (sp->rowsize == 0)
  101. return 0;
  102. return 1;
  103. }
  104. static int
  105. PredictorSetupDecode(TIFF* tif)
  106. {
  107. TIFFPredictorState* sp = PredictorState(tif);
  108. TIFFDirectory* td = &tif->tif_dir;
  109. /* Note: when PredictorSetup() fails, the effets of setupdecode() */
  110. /* will not be "canceled" so setupdecode() might be robust to */
  111. /* be called several times. */
  112. if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
  113. return 0;
  114. if (sp->predictor == 2) {
  115. switch (td->td_bitspersample) {
  116. case 8: sp->decodepfunc = horAcc8; break;
  117. case 16: sp->decodepfunc = horAcc16; break;
  118. case 32: sp->decodepfunc = horAcc32; break;
  119. }
  120. /*
  121. * Override default decoding method with one that does the
  122. * predictor stuff.
  123. */
  124. if( tif->tif_decoderow != PredictorDecodeRow )
  125. {
  126. sp->decoderow = tif->tif_decoderow;
  127. tif->tif_decoderow = PredictorDecodeRow;
  128. sp->decodestrip = tif->tif_decodestrip;
  129. tif->tif_decodestrip = PredictorDecodeTile;
  130. sp->decodetile = tif->tif_decodetile;
  131. tif->tif_decodetile = PredictorDecodeTile;
  132. }
  133. /*
  134. * If the data is horizontally differenced 16-bit data that
  135. * requires byte-swapping, then it must be byte swapped before
  136. * the accumulation step. We do this with a special-purpose
  137. * routine and override the normal post decoding logic that
  138. * the library setup when the directory was read.
  139. */
  140. if (tif->tif_flags & TIFF_SWAB) {
  141. if (sp->decodepfunc == horAcc16) {
  142. sp->decodepfunc = swabHorAcc16;
  143. tif->tif_postdecode = _TIFFNoPostDecode;
  144. } else if (sp->decodepfunc == horAcc32) {
  145. sp->decodepfunc = swabHorAcc32;
  146. tif->tif_postdecode = _TIFFNoPostDecode;
  147. }
  148. }
  149. }
  150. else if (sp->predictor == 3) {
  151. sp->decodepfunc = fpAcc;
  152. /*
  153. * Override default decoding method with one that does the
  154. * predictor stuff.
  155. */
  156. if( tif->tif_decoderow != PredictorDecodeRow )
  157. {
  158. sp->decoderow = tif->tif_decoderow;
  159. tif->tif_decoderow = PredictorDecodeRow;
  160. sp->decodestrip = tif->tif_decodestrip;
  161. tif->tif_decodestrip = PredictorDecodeTile;
  162. sp->decodetile = tif->tif_decodetile;
  163. tif->tif_decodetile = PredictorDecodeTile;
  164. }
  165. /*
  166. * The data should not be swapped outside of the floating
  167. * point predictor, the accumulation routine should return
  168. * byres in the native order.
  169. */
  170. if (tif->tif_flags & TIFF_SWAB) {
  171. tif->tif_postdecode = _TIFFNoPostDecode;
  172. }
  173. /*
  174. * Allocate buffer to keep the decoded bytes before
  175. * rearranging in the right order
  176. */
  177. }
  178. return 1;
  179. }
  180. static int
  181. PredictorSetupEncode(TIFF* tif)
  182. {
  183. TIFFPredictorState* sp = PredictorState(tif);
  184. TIFFDirectory* td = &tif->tif_dir;
  185. if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
  186. return 0;
  187. if (sp->predictor == 2) {
  188. switch (td->td_bitspersample) {
  189. case 8: sp->encodepfunc = horDiff8; break;
  190. case 16: sp->encodepfunc = horDiff16; break;
  191. case 32: sp->encodepfunc = horDiff32; break;
  192. }
  193. /*
  194. * Override default encoding method with one that does the
  195. * predictor stuff.
  196. */
  197. if( tif->tif_encoderow != PredictorEncodeRow )
  198. {
  199. sp->encoderow = tif->tif_encoderow;
  200. tif->tif_encoderow = PredictorEncodeRow;
  201. sp->encodestrip = tif->tif_encodestrip;
  202. tif->tif_encodestrip = PredictorEncodeTile;
  203. sp->encodetile = tif->tif_encodetile;
  204. tif->tif_encodetile = PredictorEncodeTile;
  205. }
  206. /*
  207. * If the data is horizontally differenced 16-bit data that
  208. * requires byte-swapping, then it must be byte swapped after
  209. * the differentiation step. We do this with a special-purpose
  210. * routine and override the normal post decoding logic that
  211. * the library setup when the directory was read.
  212. */
  213. if (tif->tif_flags & TIFF_SWAB) {
  214. if (sp->encodepfunc == horDiff16) {
  215. sp->encodepfunc = swabHorDiff16;
  216. tif->tif_postdecode = _TIFFNoPostDecode;
  217. } else if (sp->encodepfunc == horDiff32) {
  218. sp->encodepfunc = swabHorDiff32;
  219. tif->tif_postdecode = _TIFFNoPostDecode;
  220. }
  221. }
  222. }
  223. else if (sp->predictor == 3) {
  224. sp->encodepfunc = fpDiff;
  225. /*
  226. * Override default encoding method with one that does the
  227. * predictor stuff.
  228. */
  229. if( tif->tif_encoderow != PredictorEncodeRow )
  230. {
  231. sp->encoderow = tif->tif_encoderow;
  232. tif->tif_encoderow = PredictorEncodeRow;
  233. sp->encodestrip = tif->tif_encodestrip;
  234. tif->tif_encodestrip = PredictorEncodeTile;
  235. sp->encodetile = tif->tif_encodetile;
  236. tif->tif_encodetile = PredictorEncodeTile;
  237. }
  238. }
  239. return 1;
  240. }
  241. #define REPEAT4(n, op) \
  242. switch (n) { \
  243. default: { \
  244. tmsize_t i; for (i = n-4; i > 0; i--) { op; } } /*-fallthrough*/ \
  245. case 4: op; /*-fallthrough*/ \
  246. case 3: op; /*-fallthrough*/ \
  247. case 2: op; /*-fallthrough*/ \
  248. case 1: op; /*-fallthrough*/ \
  249. case 0: ; \
  250. }
  251. /* Remarks related to C standard compliance in all below functions : */
  252. /* - to avoid any undefined behavior, we only operate on unsigned types */
  253. /* since the behavior of "overflows" is defined (wrap over) */
  254. /* - when storing into the byte stream, we explicitly mask with 0xff so */
  255. /* as to make icc -check=conversions happy (not necessary by the standard) */
  256. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  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. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  320. static int
  321. horAcc16(TIFF* tif, uint8* cp0, tmsize_t cc)
  322. {
  323. tmsize_t stride = PredictorState(tif)->stride;
  324. uint16* wp = (uint16*) cp0;
  325. tmsize_t wc = cc / 2;
  326. if((cc%(2*stride))!=0)
  327. {
  328. TIFFErrorExt(tif->tif_clientdata, "horAcc16",
  329. "%s", "cc%(2*stride))!=0");
  330. return 0;
  331. }
  332. if (wc > stride) {
  333. wc -= stride;
  334. do {
  335. REPEAT4(stride, wp[stride] = (uint16)(((unsigned int)wp[stride] + (unsigned int)wp[0]) & 0xffff); wp++)
  336. wc -= stride;
  337. } while (wc > 0);
  338. }
  339. return 1;
  340. }
  341. static int
  342. swabHorAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
  343. {
  344. uint32* wp = (uint32*) cp0;
  345. tmsize_t wc = cc / 4;
  346. TIFFSwabArrayOfLong(wp, wc);
  347. return horAcc32(tif, cp0, cc);
  348. }
  349. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  350. static int
  351. horAcc32(TIFF* tif, uint8* cp0, tmsize_t cc)
  352. {
  353. tmsize_t stride = PredictorState(tif)->stride;
  354. uint32* wp = (uint32*) cp0;
  355. tmsize_t wc = cc / 4;
  356. if((cc%(4*stride))!=0)
  357. {
  358. TIFFErrorExt(tif->tif_clientdata, "horAcc32",
  359. "%s", "cc%(4*stride))!=0");
  360. return 0;
  361. }
  362. if (wc > stride) {
  363. wc -= stride;
  364. do {
  365. REPEAT4(stride, wp[stride] += wp[0]; wp++)
  366. wc -= stride;
  367. } while (wc > 0);
  368. }
  369. return 1;
  370. }
  371. /*
  372. * Floating point predictor accumulation routine.
  373. */
  374. static int
  375. fpAcc(TIFF* tif, uint8* cp0, tmsize_t cc)
  376. {
  377. tmsize_t stride = PredictorState(tif)->stride;
  378. uint32 bps = tif->tif_dir.td_bitspersample / 8;
  379. tmsize_t wc = cc / bps;
  380. tmsize_t count = cc;
  381. uint8 *cp = (uint8 *) cp0;
  382. uint8 *tmp;
  383. if(cc%(bps*stride)!=0)
  384. {
  385. TIFFErrorExt(tif->tif_clientdata, "fpAcc",
  386. "%s", "cc%(bps*stride))!=0");
  387. return 0;
  388. }
  389. tmp = (uint8 *)_TIFFmalloc(cc);
  390. if (!tmp)
  391. return 0;
  392. while (count > stride) {
  393. REPEAT4(stride, cp[stride] =
  394. (unsigned char) ((cp[stride] + cp[0]) & 0xff); cp++)
  395. count -= stride;
  396. }
  397. _TIFFmemcpy(tmp, cp0, cc);
  398. cp = (uint8 *) cp0;
  399. for (count = 0; count < wc; count++) {
  400. uint32 byte;
  401. for (byte = 0; byte < bps; byte++) {
  402. #if WORDS_BIGENDIAN
  403. cp[bps * count + byte] = tmp[byte * wc + count];
  404. #else
  405. cp[bps * count + byte] =
  406. tmp[(bps - byte - 1) * wc + count];
  407. #endif
  408. }
  409. }
  410. _TIFFfree(tmp);
  411. return 1;
  412. }
  413. /*
  414. * Decode a scanline and apply the predictor routine.
  415. */
  416. static int
  417. PredictorDecodeRow(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
  418. {
  419. TIFFPredictorState *sp = PredictorState(tif);
  420. assert(sp != NULL);
  421. assert(sp->decoderow != NULL);
  422. assert(sp->decodepfunc != NULL);
  423. if ((*sp->decoderow)(tif, op0, occ0, s)) {
  424. return (*sp->decodepfunc)(tif, op0, occ0);
  425. } else
  426. return 0;
  427. }
  428. /*
  429. * Decode a tile/strip and apply the predictor routine.
  430. * Note that horizontal differencing must be done on a
  431. * row-by-row basis. The width of a "row" has already
  432. * been calculated at pre-decode time according to the
  433. * strip/tile dimensions.
  434. */
  435. static int
  436. PredictorDecodeTile(TIFF* tif, uint8* op0, tmsize_t occ0, uint16 s)
  437. {
  438. TIFFPredictorState *sp = PredictorState(tif);
  439. assert(sp != NULL);
  440. assert(sp->decodetile != NULL);
  441. if ((*sp->decodetile)(tif, op0, occ0, s)) {
  442. tmsize_t rowsize = sp->rowsize;
  443. assert(rowsize > 0);
  444. if((occ0%rowsize) !=0)
  445. {
  446. TIFFErrorExt(tif->tif_clientdata, "PredictorDecodeTile",
  447. "%s", "occ0%rowsize != 0");
  448. return 0;
  449. }
  450. assert(sp->decodepfunc != NULL);
  451. while (occ0 > 0) {
  452. if( !(*sp->decodepfunc)(tif, op0, rowsize) )
  453. return 0;
  454. occ0 -= rowsize;
  455. op0 += rowsize;
  456. }
  457. return 1;
  458. } else
  459. return 0;
  460. }
  461. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  462. static int
  463. horDiff8(TIFF* tif, uint8* cp0, tmsize_t cc)
  464. {
  465. TIFFPredictorState* sp = PredictorState(tif);
  466. tmsize_t stride = sp->stride;
  467. unsigned char* cp = (unsigned char*) cp0;
  468. if((cc%stride)!=0)
  469. {
  470. TIFFErrorExt(tif->tif_clientdata, "horDiff8",
  471. "%s", "(cc%stride)!=0");
  472. return 0;
  473. }
  474. if (cc > stride) {
  475. cc -= stride;
  476. /*
  477. * Pipeline the most common cases.
  478. */
  479. if (stride == 3) {
  480. unsigned int r1, g1, b1;
  481. unsigned int r2 = cp[0];
  482. unsigned int g2 = cp[1];
  483. unsigned int b2 = cp[2];
  484. do {
  485. r1 = cp[3]; cp[3] = (unsigned char)((r1-r2)&0xff); r2 = r1;
  486. g1 = cp[4]; cp[4] = (unsigned char)((g1-g2)&0xff); g2 = g1;
  487. b1 = cp[5]; cp[5] = (unsigned char)((b1-b2)&0xff); b2 = b1;
  488. cp += 3;
  489. } while ((cc -= 3) > 0);
  490. } else if (stride == 4) {
  491. unsigned int r1, g1, b1, a1;
  492. unsigned int r2 = cp[0];
  493. unsigned int g2 = cp[1];
  494. unsigned int b2 = cp[2];
  495. unsigned int a2 = cp[3];
  496. do {
  497. r1 = cp[4]; cp[4] = (unsigned char)((r1-r2)&0xff); r2 = r1;
  498. g1 = cp[5]; cp[5] = (unsigned char)((g1-g2)&0xff); g2 = g1;
  499. b1 = cp[6]; cp[6] = (unsigned char)((b1-b2)&0xff); b2 = b1;
  500. a1 = cp[7]; cp[7] = (unsigned char)((a1-a2)&0xff); a2 = a1;
  501. cp += 4;
  502. } while ((cc -= 4) > 0);
  503. } else {
  504. cp += cc - 1;
  505. do {
  506. REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--)
  507. } while ((cc -= stride) > 0);
  508. }
  509. }
  510. return 1;
  511. }
  512. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  513. static int
  514. horDiff16(TIFF* tif, uint8* cp0, tmsize_t cc)
  515. {
  516. TIFFPredictorState* sp = PredictorState(tif);
  517. tmsize_t stride = sp->stride;
  518. uint16 *wp = (uint16*) cp0;
  519. tmsize_t wc = cc/2;
  520. if((cc%(2*stride))!=0)
  521. {
  522. TIFFErrorExt(tif->tif_clientdata, "horDiff8",
  523. "%s", "(cc%(2*stride))!=0");
  524. return 0;
  525. }
  526. if (wc > stride) {
  527. wc -= stride;
  528. wp += wc - 1;
  529. do {
  530. REPEAT4(stride, wp[stride] = (uint16)(((unsigned int)wp[stride] - (unsigned int)wp[0]) & 0xffff); wp--)
  531. wc -= stride;
  532. } while (wc > 0);
  533. }
  534. return 1;
  535. }
  536. static int
  537. swabHorDiff16(TIFF* tif, uint8* cp0, tmsize_t cc)
  538. {
  539. uint16* wp = (uint16*) cp0;
  540. tmsize_t wc = cc / 2;
  541. if( !horDiff16(tif, cp0, cc) )
  542. return 0;
  543. TIFFSwabArrayOfShort(wp, wc);
  544. return 1;
  545. }
  546. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  547. static int
  548. horDiff32(TIFF* tif, uint8* cp0, tmsize_t cc)
  549. {
  550. TIFFPredictorState* sp = PredictorState(tif);
  551. tmsize_t stride = sp->stride;
  552. uint32 *wp = (uint32*) cp0;
  553. tmsize_t wc = cc/4;
  554. if((cc%(4*stride))!=0)
  555. {
  556. TIFFErrorExt(tif->tif_clientdata, "horDiff32",
  557. "%s", "(cc%(4*stride))!=0");
  558. return 0;
  559. }
  560. if (wc > stride) {
  561. wc -= stride;
  562. wp += wc - 1;
  563. do {
  564. REPEAT4(stride, wp[stride] -= wp[0]; wp--)
  565. wc -= stride;
  566. } while (wc > 0);
  567. }
  568. return 1;
  569. }
  570. static int
  571. swabHorDiff32(TIFF* tif, uint8* cp0, tmsize_t cc)
  572. {
  573. uint32* wp = (uint32*) cp0;
  574. tmsize_t wc = cc / 4;
  575. if( !horDiff32(tif, cp0, cc) )
  576. return 0;
  577. TIFFSwabArrayOfLong(wp, wc);
  578. return 1;
  579. }
  580. /*
  581. * Floating point predictor differencing routine.
  582. */
  583. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  584. static int
  585. fpDiff(TIFF* tif, uint8* cp0, tmsize_t cc)
  586. {
  587. tmsize_t stride = PredictorState(tif)->stride;
  588. uint32 bps = tif->tif_dir.td_bitspersample / 8;
  589. tmsize_t wc = cc / bps;
  590. tmsize_t count;
  591. uint8 *cp = (uint8 *) cp0;
  592. uint8 *tmp;
  593. if((cc%(bps*stride))!=0)
  594. {
  595. TIFFErrorExt(tif->tif_clientdata, "fpDiff",
  596. "%s", "(cc%(bps*stride))!=0");
  597. return 0;
  598. }
  599. tmp = (uint8 *)_TIFFmalloc(cc);
  600. if (!tmp)
  601. return 0;
  602. _TIFFmemcpy(tmp, cp0, cc);
  603. for (count = 0; count < wc; count++) {
  604. uint32 byte;
  605. for (byte = 0; byte < bps; byte++) {
  606. #if WORDS_BIGENDIAN
  607. cp[byte * wc + count] = tmp[bps * count + byte];
  608. #else
  609. cp[(bps - byte - 1) * wc + count] =
  610. tmp[bps * count + byte];
  611. #endif
  612. }
  613. }
  614. _TIFFfree(tmp);
  615. cp = (uint8 *) cp0;
  616. cp += cc - stride - 1;
  617. for (count = cc; count > stride; count -= stride)
  618. REPEAT4(stride, cp[stride] = (unsigned char)((cp[stride] - cp[0])&0xff); cp--)
  619. return 1;
  620. }
  621. static int
  622. PredictorEncodeRow(TIFF* tif, uint8* bp, tmsize_t cc, uint16 s)
  623. {
  624. TIFFPredictorState *sp = PredictorState(tif);
  625. assert(sp != NULL);
  626. assert(sp->encodepfunc != NULL);
  627. assert(sp->encoderow != NULL);
  628. /* XXX horizontal differencing alters user's data XXX */
  629. if( !(*sp->encodepfunc)(tif, bp, cc) )
  630. return 0;
  631. return (*sp->encoderow)(tif, bp, cc, s);
  632. }
  633. static int
  634. PredictorEncodeTile(TIFF* tif, uint8* bp0, tmsize_t cc0, uint16 s)
  635. {
  636. static const char module[] = "PredictorEncodeTile";
  637. TIFFPredictorState *sp = PredictorState(tif);
  638. uint8 *working_copy;
  639. tmsize_t cc = cc0, rowsize;
  640. unsigned char* bp;
  641. int result_code;
  642. assert(sp != NULL);
  643. assert(sp->encodepfunc != NULL);
  644. assert(sp->encodetile != NULL);
  645. /*
  646. * Do predictor manipulation in a working buffer to avoid altering
  647. * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
  648. */
  649. working_copy = (uint8*) _TIFFmalloc(cc0);
  650. if( working_copy == NULL )
  651. {
  652. TIFFErrorExt(tif->tif_clientdata, module,
  653. "Out of memory allocating " TIFF_SSIZE_FORMAT " byte temp buffer.",
  654. cc0 );
  655. return 0;
  656. }
  657. memcpy( working_copy, bp0, cc0 );
  658. bp = working_copy;
  659. rowsize = sp->rowsize;
  660. assert(rowsize > 0);
  661. if((cc0%rowsize)!=0)
  662. {
  663. TIFFErrorExt(tif->tif_clientdata, "PredictorEncodeTile",
  664. "%s", "(cc0%rowsize)!=0");
  665. _TIFFfree( working_copy );
  666. return 0;
  667. }
  668. while (cc > 0) {
  669. (*sp->encodepfunc)(tif, bp, rowsize);
  670. cc -= rowsize;
  671. bp += rowsize;
  672. }
  673. result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
  674. _TIFFfree( working_copy );
  675. return result_code;
  676. }
  677. #define FIELD_PREDICTOR (FIELD_CODEC+0) /* XXX */
  678. static const TIFFField predictFields[] = {
  679. { TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16, TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL },
  680. };
  681. static int
  682. PredictorVSetField(TIFF* tif, uint32 tag, va_list ap)
  683. {
  684. TIFFPredictorState *sp = PredictorState(tif);
  685. assert(sp != NULL);
  686. assert(sp->vsetparent != NULL);
  687. switch (tag) {
  688. case TIFFTAG_PREDICTOR:
  689. sp->predictor = (uint16) va_arg(ap, uint16_vap);
  690. TIFFSetFieldBit(tif, FIELD_PREDICTOR);
  691. break;
  692. default:
  693. return (*sp->vsetparent)(tif, tag, ap);
  694. }
  695. tif->tif_flags |= TIFF_DIRTYDIRECT;
  696. return 1;
  697. }
  698. static int
  699. PredictorVGetField(TIFF* tif, uint32 tag, va_list ap)
  700. {
  701. TIFFPredictorState *sp = PredictorState(tif);
  702. assert(sp != NULL);
  703. assert(sp->vgetparent != NULL);
  704. switch (tag) {
  705. case TIFFTAG_PREDICTOR:
  706. *va_arg(ap, uint16*) = (uint16)sp->predictor;
  707. break;
  708. default:
  709. return (*sp->vgetparent)(tif, tag, ap);
  710. }
  711. return 1;
  712. }
  713. static void
  714. PredictorPrintDir(TIFF* tif, FILE* fd, long flags)
  715. {
  716. TIFFPredictorState* sp = PredictorState(tif);
  717. (void) flags;
  718. if (TIFFFieldSet(tif,FIELD_PREDICTOR)) {
  719. fprintf(fd, " Predictor: ");
  720. switch (sp->predictor) {
  721. case 1: fprintf(fd, "none "); break;
  722. case 2: fprintf(fd, "horizontal differencing "); break;
  723. case 3: fprintf(fd, "floating point predictor "); break;
  724. }
  725. fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor);
  726. }
  727. if (sp->printdir)
  728. (*sp->printdir)(tif, fd, flags);
  729. }
  730. int
  731. TIFFPredictorInit(TIFF* tif)
  732. {
  733. TIFFPredictorState* sp = PredictorState(tif);
  734. assert(sp != 0);
  735. /*
  736. * Merge codec-specific tag information.
  737. */
  738. if (!_TIFFMergeFields(tif, predictFields,
  739. TIFFArrayCount(predictFields))) {
  740. TIFFErrorExt(tif->tif_clientdata, "TIFFPredictorInit",
  741. "Merging Predictor codec-specific tags failed");
  742. return 0;
  743. }
  744. /*
  745. * Override parent get/set field methods.
  746. */
  747. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  748. tif->tif_tagmethods.vgetfield =
  749. PredictorVGetField;/* hook for predictor tag */
  750. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  751. tif->tif_tagmethods.vsetfield =
  752. PredictorVSetField;/* hook for predictor tag */
  753. sp->printdir = tif->tif_tagmethods.printdir;
  754. tif->tif_tagmethods.printdir =
  755. PredictorPrintDir; /* hook for predictor tag */
  756. sp->setupdecode = tif->tif_setupdecode;
  757. tif->tif_setupdecode = PredictorSetupDecode;
  758. sp->setupencode = tif->tif_setupencode;
  759. tif->tif_setupencode = PredictorSetupEncode;
  760. sp->predictor = 1; /* default value */
  761. sp->encodepfunc = NULL; /* no predictor routine */
  762. sp->decodepfunc = NULL; /* no predictor routine */
  763. return 1;
  764. }
  765. int
  766. TIFFPredictorCleanup(TIFF* tif)
  767. {
  768. TIFFPredictorState* sp = PredictorState(tif);
  769. assert(sp != 0);
  770. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  771. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  772. tif->tif_tagmethods.printdir = sp->printdir;
  773. tif->tif_setupdecode = sp->setupdecode;
  774. tif->tif_setupencode = sp->setupencode;
  775. return 1;
  776. }
  777. /* vim: set ts=8 sts=8 sw=8 noet: */
  778. /*
  779. * Local Variables:
  780. * mode: c
  781. * c-basic-offset: 8
  782. * fill-column: 78
  783. * End:
  784. */