tif_tile.c 8.5 KB

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