tif_aux.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (c) 1991-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. * Auxiliary Support Routines.
  28. */
  29. #include "tif_predict.h"
  30. #include "tiffiop.h"
  31. #include <float.h>
  32. #include <math.h>
  33. uint32_t _TIFFMultiply32(TIFF *tif, uint32_t first, uint32_t second,
  34. const char *where)
  35. {
  36. if (second && first > UINT32_MAX / second)
  37. {
  38. TIFFErrorExtR(tif, where, "Integer overflow in %s", where);
  39. return 0;
  40. }
  41. return first * second;
  42. }
  43. uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second,
  44. const char *where)
  45. {
  46. if (second && first > UINT64_MAX / second)
  47. {
  48. TIFFErrorExtR(tif, where, "Integer overflow in %s", where);
  49. return 0;
  50. }
  51. return first * second;
  52. }
  53. tmsize_t _TIFFMultiplySSize(TIFF *tif, tmsize_t first, tmsize_t second,
  54. const char *where)
  55. {
  56. if (first <= 0 || second <= 0)
  57. {
  58. if (tif != NULL && where != NULL)
  59. {
  60. TIFFErrorExtR(tif, where,
  61. "Invalid argument to _TIFFMultiplySSize() in %s",
  62. where);
  63. }
  64. return 0;
  65. }
  66. if (first > TIFF_TMSIZE_T_MAX / second)
  67. {
  68. if (tif != NULL && where != NULL)
  69. {
  70. TIFFErrorExtR(tif, where, "Integer overflow in %s", where);
  71. }
  72. return 0;
  73. }
  74. return first * second;
  75. }
  76. tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
  77. {
  78. if (val > (uint64_t)TIFF_TMSIZE_T_MAX)
  79. {
  80. if (tif != NULL && module != NULL)
  81. {
  82. TIFFErrorExtR(tif, module, "Integer overflow");
  83. }
  84. return 0;
  85. }
  86. return (tmsize_t)val;
  87. }
  88. void *_TIFFCheckRealloc(TIFF *tif, void *buffer, tmsize_t nmemb,
  89. tmsize_t elem_size, const char *what)
  90. {
  91. void *cp = NULL;
  92. tmsize_t count = _TIFFMultiplySSize(tif, nmemb, elem_size, NULL);
  93. /*
  94. * Check for integer overflow.
  95. */
  96. if (count != 0)
  97. {
  98. cp = _TIFFreallocExt(tif, buffer, count);
  99. }
  100. if (cp == NULL)
  101. {
  102. TIFFErrorExtR(tif, tif->tif_name,
  103. "Failed to allocate memory for %s "
  104. "(%" TIFF_SSIZE_FORMAT " elements of %" TIFF_SSIZE_FORMAT
  105. " bytes each)",
  106. what, nmemb, elem_size);
  107. }
  108. return cp;
  109. }
  110. void *_TIFFCheckMalloc(TIFF *tif, tmsize_t nmemb, tmsize_t elem_size,
  111. const char *what)
  112. {
  113. return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
  114. }
  115. static int TIFFDefaultTransferFunction(TIFF *tif, TIFFDirectory *td)
  116. {
  117. uint16_t **tf = td->td_transferfunction;
  118. tmsize_t i, n, nbytes;
  119. tf[0] = tf[1] = tf[2] = 0;
  120. if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
  121. return 0;
  122. n = ((tmsize_t)1) << td->td_bitspersample;
  123. nbytes = n * sizeof(uint16_t);
  124. tf[0] = (uint16_t *)_TIFFmallocExt(tif, nbytes);
  125. if (tf[0] == NULL)
  126. return 0;
  127. tf[0][0] = 0;
  128. for (i = 1; i < n; i++)
  129. {
  130. double t = (double)i / ((double)n - 1.);
  131. tf[0][i] = (uint16_t)floor(65535. * pow(t, 2.2) + .5);
  132. }
  133. if (td->td_samplesperpixel - td->td_extrasamples > 1)
  134. {
  135. tf[1] = (uint16_t *)_TIFFmallocExt(tif, nbytes);
  136. if (tf[1] == NULL)
  137. goto bad;
  138. _TIFFmemcpy(tf[1], tf[0], nbytes);
  139. tf[2] = (uint16_t *)_TIFFmallocExt(tif, nbytes);
  140. if (tf[2] == NULL)
  141. goto bad;
  142. _TIFFmemcpy(tf[2], tf[0], nbytes);
  143. }
  144. return 1;
  145. bad:
  146. if (tf[0])
  147. _TIFFfreeExt(tif, tf[0]);
  148. if (tf[1])
  149. _TIFFfreeExt(tif, tf[1]);
  150. if (tf[2])
  151. _TIFFfreeExt(tif, tf[2]);
  152. tf[0] = tf[1] = tf[2] = 0;
  153. return 0;
  154. }
  155. static int TIFFDefaultRefBlackWhite(TIFF *tif, TIFFDirectory *td)
  156. {
  157. int i;
  158. td->td_refblackwhite = (float *)_TIFFmallocExt(tif, 6 * sizeof(float));
  159. if (td->td_refblackwhite == NULL)
  160. return 0;
  161. if (td->td_photometric == PHOTOMETRIC_YCBCR)
  162. {
  163. /*
  164. * YCbCr (Class Y) images must have the ReferenceBlackWhite
  165. * tag set. Fix the broken images, which lacks that tag.
  166. */
  167. td->td_refblackwhite[0] = 0.0F;
  168. td->td_refblackwhite[1] = td->td_refblackwhite[3] =
  169. td->td_refblackwhite[5] = 255.0F;
  170. td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
  171. }
  172. else
  173. {
  174. /*
  175. * Assume RGB (Class R)
  176. */
  177. for (i = 0; i < 3; i++)
  178. {
  179. td->td_refblackwhite[2 * i + 0] = 0;
  180. td->td_refblackwhite[2 * i + 1] =
  181. (float)((1L << td->td_bitspersample) - 1L);
  182. }
  183. }
  184. return 1;
  185. }
  186. /*
  187. * Like TIFFGetField, but return any default
  188. * value if the tag is not present in the directory.
  189. *
  190. * NB: We use the value in the directory, rather than
  191. * explicit values so that defaults exist only one
  192. * place in the library -- in TIFFDefaultDirectory.
  193. */
  194. int TIFFVGetFieldDefaulted(TIFF *tif, uint32_t tag, va_list ap)
  195. {
  196. TIFFDirectory *td = &tif->tif_dir;
  197. if (TIFFVGetField(tif, tag, ap))
  198. return (1);
  199. switch (tag)
  200. {
  201. case TIFFTAG_SUBFILETYPE:
  202. *va_arg(ap, uint32_t *) = td->td_subfiletype;
  203. return (1);
  204. case TIFFTAG_BITSPERSAMPLE:
  205. *va_arg(ap, uint16_t *) = td->td_bitspersample;
  206. return (1);
  207. case TIFFTAG_THRESHHOLDING:
  208. *va_arg(ap, uint16_t *) = td->td_threshholding;
  209. return (1);
  210. case TIFFTAG_FILLORDER:
  211. *va_arg(ap, uint16_t *) = td->td_fillorder;
  212. return (1);
  213. case TIFFTAG_ORIENTATION:
  214. *va_arg(ap, uint16_t *) = td->td_orientation;
  215. return (1);
  216. case TIFFTAG_SAMPLESPERPIXEL:
  217. *va_arg(ap, uint16_t *) = td->td_samplesperpixel;
  218. return (1);
  219. case TIFFTAG_ROWSPERSTRIP:
  220. *va_arg(ap, uint32_t *) = td->td_rowsperstrip;
  221. return (1);
  222. case TIFFTAG_MINSAMPLEVALUE:
  223. *va_arg(ap, uint16_t *) = td->td_minsamplevalue;
  224. return (1);
  225. case TIFFTAG_MAXSAMPLEVALUE:
  226. {
  227. uint16_t maxsamplevalue;
  228. /* td_bitspersample=1 is always set in TIFFDefaultDirectory().
  229. * Therefore, td_maxsamplevalue has to be re-calculated in
  230. * TIFFGetFieldDefaulted(). */
  231. if (td->td_bitspersample > 0)
  232. {
  233. /* This shift operation into a uint16_t limits the value to
  234. * 65535 even if td_bitspersamle is > 16 */
  235. if (td->td_bitspersample <= 16)
  236. {
  237. maxsamplevalue = (1 << td->td_bitspersample) -
  238. 1; /* 2**(BitsPerSample) - 1 */
  239. }
  240. else
  241. {
  242. maxsamplevalue = 65535;
  243. }
  244. }
  245. else
  246. {
  247. maxsamplevalue = 0;
  248. }
  249. *va_arg(ap, uint16_t *) = maxsamplevalue;
  250. return (1);
  251. }
  252. case TIFFTAG_PLANARCONFIG:
  253. *va_arg(ap, uint16_t *) = td->td_planarconfig;
  254. return (1);
  255. case TIFFTAG_RESOLUTIONUNIT:
  256. *va_arg(ap, uint16_t *) = td->td_resolutionunit;
  257. return (1);
  258. case TIFFTAG_PREDICTOR:
  259. {
  260. TIFFPredictorState *sp = (TIFFPredictorState *)tif->tif_data;
  261. if (sp == NULL)
  262. {
  263. TIFFErrorExtR(
  264. tif, tif->tif_name,
  265. "Cannot get \"Predictor\" tag as plugin is not configured");
  266. *va_arg(ap, uint16_t *) = 0;
  267. return 0;
  268. }
  269. *va_arg(ap, uint16_t *) = (uint16_t)sp->predictor;
  270. return 1;
  271. }
  272. case TIFFTAG_DOTRANGE:
  273. *va_arg(ap, uint16_t *) = 0;
  274. *va_arg(ap, uint16_t *) = (1 << td->td_bitspersample) - 1;
  275. return (1);
  276. case TIFFTAG_INKSET:
  277. *va_arg(ap, uint16_t *) = INKSET_CMYK;
  278. return 1;
  279. case TIFFTAG_NUMBEROFINKS:
  280. *va_arg(ap, uint16_t *) = 4;
  281. return (1);
  282. case TIFFTAG_EXTRASAMPLES:
  283. *va_arg(ap, uint16_t *) = td->td_extrasamples;
  284. *va_arg(ap, const uint16_t **) = td->td_sampleinfo;
  285. return (1);
  286. case TIFFTAG_MATTEING:
  287. *va_arg(ap, uint16_t *) =
  288. (td->td_extrasamples == 1 &&
  289. td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  290. return (1);
  291. case TIFFTAG_TILEDEPTH:
  292. *va_arg(ap, uint32_t *) = td->td_tiledepth;
  293. return (1);
  294. case TIFFTAG_DATATYPE:
  295. *va_arg(ap, uint16_t *) = td->td_sampleformat - 1;
  296. return (1);
  297. case TIFFTAG_SAMPLEFORMAT:
  298. *va_arg(ap, uint16_t *) = td->td_sampleformat;
  299. return (1);
  300. case TIFFTAG_IMAGEDEPTH:
  301. *va_arg(ap, uint32_t *) = td->td_imagedepth;
  302. return (1);
  303. case TIFFTAG_YCBCRCOEFFICIENTS:
  304. {
  305. /* defaults are from CCIR Recommendation 601-1 */
  306. static const float ycbcrcoeffs[] = {0.299f, 0.587f, 0.114f};
  307. *va_arg(ap, const float **) = ycbcrcoeffs;
  308. return 1;
  309. }
  310. case TIFFTAG_YCBCRSUBSAMPLING:
  311. *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[0];
  312. *va_arg(ap, uint16_t *) = td->td_ycbcrsubsampling[1];
  313. return (1);
  314. case TIFFTAG_YCBCRPOSITIONING:
  315. *va_arg(ap, uint16_t *) = td->td_ycbcrpositioning;
  316. return (1);
  317. case TIFFTAG_WHITEPOINT:
  318. {
  319. /* TIFF 6.0 specification tells that it is no default
  320. value for the WhitePoint, but AdobePhotoshop TIFF
  321. Technical Note tells that it should be CIE D50. */
  322. static const float whitepoint[] = {
  323. D50_X0 / (D50_X0 + D50_Y0 + D50_Z0),
  324. D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0)};
  325. *va_arg(ap, const float **) = whitepoint;
  326. return 1;
  327. }
  328. case TIFFTAG_TRANSFERFUNCTION:
  329. if (!td->td_transferfunction[0] &&
  330. !TIFFDefaultTransferFunction(tif, td))
  331. {
  332. TIFFErrorExtR(tif, tif->tif_name,
  333. "No space for \"TransferFunction\" tag");
  334. return (0);
  335. }
  336. *va_arg(ap, const uint16_t **) = td->td_transferfunction[0];
  337. if (td->td_samplesperpixel - td->td_extrasamples > 1)
  338. {
  339. *va_arg(ap, const uint16_t **) = td->td_transferfunction[1];
  340. *va_arg(ap, const uint16_t **) = td->td_transferfunction[2];
  341. }
  342. return (1);
  343. case TIFFTAG_REFERENCEBLACKWHITE:
  344. if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(tif, td))
  345. return (0);
  346. *va_arg(ap, const float **) = td->td_refblackwhite;
  347. return (1);
  348. }
  349. return 0;
  350. }
  351. /*
  352. * Like TIFFGetField, but return any default
  353. * value if the tag is not present in the directory.
  354. */
  355. int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag, ...)
  356. {
  357. int ok;
  358. va_list ap;
  359. va_start(ap, tag);
  360. ok = TIFFVGetFieldDefaulted(tif, tag, ap);
  361. va_end(ap);
  362. return (ok);
  363. }
  364. struct _Int64Parts
  365. {
  366. int32_t low, high;
  367. };
  368. typedef union
  369. {
  370. struct _Int64Parts part;
  371. int64_t value;
  372. } _Int64;
  373. float _TIFFUInt64ToFloat(uint64_t ui64)
  374. {
  375. _Int64 i;
  376. i.value = ui64;
  377. if (i.part.high >= 0)
  378. {
  379. return (float)i.value;
  380. }
  381. else
  382. {
  383. long double df;
  384. df = (long double)i.value;
  385. df += 18446744073709551616.0; /* adding 2**64 */
  386. return (float)df;
  387. }
  388. }
  389. double _TIFFUInt64ToDouble(uint64_t ui64)
  390. {
  391. _Int64 i;
  392. i.value = ui64;
  393. if (i.part.high >= 0)
  394. {
  395. return (double)i.value;
  396. }
  397. else
  398. {
  399. long double df;
  400. df = (long double)i.value;
  401. df += 18446744073709551616.0; /* adding 2**64 */
  402. return (double)df;
  403. }
  404. }
  405. float _TIFFClampDoubleToFloat(double val)
  406. {
  407. if (val > FLT_MAX)
  408. return FLT_MAX;
  409. if (val < -FLT_MAX)
  410. return -FLT_MAX;
  411. return (float)val;
  412. }
  413. uint32_t _TIFFClampDoubleToUInt32(double val)
  414. {
  415. if (val < 0)
  416. return 0;
  417. if (val > 0xFFFFFFFFU || val != val)
  418. return 0xFFFFFFFFU;
  419. return (uint32_t)val;
  420. }
  421. int _TIFFSeekOK(TIFF *tif, toff_t off)
  422. {
  423. /* Huge offsets, especially -1 / UINT64_MAX, can cause issues */
  424. /* See http://bugzilla.maptools.org/show_bug.cgi?id=2726 */
  425. return off <= (~(uint64_t)0) / 2 && TIFFSeekFile(tif, off, SEEK_SET) == off;
  426. }