Coverage Report

Created: 2025-11-16 06:25

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