2
0

tif_aux.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* $Id: tif_aux.c,v 1.26 2010-07-01 15:33:28 dron Exp $ */
  2. /*
  3. * Copyright (c) 1991-1997 Sam Leffler
  4. * Copyright (c) 1991-1997 Silicon Graphics, Inc.
  5. *
  6. * Permission to use, copy, modify, distribute, and sell this software and
  7. * its documentation for any purpose is hereby granted without fee, provided
  8. * that (i) the above copyright notices and this permission notice appear in
  9. * all copies of the software and related documentation, and (ii) the names of
  10. * Sam Leffler and Silicon Graphics may not be used in any advertising or
  11. * publicity relating to the software without the specific, prior written
  12. * permission of Sam Leffler and Silicon Graphics.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  15. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  16. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  17. *
  18. * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  19. * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  20. * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  21. * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
  22. * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  23. * OF THIS SOFTWARE.
  24. */
  25. /*
  26. * TIFF Library.
  27. *
  28. * Auxiliary Support Routines.
  29. */
  30. #include "tiffiop.h"
  31. #include "tif_predict.h"
  32. #include <math.h>
  33. uint32
  34. _TIFFMultiply32(TIFF* tif, uint32 first, uint32 second, const char* where)
  35. {
  36. uint32 bytes = first * second;
  37. if (second && bytes / second != first) {
  38. TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
  39. bytes = 0;
  40. }
  41. return bytes;
  42. }
  43. uint64
  44. _TIFFMultiply64(TIFF* tif, uint64 first, uint64 second, const char* where)
  45. {
  46. uint64 bytes = first * second;
  47. if (second && bytes / second != first) {
  48. TIFFErrorExt(tif->tif_clientdata, where, "Integer overflow in %s", where);
  49. bytes = 0;
  50. }
  51. return bytes;
  52. }
  53. void*
  54. _TIFFCheckRealloc(TIFF* tif, void* buffer,
  55. tmsize_t nmemb, tmsize_t elem_size, const char* what)
  56. {
  57. void* cp = NULL;
  58. tmsize_t bytes = nmemb * elem_size;
  59. /*
  60. * XXX: Check for integer overflow.
  61. */
  62. if (nmemb && elem_size && bytes / elem_size == nmemb)
  63. cp = _TIFFrealloc(buffer, bytes);
  64. if (cp == NULL) {
  65. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  66. "Failed to allocate memory for %s "
  67. "(%ld elements of %ld bytes each)",
  68. what,(long) nmemb, (long) elem_size);
  69. }
  70. return cp;
  71. }
  72. void*
  73. _TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what)
  74. {
  75. return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
  76. }
  77. static int
  78. TIFFDefaultTransferFunction(TIFFDirectory* td)
  79. {
  80. uint16 **tf = td->td_transferfunction;
  81. tmsize_t i, n, nbytes;
  82. tf[0] = tf[1] = tf[2] = 0;
  83. if (td->td_bitspersample >= sizeof(tmsize_t) * 8 - 2)
  84. return 0;
  85. n = ((tmsize_t)1)<<td->td_bitspersample;
  86. nbytes = n * sizeof (uint16);
  87. if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
  88. return 0;
  89. tf[0][0] = 0;
  90. for (i = 1; i < n; i++) {
  91. double t = (double)i/((double) n-1.);
  92. tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
  93. }
  94. if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  95. if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
  96. goto bad;
  97. _TIFFmemcpy(tf[1], tf[0], nbytes);
  98. if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
  99. goto bad;
  100. _TIFFmemcpy(tf[2], tf[0], nbytes);
  101. }
  102. return 1;
  103. bad:
  104. if (tf[0])
  105. _TIFFfree(tf[0]);
  106. if (tf[1])
  107. _TIFFfree(tf[1]);
  108. if (tf[2])
  109. _TIFFfree(tf[2]);
  110. tf[0] = tf[1] = tf[2] = 0;
  111. return 0;
  112. }
  113. static int
  114. TIFFDefaultRefBlackWhite(TIFFDirectory* td)
  115. {
  116. int i;
  117. if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
  118. return 0;
  119. if (td->td_photometric == PHOTOMETRIC_YCBCR) {
  120. /*
  121. * YCbCr (Class Y) images must have the ReferenceBlackWhite
  122. * tag set. Fix the broken images, which lacks that tag.
  123. */
  124. td->td_refblackwhite[0] = 0.0F;
  125. td->td_refblackwhite[1] = td->td_refblackwhite[3] =
  126. td->td_refblackwhite[5] = 255.0F;
  127. td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
  128. } else {
  129. /*
  130. * Assume RGB (Class R)
  131. */
  132. for (i = 0; i < 3; i++) {
  133. td->td_refblackwhite[2*i+0] = 0;
  134. td->td_refblackwhite[2*i+1] =
  135. (float)((1L<<td->td_bitspersample)-1L);
  136. }
  137. }
  138. return 1;
  139. }
  140. /*
  141. * Like TIFFGetField, but return any default
  142. * value if the tag is not present in the directory.
  143. *
  144. * NB: We use the value in the directory, rather than
  145. * explcit values so that defaults exist only one
  146. * place in the library -- in TIFFDefaultDirectory.
  147. */
  148. int
  149. TIFFVGetFieldDefaulted(TIFF* tif, uint32 tag, va_list ap)
  150. {
  151. TIFFDirectory *td = &tif->tif_dir;
  152. if (TIFFVGetField(tif, tag, ap))
  153. return (1);
  154. switch (tag) {
  155. case TIFFTAG_SUBFILETYPE:
  156. *va_arg(ap, uint32 *) = td->td_subfiletype;
  157. return (1);
  158. case TIFFTAG_BITSPERSAMPLE:
  159. *va_arg(ap, uint16 *) = td->td_bitspersample;
  160. return (1);
  161. case TIFFTAG_THRESHHOLDING:
  162. *va_arg(ap, uint16 *) = td->td_threshholding;
  163. return (1);
  164. case TIFFTAG_FILLORDER:
  165. *va_arg(ap, uint16 *) = td->td_fillorder;
  166. return (1);
  167. case TIFFTAG_ORIENTATION:
  168. *va_arg(ap, uint16 *) = td->td_orientation;
  169. return (1);
  170. case TIFFTAG_SAMPLESPERPIXEL:
  171. *va_arg(ap, uint16 *) = td->td_samplesperpixel;
  172. return (1);
  173. case TIFFTAG_ROWSPERSTRIP:
  174. *va_arg(ap, uint32 *) = td->td_rowsperstrip;
  175. return (1);
  176. case TIFFTAG_MINSAMPLEVALUE:
  177. *va_arg(ap, uint16 *) = td->td_minsamplevalue;
  178. return (1);
  179. case TIFFTAG_MAXSAMPLEVALUE:
  180. *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
  181. return (1);
  182. case TIFFTAG_PLANARCONFIG:
  183. *va_arg(ap, uint16 *) = td->td_planarconfig;
  184. return (1);
  185. case TIFFTAG_RESOLUTIONUNIT:
  186. *va_arg(ap, uint16 *) = td->td_resolutionunit;
  187. return (1);
  188. case TIFFTAG_PREDICTOR:
  189. {
  190. TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
  191. *va_arg(ap, uint16*) = (uint16) sp->predictor;
  192. return 1;
  193. }
  194. case TIFFTAG_DOTRANGE:
  195. *va_arg(ap, uint16 *) = 0;
  196. *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
  197. return (1);
  198. case TIFFTAG_INKSET:
  199. *va_arg(ap, uint16 *) = INKSET_CMYK;
  200. return 1;
  201. case TIFFTAG_NUMBEROFINKS:
  202. *va_arg(ap, uint16 *) = 4;
  203. return (1);
  204. case TIFFTAG_EXTRASAMPLES:
  205. *va_arg(ap, uint16 *) = td->td_extrasamples;
  206. *va_arg(ap, uint16 **) = td->td_sampleinfo;
  207. return (1);
  208. case TIFFTAG_MATTEING:
  209. *va_arg(ap, uint16 *) =
  210. (td->td_extrasamples == 1 &&
  211. td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  212. return (1);
  213. case TIFFTAG_TILEDEPTH:
  214. *va_arg(ap, uint32 *) = td->td_tiledepth;
  215. return (1);
  216. case TIFFTAG_DATATYPE:
  217. *va_arg(ap, uint16 *) = td->td_sampleformat-1;
  218. return (1);
  219. case TIFFTAG_SAMPLEFORMAT:
  220. *va_arg(ap, uint16 *) = td->td_sampleformat;
  221. return(1);
  222. case TIFFTAG_IMAGEDEPTH:
  223. *va_arg(ap, uint32 *) = td->td_imagedepth;
  224. return (1);
  225. case TIFFTAG_YCBCRCOEFFICIENTS:
  226. {
  227. /* defaults are from CCIR Recommendation 601-1 */
  228. static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
  229. *va_arg(ap, float **) = ycbcrcoeffs;
  230. return 1;
  231. }
  232. case TIFFTAG_YCBCRSUBSAMPLING:
  233. *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
  234. *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
  235. return (1);
  236. case TIFFTAG_YCBCRPOSITIONING:
  237. *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
  238. return (1);
  239. case TIFFTAG_WHITEPOINT:
  240. {
  241. static float whitepoint[2];
  242. /* TIFF 6.0 specification tells that it is no default
  243. value for the WhitePoint, but AdobePhotoshop TIFF
  244. Technical Note tells that it should be CIE D50. */
  245. whitepoint[0] = D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
  246. whitepoint[1] = D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
  247. *va_arg(ap, float **) = whitepoint;
  248. return 1;
  249. }
  250. case TIFFTAG_TRANSFERFUNCTION:
  251. if (!td->td_transferfunction[0] &&
  252. !TIFFDefaultTransferFunction(td)) {
  253. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
  254. return (0);
  255. }
  256. *va_arg(ap, uint16 **) = td->td_transferfunction[0];
  257. if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  258. *va_arg(ap, uint16 **) = td->td_transferfunction[1];
  259. *va_arg(ap, uint16 **) = td->td_transferfunction[2];
  260. }
  261. return (1);
  262. case TIFFTAG_REFERENCEBLACKWHITE:
  263. if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
  264. return (0);
  265. *va_arg(ap, float **) = td->td_refblackwhite;
  266. return (1);
  267. }
  268. return 0;
  269. }
  270. /*
  271. * Like TIFFGetField, but return any default
  272. * value if the tag is not present in the directory.
  273. */
  274. int
  275. TIFFGetFieldDefaulted(TIFF* tif, uint32 tag, ...)
  276. {
  277. int ok;
  278. va_list ap;
  279. va_start(ap, tag);
  280. ok = TIFFVGetFieldDefaulted(tif, tag, ap);
  281. va_end(ap);
  282. return (ok);
  283. }
  284. struct _Int64Parts {
  285. int32 low, high;
  286. };
  287. typedef union {
  288. struct _Int64Parts part;
  289. int64 value;
  290. } _Int64;
  291. float
  292. _TIFFUInt64ToFloat(uint64 ui64)
  293. {
  294. _Int64 i;
  295. i.value = ui64;
  296. if (i.part.high >= 0) {
  297. return (float)i.value;
  298. } else {
  299. long double df;
  300. df = (long double)i.value;
  301. df += 18446744073709551616.0; /* adding 2**64 */
  302. return (float)df;
  303. }
  304. }
  305. double
  306. _TIFFUInt64ToDouble(uint64 ui64)
  307. {
  308. _Int64 i;
  309. i.value = ui64;
  310. if (i.part.high >= 0) {
  311. return (double)i.value;
  312. } else {
  313. long double df;
  314. df = (long double)i.value;
  315. df += 18446744073709551616.0; /* adding 2**64 */
  316. return (double)df;
  317. }
  318. }
  319. /* vim: set ts=8 sts=8 sw=8 noet: */
  320. /*
  321. * Local Variables:
  322. * mode: c
  323. * c-basic-offset: 8
  324. * fill-column: 78
  325. * End:
  326. */