LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_strip.c
Go to the documentation of this file.
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 */
36#include "tiffiop.h"
37
38/*
39 * Compute which strip a (row,sample) value is in.
40 */
41uint32_t TIFFComputeStrip(TIFF *tif, uint32_t row, uint16_t sample)
42{
43 static const char module[] = "TIFFComputeStrip";
44 TIFFDirectory *td = &tif->tif_dir;
45 uint32_t strip;
46
47 if (td->td_rowsperstrip == 0)
48 {
49 TIFFErrorExtR(tif, module,
50 "Cannot compute strip: RowsPerStrip is zero");
51 return 0;
52 }
53 strip = row / td->td_rowsperstrip;
54 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
55 {
56 uint64_t sample_offset;
57 uint64_t strip64;
58 if (sample >= td->td_samplesperpixel)
59 {
60 TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
61 (unsigned long)sample,
62 (unsigned long)td->td_samplesperpixel);
63 return (0);
64 }
65 sample_offset = _TIFFMultiply64(tif, sample, td->td_stripsperimage,
66 "TIFFComputeStrip");
67 if (sample_offset == 0 && sample != 0 && td->td_stripsperimage != 0)
68 return (0);
69 strip64 = _TIFFAdd64(tif, sample_offset, strip, "TIFFComputeStrip");
70 if (strip64 == 0 && (sample_offset != 0 || strip != 0))
71 return (0);
72 strip = _TIFFCastUInt64ToUInt32(tif, strip64, "TIFFComputeStrip");
73 if (strip == 0 && strip64 != 0)
74 return (0);
75 }
76 return (strip);
77}
78
79/*
80 * Compute how many strips are in an image.
81 */
83{
84 TIFFDirectory *td = &tif->tif_dir;
85 uint32_t nstrips;
86
87 if (td->td_rowsperstrip == 0)
88 {
89 TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");
90 return 0;
91 }
92 nstrips = (td->td_rowsperstrip == (uint32_t)-1
93 ? 1
96 nstrips =
97 _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
98 "TIFFNumberOfStrips");
99 return (nstrips);
100}
101
102/*
103 * Compute the # bytes in a variable height, row-aligned strip if isStrip is
104 * TRUE, or in a tile if isStrip is FALSE
105 */
106uint64_t _TIFFStrileSize64(TIFF *tif, uint32_t nrows, int isStrip)
107{
108 static const char module[] = "_TIFFStrileSize64";
109 TIFFDirectory *td = &tif->tif_dir;
110 if (isStrip)
111 {
112 if (nrows == (uint32_t)(-1))
113 nrows = td->td_imagelength;
114 }
115 else
116 {
117 if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
118 td->td_tiledepth == 0)
119 return (0);
120 }
121 if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
122 (td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
123 {
124 /*
125 * Packed YCbCr data contain one Cb+Cr for every
126 * HorizontalSampling*VerticalSampling Y values.
127 * Must also roundup width and height when calculating
128 * since images that are not a multiple of the
129 * horizontal/vertical subsampling area include
130 * YCbCr data for the extended image.
131 */
132 uint16_t ycbcrsubsampling[2];
133 uint16_t samplingblock_samples;
134 uint32_t samplingblocks_hor;
135 uint32_t samplingblocks_ver;
136 uint64_t samplingrow_samples;
137 uint64_t samplingrow_size;
138 if (td->td_samplesperpixel != 3)
139 {
140 TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
141 return 0;
142 }
144 ycbcrsubsampling + 0, ycbcrsubsampling + 1);
145 if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
146 ycbcrsubsampling[0] != 4) ||
147 (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
148 ycbcrsubsampling[1] != 4) ||
149 (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))
150 {
151 TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
152 ycbcrsubsampling[0], ycbcrsubsampling[1]);
153 return 0;
154 }
155 samplingblock_samples =
156 (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2);
157 const uint32_t width = isStrip ? td->td_imagewidth : td->td_tilewidth;
158 samplingblocks_hor = TIFFhowmany_32(width, ycbcrsubsampling[0]);
159 samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
160 samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
161 samplingblock_samples, module);
162 samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
163 tif, samplingrow_samples, td->td_bitspersample, module));
164 return (
165 _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
166 }
167 else
168 return (_TIFFMultiply64(tif, nrows,
169 isStrip ? TIFFScanlineSize64(tif)
170 : TIFFTileRowSize64(tif),
171 module));
172}
173
174/*
175 * Compute the # bytes in a variable height, row-aligned strip.
176 */
177uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
178{
179 return _TIFFStrileSize64(tif, nrows, /* isStrip = */ TRUE);
180}
181
182tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
183{
184 static const char module[] = "TIFFVStripSize";
185 uint64_t m;
186 m = TIFFVStripSize64(tif, nrows);
187 return _TIFFCastUInt64ToSSize(tif, m, module);
188}
189
190/*
191 * Compute the # bytes in a raw strip.
192 */
193uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip)
194{
195 static const char module[] = "TIFFRawStripSize64";
196 uint64_t bytecount = TIFFGetStrileByteCount(tif, strip);
197
198 if (bytecount == 0)
199 {
200 TIFFErrorExtR(tif, module,
201 "%" PRIu64 ": Invalid strip byte count, strip %lu",
202 (uint64_t)bytecount, (unsigned long)strip);
203 bytecount = (uint64_t)-1;
204 }
205
206 return bytecount;
207}
208tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip)
209{
210 static const char module[] = "TIFFRawStripSize";
211 uint64_t m;
212 tmsize_t n;
213 m = TIFFRawStripSize64(tif, strip);
214 if (m == (uint64_t)(-1))
215 n = (tmsize_t)(-1);
216 else
217 {
218 n = (tmsize_t)m;
219 if ((uint64_t)n != m)
220 {
221 TIFFErrorExtR(tif, module, "Integer overflow");
222 n = 0;
223 }
224 }
225 return (n);
226}
227
228/*
229 * Compute the # bytes in a (row-aligned) strip.
230 *
231 * Note that if RowsPerStrip is larger than the
232 * recorded ImageLength, then the strip size is
233 * truncated to reflect the actual space required
234 * to hold the strip.
235 */
236uint64_t TIFFStripSize64(TIFF *tif)
237{
238 TIFFDirectory *td = &tif->tif_dir;
239 uint32_t rps = td->td_rowsperstrip;
240 if (rps > td->td_imagelength)
241 rps = td->td_imagelength;
242 return (TIFFVStripSize64(tif, rps));
243}
245{
246 static const char module[] = "TIFFStripSize";
247 uint64_t m;
248 m = TIFFStripSize64(tif);
249 return _TIFFCastUInt64ToSSize(tif, m, module);
250}
251
252/*
253 * Compute a default strip size based on the image
254 * characteristics and a requested value. If the
255 * request is <1 then we choose a strip size according
256 * to certain heuristics.
257 */
258uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request)
259{
260 return (*tif->tif_defstripsize)(tif, request);
261}
262
263uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
264{
265 if ((int32_t)s < 1)
266 {
267 /*
268 * If RowsPerStrip is unspecified, try to break the
269 * image up into strips that are approximately
270 * STRIP_SIZE_DEFAULT bytes long.
271 */
272 uint64_t scanlinesize;
273 uint64_t rows;
274 scanlinesize = TIFFScanlineSize64(tif);
275 if (scanlinesize == 0)
276 scanlinesize = 1;
277 rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize;
278 if (rows == 0)
279 rows = 1;
280 else if (rows > 0xFFFFFFFF)
281 rows = 0xFFFFFFFF;
282 s = (uint32_t)rows;
283 }
284 return (s);
285}
286
287/*
288 * Return the number of bytes to read/write in a call to
289 * one of the scanline-oriented i/o routines. Note that
290 * this number may be 1/samples-per-pixel if data is
291 * stored as separate planes.
292 * The ScanlineSize in case of YCbCrSubsampling is defined as the
293 * strip size divided by the strip height, i.e. the size of a pack of vertical
294 * subsampling lines divided by vertical subsampling. It should thus make
295 * sense when multiplied by a multiple of vertical subsampling.
296 */
298{
299 static const char module[] = "TIFFScanlineSize64";
300 TIFFDirectory *td = &tif->tif_dir;
301 uint64_t scanline_size;
302 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
303 {
304 if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&
305 (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
306 {
307 uint16_t ycbcrsubsampling[2];
308 uint16_t samplingblock_samples;
309 uint32_t samplingblocks_hor;
310 uint64_t samplingrow_samples;
311 uint64_t samplingrow_size;
312 if (td->td_samplesperpixel != 3)
313 {
314 TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
315 return 0;
316 }
318 ycbcrsubsampling + 0, ycbcrsubsampling + 1);
319 if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&
320 (ycbcrsubsampling[0] != 4)) ||
321 ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&
322 (ycbcrsubsampling[1] != 4)) ||
323 ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))
324 {
325 TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");
326 return 0;
327 }
328 samplingblock_samples =
329 (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2);
330 samplingblocks_hor =
331 TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
332 samplingrow_samples = _TIFFMultiply64(
333 tif, samplingblocks_hor, samplingblock_samples, module);
334 samplingrow_size =
335 TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,
336 td->td_bitspersample, module),
337 8);
338 scanline_size = (samplingrow_size / ycbcrsubsampling[1]);
339 }
340 else
341 {
342 uint64_t scanline_samples;
343 uint32_t scanline_width = td->td_imagewidth;
344
345#if 0
346 // Tries to fix https://gitlab.com/libtiff/libtiff/-/merge_requests/564
347 // but causes regression when decoding legit files with tiffcp -c none
348 // Cf https://gitlab.com/libtiff/libtiff/-/merge_requests/644
349 if (td->td_photometric == PHOTOMETRIC_YCBCR)
350 {
351 uint16_t subsampling_hor;
352 uint16_t ignored;
354 &subsampling_hor, &ignored);
355 if (subsampling_hor > 1) // roundup width for YCbCr
356 scanline_width =
357 TIFFroundup_32(scanline_width, subsampling_hor);
358 }
359#endif
360
361 scanline_samples = _TIFFMultiply64(tif, scanline_width,
362 td->td_samplesperpixel, module);
363 scanline_size =
364 TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
365 td->td_bitspersample, module),
366 8);
367 }
368 }
369 else
370 {
371 scanline_size =
372 TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,
373 td->td_bitspersample, module),
374 8);
375 }
376 if (scanline_size == 0)
377 {
378 TIFFErrorExtR(tif, module, "Computed scanline size is zero");
379 return 0;
380 }
381 return (scanline_size);
382}
384{
385 static const char module[] = "TIFFScanlineSize";
386 uint64_t m;
387 m = TIFFScanlineSize64(tif);
388 return _TIFFCastUInt64ToSSize(tif, m, module);
389}
390
391/*
392 * Return the number of bytes required to store a complete
393 * decoded and packed raster scanline (as opposed to the
394 * I/O size returned by TIFFScanlineSize which may be less
395 * if data is store as separate planes).
396 */
398{
399 static const char module[] = "TIFFRasterScanlineSize64";
400 TIFFDirectory *td = &tif->tif_dir;
401 uint64_t scanline;
402
403 scanline =
404 _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
405 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
406 {
407 scanline =
408 _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
409 return (TIFFhowmany8_64(scanline));
410 }
411 else
412 return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
413 td->td_samplesperpixel, module));
414}
416{
417 static const char module[] = "TIFFRasterScanlineSize";
418 uint64_t m;
420 return _TIFFCastUInt64ToSSize(tif, m, module);
421}
uint32_t _TIFFCastUInt64ToUInt32(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
Definition tif_aux.c:153
uint64_t _TIFFAdd64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Check and add two unsigned operands.
Definition tif_aux.c:85
uint32_t _TIFFMultiply32(TIFF *tif, uint32_t first, uint32_t second, const char *where)
Multiply two unsigned values with an overflow check.
Definition tif_aux.c:40
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
Definition tif_aux.c:140
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Multiply two unsigned values with an overflow check.
Definition tif_aux.c:46
#define STRIP_SIZE_DEFAULT
Define the default target strip size in bytes.
Definition tif_config.h:248
uint64_t _TIFFStrileSize64(TIFF *tif, uint32_t nrows, int isStrip)
Compute row-aligned decoded storage for a strip or tile-height request.
Definition tif_strip.c:106
uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
Choose a default strip height near STRIP_SIZE_DEFAULT bytes.
Definition tif_strip.c:263
#define TIFFhowmany_64(x, y)
Compute a 64-bit ceiling quotient using an unchecked addition.
Definition tiffiop.h:1115
#define isUpSampled(tif)
Test whether the codec reports upsampled output.
Definition tiffiop.h:869
#define TIFFroundup_32(x, y)
Round a 32-bit value up to a multiple using the guarded quotient.
Definition tiffiop.h:1102
#define TRUE
Represent the legacy internal true value.
Definition tiffiop.h:131
#define TIFFhowmany_32(x, y)
Compute a guarded 32-bit ceiling quotient.
Definition tiffiop.h:1056
#define TIFFhowmany8_64(x)
Compute the byte count needed to hold a 64-bit bit count.
Definition tiffiop.h:1127
void TIFFWarningExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a warning diagnostic to the configured handlers.
Definition tif_warning.c:85
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
tmsize_t TIFFStripSize(TIFF *tif)
Compute the decoded-size estimate for a nominal strip.
Definition tif_strip.c:244
uint64_t TIFFRawStripSize64(TIFF *tif, uint32_t strip)
Query the stored byte count for one raw strip.
Definition tif_strip.c:193
tmsize_t TIFFScanlineSize(TIFF *tif)
Compute the current scanline I/O byte estimate.
Definition tif_strip.c:383
tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
Compute the decoded-size estimate for a requested strip height.
Definition tif_strip.c:182
uint64_t TIFFScanlineSize64(TIFF *tif)
Compute the current scanline I/O byte estimate.
Definition tif_strip.c:297
uint32_t TIFFNumberOfStrips(TIFF *tif)
Compute the strip count from current image geometry.
Definition tif_strip.c:82
uint32_t TIFFComputeStrip(TIFF *tif, uint32_t row, uint16_t sample)
Compute a strip index from a row and optional sample plane.
Definition tif_strip.c:41
uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
Compute the decoded-size estimate for a requested strip height.
Definition tif_strip.c:177
tmsize_t TIFFRawStripSize(TIFF *tif, uint32_t strip)
Convert one raw-strip byte count to the legacy signed-size result.
Definition tif_strip.c:208
uint64_t TIFFStripSize64(TIFF *tif)
Compute the decoded-size estimate for a nominal strip.
Definition tif_strip.c:236
uint32_t TIFFDefaultStripSize(TIFF *tif, uint32_t request)
Ask the installed sizing method for a RowsPerStrip recommendation.
Definition tif_strip.c:258
uint64_t TIFFRasterScanlineSize64(TIFF *tif)
Compute the packed full-raster row byte count.
Definition tif_strip.c:397
uint64_t TIFFTileRowSize64(TIFF *tif)
Compute the packed byte count of one tile row.
Definition tif_tile.c:187
tmsize_t TIFFRasterScanlineSize(TIFF *tif)
Compute the packed full-raster row byte count.
Definition tif_strip.c:415
uint64_t TIFFGetStrileByteCount(TIFF *tif, uint32_t strile)
Query one logical strile byte count.
int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag,...)
Query a present tag or a supported default.
Definition tif_aux.c:591
#define PLANARCONFIG_SEPARATE
Separate planes of data.
Definition tiff.h:474
#define PHOTOMETRIC_YCBCR
CCIR 601 (introduced in TIFF 6.0).
Definition tiff.h:396
#define TIFFTAG_YCBCRSUBSAMPLING
TIFF tag: YCbCr subsampling factors (introduced in TIFF 6.0).
Definition tiff.h:729
#define PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint32_t td_rowsperstrip
Rows per strip; UINT32_MAX represents one whole-image strip.
Definition tif_dir.h:184
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
Definition tif_dir.h:200
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Definition tif_dir.h:182
uint32_t td_imagelength
Image length in rows.
Definition tif_dir.h:156
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFStripMethod tif_defstripsize
Codec callback computing or constraining a default strip size.
Definition tiffiop.h:663
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
Declare private library state, callbacks, and implementation helpers.