tif_strip.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_t
  34. TIFFComputeStrip(TIFF* tif, uint32_t row, uint16_t sample)
  35. {
  36. static const char module[] = "TIFFComputeStrip";
  37. TIFFDirectory *td = &tif->tif_dir;
  38. uint32_t 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_t)sample * td->td_stripsperimage;
  48. }
  49. return (strip);
  50. }
  51. /*
  52. * Compute how many strips are in an image.
  53. */
  54. uint32_t
  55. TIFFNumberOfStrips(TIFF* tif)
  56. {
  57. TIFFDirectory *td = &tif->tif_dir;
  58. uint32_t nstrips;
  59. nstrips = (td->td_rowsperstrip == (uint32_t) -1 ? 1 :
  60. TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
  61. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  62. nstrips = _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
  63. "TIFFNumberOfStrips");
  64. return (nstrips);
  65. }
  66. /*
  67. * Compute the # bytes in a variable height, row-aligned strip.
  68. */
  69. uint64_t
  70. TIFFVStripSize64(TIFF* tif, uint32_t nrows)
  71. {
  72. static const char module[] = "TIFFVStripSize64";
  73. TIFFDirectory *td = &tif->tif_dir;
  74. if (nrows==(uint32_t)(-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_t ycbcrsubsampling[2];
  89. uint16_t samplingblock_samples;
  90. uint32_t samplingblocks_hor;
  91. uint32_t samplingblocks_ver;
  92. uint64_t samplingrow_samples;
  93. uint64_t 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_t nrows)
  123. {
  124. static const char module[] = "TIFFVStripSize";
  125. uint64_t 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_t
  133. TIFFRawStripSize64(TIFF* tif, uint32_t strip)
  134. {
  135. static const char module[] = "TIFFRawStripSize64";
  136. uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
  137. if (bytecount == 0)
  138. {
  139. TIFFErrorExt(tif->tif_clientdata, module,
  140. "%"PRIu64": Invalid strip byte count, strip %lu",
  141. (uint64_t) bytecount,
  142. (unsigned long) strip);
  143. bytecount = (uint64_t) -1;
  144. }
  145. return bytecount;
  146. }
  147. tmsize_t
  148. TIFFRawStripSize(TIFF* tif, uint32_t strip)
  149. {
  150. static const char module[] = "TIFFRawStripSize";
  151. uint64_t m;
  152. tmsize_t n;
  153. m=TIFFRawStripSize64(tif,strip);
  154. if (m==(uint64_t)(-1))
  155. n=(tmsize_t)(-1);
  156. else
  157. {
  158. n=(tmsize_t)m;
  159. if ((uint64_t)n != m)
  160. {
  161. TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
  162. n=0;
  163. }
  164. }
  165. return(n);
  166. }
  167. /*
  168. * Compute the # bytes in a (row-aligned) strip.
  169. *
  170. * Note that if RowsPerStrip is larger than the
  171. * recorded ImageLength, then the strip size is
  172. * truncated to reflect the actual space required
  173. * to hold the strip.
  174. */
  175. uint64_t
  176. TIFFStripSize64(TIFF* tif)
  177. {
  178. TIFFDirectory* td = &tif->tif_dir;
  179. uint32_t rps = td->td_rowsperstrip;
  180. if (rps > td->td_imagelength)
  181. rps = td->td_imagelength;
  182. return (TIFFVStripSize64(tif, rps));
  183. }
  184. tmsize_t
  185. TIFFStripSize(TIFF* tif)
  186. {
  187. static const char module[] = "TIFFStripSize";
  188. uint64_t m;
  189. m=TIFFStripSize64(tif);
  190. return _TIFFCastUInt64ToSSize(tif, m, module);
  191. }
  192. /*
  193. * Compute a default strip size based on the image
  194. * characteristics and a requested value. If the
  195. * request is <1 then we choose a strip size according
  196. * to certain heuristics.
  197. */
  198. uint32_t
  199. TIFFDefaultStripSize(TIFF* tif, uint32_t request)
  200. {
  201. return (*tif->tif_defstripsize)(tif, request);
  202. }
  203. uint32_t
  204. _TIFFDefaultStripSize(TIFF* tif, uint32_t s)
  205. {
  206. if ((int32_t) s < 1) {
  207. /*
  208. * If RowsPerStrip is unspecified, try to break the
  209. * image up into strips that are approximately
  210. * STRIP_SIZE_DEFAULT bytes long.
  211. */
  212. uint64_t scanlinesize;
  213. uint64_t rows;
  214. scanlinesize=TIFFScanlineSize64(tif);
  215. if (scanlinesize==0)
  216. scanlinesize=1;
  217. rows= (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize;
  218. if (rows==0)
  219. rows=1;
  220. else if (rows>0xFFFFFFFF)
  221. rows=0xFFFFFFFF;
  222. s=(uint32_t)rows;
  223. }
  224. return (s);
  225. }
  226. /*
  227. * Return the number of bytes to read/write in a call to
  228. * one of the scanline-oriented i/o routines. Note that
  229. * this number may be 1/samples-per-pixel if data is
  230. * stored as separate planes.
  231. * The ScanlineSize in case of YCbCrSubsampling is defined as the
  232. * strip size divided by the strip height, i.e. the size of a pack of vertical
  233. * subsampling lines divided by vertical subsampling. It should thus make
  234. * sense when multiplied by a multiple of vertical subsampling.
  235. */
  236. uint64_t
  237. TIFFScanlineSize64(TIFF* tif)
  238. {
  239. static const char module[] = "TIFFScanlineSize64";
  240. TIFFDirectory *td = &tif->tif_dir;
  241. uint64_t scanline_size;
  242. if (td->td_planarconfig==PLANARCONFIG_CONTIG)
  243. {
  244. if ((td->td_photometric==PHOTOMETRIC_YCBCR)&&
  245. (td->td_samplesperpixel==3)&&
  246. (!isUpSampled(tif)))
  247. {
  248. uint16_t ycbcrsubsampling[2];
  249. uint16_t samplingblock_samples;
  250. uint32_t samplingblocks_hor;
  251. uint64_t samplingrow_samples;
  252. uint64_t samplingrow_size;
  253. if(td->td_samplesperpixel!=3)
  254. {
  255. TIFFErrorExt(tif->tif_clientdata,module,
  256. "Invalid td_samplesperpixel value");
  257. return 0;
  258. }
  259. TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,
  260. ycbcrsubsampling+0,
  261. ycbcrsubsampling+1);
  262. if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
  263. ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
  264. {
  265. TIFFErrorExt(tif->tif_clientdata,module,
  266. "Invalid YCbCr subsampling");
  267. return 0;
  268. }
  269. samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
  270. samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
  271. samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
  272. samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
  273. scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
  274. }
  275. else
  276. {
  277. uint64_t scanline_samples;
  278. scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
  279. scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
  280. }
  281. }
  282. else
  283. {
  284. scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,td->td_imagewidth,td->td_bitspersample,module),8);
  285. }
  286. if (scanline_size == 0)
  287. {
  288. TIFFErrorExt(tif->tif_clientdata,module,"Computed scanline size is zero");
  289. return 0;
  290. }
  291. return(scanline_size);
  292. }
  293. tmsize_t
  294. TIFFScanlineSize(TIFF* tif)
  295. {
  296. static const char module[] = "TIFFScanlineSize";
  297. uint64_t m;
  298. m=TIFFScanlineSize64(tif);
  299. return _TIFFCastUInt64ToSSize(tif, m, module);
  300. }
  301. /*
  302. * Return the number of bytes required to store a complete
  303. * decoded and packed raster scanline (as opposed to the
  304. * I/O size returned by TIFFScanlineSize which may be less
  305. * if data is store as separate planes).
  306. */
  307. uint64_t
  308. TIFFRasterScanlineSize64(TIFF* tif)
  309. {
  310. static const char module[] = "TIFFRasterScanlineSize64";
  311. TIFFDirectory *td = &tif->tif_dir;
  312. uint64_t scanline;
  313. scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
  314. if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
  315. scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
  316. return (TIFFhowmany8_64(scanline));
  317. } else
  318. return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
  319. td->td_samplesperpixel, module));
  320. }
  321. tmsize_t
  322. TIFFRasterScanlineSize(TIFF* tif)
  323. {
  324. static const char module[] = "TIFFRasterScanlineSize";
  325. uint64_t m;
  326. m=TIFFRasterScanlineSize64(tif);
  327. return _TIFFCastUInt64ToSSize(tif, m, module);
  328. }
  329. /* vim: set ts=8 sts=8 sw=8 noet: */
  330. /*
  331. * Local Variables:
  332. * mode: c
  333. * c-basic-offset: 8
  334. * fill-column: 78
  335. * End:
  336. */