Coverage Report

Created: 2026-11-12 07:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtiff/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
108k
{
37
108k
    static const char module[] = "TIFFComputeStrip";
38
108k
    TIFFDirectory *td = &tif->tif_dir;
39
108k
    uint32_t strip;
40
41
108k
    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
108k
    strip = row / td->td_rowsperstrip;
48
108k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
49
10.3k
    {
50
10.3k
        uint64_t sample_offset;
51
10.3k
        uint64_t strip64;
52
10.3k
        if (sample >= td->td_samplesperpixel)
53
4
        {
54
4
            TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
55
4
                          (unsigned long)sample,
56
4
                          (unsigned long)td->td_samplesperpixel);
57
4
            return (0);
58
4
        }
59
10.3k
        sample_offset = _TIFFMultiply64(tif, sample, td->td_stripsperimage,
60
10.3k
                                        "TIFFComputeStrip");
61
10.3k
        if (sample_offset == 0 && sample != 0 && td->td_stripsperimage != 0)
62
0
            return (0);
63
10.3k
        strip64 = _TIFFAdd64(tif, sample_offset, strip, "TIFFComputeStrip");
64
10.3k
        if (strip64 == 0 && (sample_offset != 0 || strip != 0))
65
0
            return (0);
66
10.3k
        strip = _TIFFCastUInt64ToUInt32(tif, strip64, "TIFFComputeStrip");
67
10.3k
        if (strip == 0 && strip64 != 0)
68
0
            return (0);
69
10.3k
    }
70
108k
    return (strip);
71
108k
}
72
73
/*
74
 * Compute how many strips are in an image.
75
 */
76
uint32_t TIFFNumberOfStrips(TIFF *tif)
77
138k
{
78
138k
    TIFFDirectory *td = &tif->tif_dir;
79
138k
    uint32_t nstrips;
80
81
138k
    if (td->td_rowsperstrip == 0)
82
0
    {
83
0
        TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");
84
0
        return 0;
85
0
    }
86
138k
    nstrips = (td->td_rowsperstrip == (uint32_t)-1
87
138k
                   ? 1
88
138k
                   : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
89
138k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
90
6.10k
        nstrips =
91
6.10k
            _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
92
6.10k
                            "TIFFNumberOfStrips");
93
138k
    return (nstrips);
94
138k
}
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
2.66M
{
102
2.66M
    static const char module[] = "_TIFFStrileSize64";
103
2.66M
    TIFFDirectory *td = &tif->tif_dir;
104
2.66M
    if (isStrip)
105
552k
    {
106
552k
        if (nrows == (uint32_t)(-1))
107
403
            nrows = td->td_imagelength;
108
552k
    }
109
2.10M
    else
110
2.10M
    {
111
2.10M
        if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
112
2.10M
            td->td_tiledepth == 0)
113
1.34k
            return (0);
114
2.10M
    }
115
2.66M
    if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
116
2.05M
        (td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
117
112k
    {
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
112k
        uint16_t ycbcrsubsampling[2];
127
112k
        uint16_t samplingblock_samples;
128
112k
        uint32_t samplingblocks_hor;
129
112k
        uint32_t samplingblocks_ver;
130
112k
        uint64_t samplingrow_samples;
131
112k
        uint64_t samplingrow_size;
132
112k
        if (td->td_samplesperpixel != 3)
133
249
        {
134
249
            TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
135
249
            return 0;
136
249
        }
137
112k
        TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
138
112k
                              ycbcrsubsampling + 0, ycbcrsubsampling + 1);
139
112k
        if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
140
7.69k
             ycbcrsubsampling[0] != 4) ||
141
112k
            (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
142
2.90k
             ycbcrsubsampling[1] != 4) ||
143
112k
            (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))
144
72
        {
145
72
            TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
146
72
                          ycbcrsubsampling[0], ycbcrsubsampling[1]);
147
72
            return 0;
148
72
        }
149
112k
        samplingblock_samples =
150
112k
            (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2);
151
112k
        const uint32_t width = isStrip ? td->td_imagewidth : td->td_tilewidth;
152
112k
        samplingblocks_hor = TIFFhowmany_32(width, ycbcrsubsampling[0]);
153
112k
        samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
154
112k
        samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
155
112k
                                              samplingblock_samples, module);
156
112k
        samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
157
112k
            tif, samplingrow_samples, td->td_bitspersample, module));
158
112k
        return (
159
112k
            _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
160
112k
    }
161
2.54M
    else
162
2.54M
        return (_TIFFMultiply64(tif, nrows,
163
2.54M
                                isStrip ? TIFFScanlineSize64(tif)
164
2.54M
                                        : TIFFTileRowSize64(tif),
165
2.54M
                                module));
166
2.66M
}
167
168
/*
169
 * Compute the # bytes in a variable height, row-aligned strip.
170
 */
171
uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
172
552k
{
173
552k
    return _TIFFStrileSize64(tif, nrows, /* isStrip = */ TRUE);
174
552k
}
175
176
tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
177
193k
{
178
193k
    static const char module[] = "TIFFVStripSize";
179
193k
    uint64_t m;
180
193k
    m = TIFFVStripSize64(tif, nrows);
181
193k
    return _TIFFCastUInt64ToSSize(tif, m, module);
182
193k
}
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
341k
{
232
341k
    TIFFDirectory *td = &tif->tif_dir;
233
341k
    uint32_t rps = td->td_rowsperstrip;
234
341k
    if (rps > td->td_imagelength)
235
113k
        rps = td->td_imagelength;
236
341k
    return (TIFFVStripSize64(tif, rps));
237
341k
}
238
tmsize_t TIFFStripSize(TIFF *tif)
239
287k
{
240
287k
    static const char module[] = "TIFFStripSize";
241
287k
    uint64_t m;
242
287k
    m = TIFFStripSize64(tif);
243
287k
    return _TIFFCastUInt64ToSSize(tif, m, module);
244
287k
}
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
5.96k
{
254
5.96k
    return (*tif->tif_defstripsize)(tif, request);
255
5.96k
}
256
257
uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
258
5.96k
{
259
5.96k
    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
5.96k
    return (s);
279
5.96k
}
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
822k
{
293
822k
    static const char module[] = "TIFFScanlineSize64";
294
822k
    TIFFDirectory *td = &tif->tif_dir;
295
822k
    uint64_t scanline_size;
296
822k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
297
737k
    {
298
737k
        if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&
299
52.2k
            (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
300
45.2k
        {
301
45.2k
            uint16_t ycbcrsubsampling[2];
302
45.2k
            uint16_t samplingblock_samples;
303
45.2k
            uint32_t samplingblocks_hor;
304
45.2k
            uint64_t samplingrow_samples;
305
45.2k
            uint64_t samplingrow_size;
306
45.2k
            if (td->td_samplesperpixel != 3)
307
0
            {
308
0
                TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
309
0
                return 0;
310
0
            }
311
45.2k
            TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
312
45.2k
                                  ycbcrsubsampling + 0, ycbcrsubsampling + 1);
313
45.2k
            if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&
314
2.09k
                 (ycbcrsubsampling[0] != 4)) ||
315
45.1k
                ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&
316
1.25k
                 (ycbcrsubsampling[1] != 4)) ||
317
45.0k
                ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))
318
139
            {
319
139
                TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");
320
139
                return 0;
321
139
            }
322
45.0k
            samplingblock_samples =
323
45.0k
                (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2);
324
45.0k
            samplingblocks_hor =
325
45.0k
                TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
326
45.0k
            samplingrow_samples = _TIFFMultiply64(
327
45.0k
                tif, samplingblocks_hor, samplingblock_samples, module);
328
45.0k
            samplingrow_size =
329
45.0k
                TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,
330
45.0k
                                               td->td_bitspersample, module),
331
45.0k
                               8);
332
45.0k
            scanline_size = (samplingrow_size / ycbcrsubsampling[1]);
333
45.0k
        }
334
692k
        else
335
692k
        {
336
692k
            uint64_t scanline_samples;
337
692k
            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
692k
            scanline_samples = _TIFFMultiply64(tif, scanline_width,
356
692k
                                               td->td_samplesperpixel, module);
357
692k
            scanline_size =
358
692k
                TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
359
692k
                                               td->td_bitspersample, module),
360
692k
                               8);
361
692k
        }
362
737k
    }
363
84.6k
    else
364
84.6k
    {
365
84.6k
        scanline_size =
366
84.6k
            TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,
367
84.6k
                                           td->td_bitspersample, module),
368
84.6k
                           8);
369
84.6k
    }
370
821k
    if (scanline_size == 0)
371
7.10k
    {
372
7.10k
        TIFFErrorExtR(tif, module, "Computed scanline size is zero");
373
7.10k
        return 0;
374
7.10k
    }
375
814k
    return (scanline_size);
376
821k
}
377
tmsize_t TIFFScanlineSize(TIFF *tif)
378
265k
{
379
265k
    static const char module[] = "TIFFScanlineSize";
380
265k
    uint64_t m;
381
265k
    m = TIFFScanlineSize64(tif);
382
265k
    return _TIFFCastUInt64ToSSize(tif, m, module);
383
265k
}
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
}