tif_aux.c 11 KB

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