Coverage Report

Created: 2026-04-10 07:04

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