tif_predict.c 34 KB

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