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 183786 : uint32_t TIFFNumberOfStrips(TIFF *tif)
66 : {
67 183786 : TIFFDirectory *td = &tif->tif_dir;
68 : uint32_t nstrips;
69 :
70 183786 : if (td->td_rowsperstrip == 0)
71 : {
72 0 : TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");
73 0 : return 0;
74 : }
75 367572 : nstrips = (td->td_rowsperstrip == (uint32_t)-1
76 : ? 1
77 183786 : : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
78 183786 : if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
79 : nstrips =
80 14732 : _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
81 : "TIFFNumberOfStrips");
82 183727 : 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 4919910 : uint64_t _TIFFStrileSize64(TIFF *tif, uint32_t nrows, int isStrip)
90 : {
91 : static const char module[] = "_TIFFStrileSize64";
92 4919910 : TIFFDirectory *td = &tif->tif_dir;
93 4919910 : if (isStrip)
94 : {
95 4748690 : if (nrows == (uint32_t)(-1))
96 0 : nrows = td->td_imagelength;
97 : }
98 : else
99 : {
100 171219 : if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
101 171640 : td->td_tiledepth == 0)
102 0 : return (0);
103 : }
104 4919910 : if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
105 4814240 : (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 1744 : if (td->td_samplesperpixel != 3)
122 : {
123 0 : TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
124 0 : return 0;
125 : }
126 1744 : TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
127 : ycbcrsubsampling + 0, ycbcrsubsampling + 1);
128 1744 : if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
129 15 : ycbcrsubsampling[0] != 4) ||
130 1744 : (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
131 8 : ycbcrsubsampling[1] != 4) ||
132 1744 : (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 1744 : samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
139 1744 : const uint32_t width = isStrip ? td->td_imagewidth : td->td_tilewidth;
140 1744 : samplingblocks_hor = TIFFhowmany_32(width, ycbcrsubsampling[0]);
141 1744 : samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
142 1744 : samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
143 : samplingblock_samples, module);
144 1744 : samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
145 : tif, samplingrow_samples, td->td_bitspersample, module));
146 : return (
147 1744 : _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
148 : }
149 : else
150 9836570 : return (_TIFFMultiply64(tif, nrows,
151 4747710 : isStrip ? TIFFScanlineSize64(tif)
152 170454 : : TIFFTileRowSize64(tif),
153 : module));
154 : }
155 :
156 : /*
157 : * Compute the # bytes in a variable height, row-aligned strip.
158 : */
159 4748370 : uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
160 : {
161 4748370 : return _TIFFStrileSize64(tif, nrows, /* isStrip = */ TRUE);
162 : }
163 :
164 2102050 : tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
165 : {
166 : static const char module[] = "TIFFVStripSize";
167 : uint64_t m;
168 2102050 : m = TIFFVStripSize64(tif, nrows);
169 2102050 : return _TIFFCastUInt64ToSSize(tif, m, module);
170 : }
171 :
172 : /*
173 : * Compute the # bytes in a raw strip.
174 : */
175 0 : uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip)
176 : {
177 : static const char module[] = "TIFFRawStripSize64";
178 0 : uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
179 :
180 0 : if (bytecount == 0)
181 : {
182 0 : TIFFErrorExtR(tif, module,
183 : "%" PRIu64 ": Invalid strip byte count, strip %lu",
184 : (uint64_t)bytecount, (unsigned long)strip);
185 0 : bytecount = (uint64_t)-1;
186 : }
187 :
188 0 : return bytecount;
189 : }
190 0 : tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip)
191 : {
192 : static const char module[] = "TIFFRawStripSize";
193 : uint64_t m;
194 : tmsize_t n;
195 0 : m = TIFFRawStripSize64(tif, strip);
196 0 : if (m == (uint64_t)(-1))
197 0 : n = (tmsize_t)(-1);
198 : else
199 : {
200 0 : n = (tmsize_t)m;
201 0 : if ((uint64_t)n != m)
202 : {
203 0 : TIFFErrorExtR(tif, module, "Integer overflow");
204 0 : n = 0;
205 : }
206 : }
207 0 : return (n);
208 : }
209 :
210 : /*
211 : * Compute the # bytes in a (row-aligned) strip.
212 : *
213 : * Note that if RowsPerStrip is larger than the
214 : * recorded ImageLength, then the strip size is
215 : * truncated to reflect the actual space required
216 : * to hold the strip.
217 : */
218 2631810 : uint64_t TIFFStripSize64(TIFF *tif)
219 : {
220 2631810 : TIFFDirectory *td = &tif->tif_dir;
221 2631810 : uint32_t rps = td->td_rowsperstrip;
222 2631810 : if (rps > td->td_imagelength)
223 119 : rps = td->td_imagelength;
224 2631810 : return (TIFFVStripSize64(tif, rps));
225 : }
226 2601970 : tmsize_t TIFFStripSize(TIFF *tif)
227 : {
228 : static const char module[] = "TIFFStripSize";
229 : uint64_t m;
230 2601970 : m = TIFFStripSize64(tif);
231 2601880 : return _TIFFCastUInt64ToSSize(tif, m, module);
232 : }
233 :
234 : /*
235 : * Compute a default strip size based on the image
236 : * characteristics and a requested value. If the
237 : * request is <1 then we choose a strip size according
238 : * to certain heuristics.
239 : */
240 6920 : uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request)
241 : {
242 6920 : return (*tif->tif_defstripsize)(tif, request);
243 : }
244 :
245 6920 : uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
246 : {
247 6920 : if ((int32_t)s < 1)
248 : {
249 : /*
250 : * If RowsPerStrip is unspecified, try to break the
251 : * image up into strips that are approximately
252 : * STRIP_SIZE_DEFAULT bytes long.
253 : */
254 : uint64_t scanlinesize;
255 : uint64_t rows;
256 6920 : scanlinesize = TIFFScanlineSize64(tif);
257 6920 : if (scanlinesize == 0)
258 0 : scanlinesize = 1;
259 6920 : rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize;
260 6920 : if (rows == 0)
261 32 : rows = 1;
262 6888 : else if (rows > 0xFFFFFFFF)
263 0 : rows = 0xFFFFFFFF;
264 6920 : s = (uint32_t)rows;
265 : }
266 6920 : return (s);
267 : }
268 :
269 : /*
270 : * Return the number of bytes to read/write in a call to
271 : * one of the scanline-oriented i/o routines. Note that
272 : * this number may be 1/samples-per-pixel if data is
273 : * stored as separate planes.
274 : * The ScanlineSize in case of YCbCrSubsampling is defined as the
275 : * strip size divided by the strip height, i.e. the size of a pack of vertical
276 : * subsampling lines divided by vertical subsampling. It should thus make
277 : * sense when multiplied by a multiple of vertical subsampling.
278 : */
279 4925080 : uint64_t TIFFScanlineSize64(TIFF *tif)
280 : {
281 : static const char module[] = "TIFFScanlineSize64";
282 4925080 : TIFFDirectory *td = &tif->tif_dir;
283 : uint64_t scanline_size;
284 4925080 : if (td->td_planarconfig == PLANARCONFIG_CONTIG)
285 : {
286 4791980 : if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&
287 14970 : (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
288 3291 : {
289 : uint16_t ycbcrsubsampling[2];
290 : uint16_t samplingblock_samples;
291 : uint32_t samplingblocks_hor;
292 : uint64_t samplingrow_samples;
293 : uint64_t samplingrow_size;
294 3291 : if (td->td_samplesperpixel != 3)
295 : {
296 0 : TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
297 0 : return 0;
298 : }
299 3291 : TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
300 : ycbcrsubsampling + 0, ycbcrsubsampling + 1);
301 3291 : if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&
302 10 : (ycbcrsubsampling[0] != 4)) ||
303 3291 : ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&
304 6 : (ycbcrsubsampling[1] != 4)) ||
305 3291 : ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))
306 : {
307 0 : TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");
308 0 : return 0;
309 : }
310 3291 : samplingblock_samples =
311 3291 : ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
312 3291 : samplingblocks_hor =
313 3291 : TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
314 3291 : samplingrow_samples = _TIFFMultiply64(
315 : tif, samplingblocks_hor, samplingblock_samples, module);
316 3291 : samplingrow_size =
317 3291 : TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,
318 : td->td_bitspersample, module),
319 : 8);
320 3291 : scanline_size = (samplingrow_size / ycbcrsubsampling[1]);
321 : }
322 : else
323 : {
324 : uint64_t scanline_samples;
325 4788680 : uint32_t scanline_width = td->td_imagewidth;
326 :
327 : #if 0
328 : // Tries to fix https://gitlab.com/libtiff/libtiff/-/merge_requests/564
329 : // but causes regression when decoding legit files with tiffcp -c none
330 : // Cf https://gitlab.com/libtiff/libtiff/-/merge_requests/644
331 : if (td->td_photometric == PHOTOMETRIC_YCBCR)
332 : {
333 : uint16_t subsampling_hor;
334 : uint16_t ignored;
335 : TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
336 : &subsampling_hor, &ignored);
337 : if (subsampling_hor > 1) // roundup width for YCbCr
338 : scanline_width =
339 : TIFFroundup_32(scanline_width, subsampling_hor);
340 : }
341 : #endif
342 :
343 4788680 : scanline_samples = _TIFFMultiply64(tif, scanline_width,
344 4788680 : td->td_samplesperpixel, module);
345 4788310 : scanline_size =
346 4788590 : TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
347 : td->td_bitspersample, module),
348 : 8);
349 : }
350 : }
351 : else
352 : {
353 133100 : scanline_size =
354 133104 : TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,
355 : td->td_bitspersample, module),
356 : 8);
357 : }
358 4924700 : if (scanline_size == 0)
359 : {
360 0 : TIFFErrorExtR(tif, module, "Computed scanline size is zero");
361 0 : return 0;
362 : }
363 4924700 : return (scanline_size);
364 : }
365 149626 : tmsize_t TIFFScanlineSize(TIFF *tif)
366 : {
367 : static const char module[] = "TIFFScanlineSize";
368 : uint64_t m;
369 149626 : m = TIFFScanlineSize64(tif);
370 149637 : return _TIFFCastUInt64ToSSize(tif, m, module);
371 : }
372 :
373 : /*
374 : * Return the number of bytes required to store a complete
375 : * decoded and packed raster scanline (as opposed to the
376 : * I/O size returned by TIFFScanlineSize which may be less
377 : * if data is store as separate planes).
378 : */
379 0 : uint64_t TIFFRasterScanlineSize64(TIFF *tif)
380 : {
381 : static const char module[] = "TIFFRasterScanlineSize64";
382 0 : TIFFDirectory *td = &tif->tif_dir;
383 : uint64_t scanline;
384 :
385 : scanline =
386 0 : _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
387 0 : if (td->td_planarconfig == PLANARCONFIG_CONTIG)
388 : {
389 : scanline =
390 0 : _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
391 0 : return (TIFFhowmany8_64(scanline));
392 : }
393 : else
394 0 : return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
395 0 : td->td_samplesperpixel, module));
396 : }
397 0 : tmsize_t TIFFRasterScanlineSize(TIFF *tif)
398 : {
399 : static const char module[] = "TIFFRasterScanlineSize";
400 : uint64_t m;
401 0 : m = TIFFRasterScanlineSize64(tif);
402 0 : return _TIFFCastUInt64ToSSize(tif, m, module);
403 : }
|