tif_strip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. * Strip-organized Image Support Routines.
  28. */
  29. #include "tiffiop.h"
  30. /*
  31. * Compute which strip a (row,sample) value is in.
  32. */
  33. uint32
  34. TIFFComputeStrip(TIFF* tif, uint32 row, uint16 sample)
  35. {
  36. static const char module[] = "TIFFComputeStrip";
  37. TIFFDirectory *td = &tif->tif_dir;
  38. uint32 strip;
  39. strip = row / td->td_rowsperstrip;
  40. if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
  41. if (sample >= td->td_samplesperpixel) {
  42. TIFFErrorExt(tif->tif_clientdata, module,
  43. "%lu: Sample out of range, max %lu",
  44. (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
  45. return (0);
  46. }
  47. strip += (uint32)sample*td->td_stripsperimage;
  48. }
  49. return (strip);
  50. }
  51. /*
  52. * Compute how many strips are in an image.
  53. */
  54. uint32
  55. TIFFNumberOfStrips(TIFF* tif)
  56. {
  57. TIFFDirectory *td = &tif->tif_dir;
  58. uint32 nstrips;
  59. nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
  60. TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
  61. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  62. nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
  63. "TIFFNumberOfStrips");
  64. return (nstrips);
  65. }
  66. /*
  67. * Compute the # bytes in a variable height, row-aligned strip.
  68. */
  69. uint64
  70. TIFFVStripSize64(TIFF* tif, uint32 nrows)
  71. {
  72. static const char module[] = "TIFFVStripSize64";
  73. TIFFDirectory *td = &tif->tif_dir;
  74. if (nrows==(uint32)(-1))
  75. nrows=td->td_imagelength;
  76. if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
  77. (td->td_photometric == PHOTOMETRIC_YCBCR)&&
  78. (!isUpSampled(tif)))
  79. {
  80. /*
  81. * Packed YCbCr data contain one Cb+Cr for every
  82. * HorizontalSampling*VerticalSampling Y values.
  83. * Must also roundup width and height when calculating
  84. * since images that are not a multiple of the
  85. * horizontal/vertical subsampling area include
  86. * YCbCr data for the extended image.
  87. */
  88. uint16 ycbcrsubsampling[2];
  89. uint16 samplingblock_samples;
  90. uint32 samplingblocks_hor;
  91. uint32 samplingblocks_ver;
  92. uint64 samplingrow_samples;
  93. uint64 samplingrow_size;
  94. if(td->td_samplesperpixel!=3)
  95. {
  96. TIFFErrorExt(tif->tif_clientdata,module,
  97. "Invalid td_samplesperpixel value");
  98. return 0;
  99. }
  100. TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
  101. ycbcrsubsampling+1);
  102. if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
  103. ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
  104. {
  105. TIFFErrorExt(tif->tif_clientdata,module,
  106. "Invalid YCbCr subsampling (%dx%d)",
  107. ycbcrsubsampling[0],
  108. ycbcrsubsampling[1] );
  109. return 0;
  110. }
  111. samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
  112. samplingblocks_hor=TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
  113. samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
  114. samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
  115. samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
  116. return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
  117. }
  118. else
  119. return(_TIFFMultiply64(tif,nrows,TIFFScanlineSize64(tif),module));
  120. }
  121. tmsize_t
  122. TIFFVStripSize(TIFF* tif, uint32 nrows)
  123. {
  124. static const char module[] = "TIFFVStripSize";
  125. uint64 m;
  126. m=TIFFVStripSize64(tif,nrows);
  127. return _TIFFCastUInt64ToSSize(tif, m, module);
  128. }
  129. /*
  130. * Compute the # bytes in a raw strip.
  131. */
  132. uint64
  133. TIFFRawStripSize64(TIFF* tif, uint32 strip)
  134. {
  135. static const char module[] = "TIFFRawStripSize64";
  136. uint64 bytecount = TIFFGetStrileByteCount(tif, strip);
  137. if (bytecount == 0)
  138. {
  139. #if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
  140. TIFFErrorExt(tif->tif_clientdata, module,
  141. "%I64u: Invalid strip byte count, strip %lu",
  142. (unsigned __int64) bytecount,
  143. (unsigned long) strip);
  144. #else
  145. TIFFErrorExt(tif->tif_clientdata, module,
  146. "%llu: Invalid strip byte count, strip %lu",
  147. (unsigned long long) bytecount,
  148. (unsigned long) strip);
  149. #endif
  150. bytecount = (uint64) -1;
  151. }
  152. return bytecount;
  153. }
  154. tmsize_t
  155. TIFFRawStripSize(TIFF* tif, uint32 strip)
  156. {
  157. static const char module[] = "TIFFRawStripSize";
  158. uint64 m;
  159. tmsize_t n;
  160. m=TIFFRawStripSize64(tif,strip);
  161. if (m==(uint64)(-1))
  162. n=(tmsize_t)(-1);
  163. else
  164. {
  165. n=(tmsize_t)m;
  166. if ((uint64)n!=m)
  167. {
  168. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  169. n=0;
  170. }
  171. }
  172. return(n);
  173. }
  174. /*
  175. * Compute the # bytes in a (row-aligned) strip.
  176. *
  177. * Note that if RowsPerStrip is larger than the
  178. * recorded ImageLength, then the strip size is
  179. * truncated to reflect the actual space required
  180. * to hold the strip.
  181. */
  182. uint64
  183. TIFFStripSize64(TIFF* tif)
  184. {
  185. TIFFDirectory* td = &tif->tif_dir;
  186. uint32 rps = td->td_rowsperstrip;
  187. if (rps > td->td_imagelength)
  188. rps = td->td_imagelength;
  189. return (TIFFVStripSize64(tif, rps));
  190. }
  191. tmsize_t
  192. TIFFStripSize(TIFF* tif)
  193. {
  194. static const char module[] = "TIFFStripSize";
  195. uint64 m;
  196. m=TIFFStripSize64(tif);
  197. return _TIFFCastUInt64ToSSize(tif, m, module);
  198. }
  199. /*
  200. * Compute a default strip size based on the image
  201. * characteristics and a requested value. If the
  202. * request is <1 then we choose a strip size according
  203. * to certain heuristics.
  204. */
  205. uint32
  206. TIFFDefaultStripSize(TIFF* tif, uint32 request)
  207. {
  208. return (*tif->tif_defstripsize)(tif, request);
  209. }
  210. uint32
  211. _TIFFDefaultStripSize(TIFF* tif, uint32 s)
  212. {
  213. if ((int32) s < 1) {
  214. /*
  215. * If RowsPerStrip is unspecified, try to break the
  216. * image up into strips that are approximately
  217. * STRIP_SIZE_DEFAULT bytes long.
  218. */
  219. uint64 scanlinesize;
  220. uint64 rows;
  221. scanlinesize=TIFFScanlineSize64(tif);
  222. if (scanlinesize==0)
  223. scanlinesize=1;
  224. rows=(uint64)STRIP_SIZE_DEFAULT/scanlinesize;
  225. if (rows==0)
  226. rows=1;
  227. else if (rows>0xFFFFFFFF)
  228. rows=0xFFFFFFFF;
  229. s=(uint32)rows;
  230. }
  231. return (s);
  232. }
  233. /*
  234. * Return the number of bytes to read/write in a call to
  235. * one of the scanline-oriented i/o routines. Note that
  236. * this number may be 1/samples-per-pixel if data is
  237. * stored as separate planes.
  238. * The ScanlineSize in case of YCbCrSubsampling is defined as the
  239. * strip size divided by the strip height, i.e. the size of a pack of vertical
  240. * subsampling lines divided by vertical subsampling. It should thus make
  241. * sense when multiplied by a multiple of vertical subsampling.
  242. */
  243. uint64
  244. TIFFScanlineSize64(TIFF* tif)
  245. {
  246. static const char module[] = "TIFFScanlineSize64";
  247. TIFFDirectory *td = &tif->tif_dir;
  248. uint64 scanline_size;
  249. if (td->td_planarconfig==PLANARCONFIG_CONTIG)
  250. {
  251. if ((td->td_photometric==PHOTOMETRIC_YCBCR)&&
  252. (td->td_samplesperpixel==3)&&
  253. (!isUpSampled(tif)))
  254. {
  255. uint16 ycbcrsubsampling[2];
  256. uint16 samplingblock_samples;
  257. uint32 samplingblocks_hor;
  258. uint64 samplingrow_samples;
  259. uint64 samplingrow_size;
  260. if(td->td_samplesperpixel!=3)
  261. {
  262. TIFFErrorExt(tif->tif_clientdata,module,
  263. "Invalid td_samplesperpixel value");
  264. return 0;
  265. }
  266. TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,
  267. ycbcrsubsampling+0,
  268. ycbcrsubsampling+1);
  269. if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
  270. ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
  271. {
  272. TIFFErrorExt(tif->tif_clientdata,module,
  273. "Invalid YCbCr subsampling");
  274. return 0;
  275. }
  276. samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
  277. samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
  278. samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
  279. samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
  280. scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
  281. }
  282. else
  283. {
  284. uint64 scanline_samples;
  285. scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
  286. scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
  287. }
  288. }
  289. else
  290. {
  291. scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,td->td_imagewidth,td->td_bitspersample,module),8);
  292. }
  293. if (scanline_size == 0)
  294. {
  295. TIFFErrorExt(tif->tif_clientdata,module,"Computed scanline size is zero");
  296. return 0;
  297. }
  298. return(scanline_size);
  299. }
  300. tmsize_t
  301. TIFFScanlineSize(TIFF* tif)
  302. {
  303. static const char module[] = "TIFFScanlineSize";
  304. uint64 m;
  305. m=TIFFScanlineSize64(tif);
  306. return _TIFFCastUInt64ToSSize(tif, m, module);
  307. }
  308. /*
  309. * Return the number of bytes required to store a complete
  310. * decoded and packed raster scanline (as opposed to the
  311. * I/O size returned by TIFFScanlineSize which may be less
  312. * if data is store as separate planes).
  313. */
  314. uint64
  315. TIFFRasterScanlineSize64(TIFF* tif)
  316. {
  317. static const char module[] = "TIFFRasterScanlineSize64";
  318. TIFFDirectory *td = &tif->tif_dir;
  319. uint64 scanline;
  320. scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
  321. if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
  322. scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
  323. return (TIFFhowmany8_64(scanline));
  324. } else
  325. return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
  326. td->td_samplesperpixel, module));
  327. }
  328. tmsize_t
  329. TIFFRasterScanlineSize(TIFF* tif)
  330. {
  331. static const char module[] = "TIFFRasterScanlineSize";
  332. uint64 m;
  333. m=TIFFRasterScanlineSize64(tif);
  334. return _TIFFCastUInt64ToSSize(tif, m, module);
  335. }
  336. /* vim: set ts=8 sts=8 sw=8 noet: */
  337. /*
  338. * Local Variables:
  339. * mode: c
  340. * c-basic-offset: 8
  341. * fill-column: 78
  342. * End:
  343. */