tif_strip.c 12 KB

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