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