tif_tile.c 8.1 KB

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