tif_strip.c 13 KB

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