/src/FreeImage/Source/LibTIFF/tif_tile.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 280 lines · 181 code · 23 blank · 76 comment · 59 complexity · b3e00c4813bfcf9cf8082fb7f25efd32 MD5 · raw file

  1. /* $Id: tif_tile.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. * Tiled Image Support Routines.
  29. */
  30. #include "tiffiop.h"
  31. static uint32
  32. summarize(TIFF* tif, size_t summand1, size_t summand2, const char* where)
  33. {
  34. /*
  35. * XXX: We are using casting to uint32 here, because sizeof(size_t)
  36. * may be larger than sizeof(uint32) on 64-bit architectures.
  37. */
  38. uint32 bytes = summand1 + summand2;
  39. if (bytes - summand1 != summand2) {
  40. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Integer overflow in %s", where);
  41. bytes = 0;
  42. }
  43. return (bytes);
  44. }
  45. static uint32
  46. multiply(TIFF* tif, size_t nmemb, size_t elem_size, const char* where)
  47. {
  48. uint32 bytes = nmemb * elem_size;
  49. if (elem_size && bytes / elem_size != nmemb) {
  50. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Integer overflow in %s", where);
  51. bytes = 0;
  52. }
  53. return (bytes);
  54. }
  55. /*
  56. * Compute which tile an (x,y,z,s) value is in.
  57. */
  58. ttile_t
  59. TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
  60. {
  61. TIFFDirectory *td = &tif->tif_dir;
  62. uint32 dx = td->td_tilewidth;
  63. uint32 dy = td->td_tilelength;
  64. uint32 dz = td->td_tiledepth;
  65. ttile_t tile = 1;
  66. if (td->td_imagedepth == 1)
  67. z = 0;
  68. if (dx == (uint32) -1)
  69. dx = td->td_imagewidth;
  70. if (dy == (uint32) -1)
  71. dy = td->td_imagelength;
  72. if (dz == (uint32) -1)
  73. dz = td->td_imagedepth;
  74. if (dx != 0 && dy != 0 && dz != 0) {
  75. uint32 xpt = TIFFhowmany(td->td_imagewidth, dx);
  76. uint32 ypt = TIFFhowmany(td->td_imagelength, dy);
  77. uint32 zpt = TIFFhowmany(td->td_imagedepth, dz);
  78. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  79. tile = (xpt*ypt*zpt)*s +
  80. (xpt*ypt)*(z/dz) +
  81. xpt*(y/dy) +
  82. x/dx;
  83. else
  84. tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;
  85. }
  86. return (tile);
  87. }
  88. /*
  89. * Check an (x,y,z,s) coordinate
  90. * against the image bounds.
  91. */
  92. int
  93. TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
  94. {
  95. TIFFDirectory *td = &tif->tif_dir;
  96. if (x >= td->td_imagewidth) {
  97. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  98. "%lu: Col out of range, max %lu",
  99. (unsigned long) x,
  100. (unsigned long) (td->td_imagewidth - 1));
  101. return (0);
  102. }
  103. if (y >= td->td_imagelength) {
  104. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  105. "%lu: Row out of range, max %lu",
  106. (unsigned long) y,
  107. (unsigned long) (td->td_imagelength - 1));
  108. return (0);
  109. }
  110. if (z >= td->td_imagedepth) {
  111. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  112. "%lu: Depth out of range, max %lu",
  113. (unsigned long) z,
  114. (unsigned long) (td->td_imagedepth - 1));
  115. return (0);
  116. }
  117. if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
  118. s >= td->td_samplesperpixel) {
  119. TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
  120. "%lu: Sample out of range, max %lu",
  121. (unsigned long) s,
  122. (unsigned long) (td->td_samplesperpixel - 1));
  123. return (0);
  124. }
  125. return (1);
  126. }
  127. /*
  128. * Compute how many tiles are in an image.
  129. */
  130. ttile_t
  131. TIFFNumberOfTiles(TIFF* tif)
  132. {
  133. TIFFDirectory *td = &tif->tif_dir;
  134. uint32 dx = td->td_tilewidth;
  135. uint32 dy = td->td_tilelength;
  136. uint32 dz = td->td_tiledepth;
  137. ttile_t ntiles;
  138. if (dx == (uint32) -1)
  139. dx = td->td_imagewidth;
  140. if (dy == (uint32) -1)
  141. dy = td->td_imagelength;
  142. if (dz == (uint32) -1)
  143. dz = td->td_imagedepth;
  144. ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
  145. multiply(tif, multiply(tif, TIFFhowmany(td->td_imagewidth, dx),
  146. TIFFhowmany(td->td_imagelength, dy),
  147. "TIFFNumberOfTiles"),
  148. TIFFhowmany(td->td_imagedepth, dz), "TIFFNumberOfTiles");
  149. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  150. ntiles = multiply(tif, ntiles, td->td_samplesperpixel,
  151. "TIFFNumberOfTiles");
  152. return (ntiles);
  153. }
  154. /*
  155. * Compute the # bytes in each row of a tile.
  156. */
  157. tsize_t
  158. TIFFTileRowSize(TIFF* tif)
  159. {
  160. TIFFDirectory *td = &tif->tif_dir;
  161. tsize_t rowsize;
  162. if (td->td_tilelength == 0 || td->td_tilewidth == 0)
  163. return ((tsize_t) 0);
  164. rowsize = multiply(tif, td->td_bitspersample, td->td_tilewidth,
  165. "TIFFTileRowSize");
  166. if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  167. rowsize = multiply(tif, rowsize, td->td_samplesperpixel,
  168. "TIFFTileRowSize");
  169. return ((tsize_t) TIFFhowmany8(rowsize));
  170. }
  171. /*
  172. * Compute the # bytes in a variable length, row-aligned tile.
  173. */
  174. tsize_t
  175. TIFFVTileSize(TIFF* tif, uint32 nrows)
  176. {
  177. TIFFDirectory *td = &tif->tif_dir;
  178. tsize_t tilesize;
  179. if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
  180. td->td_tiledepth == 0)
  181. return ((tsize_t) 0);
  182. if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
  183. td->td_photometric == PHOTOMETRIC_YCBCR &&
  184. !isUpSampled(tif)) {
  185. /*
  186. * Packed YCbCr data contain one Cb+Cr for every
  187. * HorizontalSampling*VerticalSampling Y values.
  188. * Must also roundup width and height when calculating
  189. * since images that are not a multiple of the
  190. * horizontal/vertical subsampling area include
  191. * YCbCr data for the extended image.
  192. */
  193. tsize_t w =
  194. TIFFroundup(td->td_tilewidth, td->td_ycbcrsubsampling[0]);
  195. tsize_t rowsize =
  196. TIFFhowmany8(multiply(tif, w, td->td_bitspersample,
  197. "TIFFVTileSize"));
  198. tsize_t samplingarea =
  199. td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
  200. if (samplingarea == 0) {
  201. TIFFErrorExt(tif->tif_clientdata, tif->tif_name, "Invalid YCbCr subsampling");
  202. return 0;
  203. }
  204. nrows = TIFFroundup(nrows, td->td_ycbcrsubsampling[1]);
  205. /* NB: don't need TIFFhowmany here 'cuz everything is rounded */
  206. tilesize = multiply(tif, nrows, rowsize, "TIFFVTileSize");
  207. tilesize = summarize(tif, tilesize,
  208. multiply(tif, 2, tilesize / samplingarea,
  209. "TIFFVTileSize"),
  210. "TIFFVTileSize");
  211. } else
  212. tilesize = multiply(tif, nrows, TIFFTileRowSize(tif),
  213. "TIFFVTileSize");
  214. return ((tsize_t)
  215. multiply(tif, tilesize, td->td_tiledepth, "TIFFVTileSize"));
  216. }
  217. /*
  218. * Compute the # bytes in a row-aligned tile.
  219. */
  220. tsize_t
  221. TIFFTileSize(TIFF* tif)
  222. {
  223. return (TIFFVTileSize(tif, tif->tif_dir.td_tilelength));
  224. }
  225. /*
  226. * Compute a default tile size based on the image
  227. * characteristics and a requested value. If a
  228. * request is <1 then we choose a size according
  229. * to certain heuristics.
  230. */
  231. void
  232. TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
  233. {
  234. (*tif->tif_deftilesize)(tif, tw, th);
  235. }
  236. void
  237. _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
  238. {
  239. (void) tif;
  240. if (*(int32*) tw < 1)
  241. *tw = 256;
  242. if (*(int32*) th < 1)
  243. *th = 256;
  244. /* roundup to a multiple of 16 per the spec */
  245. if (*tw & 0xf)
  246. *tw = TIFFroundup(*tw, 16);
  247. if (*th & 0xf)
  248. *th = TIFFroundup(*th, 16);
  249. }
  250. /* vim: set ts=8 sts=8 sw=8 noet: */
  251. /*
  252. * Local Variables:
  253. * mode: c
  254. * c-basic-offset: 8
  255. * fill-column: 78
  256. * End:
  257. */