tif_predict.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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 "tif_predict.h"
  30. #include "tiffiop.h"
  31. #define PredictorState(tif) ((TIFFPredictorState *)(tif)->tif_data)
  32. static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  33. static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  34. static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  35. static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  36. static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  37. static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  38. static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  39. static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  40. static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  41. static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  42. static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  43. static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  44. static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  45. static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  46. static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  47. static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc);
  48. static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
  49. uint16_t s);
  50. static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
  51. uint16_t s);
  52. static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s);
  53. static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
  54. uint16_t s);
  55. static int PredictorSetup(TIFF *tif)
  56. {
  57. static const char module[] = "PredictorSetup";
  58. TIFFPredictorState *sp = PredictorState(tif);
  59. TIFFDirectory *td = &tif->tif_dir;
  60. switch (sp->predictor) /* no differencing */
  61. {
  62. case PREDICTOR_NONE:
  63. return 1;
  64. case PREDICTOR_HORIZONTAL:
  65. if (td->td_bitspersample != 8 && td->td_bitspersample != 16 &&
  66. td->td_bitspersample != 32 && td->td_bitspersample != 64)
  67. {
  68. TIFFErrorExtR(tif, module,
  69. "Horizontal differencing \"Predictor\" not "
  70. "supported with %" PRIu16 "-bit samples",
  71. td->td_bitspersample);
  72. return 0;
  73. }
  74. break;
  75. case PREDICTOR_FLOATINGPOINT:
  76. if (td->td_sampleformat != SAMPLEFORMAT_IEEEFP)
  77. {
  78. TIFFErrorExtR(
  79. tif, module,
  80. "Floating point \"Predictor\" not supported with %" PRIu16
  81. " data format",
  82. td->td_sampleformat);
  83. return 0;
  84. }
  85. if (td->td_bitspersample != 16 && td->td_bitspersample != 24 &&
  86. td->td_bitspersample != 32 && td->td_bitspersample != 64)
  87. { /* Should 64 be allowed? */
  88. TIFFErrorExtR(
  89. tif, module,
  90. "Floating point \"Predictor\" not supported with %" PRIu16
  91. "-bit samples",
  92. td->td_bitspersample);
  93. return 0;
  94. }
  95. break;
  96. default:
  97. TIFFErrorExtR(tif, module, "\"Predictor\" value %d not supported",
  98. sp->predictor);
  99. return 0;
  100. }
  101. sp->stride =
  102. (td->td_planarconfig == PLANARCONFIG_CONTIG ? td->td_samplesperpixel
  103. : 1);
  104. /*
  105. * Calculate the scanline/tile-width size in bytes.
  106. */
  107. if (isTiled(tif))
  108. sp->rowsize = TIFFTileRowSize(tif);
  109. else
  110. sp->rowsize = TIFFScanlineSize(tif);
  111. if (sp->rowsize == 0)
  112. return 0;
  113. return 1;
  114. }
  115. static int PredictorSetupDecode(TIFF *tif)
  116. {
  117. TIFFPredictorState *sp = PredictorState(tif);
  118. TIFFDirectory *td = &tif->tif_dir;
  119. /* Note: when PredictorSetup() fails, the effets of setupdecode() */
  120. /* will not be "canceled" so setupdecode() might be robust to */
  121. /* be called several times. */
  122. if (!(*sp->setupdecode)(tif) || !PredictorSetup(tif))
  123. return 0;
  124. if (sp->predictor == 2)
  125. {
  126. switch (td->td_bitspersample)
  127. {
  128. case 8:
  129. sp->decodepfunc = horAcc8;
  130. break;
  131. case 16:
  132. sp->decodepfunc = horAcc16;
  133. break;
  134. case 32:
  135. sp->decodepfunc = horAcc32;
  136. break;
  137. case 64:
  138. sp->decodepfunc = horAcc64;
  139. break;
  140. }
  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. * If the data is horizontally differenced 16-bit data that
  156. * requires byte-swapping, then it must be byte swapped before
  157. * the accumulation step. We do this with a special-purpose
  158. * routine and override the normal post decoding logic that
  159. * the library setup when the directory was read.
  160. */
  161. if (tif->tif_flags & TIFF_SWAB)
  162. {
  163. if (sp->decodepfunc == horAcc16)
  164. {
  165. sp->decodepfunc = swabHorAcc16;
  166. tif->tif_postdecode = _TIFFNoPostDecode;
  167. }
  168. else if (sp->decodepfunc == horAcc32)
  169. {
  170. sp->decodepfunc = swabHorAcc32;
  171. tif->tif_postdecode = _TIFFNoPostDecode;
  172. }
  173. else if (sp->decodepfunc == horAcc64)
  174. {
  175. sp->decodepfunc = swabHorAcc64;
  176. tif->tif_postdecode = _TIFFNoPostDecode;
  177. }
  178. }
  179. }
  180. else if (sp->predictor == 3)
  181. {
  182. sp->decodepfunc = fpAcc;
  183. /*
  184. * Override default decoding method with one that does the
  185. * predictor stuff.
  186. */
  187. if (tif->tif_decoderow != PredictorDecodeRow)
  188. {
  189. sp->decoderow = tif->tif_decoderow;
  190. tif->tif_decoderow = PredictorDecodeRow;
  191. sp->decodestrip = tif->tif_decodestrip;
  192. tif->tif_decodestrip = PredictorDecodeTile;
  193. sp->decodetile = tif->tif_decodetile;
  194. tif->tif_decodetile = PredictorDecodeTile;
  195. }
  196. /*
  197. * The data should not be swapped outside of the floating
  198. * point predictor, the accumulation routine should return
  199. * byres in the native order.
  200. */
  201. if (tif->tif_flags & TIFF_SWAB)
  202. {
  203. tif->tif_postdecode = _TIFFNoPostDecode;
  204. }
  205. /*
  206. * Allocate buffer to keep the decoded bytes before
  207. * rearranging in the right order
  208. */
  209. }
  210. return 1;
  211. }
  212. static int PredictorSetupEncode(TIFF *tif)
  213. {
  214. TIFFPredictorState *sp = PredictorState(tif);
  215. TIFFDirectory *td = &tif->tif_dir;
  216. if (!(*sp->setupencode)(tif) || !PredictorSetup(tif))
  217. return 0;
  218. if (sp->predictor == 2)
  219. {
  220. switch (td->td_bitspersample)
  221. {
  222. case 8:
  223. sp->encodepfunc = horDiff8;
  224. break;
  225. case 16:
  226. sp->encodepfunc = horDiff16;
  227. break;
  228. case 32:
  229. sp->encodepfunc = horDiff32;
  230. break;
  231. case 64:
  232. sp->encodepfunc = horDiff64;
  233. break;
  234. }
  235. /*
  236. * Override default encoding method with one that does the
  237. * predictor stuff.
  238. */
  239. if (tif->tif_encoderow != PredictorEncodeRow)
  240. {
  241. sp->encoderow = tif->tif_encoderow;
  242. tif->tif_encoderow = PredictorEncodeRow;
  243. sp->encodestrip = tif->tif_encodestrip;
  244. tif->tif_encodestrip = PredictorEncodeTile;
  245. sp->encodetile = tif->tif_encodetile;
  246. tif->tif_encodetile = PredictorEncodeTile;
  247. }
  248. /*
  249. * If the data is horizontally differenced 16-bit data that
  250. * requires byte-swapping, then it must be byte swapped after
  251. * the differentiation step. We do this with a special-purpose
  252. * routine and override the normal post decoding logic that
  253. * the library setup when the directory was read.
  254. */
  255. if (tif->tif_flags & TIFF_SWAB)
  256. {
  257. if (sp->encodepfunc == horDiff16)
  258. {
  259. sp->encodepfunc = swabHorDiff16;
  260. tif->tif_postdecode = _TIFFNoPostDecode;
  261. }
  262. else if (sp->encodepfunc == horDiff32)
  263. {
  264. sp->encodepfunc = swabHorDiff32;
  265. tif->tif_postdecode = _TIFFNoPostDecode;
  266. }
  267. else if (sp->encodepfunc == horDiff64)
  268. {
  269. sp->encodepfunc = swabHorDiff64;
  270. tif->tif_postdecode = _TIFFNoPostDecode;
  271. }
  272. }
  273. }
  274. else if (sp->predictor == 3)
  275. {
  276. sp->encodepfunc = fpDiff;
  277. /*
  278. * Override default encoding method with one that does the
  279. * predictor stuff.
  280. */
  281. if (tif->tif_encoderow != PredictorEncodeRow)
  282. {
  283. sp->encoderow = tif->tif_encoderow;
  284. tif->tif_encoderow = PredictorEncodeRow;
  285. sp->encodestrip = tif->tif_encodestrip;
  286. tif->tif_encodestrip = PredictorEncodeTile;
  287. sp->encodetile = tif->tif_encodetile;
  288. tif->tif_encodetile = PredictorEncodeTile;
  289. }
  290. }
  291. return 1;
  292. }
  293. #define REPEAT4(n, op) \
  294. switch (n) \
  295. { \
  296. default: \
  297. { \
  298. tmsize_t i; \
  299. for (i = n - 4; i > 0; i--) \
  300. { \
  301. op; \
  302. } \
  303. } /*-fallthrough*/ \
  304. case 4: \
  305. op; /*-fallthrough*/ \
  306. case 3: \
  307. op; /*-fallthrough*/ \
  308. case 2: \
  309. op; /*-fallthrough*/ \
  310. case 1: \
  311. op; /*-fallthrough*/ \
  312. case 0:; \
  313. }
  314. /* Remarks related to C standard compliance in all below functions : */
  315. /* - to avoid any undefined behavior, we only operate on unsigned types */
  316. /* since the behavior of "overflows" is defined (wrap over) */
  317. /* - when storing into the byte stream, we explicitly mask with 0xff so */
  318. /* as to make icc -check=conversions happy (not necessary by the standard) */
  319. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  320. static int horAcc8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  321. {
  322. tmsize_t stride = PredictorState(tif)->stride;
  323. unsigned char *cp = (unsigned char *)cp0;
  324. if ((cc % stride) != 0)
  325. {
  326. TIFFErrorExtR(tif, "horAcc8", "%s", "(cc%stride)!=0");
  327. return 0;
  328. }
  329. if (cc > stride)
  330. {
  331. /*
  332. * Pipeline the most common cases.
  333. */
  334. if (stride == 3)
  335. {
  336. unsigned int cr = cp[0];
  337. unsigned int cg = cp[1];
  338. unsigned int cb = cp[2];
  339. tmsize_t i = stride;
  340. for (; i < cc; i += stride)
  341. {
  342. cp[i + 0] = (unsigned char)((cr += cp[i + 0]) & 0xff);
  343. cp[i + 1] = (unsigned char)((cg += cp[i + 1]) & 0xff);
  344. cp[i + 2] = (unsigned char)((cb += cp[i + 2]) & 0xff);
  345. }
  346. }
  347. else if (stride == 4)
  348. {
  349. unsigned int cr = cp[0];
  350. unsigned int cg = cp[1];
  351. unsigned int cb = cp[2];
  352. unsigned int ca = cp[3];
  353. tmsize_t i = stride;
  354. for (; i < cc; i += stride)
  355. {
  356. cp[i + 0] = (unsigned char)((cr += cp[i + 0]) & 0xff);
  357. cp[i + 1] = (unsigned char)((cg += cp[i + 1]) & 0xff);
  358. cp[i + 2] = (unsigned char)((cb += cp[i + 2]) & 0xff);
  359. cp[i + 3] = (unsigned char)((ca += cp[i + 3]) & 0xff);
  360. }
  361. }
  362. else
  363. {
  364. cc -= stride;
  365. do
  366. {
  367. REPEAT4(stride,
  368. cp[stride] = (unsigned char)((cp[stride] + *cp) & 0xff);
  369. cp++)
  370. cc -= stride;
  371. } while (cc > 0);
  372. }
  373. }
  374. return 1;
  375. }
  376. static int swabHorAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  377. {
  378. uint16_t *wp = (uint16_t *)cp0;
  379. tmsize_t wc = cc / 2;
  380. TIFFSwabArrayOfShort(wp, wc);
  381. return horAcc16(tif, cp0, cc);
  382. }
  383. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  384. static int horAcc16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  385. {
  386. tmsize_t stride = PredictorState(tif)->stride;
  387. uint16_t *wp = (uint16_t *)cp0;
  388. tmsize_t wc = cc / 2;
  389. if ((cc % (2 * stride)) != 0)
  390. {
  391. TIFFErrorExtR(tif, "horAcc16", "%s", "cc%(2*stride))!=0");
  392. return 0;
  393. }
  394. if (wc > stride)
  395. {
  396. wc -= stride;
  397. do
  398. {
  399. REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] +
  400. (unsigned int)wp[0]) &
  401. 0xffff);
  402. wp++)
  403. wc -= stride;
  404. } while (wc > 0);
  405. }
  406. return 1;
  407. }
  408. static int swabHorAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  409. {
  410. uint32_t *wp = (uint32_t *)cp0;
  411. tmsize_t wc = cc / 4;
  412. TIFFSwabArrayOfLong(wp, wc);
  413. return horAcc32(tif, cp0, cc);
  414. }
  415. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  416. static int horAcc32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  417. {
  418. tmsize_t stride = PredictorState(tif)->stride;
  419. uint32_t *wp = (uint32_t *)cp0;
  420. tmsize_t wc = cc / 4;
  421. if ((cc % (4 * stride)) != 0)
  422. {
  423. TIFFErrorExtR(tif, "horAcc32", "%s", "cc%(4*stride))!=0");
  424. return 0;
  425. }
  426. if (wc > stride)
  427. {
  428. wc -= stride;
  429. do
  430. {
  431. REPEAT4(stride, wp[stride] += wp[0]; wp++)
  432. wc -= stride;
  433. } while (wc > 0);
  434. }
  435. return 1;
  436. }
  437. static int swabHorAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  438. {
  439. uint64_t *wp = (uint64_t *)cp0;
  440. tmsize_t wc = cc / 8;
  441. TIFFSwabArrayOfLong8(wp, wc);
  442. return horAcc64(tif, cp0, cc);
  443. }
  444. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  445. static int horAcc64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  446. {
  447. tmsize_t stride = PredictorState(tif)->stride;
  448. uint64_t *wp = (uint64_t *)cp0;
  449. tmsize_t wc = cc / 8;
  450. if ((cc % (8 * stride)) != 0)
  451. {
  452. TIFFErrorExtR(tif, "horAcc64", "%s", "cc%(8*stride))!=0");
  453. return 0;
  454. }
  455. if (wc > stride)
  456. {
  457. wc -= stride;
  458. do
  459. {
  460. REPEAT4(stride, wp[stride] += wp[0]; wp++)
  461. wc -= stride;
  462. } while (wc > 0);
  463. }
  464. return 1;
  465. }
  466. /*
  467. * Floating point predictor accumulation routine.
  468. */
  469. static int fpAcc(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  470. {
  471. tmsize_t stride = PredictorState(tif)->stride;
  472. uint32_t bps = tif->tif_dir.td_bitspersample / 8;
  473. tmsize_t wc = cc / bps;
  474. tmsize_t count = cc;
  475. uint8_t *cp = (uint8_t *)cp0;
  476. uint8_t *tmp;
  477. if (cc % (bps * stride) != 0)
  478. {
  479. TIFFErrorExtR(tif, "fpAcc", "%s", "cc%(bps*stride))!=0");
  480. return 0;
  481. }
  482. tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
  483. if (!tmp)
  484. return 0;
  485. while (count > stride)
  486. {
  487. REPEAT4(stride,
  488. cp[stride] = (unsigned char)((cp[stride] + cp[0]) & 0xff);
  489. cp++)
  490. count -= stride;
  491. }
  492. _TIFFmemcpy(tmp, cp0, cc);
  493. cp = (uint8_t *)cp0;
  494. for (count = 0; count < wc; count++)
  495. {
  496. uint32_t byte;
  497. for (byte = 0; byte < bps; byte++)
  498. {
  499. #if WORDS_BIGENDIAN
  500. cp[bps * count + byte] = tmp[byte * wc + count];
  501. #else
  502. cp[bps * count + byte] = tmp[(bps - byte - 1) * wc + count];
  503. #endif
  504. }
  505. }
  506. _TIFFfreeExt(tif, tmp);
  507. return 1;
  508. }
  509. /*
  510. * Decode a scanline and apply the predictor routine.
  511. */
  512. static int PredictorDecodeRow(TIFF *tif, uint8_t *op0, tmsize_t occ0,
  513. uint16_t s)
  514. {
  515. TIFFPredictorState *sp = PredictorState(tif);
  516. assert(sp != NULL);
  517. assert(sp->decoderow != NULL);
  518. assert(sp->decodepfunc != NULL);
  519. if ((*sp->decoderow)(tif, op0, occ0, s))
  520. {
  521. return (*sp->decodepfunc)(tif, op0, occ0);
  522. }
  523. else
  524. return 0;
  525. }
  526. /*
  527. * Decode a tile/strip and apply the predictor routine.
  528. * Note that horizontal differencing must be done on a
  529. * row-by-row basis. The width of a "row" has already
  530. * been calculated at pre-decode time according to the
  531. * strip/tile dimensions.
  532. */
  533. static int PredictorDecodeTile(TIFF *tif, uint8_t *op0, tmsize_t occ0,
  534. uint16_t s)
  535. {
  536. TIFFPredictorState *sp = PredictorState(tif);
  537. assert(sp != NULL);
  538. assert(sp->decodetile != NULL);
  539. if ((*sp->decodetile)(tif, op0, occ0, s))
  540. {
  541. tmsize_t rowsize = sp->rowsize;
  542. assert(rowsize > 0);
  543. if ((occ0 % rowsize) != 0)
  544. {
  545. TIFFErrorExtR(tif, "PredictorDecodeTile", "%s",
  546. "occ0%rowsize != 0");
  547. return 0;
  548. }
  549. assert(sp->decodepfunc != NULL);
  550. while (occ0 > 0)
  551. {
  552. if (!(*sp->decodepfunc)(tif, op0, rowsize))
  553. return 0;
  554. occ0 -= rowsize;
  555. op0 += rowsize;
  556. }
  557. return 1;
  558. }
  559. else
  560. return 0;
  561. }
  562. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  563. static int horDiff8(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  564. {
  565. TIFFPredictorState *sp = PredictorState(tif);
  566. tmsize_t stride = sp->stride;
  567. unsigned char *cp = (unsigned char *)cp0;
  568. if ((cc % stride) != 0)
  569. {
  570. TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%stride)!=0");
  571. return 0;
  572. }
  573. if (cc > stride)
  574. {
  575. cc -= stride;
  576. /*
  577. * Pipeline the most common cases.
  578. */
  579. if (stride == 3)
  580. {
  581. unsigned int r1, g1, b1;
  582. unsigned int r2 = cp[0];
  583. unsigned int g2 = cp[1];
  584. unsigned int b2 = cp[2];
  585. do
  586. {
  587. r1 = cp[3];
  588. cp[3] = (unsigned char)((r1 - r2) & 0xff);
  589. r2 = r1;
  590. g1 = cp[4];
  591. cp[4] = (unsigned char)((g1 - g2) & 0xff);
  592. g2 = g1;
  593. b1 = cp[5];
  594. cp[5] = (unsigned char)((b1 - b2) & 0xff);
  595. b2 = b1;
  596. cp += 3;
  597. } while ((cc -= 3) > 0);
  598. }
  599. else if (stride == 4)
  600. {
  601. unsigned int r1, g1, b1, a1;
  602. unsigned int r2 = cp[0];
  603. unsigned int g2 = cp[1];
  604. unsigned int b2 = cp[2];
  605. unsigned int a2 = cp[3];
  606. do
  607. {
  608. r1 = cp[4];
  609. cp[4] = (unsigned char)((r1 - r2) & 0xff);
  610. r2 = r1;
  611. g1 = cp[5];
  612. cp[5] = (unsigned char)((g1 - g2) & 0xff);
  613. g2 = g1;
  614. b1 = cp[6];
  615. cp[6] = (unsigned char)((b1 - b2) & 0xff);
  616. b2 = b1;
  617. a1 = cp[7];
  618. cp[7] = (unsigned char)((a1 - a2) & 0xff);
  619. a2 = a1;
  620. cp += 4;
  621. } while ((cc -= 4) > 0);
  622. }
  623. else
  624. {
  625. cp += cc - 1;
  626. do
  627. {
  628. REPEAT4(stride,
  629. cp[stride] =
  630. (unsigned char)((cp[stride] - cp[0]) & 0xff);
  631. cp--)
  632. } while ((cc -= stride) > 0);
  633. }
  634. }
  635. return 1;
  636. }
  637. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  638. static int horDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  639. {
  640. TIFFPredictorState *sp = PredictorState(tif);
  641. tmsize_t stride = sp->stride;
  642. uint16_t *wp = (uint16_t *)cp0;
  643. tmsize_t wc = cc / 2;
  644. if ((cc % (2 * stride)) != 0)
  645. {
  646. TIFFErrorExtR(tif, "horDiff8", "%s", "(cc%(2*stride))!=0");
  647. return 0;
  648. }
  649. if (wc > stride)
  650. {
  651. wc -= stride;
  652. wp += wc - 1;
  653. do
  654. {
  655. REPEAT4(stride, wp[stride] = (uint16_t)(((unsigned int)wp[stride] -
  656. (unsigned int)wp[0]) &
  657. 0xffff);
  658. wp--)
  659. wc -= stride;
  660. } while (wc > 0);
  661. }
  662. return 1;
  663. }
  664. static int swabHorDiff16(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  665. {
  666. uint16_t *wp = (uint16_t *)cp0;
  667. tmsize_t wc = cc / 2;
  668. if (!horDiff16(tif, cp0, cc))
  669. return 0;
  670. TIFFSwabArrayOfShort(wp, wc);
  671. return 1;
  672. }
  673. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  674. static int horDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  675. {
  676. TIFFPredictorState *sp = PredictorState(tif);
  677. tmsize_t stride = sp->stride;
  678. uint32_t *wp = (uint32_t *)cp0;
  679. tmsize_t wc = cc / 4;
  680. if ((cc % (4 * stride)) != 0)
  681. {
  682. TIFFErrorExtR(tif, "horDiff32", "%s", "(cc%(4*stride))!=0");
  683. return 0;
  684. }
  685. if (wc > stride)
  686. {
  687. wc -= stride;
  688. wp += wc - 1;
  689. do
  690. {
  691. REPEAT4(stride, wp[stride] -= wp[0]; wp--)
  692. wc -= stride;
  693. } while (wc > 0);
  694. }
  695. return 1;
  696. }
  697. static int swabHorDiff32(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  698. {
  699. uint32_t *wp = (uint32_t *)cp0;
  700. tmsize_t wc = cc / 4;
  701. if (!horDiff32(tif, cp0, cc))
  702. return 0;
  703. TIFFSwabArrayOfLong(wp, wc);
  704. return 1;
  705. }
  706. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  707. static int horDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  708. {
  709. TIFFPredictorState *sp = PredictorState(tif);
  710. tmsize_t stride = sp->stride;
  711. uint64_t *wp = (uint64_t *)cp0;
  712. tmsize_t wc = cc / 8;
  713. if ((cc % (8 * stride)) != 0)
  714. {
  715. TIFFErrorExtR(tif, "horDiff64", "%s", "(cc%(8*stride))!=0");
  716. return 0;
  717. }
  718. if (wc > stride)
  719. {
  720. wc -= stride;
  721. wp += wc - 1;
  722. do
  723. {
  724. REPEAT4(stride, wp[stride] -= wp[0]; wp--)
  725. wc -= stride;
  726. } while (wc > 0);
  727. }
  728. return 1;
  729. }
  730. static int swabHorDiff64(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  731. {
  732. uint64_t *wp = (uint64_t *)cp0;
  733. tmsize_t wc = cc / 8;
  734. if (!horDiff64(tif, cp0, cc))
  735. return 0;
  736. TIFFSwabArrayOfLong8(wp, wc);
  737. return 1;
  738. }
  739. /*
  740. * Floating point predictor differencing routine.
  741. */
  742. TIFF_NOSANITIZE_UNSIGNED_INT_OVERFLOW
  743. static int fpDiff(TIFF *tif, uint8_t *cp0, tmsize_t cc)
  744. {
  745. tmsize_t stride = PredictorState(tif)->stride;
  746. uint32_t bps = tif->tif_dir.td_bitspersample / 8;
  747. tmsize_t wc = cc / bps;
  748. tmsize_t count;
  749. uint8_t *cp = (uint8_t *)cp0;
  750. uint8_t *tmp;
  751. if ((cc % (bps * stride)) != 0)
  752. {
  753. TIFFErrorExtR(tif, "fpDiff", "%s", "(cc%(bps*stride))!=0");
  754. return 0;
  755. }
  756. tmp = (uint8_t *)_TIFFmallocExt(tif, cc);
  757. if (!tmp)
  758. return 0;
  759. _TIFFmemcpy(tmp, cp0, cc);
  760. for (count = 0; count < wc; count++)
  761. {
  762. uint32_t byte;
  763. for (byte = 0; byte < bps; byte++)
  764. {
  765. #if WORDS_BIGENDIAN
  766. cp[byte * wc + count] = tmp[bps * count + byte];
  767. #else
  768. cp[(bps - byte - 1) * wc + count] = tmp[bps * count + byte];
  769. #endif
  770. }
  771. }
  772. _TIFFfreeExt(tif, tmp);
  773. cp = (uint8_t *)cp0;
  774. cp += cc - stride - 1;
  775. for (count = cc; count > stride; count -= stride)
  776. REPEAT4(stride,
  777. cp[stride] = (unsigned char)((cp[stride] - cp[0]) & 0xff);
  778. cp--)
  779. return 1;
  780. }
  781. static int PredictorEncodeRow(TIFF *tif, uint8_t *bp, tmsize_t cc, uint16_t s)
  782. {
  783. TIFFPredictorState *sp = PredictorState(tif);
  784. assert(sp != NULL);
  785. assert(sp->encodepfunc != NULL);
  786. assert(sp->encoderow != NULL);
  787. /* XXX horizontal differencing alters user's data XXX */
  788. if (!(*sp->encodepfunc)(tif, bp, cc))
  789. return 0;
  790. return (*sp->encoderow)(tif, bp, cc, s);
  791. }
  792. static int PredictorEncodeTile(TIFF *tif, uint8_t *bp0, tmsize_t cc0,
  793. uint16_t s)
  794. {
  795. static const char module[] = "PredictorEncodeTile";
  796. TIFFPredictorState *sp = PredictorState(tif);
  797. uint8_t *working_copy;
  798. tmsize_t cc = cc0, rowsize;
  799. unsigned char *bp;
  800. int result_code;
  801. assert(sp != NULL);
  802. assert(sp->encodepfunc != NULL);
  803. assert(sp->encodetile != NULL);
  804. /*
  805. * Do predictor manipulation in a working buffer to avoid altering
  806. * the callers buffer. http://trac.osgeo.org/gdal/ticket/1965
  807. */
  808. working_copy = (uint8_t *)_TIFFmallocExt(tif, cc0);
  809. if (working_copy == NULL)
  810. {
  811. TIFFErrorExtR(tif, module,
  812. "Out of memory allocating %" PRId64 " byte temp buffer.",
  813. (int64_t)cc0);
  814. return 0;
  815. }
  816. memcpy(working_copy, bp0, cc0);
  817. bp = working_copy;
  818. rowsize = sp->rowsize;
  819. assert(rowsize > 0);
  820. if ((cc0 % rowsize) != 0)
  821. {
  822. TIFFErrorExtR(tif, "PredictorEncodeTile", "%s", "(cc0%rowsize)!=0");
  823. _TIFFfreeExt(tif, working_copy);
  824. return 0;
  825. }
  826. while (cc > 0)
  827. {
  828. (*sp->encodepfunc)(tif, bp, rowsize);
  829. cc -= rowsize;
  830. bp += rowsize;
  831. }
  832. result_code = (*sp->encodetile)(tif, working_copy, cc0, s);
  833. _TIFFfreeExt(tif, working_copy);
  834. return result_code;
  835. }
  836. #define FIELD_PREDICTOR (FIELD_CODEC + 0) /* XXX */
  837. static const TIFFField predictFields[] = {
  838. {TIFFTAG_PREDICTOR, 1, 1, TIFF_SHORT, 0, TIFF_SETGET_UINT16,
  839. TIFF_SETGET_UINT16, FIELD_PREDICTOR, FALSE, FALSE, "Predictor", NULL},
  840. };
  841. static int PredictorVSetField(TIFF *tif, uint32_t tag, va_list ap)
  842. {
  843. TIFFPredictorState *sp = PredictorState(tif);
  844. assert(sp != NULL);
  845. assert(sp->vsetparent != NULL);
  846. switch (tag)
  847. {
  848. case TIFFTAG_PREDICTOR:
  849. sp->predictor = (uint16_t)va_arg(ap, uint16_vap);
  850. TIFFSetFieldBit(tif, FIELD_PREDICTOR);
  851. break;
  852. default:
  853. return (*sp->vsetparent)(tif, tag, ap);
  854. }
  855. tif->tif_flags |= TIFF_DIRTYDIRECT;
  856. return 1;
  857. }
  858. static int PredictorVGetField(TIFF *tif, uint32_t tag, va_list ap)
  859. {
  860. TIFFPredictorState *sp = PredictorState(tif);
  861. assert(sp != NULL);
  862. assert(sp->vgetparent != NULL);
  863. switch (tag)
  864. {
  865. case TIFFTAG_PREDICTOR:
  866. *va_arg(ap, uint16_t *) = (uint16_t)sp->predictor;
  867. break;
  868. default:
  869. return (*sp->vgetparent)(tif, tag, ap);
  870. }
  871. return 1;
  872. }
  873. static void PredictorPrintDir(TIFF *tif, FILE *fd, long flags)
  874. {
  875. TIFFPredictorState *sp = PredictorState(tif);
  876. (void)flags;
  877. if (TIFFFieldSet(tif, FIELD_PREDICTOR))
  878. {
  879. fprintf(fd, " Predictor: ");
  880. switch (sp->predictor)
  881. {
  882. case 1:
  883. fprintf(fd, "none ");
  884. break;
  885. case 2:
  886. fprintf(fd, "horizontal differencing ");
  887. break;
  888. case 3:
  889. fprintf(fd, "floating point predictor ");
  890. break;
  891. }
  892. fprintf(fd, "%d (0x%x)\n", sp->predictor, sp->predictor);
  893. }
  894. if (sp->printdir)
  895. (*sp->printdir)(tif, fd, flags);
  896. }
  897. int TIFFPredictorInit(TIFF *tif)
  898. {
  899. TIFFPredictorState *sp = PredictorState(tif);
  900. assert(sp != 0);
  901. /*
  902. * Merge codec-specific tag information.
  903. */
  904. if (!_TIFFMergeFields(tif, predictFields, TIFFArrayCount(predictFields)))
  905. {
  906. TIFFErrorExtR(tif, "TIFFPredictorInit",
  907. "Merging Predictor codec-specific tags failed");
  908. return 0;
  909. }
  910. /*
  911. * Override parent get/set field methods.
  912. */
  913. sp->vgetparent = tif->tif_tagmethods.vgetfield;
  914. tif->tif_tagmethods.vgetfield =
  915. PredictorVGetField; /* hook for predictor tag */
  916. sp->vsetparent = tif->tif_tagmethods.vsetfield;
  917. tif->tif_tagmethods.vsetfield =
  918. PredictorVSetField; /* hook for predictor tag */
  919. sp->printdir = tif->tif_tagmethods.printdir;
  920. tif->tif_tagmethods.printdir =
  921. PredictorPrintDir; /* hook for predictor tag */
  922. sp->setupdecode = tif->tif_setupdecode;
  923. tif->tif_setupdecode = PredictorSetupDecode;
  924. sp->setupencode = tif->tif_setupencode;
  925. tif->tif_setupencode = PredictorSetupEncode;
  926. sp->predictor = 1; /* default value */
  927. sp->encodepfunc = NULL; /* no predictor routine */
  928. sp->decodepfunc = NULL; /* no predictor routine */
  929. return 1;
  930. }
  931. int TIFFPredictorCleanup(TIFF *tif)
  932. {
  933. TIFFPredictorState *sp = PredictorState(tif);
  934. assert(sp != 0);
  935. tif->tif_tagmethods.vgetfield = sp->vgetparent;
  936. tif->tif_tagmethods.vsetfield = sp->vsetparent;
  937. tif->tif_tagmethods.printdir = sp->printdir;
  938. tif->tif_setupdecode = sp->setupdecode;
  939. tif->tif_setupencode = sp->setupencode;
  940. return 1;
  941. }