Coverage Report

Created: 2026-05-24 07:45

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
71.5k
{
37
71.5k
    static const char module[] = "TIFFComputeStrip";
38
71.5k
    TIFFDirectory *td = &tif->tif_dir;
39
71.5k
    uint32_t strip;
40
41
71.5k
    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
71.5k
    strip = row / td->td_rowsperstrip;
48
71.5k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
49
2.14k
    {
50
2.14k
        if (sample >= td->td_samplesperpixel)
51
3
        {
52
3
            TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
53
3
                          (unsigned long)sample,
54
3
                          (unsigned long)td->td_samplesperpixel);
55
3
            return (0);
56
3
        }
57
2.14k
        strip += (uint32_t)sample * td->td_stripsperimage;
58
2.14k
    }
59
71.5k
    return (strip);
60
71.5k
}
61
62
/*
63
 * Compute how many strips are in an image.
64
 */
65
uint32_t TIFFNumberOfStrips(TIFF *tif)
66
456k
{
67
456k
    TIFFDirectory *td = &tif->tif_dir;
68
456k
    uint32_t nstrips;
69
70
456k
    if (td->td_rowsperstrip == 0)
71
0
    {
72
0
        TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");
73
0
        return 0;
74
0
    }
75
456k
    nstrips = (td->td_rowsperstrip == (uint32_t)-1
76
456k
                   ? 1
77
456k
                   : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
78
456k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
79
16.9k
        nstrips =
80
16.9k
            _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
81
16.9k
                            "TIFFNumberOfStrips");
82
456k
    return (nstrips);
83
456k
}
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
4.16M
{
91
4.16M
    static const char module[] = "_TIFFStrileSize64";
92
4.16M
    TIFFDirectory *td = &tif->tif_dir;
93
4.16M
    if (isStrip)
94
939k
    {
95
939k
        if (nrows == (uint32_t)(-1))
96
123
            nrows = td->td_imagelength;
97
939k
    }
98
3.22M
    else
99
3.22M
    {
100
3.22M
        if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
101
3.17M
            td->td_tiledepth == 0)
102
45.8k
            return (0);
103
3.22M
    }
104
4.11M
    if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
105
4.07M
        (td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
106
138k
    {
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
138k
        uint16_t ycbcrsubsampling[2];
116
138k
        uint16_t samplingblock_samples;
117
138k
        uint32_t samplingblocks_hor;
118
138k
        uint32_t samplingblocks_ver;
119
138k
        uint64_t samplingrow_samples;
120
138k
        uint64_t samplingrow_size;
121
138k
        if (td->td_samplesperpixel != 3)
122
17.1k
        {
123
17.1k
            TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
124
17.1k
            return 0;
125
17.1k
        }
126
121k
        TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
127
121k
                              ycbcrsubsampling + 0, ycbcrsubsampling + 1);
128
121k
        if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
129
33.0k
             ycbcrsubsampling[0] != 4) ||
130
121k
            (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
131
5.97k
             ycbcrsubsampling[1] != 4) ||
132
121k
            (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))
133
67
        {
134
67
            TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
135
67
                          ycbcrsubsampling[0], ycbcrsubsampling[1]);
136
67
            return 0;
137
67
        }
138
121k
        samplingblock_samples =
139
121k
            (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2);
140
121k
        const uint32_t width = isStrip ? td->td_imagewidth : td->td_tilewidth;
141
121k
        samplingblocks_hor = TIFFhowmany_32(width, ycbcrsubsampling[0]);
142
121k
        samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
143
121k
        samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
144
121k
                                              samplingblock_samples, module);
145
121k
        samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
146
121k
            tif, samplingrow_samples, td->td_bitspersample, module));
147
121k
        return (
148
121k
            _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
149
121k
    }
150
3.98M
    else
151
3.98M
        return (_TIFFMultiply64(tif, nrows,
152
3.98M
                                isStrip ? TIFFScanlineSize64(tif)
153
3.98M
                                        : TIFFTileRowSize64(tif),
154
3.98M
                                module));
155
4.11M
}
156
157
/*
158
 * Compute the # bytes in a variable height, row-aligned strip.
159
 */
160
uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
161
939k
{
162
939k
    return _TIFFStrileSize64(tif, nrows, /* isStrip = */ TRUE);
163
939k
}
164
165
tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
166
235k
{
167
235k
    static const char module[] = "TIFFVStripSize";
168
235k
    uint64_t m;
169
235k
    m = TIFFVStripSize64(tif, nrows);
170
235k
    return _TIFFCastUInt64ToSSize(tif, m, module);
171
235k
}
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
667k
{
221
667k
    TIFFDirectory *td = &tif->tif_dir;
222
667k
    uint32_t rps = td->td_rowsperstrip;
223
667k
    if (rps > td->td_imagelength)
224
92.7k
        rps = td->td_imagelength;
225
667k
    return (TIFFVStripSize64(tif, rps));
226
667k
}
227
tmsize_t TIFFStripSize(TIFF *tif)
228
519k
{
229
519k
    static const char module[] = "TIFFStripSize";
230
519k
    uint64_t m;
231
519k
    m = TIFFStripSize64(tif);
232
519k
    return _TIFFCastUInt64ToSSize(tif, m, module);
233
519k
}
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
34.3k
{
243
34.3k
    return (*tif->tif_defstripsize)(tif, request);
244
34.3k
}
245
246
uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
247
34.3k
{
248
34.3k
    if ((int32_t)s < 1)
249
34.3k
    {
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
34.3k
        uint64_t scanlinesize;
256
34.3k
        uint64_t rows;
257
34.3k
        scanlinesize = TIFFScanlineSize64(tif);
258
34.3k
        if (scanlinesize == 0)
259
0
            scanlinesize = 1;
260
34.3k
        rows = (uint64_t)STRIP_SIZE_DEFAULT / scanlinesize;
261
34.3k
        if (rows == 0)
262
6
            rows = 1;
263
34.3k
        else if (rows > 0xFFFFFFFF)
264
0
            rows = 0xFFFFFFFF;
265
34.3k
        s = (uint32_t)rows;
266
34.3k
    }
267
34.3k
    return (s);
268
34.3k
}
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
1.42M
{
282
1.42M
    static const char module[] = "TIFFScanlineSize64";
283
1.42M
    TIFFDirectory *td = &tif->tif_dir;
284
1.42M
    uint64_t scanline_size;
285
1.42M
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
286
1.36M
    {
287
1.36M
        if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&
288
89.4k
            (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
289
47.8k
        {
290
47.8k
            uint16_t ycbcrsubsampling[2];
291
47.8k
            uint16_t samplingblock_samples;
292
47.8k
            uint32_t samplingblocks_hor;
293
47.8k
            uint64_t samplingrow_samples;
294
47.8k
            uint64_t samplingrow_size;
295
47.8k
            if (td->td_samplesperpixel != 3)
296
0
            {
297
0
                TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
298
0
                return 0;
299
0
            }
300
47.8k
            TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
301
47.8k
                                  ycbcrsubsampling + 0, ycbcrsubsampling + 1);
302
47.8k
            if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&
303
10.4k
                 (ycbcrsubsampling[0] != 4)) ||
304
47.8k
                ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&
305
1.94k
                 (ycbcrsubsampling[1] != 4)) ||
306
47.7k
                ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))
307
115
            {
308
115
                TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");
309
115
                return 0;
310
115
            }
311
47.7k
            samplingblock_samples =
312
47.7k
                (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2);
313
47.7k
            samplingblocks_hor =
314
47.7k
                TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
315
47.7k
            samplingrow_samples = _TIFFMultiply64(
316
47.7k
                tif, samplingblocks_hor, samplingblock_samples, module);
317
47.7k
            samplingrow_size =
318
47.7k
                TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,
319
47.7k
                                               td->td_bitspersample, module),
320
47.7k
                               8);
321
47.7k
            scanline_size = (samplingrow_size / ycbcrsubsampling[1]);
322
47.7k
        }
323
1.31M
        else
324
1.31M
        {
325
1.31M
            uint64_t scanline_samples;
326
1.31M
            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
1.31M
            scanline_samples = _TIFFMultiply64(tif, scanline_width,
345
1.31M
                                               td->td_samplesperpixel, module);
346
1.31M
            scanline_size =
347
1.31M
                TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
348
1.31M
                                               td->td_bitspersample, module),
349
1.31M
                               8);
350
1.31M
        }
351
1.36M
    }
352
65.7k
    else
353
65.7k
    {
354
65.7k
        scanline_size =
355
65.7k
            TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,
356
65.7k
                                           td->td_bitspersample, module),
357
65.7k
                           8);
358
65.7k
    }
359
1.42M
    if (scanline_size == 0)
360
149k
    {
361
149k
        TIFFErrorExtR(tif, module, "Computed scanline size is zero");
362
149k
        return 0;
363
149k
    }
364
1.27M
    return (scanline_size);
365
1.42M
}
366
tmsize_t TIFFScanlineSize(TIFF *tif)
367
440k
{
368
440k
    static const char module[] = "TIFFScanlineSize";
369
440k
    uint64_t m;
370
440k
    m = TIFFScanlineSize64(tif);
371
440k
    return _TIFFCastUInt64ToSSize(tif, m, module);
372
440k
}
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
}