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