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