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