/src/gdal/frmts/gtiff/libtiff/tif_strip.c
Line | Count | Source |
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, |
44 | 0 | "Cannot compute strip: RowsPerStrip is zero"); |
45 | 0 | return 0; |
46 | 0 | } |
47 | 0 | strip = row / td->td_rowsperstrip; |
48 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
49 | 0 | { |
50 | 0 | uint64_t sample_offset; |
51 | 0 | uint64_t strip64; |
52 | 0 | if (sample >= td->td_samplesperpixel) |
53 | 0 | { |
54 | 0 | TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu", |
55 | 0 | (unsigned long)sample, |
56 | 0 | (unsigned long)td->td_samplesperpixel); |
57 | 0 | return (0); |
58 | 0 | } |
59 | 0 | sample_offset = _TIFFMultiply64(tif, sample, td->td_stripsperimage, |
60 | 0 | "TIFFComputeStrip"); |
61 | 0 | if (sample_offset == 0 && sample != 0 && td->td_stripsperimage != 0) |
62 | 0 | return (0); |
63 | 0 | strip64 = _TIFFAdd64(tif, sample_offset, strip, "TIFFComputeStrip"); |
64 | 0 | if (strip64 == 0 && (sample_offset != 0 || strip != 0)) |
65 | 0 | return (0); |
66 | 0 | strip = _TIFFCastUInt64ToUInt32(tif, strip64, "TIFFComputeStrip"); |
67 | 0 | if (strip == 0 && strip64 != 0) |
68 | 0 | return (0); |
69 | 0 | } |
70 | 0 | return (strip); |
71 | 0 | } |
72 | | |
73 | | /* |
74 | | * Compute how many strips are in an image. |
75 | | */ |
76 | | uint32_t TIFFNumberOfStrips(TIFF *tif) |
77 | 0 | { |
78 | 0 | TIFFDirectory *td = &tif->tif_dir; |
79 | 0 | uint32_t nstrips; |
80 | |
|
81 | 0 | if (td->td_rowsperstrip == 0) |
82 | 0 | { |
83 | 0 | TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero"); |
84 | 0 | return 0; |
85 | 0 | } |
86 | 0 | nstrips = (td->td_rowsperstrip == (uint32_t)-1 |
87 | 0 | ? 1 |
88 | 0 | : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip)); |
89 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
90 | 0 | nstrips = |
91 | 0 | _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel, |
92 | 0 | "TIFFNumberOfStrips"); |
93 | 0 | return (nstrips); |
94 | 0 | } |
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 | | uint64_t _TIFFStrileSize64(TIFF *tif, uint32_t nrows, int isStrip) |
101 | 0 | { |
102 | 0 | static const char module[] = "_TIFFStrileSize64"; |
103 | 0 | TIFFDirectory *td = &tif->tif_dir; |
104 | 0 | if (isStrip) |
105 | 0 | { |
106 | 0 | if (nrows == (uint32_t)(-1)) |
107 | 0 | nrows = td->td_imagelength; |
108 | 0 | } |
109 | 0 | else |
110 | 0 | { |
111 | 0 | if (td->td_tilelength == 0 || td->td_tilewidth == 0 || |
112 | 0 | td->td_tiledepth == 0) |
113 | 0 | return (0); |
114 | 0 | } |
115 | 0 | if ((td->td_planarconfig == PLANARCONFIG_CONTIG) && |
116 | 0 | (td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif))) |
117 | 0 | { |
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 | 0 | uint16_t ycbcrsubsampling[2]; |
127 | 0 | uint16_t samplingblock_samples; |
128 | 0 | uint32_t samplingblocks_hor; |
129 | 0 | uint32_t samplingblocks_ver; |
130 | 0 | uint64_t samplingrow_samples; |
131 | 0 | uint64_t samplingrow_size; |
132 | 0 | if (td->td_samplesperpixel != 3) |
133 | 0 | { |
134 | 0 | TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value"); |
135 | 0 | return 0; |
136 | 0 | } |
137 | 0 | TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, |
138 | 0 | ycbcrsubsampling + 0, ycbcrsubsampling + 1); |
139 | 0 | if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && |
140 | 0 | ycbcrsubsampling[0] != 4) || |
141 | 0 | (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && |
142 | 0 | ycbcrsubsampling[1] != 4) || |
143 | 0 | (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0)) |
144 | 0 | { |
145 | 0 | TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)", |
146 | 0 | ycbcrsubsampling[0], ycbcrsubsampling[1]); |
147 | 0 | return 0; |
148 | 0 | } |
149 | 0 | samplingblock_samples = |
150 | 0 | (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2); |
151 | 0 | const uint32_t width = isStrip ? td->td_imagewidth : td->td_tilewidth; |
152 | 0 | samplingblocks_hor = TIFFhowmany_32(width, ycbcrsubsampling[0]); |
153 | 0 | samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]); |
154 | 0 | samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor, |
155 | 0 | samplingblock_samples, module); |
156 | 0 | samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64( |
157 | 0 | tif, samplingrow_samples, td->td_bitspersample, module)); |
158 | 0 | return ( |
159 | 0 | _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module)); |
160 | 0 | } |
161 | 0 | else |
162 | 0 | return (_TIFFMultiply64(tif, nrows, |
163 | 0 | isStrip ? TIFFScanlineSize64(tif) |
164 | 0 | : TIFFTileRowSize64(tif), |
165 | 0 | module)); |
166 | 0 | } |
167 | | |
168 | | /* |
169 | | * Compute the # bytes in a variable height, row-aligned strip. |
170 | | */ |
171 | | uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows) |
172 | 0 | { |
173 | 0 | return _TIFFStrileSize64(tif, nrows, /* isStrip = */ TRUE); |
174 | 0 | } |
175 | | |
176 | | tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows) |
177 | 0 | { |
178 | 0 | static const char module[] = "TIFFVStripSize"; |
179 | 0 | uint64_t m; |
180 | 0 | m = TIFFVStripSize64(tif, nrows); |
181 | 0 | return _TIFFCastUInt64ToSSize(tif, m, module); |
182 | 0 | } |
183 | | |
184 | | /* |
185 | | * Compute the # bytes in a raw strip. |
186 | | */ |
187 | | uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip) |
188 | 0 | { |
189 | 0 | static const char module[] = "TIFFRawStripSize64"; |
190 | 0 | uint64_t bytecount = TIFFGetStrileByteCount(tif, strip); |
191 | |
|
192 | 0 | if (bytecount == 0) |
193 | 0 | { |
194 | 0 | TIFFErrorExtR(tif, module, |
195 | 0 | "%" PRIu64 ": Invalid strip byte count, strip %lu", |
196 | 0 | (uint64_t)bytecount, (unsigned long)strip); |
197 | 0 | bytecount = (uint64_t)-1; |
198 | 0 | } |
199 | |
|
200 | 0 | return bytecount; |
201 | 0 | } |
202 | | tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip) |
203 | 0 | { |
204 | 0 | static const char module[] = "TIFFRawStripSize"; |
205 | 0 | uint64_t m; |
206 | 0 | tmsize_t n; |
207 | 0 | m = TIFFRawStripSize64(tif, strip); |
208 | 0 | if (m == (uint64_t)(-1)) |
209 | 0 | n = (tmsize_t)(-1); |
210 | 0 | else |
211 | 0 | { |
212 | 0 | n = (tmsize_t)m; |
213 | 0 | if ((uint64_t)n != m) |
214 | 0 | { |
215 | 0 | TIFFErrorExtR(tif, module, "Integer overflow"); |
216 | 0 | n = 0; |
217 | 0 | } |
218 | 0 | } |
219 | 0 | return (n); |
220 | 0 | } |
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 | | uint64_t TIFFStripSize64(TIFF *tif) |
231 | 0 | { |
232 | 0 | TIFFDirectory *td = &tif->tif_dir; |
233 | 0 | uint32_t rps = td->td_rowsperstrip; |
234 | 0 | if (rps > td->td_imagelength) |
235 | 0 | rps = td->td_imagelength; |
236 | 0 | return (TIFFVStripSize64(tif, rps)); |
237 | 0 | } |
238 | | tmsize_t TIFFStripSize(TIFF *tif) |
239 | 0 | { |
240 | 0 | static const char module[] = "TIFFStripSize"; |
241 | 0 | uint64_t m; |
242 | 0 | m = TIFFStripSize64(tif); |
243 | 0 | return _TIFFCastUInt64ToSSize(tif, m, module); |
244 | 0 | } |
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 | | uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request) |
253 | 0 | { |
254 | 0 | return (*tif->tif_defstripsize)(tif, request); |
255 | 0 | } |
256 | | |
257 | | uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s) |
258 | 0 | { |
259 | 0 | if ((int32_t)s < 1) |
260 | 0 | { |
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 | 0 | uint64_t scanlinesize; |
267 | 0 | uint64_t rows; |
268 | 0 | scanlinesize = TIFFScanlineSize64(tif); |
269 | 0 | if (scanlinesize == 0) |
270 | 0 | scanlinesize = 1; |
271 | 0 | rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize; |
272 | 0 | if (rows == 0) |
273 | 0 | rows = 1; |
274 | 0 | else if (rows > 0xFFFFFFFF) |
275 | 0 | rows = 0xFFFFFFFF; |
276 | 0 | s = (uint32_t)rows; |
277 | 0 | } |
278 | 0 | return (s); |
279 | 0 | } |
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 | | uint64_t TIFFScanlineSize64(TIFF *tif) |
292 | 0 | { |
293 | 0 | static const char module[] = "TIFFScanlineSize64"; |
294 | 0 | TIFFDirectory *td = &tif->tif_dir; |
295 | 0 | uint64_t scanline_size; |
296 | 0 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
297 | 0 | { |
298 | 0 | if ((td->td_photometric == PHOTOMETRIC_YCBCR) && |
299 | 0 | (td->td_samplesperpixel == 3) && (!isUpSampled(tif))) |
300 | 0 | { |
301 | 0 | uint16_t ycbcrsubsampling[2]; |
302 | 0 | uint16_t samplingblock_samples; |
303 | 0 | uint32_t samplingblocks_hor; |
304 | 0 | uint64_t samplingrow_samples; |
305 | 0 | uint64_t samplingrow_size; |
306 | 0 | if (td->td_samplesperpixel != 3) |
307 | 0 | { |
308 | 0 | TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value"); |
309 | 0 | return 0; |
310 | 0 | } |
311 | 0 | TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING, |
312 | 0 | ycbcrsubsampling + 0, ycbcrsubsampling + 1); |
313 | 0 | if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) && |
314 | 0 | (ycbcrsubsampling[0] != 4)) || |
315 | 0 | ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) && |
316 | 0 | (ycbcrsubsampling[1] != 4)) || |
317 | 0 | ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0))) |
318 | 0 | { |
319 | 0 | TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling"); |
320 | 0 | return 0; |
321 | 0 | } |
322 | 0 | samplingblock_samples = |
323 | 0 | (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2); |
324 | 0 | samplingblocks_hor = |
325 | 0 | TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]); |
326 | 0 | samplingrow_samples = _TIFFMultiply64( |
327 | 0 | tif, samplingblocks_hor, samplingblock_samples, module); |
328 | 0 | samplingrow_size = |
329 | 0 | TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples, |
330 | 0 | td->td_bitspersample, module), |
331 | 0 | 8); |
332 | 0 | scanline_size = (samplingrow_size / ycbcrsubsampling[1]); |
333 | 0 | } |
334 | 0 | else |
335 | 0 | { |
336 | 0 | uint64_t scanline_samples; |
337 | 0 | 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 | 0 | scanline_samples = _TIFFMultiply64(tif, scanline_width, |
356 | 0 | td->td_samplesperpixel, module); |
357 | 0 | scanline_size = |
358 | 0 | TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples, |
359 | 0 | td->td_bitspersample, module), |
360 | 0 | 8); |
361 | 0 | } |
362 | 0 | } |
363 | 0 | else |
364 | 0 | { |
365 | 0 | scanline_size = |
366 | 0 | TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth, |
367 | 0 | td->td_bitspersample, module), |
368 | 0 | 8); |
369 | 0 | } |
370 | 0 | if (scanline_size == 0) |
371 | 0 | { |
372 | 0 | TIFFErrorExtR(tif, module, "Computed scanline size is zero"); |
373 | 0 | return 0; |
374 | 0 | } |
375 | 0 | return (scanline_size); |
376 | 0 | } |
377 | | tmsize_t TIFFScanlineSize(TIFF *tif) |
378 | 0 | { |
379 | 0 | static const char module[] = "TIFFScanlineSize"; |
380 | 0 | uint64_t m; |
381 | 0 | m = TIFFScanlineSize64(tif); |
382 | 0 | return _TIFFCastUInt64ToSSize(tif, m, module); |
383 | 0 | } |
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 | | uint64_t TIFFRasterScanlineSize64(TIFF *tif) |
392 | 0 | { |
393 | 0 | static const char module[] = "TIFFRasterScanlineSize64"; |
394 | 0 | TIFFDirectory *td = &tif->tif_dir; |
395 | 0 | uint64_t scanline; |
396 | |
|
397 | 0 | scanline = |
398 | 0 | _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module); |
399 | 0 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
400 | 0 | { |
401 | 0 | scanline = |
402 | 0 | _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module); |
403 | 0 | return (TIFFhowmany8_64(scanline)); |
404 | 0 | } |
405 | 0 | else |
406 | 0 | return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline), |
407 | 0 | td->td_samplesperpixel, module)); |
408 | 0 | } |
409 | | tmsize_t TIFFRasterScanlineSize(TIFF *tif) |
410 | 0 | { |
411 | 0 | static const char module[] = "TIFFRasterScanlineSize"; |
412 | 0 | uint64_t m; |
413 | 0 | m = TIFFRasterScanlineSize64(tif); |
414 | 0 | return _TIFFCastUInt64ToSSize(tif, m, module); |
415 | 0 | } |