Debian Codegraph @

texk/xdvipsk/thirdparty/libtiff/libtiff-src/tif_strip.c

raw
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 uint32_t TIFFComputeStrip(TIFF *tif, uint32_t row, uint16_t sample)
36 {
37 static const char module[] = "TIFFComputeStrip";
38 TIFFDirectory *td = &tif->tif_dir;
39 uint32_t strip;
40
41 if (td->td_rowsperstrip == 0)
42 {
43 TIFFErrorExtR(tif, module, "Cannot compute strip: RowsPerStrip is zero");
44 return 0;
45 }
46 strip = row / td->td_rowsperstrip;
47 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
48 {
49 if (sample >= td->td_samplesperpixel)
50 {
51 TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
52 (unsigned long)sample,
53 (unsigned long)td->td_samplesperpixel);
54 return (0);
55 }
56 strip += (uint32_t)sample * td->td_stripsperimage;
57 }
58 return (strip);
59 }
60
61 /*
62 * Compute how many strips are in an image.
63 */
64 uint32_t TIFFNumberOfStrips(TIFF *tif)
65 {
66 TIFFDirectory *td = &tif->tif_dir;
67 uint32_t nstrips;
68
69 if (td->td_rowsperstrip == 0)
70 {
71 TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");
72 return 0;
73 }
74 nstrips = (td->td_rowsperstrip == (uint32_t)-1
75 ? 1
76 : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
77 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
78 nstrips =
79 _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
80 "TIFFNumberOfStrips");
81 return (nstrips);
82 }
83
84 /*
85 * Compute the # bytes in a variable height, row-aligned strip.
86 */
87 uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
88 {
89 static const char module[] = "TIFFVStripSize64";
90 TIFFDirectory *td = &tif->tif_dir;
91 if (nrows == (uint32_t)(-1))
92 nrows = td->td_imagelength;
93 if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
94 (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 if (td->td_samplesperpixel != 3)
111 {
112 TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
113 return 0;
114 }
115 TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
116 ycbcrsubsampling + 0, ycbcrsubsampling + 1);
117 if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
118 ycbcrsubsampling[0] != 4) ||
119 (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
120 ycbcrsubsampling[1] != 4) ||
121 (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))
122 {
123 TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
124 ycbcrsubsampling[0], ycbcrsubsampling[1]);
125 return 0;
126 }
127 samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
128 samplingblocks_hor =
129 TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
130 samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
131 samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
132 samplingblock_samples, module);
133 samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
134 tif, samplingrow_samples, td->td_bitspersample, module));
135 return (
136 _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
137 }
138 else
139 return (_TIFFMultiply64(tif, nrows, TIFFScanlineSize64(tif), module));
140 }
141 tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
142 {
143 static const char module[] = "TIFFVStripSize";
144 uint64_t m;
145 m = TIFFVStripSize64(tif, nrows);
146 return _TIFFCastUInt64ToSSize(tif, m, module);
147 }
148
149 /*
150 * Compute the # bytes in a raw strip.
151 */
152 uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip)
153 {
154 static const char module[] = "TIFFRawStripSize64";
155 uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
156
157 if (bytecount == 0)
158 {
159 TIFFErrorExtR(tif, module,
160 "%" PRIu64 ": Invalid strip byte count, strip %lu",
161 (uint64_t)bytecount, (unsigned long)strip);
162 bytecount = (uint64_t)-1;
163 }
164
165 return bytecount;
166 }
167 tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip)
168 {
169 static const char module[] = "TIFFRawStripSize";
170 uint64_t m;
171 tmsize_t n;
172 m = TIFFRawStripSize64(tif, strip);
173 if (m == (uint64_t)(-1))
174 n = (tmsize_t)(-1);
175 else
176 {
177 n = (tmsize_t)m;
178 if ((uint64_t)n != m)
179 {
180 TIFFErrorExtR(tif, module, "Integer overflow");
181 n = 0;
182 }
183 }
184 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 uint64_t TIFFStripSize64(TIFF *tif)
196 {
197 TIFFDirectory *td = &tif->tif_dir;
198 uint32_t rps = td->td_rowsperstrip;
199 if (rps > td->td_imagelength)
200 rps = td->td_imagelength;
201 return (TIFFVStripSize64(tif, rps));
202 }
203 tmsize_t TIFFStripSize(TIFF *tif)
204 {
205 static const char module[] = "TIFFStripSize";
206 uint64_t m;
207 m = TIFFStripSize64(tif);
208 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 uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request)
218 {
219 return (*tif->tif_defstripsize)(tif, request);
220 }
221
222 uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
223 {
224 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 scanlinesize = TIFFScanlineSize64(tif);
234 if (scanlinesize == 0)
235 scanlinesize = 1;
236 rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize;
237 if (rows == 0)
238 rows = 1;
239 else if (rows > 0xFFFFFFFF)
240 rows = 0xFFFFFFFF;
241 s = (uint32_t)rows;
242 }
243 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 uint64_t TIFFScanlineSize64(TIFF *tif)
257 {
258 static const char module[] = "TIFFScanlineSize64";
259 TIFFDirectory *td = &tif->tif_dir;
260 uint64_t scanline_size;
261 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
262 {
263 if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&
264 (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
265 {
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 if (td->td_samplesperpixel != 3)
272 {
273 TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
274 return 0;
275 }
276 TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
277 ycbcrsubsampling + 0, ycbcrsubsampling + 1);
278 if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&
279 (ycbcrsubsampling[0] != 4)) ||
280 ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&
281 (ycbcrsubsampling[1] != 4)) ||
282 ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))
283 {
284 TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");
285 return 0;
286 }
287 samplingblock_samples =
288 ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
289 samplingblocks_hor =
290 TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
291 samplingrow_samples = _TIFFMultiply64(
292 tif, samplingblocks_hor, samplingblock_samples, module);
293 samplingrow_size =
294 TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,
295 td->td_bitspersample, module),
296 8);
297 scanline_size = (samplingrow_size / ycbcrsubsampling[1]);
298 }
299 else
300 {
301 uint64_t scanline_samples;
302 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 scanline_samples = _TIFFMultiply64(tif, scanline_width,
321 td->td_samplesperpixel, module);
322 scanline_size =
323 TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
324 td->td_bitspersample, module),
325 8);
326 }
327 }
328 else
329 {
330 scanline_size =
331 TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,
332 td->td_bitspersample, module),
333 8);
334 }
335 if (scanline_size == 0)
336 {
337 TIFFErrorExtR(tif, module, "Computed scanline size is zero");
338 return 0;
339 }
340 return (scanline_size);
341 }
342 tmsize_t TIFFScanlineSize(TIFF *tif)
343 {
344 static const char module[] = "TIFFScanlineSize";
345 uint64_t m;
346 m = TIFFScanlineSize64(tif);
347 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 uint64_t TIFFRasterScanlineSize64(TIFF *tif)
357 {
358 static const char module[] = "TIFFRasterScanlineSize64";
359 TIFFDirectory *td = &tif->tif_dir;
360 uint64_t scanline;
361
362 scanline =
363 _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
364 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
365 {
366 scanline =
367 _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
368 return (TIFFhowmany8_64(scanline));
369 }
370 else
371 return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
372 td->td_samplesperpixel, module));
373 }
374 tmsize_t TIFFRasterScanlineSize(TIFF *tif)
375 {
376 static const char module[] = "TIFFRasterScanlineSize";
377 uint64_t m;
378 m = TIFFRasterScanlineSize64(tif);
379 return _TIFFCastUInt64ToSSize(tif, m, module);
380 }