tif_strip.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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. if (td->td_rowsperstrip == 0)
  39. {
  40. TIFFErrorExtR(tif, module, "Cannot compute strip: RowsPerStrip is zero");
  41. return 0;
  42. }
  43. strip = row / td->td_rowsperstrip;
  44. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  45. {
  46. if (sample >= td->td_samplesperpixel)
  47. {
  48. TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
  49. (unsigned long)sample,
  50. (unsigned long)td->td_samplesperpixel);
  51. return (0);
  52. }
  53. strip += (uint32_t)sample * td->td_stripsperimage;
  54. }
  55. return (strip);
  56. }
  57. /*
  58. * Compute how many strips are in an image.
  59. */
  60. uint32_t TIFFNumberOfStrips(TIFF *tif)
  61. {
  62. TIFFDirectory *td = &tif->tif_dir;
  63. uint32_t nstrips;
  64. if (td->td_rowsperstrip == 0)
  65. {
  66. TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");
  67. return 0;
  68. }
  69. nstrips = (td->td_rowsperstrip == (uint32_t)-1
  70. ? 1
  71. : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
  72. if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
  73. nstrips =
  74. _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
  75. "TIFFNumberOfStrips");
  76. return (nstrips);
  77. }
  78. /*
  79. * Compute the # bytes in a variable height, row-aligned strip.
  80. */
  81. uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
  82. {
  83. static const char module[] = "TIFFVStripSize64";
  84. TIFFDirectory *td = &tif->tif_dir;
  85. if (nrows == (uint32_t)(-1))
  86. nrows = td->td_imagelength;
  87. if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
  88. (td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
  89. {
  90. /*
  91. * Packed YCbCr data contain one Cb+Cr for every
  92. * HorizontalSampling*VerticalSampling Y values.
  93. * Must also roundup width and height when calculating
  94. * since images that are not a multiple of the
  95. * horizontal/vertical subsampling area include
  96. * YCbCr data for the extended image.
  97. */
  98. uint16_t ycbcrsubsampling[2];
  99. uint16_t samplingblock_samples;
  100. uint32_t samplingblocks_hor;
  101. uint32_t samplingblocks_ver;
  102. uint64_t samplingrow_samples;
  103. uint64_t samplingrow_size;
  104. if (td->td_samplesperpixel != 3)
  105. {
  106. TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
  107. return 0;
  108. }
  109. TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
  110. ycbcrsubsampling + 0, ycbcrsubsampling + 1);
  111. if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
  112. ycbcrsubsampling[0] != 4) ||
  113. (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
  114. ycbcrsubsampling[1] != 4) ||
  115. (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))
  116. {
  117. TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
  118. ycbcrsubsampling[0], ycbcrsubsampling[1]);
  119. return 0;
  120. }
  121. samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
  122. samplingblocks_hor =
  123. TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
  124. samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
  125. samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
  126. samplingblock_samples, module);
  127. samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
  128. tif, samplingrow_samples, td->td_bitspersample, module));
  129. return (
  130. _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
  131. }
  132. else
  133. return (_TIFFMultiply64(tif, nrows, TIFFScanlineSize64(tif), module));
  134. }
  135. tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
  136. {
  137. static const char module[] = "TIFFVStripSize";
  138. uint64_t m;
  139. m = TIFFVStripSize64(tif, nrows);
  140. return _TIFFCastUInt64ToSSize(tif, m, module);
  141. }
  142. /*
  143. * Compute the # bytes in a raw strip.
  144. */
  145. uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip)
  146. {
  147. static const char module[] = "TIFFRawStripSize64";
  148. uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
  149. if (bytecount == 0)
  150. {
  151. TIFFErrorExtR(tif, module,
  152. "%" PRIu64 ": Invalid strip byte count, strip %lu",
  153. (uint64_t)bytecount, (unsigned long)strip);
  154. bytecount = (uint64_t)-1;
  155. }
  156. return bytecount;
  157. }
  158. tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip)
  159. {
  160. static const char module[] = "TIFFRawStripSize";
  161. uint64_t m;
  162. tmsize_t n;
  163. m = TIFFRawStripSize64(tif, strip);
  164. if (m == (uint64_t)(-1))
  165. n = (tmsize_t)(-1);
  166. else
  167. {
  168. n = (tmsize_t)m;
  169. if ((uint64_t)n != m)
  170. {
  171. TIFFErrorExtR(tif, module, "Integer overflow");
  172. n = 0;
  173. }
  174. }
  175. return (n);
  176. }
  177. /*
  178. * Compute the # bytes in a (row-aligned) strip.
  179. *
  180. * Note that if RowsPerStrip is larger than the
  181. * recorded ImageLength, then the strip size is
  182. * truncated to reflect the actual space required
  183. * to hold the strip.
  184. */
  185. uint64_t TIFFStripSize64(TIFF *tif)
  186. {
  187. TIFFDirectory *td = &tif->tif_dir;
  188. uint32_t rps = td->td_rowsperstrip;
  189. if (rps > td->td_imagelength)
  190. rps = td->td_imagelength;
  191. return (TIFFVStripSize64(tif, rps));
  192. }
  193. tmsize_t TIFFStripSize(TIFF *tif)
  194. {
  195. static const char module[] = "TIFFStripSize";
  196. uint64_t m;
  197. m = TIFFStripSize64(tif);
  198. return _TIFFCastUInt64ToSSize(tif, m, module);
  199. }
  200. /*
  201. * Compute a default strip size based on the image
  202. * characteristics and a requested value. If the
  203. * request is <1 then we choose a strip size according
  204. * to certain heuristics.
  205. */
  206. uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request)
  207. {
  208. return (*tif->tif_defstripsize)(tif, request);
  209. }
  210. uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
  211. {
  212. if ((int32_t)s < 1)
  213. {
  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_t scanlinesize;
  220. uint64_t rows;
  221. scanlinesize = TIFFScanlineSize64(tif);
  222. if (scanlinesize == 0)
  223. scanlinesize = 1;
  224. rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize;
  225. if (rows == 0)
  226. rows = 1;
  227. else if (rows > 0xFFFFFFFF)
  228. rows = 0xFFFFFFFF;
  229. s = (uint32_t)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_t TIFFScanlineSize64(TIFF *tif)
  244. {
  245. static const char module[] = "TIFFScanlineSize64";
  246. TIFFDirectory *td = &tif->tif_dir;
  247. uint64_t scanline_size;
  248. if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  249. {
  250. if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&
  251. (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
  252. {
  253. uint16_t ycbcrsubsampling[2];
  254. uint16_t samplingblock_samples;
  255. uint32_t samplingblocks_hor;
  256. uint64_t samplingrow_samples;
  257. uint64_t samplingrow_size;
  258. if (td->td_samplesperpixel != 3)
  259. {
  260. TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
  261. return 0;
  262. }
  263. TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
  264. ycbcrsubsampling + 0, ycbcrsubsampling + 1);
  265. if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&
  266. (ycbcrsubsampling[0] != 4)) ||
  267. ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&
  268. (ycbcrsubsampling[1] != 4)) ||
  269. ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))
  270. {
  271. TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");
  272. return 0;
  273. }
  274. samplingblock_samples =
  275. ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
  276. samplingblocks_hor =
  277. TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
  278. samplingrow_samples = _TIFFMultiply64(
  279. tif, samplingblocks_hor, samplingblock_samples, module);
  280. samplingrow_size =
  281. TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,
  282. td->td_bitspersample, module),
  283. 8);
  284. scanline_size = (samplingrow_size / ycbcrsubsampling[1]);
  285. }
  286. else
  287. {
  288. uint64_t scanline_samples;
  289. uint32_t scanline_width = td->td_imagewidth;
  290. #if 0
  291. // Tries to fix https://gitlab.com/libtiff/libtiff/-/merge_requests/564
  292. // but causes regression when decoding legit files with tiffcp -c none
  293. // Cf https://gitlab.com/libtiff/libtiff/-/merge_requests/644
  294. if (td->td_photometric == PHOTOMETRIC_YCBCR)
  295. {
  296. uint16_t subsampling_hor;
  297. uint16_t ignored;
  298. TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
  299. &subsampling_hor, &ignored);
  300. if (subsampling_hor > 1) // roundup width for YCbCr
  301. scanline_width =
  302. TIFFroundup_32(scanline_width, subsampling_hor);
  303. }
  304. #endif
  305. scanline_samples = _TIFFMultiply64(tif, scanline_width,
  306. td->td_samplesperpixel, module);
  307. scanline_size =
  308. TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
  309. td->td_bitspersample, module),
  310. 8);
  311. }
  312. }
  313. else
  314. {
  315. scanline_size =
  316. TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,
  317. td->td_bitspersample, module),
  318. 8);
  319. }
  320. if (scanline_size == 0)
  321. {
  322. TIFFWarningExtR(tif, module, "Computed scanline size is zero");
  323. return 0;
  324. }
  325. return (scanline_size);
  326. }
  327. tmsize_t TIFFScanlineSize(TIFF *tif)
  328. {
  329. static const char module[] = "TIFFScanlineSize";
  330. uint64_t m;
  331. m = TIFFScanlineSize64(tif);
  332. return _TIFFCastUInt64ToSSize(tif, m, module);
  333. }
  334. /*
  335. * Return the number of bytes required to store a complete
  336. * decoded and packed raster scanline (as opposed to the
  337. * I/O size returned by TIFFScanlineSize which may be less
  338. * if data is store as separate planes).
  339. */
  340. uint64_t TIFFRasterScanlineSize64(TIFF *tif)
  341. {
  342. static const char module[] = "TIFFRasterScanlineSize64";
  343. TIFFDirectory *td = &tif->tif_dir;
  344. uint64_t scanline;
  345. scanline =
  346. _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
  347. if (td->td_planarconfig == PLANARCONFIG_CONTIG)
  348. {
  349. scanline =
  350. _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
  351. return (TIFFhowmany8_64(scanline));
  352. }
  353. else
  354. return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
  355. td->td_samplesperpixel, module));
  356. }
  357. tmsize_t TIFFRasterScanlineSize(TIFF *tif)
  358. {
  359. static const char module[] = "TIFFRasterScanlineSize";
  360. uint64_t m;
  361. m = TIFFRasterScanlineSize64(tif);
  362. return _TIFFCastUInt64ToSSize(tif, m, module);
  363. }