tif_tile.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * Tiled Image Support Routines.
  28. */
  29. #include "tiffiop.h"
  30. /*
  31. * Compute which tile an (x,y,z,s) value is in.
  32. */
  33. uint32_t TIFFComputeTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z,
  34. uint16_t s)
  35. {
  36. TIFFDirectory *td = &tif->tif_dir;
  37. uint32_t dx = td->td_tilewidth;
  38. uint32_t dy = td->td_tilelength;
  39. uint32_t dz = td->td_tiledepth;
  40. uint32_t tile = 1;
  41. if (td->td_imagedepth == 1)
  42. z = 0;
  43. if (dx == (uint32_t)-1)
  44. dx = td->td_imagewidth;
  45. if (dy == (uint32_t)-1)
  46. dy = td->td_imagelength;
  47. if (dz == (uint32_t)-1)
  48. dz = td->td_imagedepth;
  49. if (dx != 0 && dy != 0 && dz != 0)
  50. {
  51. uint32_t xpt = TIFFhowmany_32(td->td_imagewidth, dx);
  52. uint32_t ypt = TIFFhowmany_32(td->td_imagelength, dy);
  53. uint32_t zpt = TIFFhowmany_32(td->td_imagedepth, dz);
  54. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  55. tile = (xpt * ypt * zpt) * s + (xpt * ypt) * (z / dz) +
  56. xpt * (y / dy) + x / dx;
  57. else
  58. tile = (xpt * ypt) * (z / dz) + xpt * (y / dy) + x / dx;
  59. }
  60. return (tile);
  61. }
  62. /*
  63. * Check an (x,y,z,s) coordinate
  64. * against the image bounds.
  65. */
  66. int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
  67. {
  68. TIFFDirectory *td = &tif->tif_dir;
  69. if (x >= td->td_imagewidth)
  70. {
  71. TIFFErrorExtR(tif, tif->tif_name, "%lu: Col out of range, max %lu",
  72. (unsigned long)x, (unsigned long)(td->td_imagewidth - 1));
  73. return (0);
  74. }
  75. if (y >= td->td_imagelength)
  76. {
  77. TIFFErrorExtR(tif, tif->tif_name, "%lu: Row out of range, max %lu",
  78. (unsigned long)y,
  79. (unsigned long)(td->td_imagelength - 1));
  80. return (0);
  81. }
  82. if (z >= td->td_imagedepth)
  83. {
  84. TIFFErrorExtR(tif, tif->tif_name, "%lu: Depth out of range, max %lu",
  85. (unsigned long)z, (unsigned long)(td->td_imagedepth - 1));
  86. return (0);
  87. }
  88. if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
  89. s >= td->td_samplesperpixel)
  90. {
  91. TIFFErrorExtR(tif, tif->tif_name, "%lu: Sample out of range, max %lu",
  92. (unsigned long)s,
  93. (unsigned long)(td->td_samplesperpixel - 1));
  94. return (0);
  95. }
  96. return (1);
  97. }
  98. /*
  99. * Compute how many tiles are in an image.
  100. */
  101. uint32_t TIFFNumberOfTiles(TIFF *tif)
  102. {
  103. TIFFDirectory *td = &tif->tif_dir;
  104. uint32_t dx = td->td_tilewidth;
  105. uint32_t dy = td->td_tilelength;
  106. uint32_t dz = td->td_tiledepth;
  107. uint32_t ntiles;
  108. if (dx == (uint32_t)-1)
  109. dx = td->td_imagewidth;
  110. if (dy == (uint32_t)-1)
  111. dy = td->td_imagelength;
  112. if (dz == (uint32_t)-1)
  113. dz = td->td_imagedepth;
  114. ntiles =
  115. (dx == 0 || dy == 0 || dz == 0)
  116. ? 0
  117. : _TIFFMultiply32(
  118. tif,
  119. _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
  120. TIFFhowmany_32(td->td_imagelength, dy),
  121. "TIFFNumberOfTiles"),
  122. TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
  123. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  124. ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
  125. "TIFFNumberOfTiles");
  126. return (ntiles);
  127. }
  128. /*
  129. * Compute the # bytes in each row of a tile.
  130. */
  131. uint64_t TIFFTileRowSize64(TIFF *tif)
  132. {
  133. static const char module[] = "TIFFTileRowSize64";
  134. TIFFDirectory *td = &tif->tif_dir;
  135. uint64_t rowsize;
  136. uint64_t tilerowsize;
  137. if (td->td_tilelength == 0)
  138. {
  139. TIFFErrorExtR(tif, module, "Tile length is zero");
  140. return 0;
  141. }
  142. if (td->td_tilewidth == 0)
  143. {
  144. TIFFErrorExtR(tif, module, "Tile width is zero");
  145. return (0);
  146. }
  147. rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
  148. "TIFFTileRowSize");
  149. if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  150. {
  151. if (td->td_samplesperpixel == 0)
  152. {
  153. TIFFErrorExtR(tif, module, "Samples per pixel is zero");
  154. return 0;
  155. }
  156. rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
  157. "TIFFTileRowSize");
  158. }
  159. tilerowsize = TIFFhowmany8_64(rowsize);
  160. if (tilerowsize == 0)
  161. {
  162. TIFFErrorExtR(tif, module, "Computed tile row size is zero");
  163. return 0;
  164. }
  165. return (tilerowsize);
  166. }
  167. tmsize_t TIFFTileRowSize(TIFF *tif)
  168. {
  169. static const char module[] = "TIFFTileRowSize";
  170. uint64_t m;
  171. m = TIFFTileRowSize64(tif);
  172. return _TIFFCastUInt64ToSSize(tif, m, module);
  173. }
  174. /*
  175. * Compute the # bytes in a variable length, row-aligned tile.
  176. */
  177. uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows)
  178. {
  179. static const char module[] = "TIFFVTileSize64";
  180. TIFFDirectory *td = &tif->tif_dir;
  181. if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
  182. td->td_tiledepth == 0)
  183. return (0);
  184. if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
  185. (td->td_photometric == PHOTOMETRIC_YCBCR) &&
  186. (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
  187. {
  188. /*
  189. * Packed YCbCr data contain one Cb+Cr for every
  190. * HorizontalSampling*VerticalSampling Y values.
  191. * Must also roundup width and height when calculating
  192. * since images that are not a multiple of the
  193. * horizontal/vertical subsampling area include
  194. * YCbCr data for the extended image.
  195. */
  196. uint16_t ycbcrsubsampling[2];
  197. uint16_t samplingblock_samples;
  198. uint32_t samplingblocks_hor;
  199. uint32_t samplingblocks_ver;
  200. uint64_t samplingrow_samples;
  201. uint64_t samplingrow_size;
  202. TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
  203. ycbcrsubsampling + 0, ycbcrsubsampling + 1);
  204. if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
  205. ycbcrsubsampling[0] != 4) ||
  206. (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
  207. ycbcrsubsampling[1] != 4))
  208. {
  209. TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
  210. ycbcrsubsampling[0], ycbcrsubsampling[1]);
  211. return 0;
  212. }
  213. samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
  214. samplingblocks_hor =
  215. TIFFhowmany_32(td->td_tilewidth, ycbcrsubsampling[0]);
  216. samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
  217. samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
  218. samplingblock_samples, module);
  219. samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
  220. tif, samplingrow_samples, td->td_bitspersample, module));
  221. return (
  222. _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
  223. }
  224. else
  225. return (_TIFFMultiply64(tif, nrows, TIFFTileRowSize64(tif), module));
  226. }
  227. tmsize_t TIFFVTileSize(TIFF *tif, uint32_t nrows)
  228. {
  229. static const char module[] = "TIFFVTileSize";
  230. uint64_t m;
  231. m = TIFFVTileSize64(tif, nrows);
  232. return _TIFFCastUInt64ToSSize(tif, m, module);
  233. }
  234. /*
  235. * Compute the # bytes in a row-aligned tile.
  236. */
  237. uint64_t TIFFTileSize64(TIFF *tif)
  238. {
  239. return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
  240. }
  241. tmsize_t TIFFTileSize(TIFF *tif)
  242. {
  243. static const char module[] = "TIFFTileSize";
  244. uint64_t m;
  245. m = TIFFTileSize64(tif);
  246. return _TIFFCastUInt64ToSSize(tif, m, module);
  247. }
  248. /*
  249. * Compute a default tile size based on the image
  250. * characteristics and a requested value. If a
  251. * request is <1 then we choose a size according
  252. * to certain heuristics.
  253. */
  254. void TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
  255. {
  256. (*tif->tif_deftilesize)(tif, tw, th);
  257. }
  258. void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
  259. {
  260. (void)tif;
  261. if (*(int32_t *)tw < 1)
  262. *tw = 256;
  263. if (*(int32_t *)th < 1)
  264. *th = 256;
  265. /* roundup to a multiple of 16 per the spec */
  266. if (*tw & 0xf)
  267. *tw = TIFFroundup_32(*tw, 16);
  268. if (*th & 0xf)
  269. *th = TIFFroundup_32(*th, 16);
  270. }