tif_tile.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /* $Id: tif_tile.c,v 1.24 2015-06-07 22:35:40 bfriesen 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. static const char module[] = "TIFFTileRowSize64";
  137. TIFFDirectory *td = &tif->tif_dir;
  138. uint64 rowsize;
  139. uint64 tilerowsize;
  140. if (td->td_tilelength == 0)
  141. {
  142. TIFFErrorExt(tif->tif_clientdata,module,"Tile length is zero");
  143. return 0;
  144. }
  145. if (td->td_tilewidth == 0)
  146. {
  147. TIFFErrorExt(tif->tif_clientdata,module,"Tile width is zero");
  148. return (0);
  149. }
  150. rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
  151. "TIFFTileRowSize");
  152. if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  153. {
  154. if (td->td_samplesperpixel == 0)
  155. {
  156. TIFFErrorExt(tif->tif_clientdata,module,"Samples per pixel is zero");
  157. return 0;
  158. }
  159. rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
  160. "TIFFTileRowSize");
  161. }
  162. tilerowsize=TIFFhowmany8_64(rowsize);
  163. if (tilerowsize == 0)
  164. {
  165. TIFFErrorExt(tif->tif_clientdata,module,"Computed tile row size is zero");
  166. return 0;
  167. }
  168. return (tilerowsize);
  169. }
  170. tmsize_t
  171. TIFFTileRowSize(TIFF* tif)
  172. {
  173. static const char module[] = "TIFFTileRowSize";
  174. uint64 m;
  175. tmsize_t n;
  176. m=TIFFTileRowSize64(tif);
  177. n=(tmsize_t)m;
  178. if ((uint64)n!=m)
  179. {
  180. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  181. n=0;
  182. }
  183. return(n);
  184. }
  185. /*
  186. * Compute the # bytes in a variable length, row-aligned tile.
  187. */
  188. uint64
  189. TIFFVTileSize64(TIFF* tif, uint32 nrows)
  190. {
  191. static const char module[] = "TIFFVTileSize64";
  192. TIFFDirectory *td = &tif->tif_dir;
  193. if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
  194. td->td_tiledepth == 0)
  195. return (0);
  196. if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
  197. (td->td_photometric==PHOTOMETRIC_YCBCR)&&
  198. (td->td_samplesperpixel==3)&&
  199. (!isUpSampled(tif)))
  200. {
  201. /*
  202. * Packed YCbCr data contain one Cb+Cr for every
  203. * HorizontalSampling*VerticalSampling Y values.
  204. * Must also roundup width and height when calculating
  205. * since images that are not a multiple of the
  206. * horizontal/vertical subsampling area include
  207. * YCbCr data for the extended image.
  208. */
  209. uint16 ycbcrsubsampling[2];
  210. uint16 samplingblock_samples;
  211. uint32 samplingblocks_hor;
  212. uint32 samplingblocks_ver;
  213. uint64 samplingrow_samples;
  214. uint64 samplingrow_size;
  215. TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
  216. ycbcrsubsampling+1);
  217. if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
  218. ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
  219. {
  220. TIFFErrorExt(tif->tif_clientdata,module,
  221. "Invalid YCbCr subsampling (%dx%d)",
  222. ycbcrsubsampling[0],
  223. ycbcrsubsampling[1] );
  224. return 0;
  225. }
  226. samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
  227. samplingblocks_hor=TIFFhowmany_32(td->td_tilewidth,ycbcrsubsampling[0]);
  228. samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
  229. samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
  230. samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
  231. return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
  232. }
  233. else
  234. return(_TIFFMultiply64(tif,nrows,TIFFTileRowSize64(tif),module));
  235. }
  236. tmsize_t
  237. TIFFVTileSize(TIFF* tif, uint32 nrows)
  238. {
  239. static const char module[] = "TIFFVTileSize";
  240. uint64 m;
  241. tmsize_t n;
  242. m=TIFFVTileSize64(tif,nrows);
  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 the # bytes in a row-aligned tile.
  253. */
  254. uint64
  255. TIFFTileSize64(TIFF* tif)
  256. {
  257. return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
  258. }
  259. tmsize_t
  260. TIFFTileSize(TIFF* tif)
  261. {
  262. static const char module[] = "TIFFTileSize";
  263. uint64 m;
  264. tmsize_t n;
  265. m=TIFFTileSize64(tif);
  266. n=(tmsize_t)m;
  267. if ((uint64)n!=m)
  268. {
  269. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  270. n=0;
  271. }
  272. return(n);
  273. }
  274. /*
  275. * Compute a default tile size based on the image
  276. * characteristics and a requested value. If a
  277. * request is <1 then we choose a size according
  278. * to certain heuristics.
  279. */
  280. void
  281. TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
  282. {
  283. (*tif->tif_deftilesize)(tif, tw, th);
  284. }
  285. void
  286. _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
  287. {
  288. (void) tif;
  289. if (*(int32*) tw < 1)
  290. *tw = 256;
  291. if (*(int32*) th < 1)
  292. *th = 256;
  293. /* roundup to a multiple of 16 per the spec */
  294. if (*tw & 0xf)
  295. *tw = TIFFroundup_32(*tw, 16);
  296. if (*th & 0xf)
  297. *th = TIFFroundup_32(*th, 16);
  298. }
  299. /* vim: set ts=8 sts=8 sw=8 noet: */
  300. /*
  301. * Local Variables:
  302. * mode: c
  303. * c-basic-offset: 8
  304. * fill-column: 78
  305. * End:
  306. */