/src/FreeImage/Source/LibTIFF/tif_aux.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 290 lines · 207 code · 20 blank · 63 comment · 28 complexity · 25710496d5ec9f99fcee60a01c24534a MD5 · raw file

  1. /* $Id: tif_aux.c,v 1.37 2011/04/10 17:14:09 drolon 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. tdata_t
  34. _TIFFCheckRealloc(TIFF* tif, tdata_t buffer,
  35. size_t nmemb, size_t elem_size, const char* what)
  36. {
  37. tdata_t cp = NULL;
  38. tsize_t bytes = nmemb * elem_size;
  39. /*
  40. * XXX: Check for integer overflow.
  41. */
  42. if (nmemb && elem_size && bytes / elem_size == nmemb)
  43. cp = _TIFFrealloc(buffer, bytes);
  44. if (cp == NULL)
  45. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  46. "Failed to allocate memory for %s "
  47. "(%ld elements of %ld bytes each)",
  48. what,(long) nmemb, (long) elem_size);
  49. return cp;
  50. }
  51. tdata_t
  52. _TIFFCheckMalloc(TIFF* tif, size_t nmemb, size_t elem_size, const char* what)
  53. {
  54. return _TIFFCheckRealloc(tif, NULL, nmemb, elem_size, what);
  55. }
  56. static int
  57. TIFFDefaultTransferFunction(TIFFDirectory* td)
  58. {
  59. uint16 **tf = td->td_transferfunction;
  60. tsize_t i, n, nbytes;
  61. tf[0] = tf[1] = tf[2] = 0;
  62. if (td->td_bitspersample >= sizeof(tsize_t) * 8 - 2)
  63. return 0;
  64. n = 1<<td->td_bitspersample;
  65. nbytes = n * sizeof (uint16);
  66. if (!(tf[0] = (uint16 *)_TIFFmalloc(nbytes)))
  67. return 0;
  68. tf[0][0] = 0;
  69. for (i = 1; i < n; i++) {
  70. double t = (double)i/((double) n-1.);
  71. tf[0][i] = (uint16)floor(65535.*pow(t, 2.2) + .5);
  72. }
  73. if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  74. if (!(tf[1] = (uint16 *)_TIFFmalloc(nbytes)))
  75. goto bad;
  76. _TIFFmemcpy(tf[1], tf[0], nbytes);
  77. if (!(tf[2] = (uint16 *)_TIFFmalloc(nbytes)))
  78. goto bad;
  79. _TIFFmemcpy(tf[2], tf[0], nbytes);
  80. }
  81. return 1;
  82. bad:
  83. if (tf[0])
  84. _TIFFfree(tf[0]);
  85. if (tf[1])
  86. _TIFFfree(tf[1]);
  87. if (tf[2])
  88. _TIFFfree(tf[2]);
  89. tf[0] = tf[1] = tf[2] = 0;
  90. return 0;
  91. }
  92. static int
  93. TIFFDefaultRefBlackWhite(TIFFDirectory* td)
  94. {
  95. int i;
  96. if (!(td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float))))
  97. return 0;
  98. if (td->td_photometric == PHOTOMETRIC_YCBCR) {
  99. /*
  100. * YCbCr (Class Y) images must have the ReferenceBlackWhite
  101. * tag set. Fix the broken images, which lacks that tag.
  102. */
  103. td->td_refblackwhite[0] = 0.0F;
  104. td->td_refblackwhite[1] = td->td_refblackwhite[3] =
  105. td->td_refblackwhite[5] = 255.0F;
  106. td->td_refblackwhite[2] = td->td_refblackwhite[4] = 128.0F;
  107. } else {
  108. /*
  109. * Assume RGB (Class R)
  110. */
  111. for (i = 0; i < 3; i++) {
  112. td->td_refblackwhite[2*i+0] = 0;
  113. td->td_refblackwhite[2*i+1] =
  114. (float)((1L<<td->td_bitspersample)-1L);
  115. }
  116. }
  117. return 1;
  118. }
  119. /*
  120. * Like TIFFGetField, but return any default
  121. * value if the tag is not present in the directory.
  122. *
  123. * NB: We use the value in the directory, rather than
  124. * explcit values so that defaults exist only one
  125. * place in the library -- in TIFFDefaultDirectory.
  126. */
  127. int
  128. TIFFVGetFieldDefaulted(TIFF* tif, ttag_t tag, va_list ap)
  129. {
  130. TIFFDirectory *td = &tif->tif_dir;
  131. if (TIFFVGetField(tif, tag, ap))
  132. return (1);
  133. switch (tag) {
  134. case TIFFTAG_SUBFILETYPE:
  135. *va_arg(ap, uint32 *) = td->td_subfiletype;
  136. return (1);
  137. case TIFFTAG_BITSPERSAMPLE:
  138. *va_arg(ap, uint16 *) = td->td_bitspersample;
  139. return (1);
  140. case TIFFTAG_THRESHHOLDING:
  141. *va_arg(ap, uint16 *) = td->td_threshholding;
  142. return (1);
  143. case TIFFTAG_FILLORDER:
  144. *va_arg(ap, uint16 *) = td->td_fillorder;
  145. return (1);
  146. case TIFFTAG_ORIENTATION:
  147. *va_arg(ap, uint16 *) = td->td_orientation;
  148. return (1);
  149. case TIFFTAG_SAMPLESPERPIXEL:
  150. *va_arg(ap, uint16 *) = td->td_samplesperpixel;
  151. return (1);
  152. case TIFFTAG_ROWSPERSTRIP:
  153. *va_arg(ap, uint32 *) = td->td_rowsperstrip;
  154. return (1);
  155. case TIFFTAG_MINSAMPLEVALUE:
  156. *va_arg(ap, uint16 *) = td->td_minsamplevalue;
  157. return (1);
  158. case TIFFTAG_MAXSAMPLEVALUE:
  159. *va_arg(ap, uint16 *) = td->td_maxsamplevalue;
  160. return (1);
  161. case TIFFTAG_PLANARCONFIG:
  162. *va_arg(ap, uint16 *) = td->td_planarconfig;
  163. return (1);
  164. case TIFFTAG_RESOLUTIONUNIT:
  165. *va_arg(ap, uint16 *) = td->td_resolutionunit;
  166. return (1);
  167. case TIFFTAG_PREDICTOR:
  168. {
  169. TIFFPredictorState* sp = (TIFFPredictorState*) tif->tif_data;
  170. *va_arg(ap, uint16*) = (uint16) sp->predictor;
  171. return 1;
  172. }
  173. case TIFFTAG_DOTRANGE:
  174. *va_arg(ap, uint16 *) = 0;
  175. *va_arg(ap, uint16 *) = (1<<td->td_bitspersample)-1;
  176. return (1);
  177. case TIFFTAG_INKSET:
  178. *va_arg(ap, uint16 *) = INKSET_CMYK;
  179. return 1;
  180. case TIFFTAG_NUMBEROFINKS:
  181. *va_arg(ap, uint16 *) = 4;
  182. return (1);
  183. case TIFFTAG_EXTRASAMPLES:
  184. *va_arg(ap, uint16 *) = td->td_extrasamples;
  185. *va_arg(ap, uint16 **) = td->td_sampleinfo;
  186. return (1);
  187. case TIFFTAG_MATTEING:
  188. *va_arg(ap, uint16 *) =
  189. (td->td_extrasamples == 1 &&
  190. td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
  191. return (1);
  192. case TIFFTAG_TILEDEPTH:
  193. *va_arg(ap, uint32 *) = td->td_tiledepth;
  194. return (1);
  195. case TIFFTAG_DATATYPE:
  196. *va_arg(ap, uint16 *) = td->td_sampleformat-1;
  197. return (1);
  198. case TIFFTAG_SAMPLEFORMAT:
  199. *va_arg(ap, uint16 *) = td->td_sampleformat;
  200. return(1);
  201. case TIFFTAG_IMAGEDEPTH:
  202. *va_arg(ap, uint32 *) = td->td_imagedepth;
  203. return (1);
  204. case TIFFTAG_YCBCRCOEFFICIENTS:
  205. {
  206. /* defaults are from CCIR Recommendation 601-1 */
  207. static float ycbcrcoeffs[] = { 0.299f, 0.587f, 0.114f };
  208. *va_arg(ap, float **) = ycbcrcoeffs;
  209. return 1;
  210. }
  211. case TIFFTAG_YCBCRSUBSAMPLING:
  212. *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[0];
  213. *va_arg(ap, uint16 *) = td->td_ycbcrsubsampling[1];
  214. return (1);
  215. case TIFFTAG_YCBCRPOSITIONING:
  216. *va_arg(ap, uint16 *) = td->td_ycbcrpositioning;
  217. return (1);
  218. case TIFFTAG_WHITEPOINT:
  219. {
  220. static float whitepoint[2];
  221. /* TIFF 6.0 specification tells that it is no default
  222. value for the WhitePoint, but AdobePhotoshop TIFF
  223. Technical Note tells that it should be CIE D50. */
  224. whitepoint[0] = D50_X0 / (D50_X0 + D50_Y0 + D50_Z0);
  225. whitepoint[1] = D50_Y0 / (D50_X0 + D50_Y0 + D50_Z0);
  226. *va_arg(ap, float **) = whitepoint;
  227. return 1;
  228. }
  229. case TIFFTAG_TRANSFERFUNCTION:
  230. if (!td->td_transferfunction[0] &&
  231. !TIFFDefaultTransferFunction(td)) {
  232. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "No space for \"TransferFunction\" tag");
  233. return (0);
  234. }
  235. *va_arg(ap, uint16 **) = td->td_transferfunction[0];
  236. if (td->td_samplesperpixel - td->td_extrasamples > 1) {
  237. *va_arg(ap, uint16 **) = td->td_transferfunction[1];
  238. *va_arg(ap, uint16 **) = td->td_transferfunction[2];
  239. }
  240. return (1);
  241. case TIFFTAG_REFERENCEBLACKWHITE:
  242. if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
  243. return (0);
  244. *va_arg(ap, float **) = td->td_refblackwhite;
  245. return (1);
  246. }
  247. return 0;
  248. }
  249. /*
  250. * Like TIFFGetField, but return any default
  251. * value if the tag is not present in the directory.
  252. */
  253. int
  254. TIFFGetFieldDefaulted(TIFF* tif, ttag_t tag, ...)
  255. {
  256. int ok;
  257. va_list ap;
  258. va_start(ap, tag);
  259. ok = TIFFVGetFieldDefaulted(tif, tag, ap);
  260. va_end(ap);
  261. return (ok);
  262. }
  263. /* vim: set ts=8 sts=8 sw=8 noet: */
  264. /*
  265. * Local Variables:
  266. * mode: c
  267. * c-basic-offset: 8
  268. * fill-column: 78
  269. * End:
  270. */