Coverage Report

Created: 2026-02-14 07:11

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
58.2k
{
37
58.2k
    static const char module[] = "TIFFComputeStrip";
38
58.2k
    TIFFDirectory *td = &tif->tif_dir;
39
58.2k
    uint32_t strip;
40
41
58.2k
    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
58.2k
    strip = row / td->td_rowsperstrip;
48
58.2k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
49
5.56k
    {
50
5.56k
        if (sample >= td->td_samplesperpixel)
51
5
        {
52
5
            TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
53
5
                          (unsigned long)sample,
54
5
                          (unsigned long)td->td_samplesperpixel);
55
5
            return (0);
56
5
        }
57
5.56k
        strip += (uint32_t)sample * td->td_stripsperimage;
58
5.56k
    }
59
58.2k
    return (strip);
60
58.2k
}
61
62
/*
63
 * Compute how many strips are in an image.
64
 */
65
uint32_t TIFFNumberOfStrips(TIFF *tif)
66
157k
{
67
157k
    TIFFDirectory *td = &tif->tif_dir;
68
157k
    uint32_t nstrips;
69
70
157k
    if (td->td_rowsperstrip == 0)
71
0
    {
72
0
        TIFFWarningExtR(tif, "TIFFNumberOfStrips", "RowsPerStrip is zero");
73
0
        return 0;
74
0
    }
75
157k
    nstrips = (td->td_rowsperstrip == (uint32_t)-1
76
157k
                   ? 1
77
157k
                   : TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
78
157k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
79
5.18k
        nstrips =
80
5.18k
            _TIFFMultiply32(tif, nstrips, (uint32_t)td->td_samplesperpixel,
81
5.18k
                            "TIFFNumberOfStrips");
82
157k
    return (nstrips);
83
157k
}
84
85
/*
86
 * Compute the # bytes in a variable height, row-aligned strip.
87
 */
88
uint64_t TIFFVStripSize64(TIFF *tif, uint32_t nrows)
89
389k
{
90
389k
    static const char module[] = "TIFFVStripSize64";
91
389k
    TIFFDirectory *td = &tif->tif_dir;
92
389k
    if (nrows == (uint32_t)(-1))
93
533
        nrows = td->td_imagelength;
94
389k
    if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
95
359k
        (td->td_photometric == PHOTOMETRIC_YCBCR) && (!isUpSampled(tif)))
96
37.7k
    {
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
37.7k
        uint16_t ycbcrsubsampling[2];
106
37.7k
        uint16_t samplingblock_samples;
107
37.7k
        uint32_t samplingblocks_hor;
108
37.7k
        uint32_t samplingblocks_ver;
109
37.7k
        uint64_t samplingrow_samples;
110
37.7k
        uint64_t samplingrow_size;
111
37.7k
        if (td->td_samplesperpixel != 3)
112
478
        {
113
478
            TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
114
478
            return 0;
115
478
        }
116
37.2k
        TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
117
37.2k
                              ycbcrsubsampling + 0, ycbcrsubsampling + 1);
118
37.2k
        if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
119
2.07k
             ycbcrsubsampling[0] != 4) ||
120
37.2k
            (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
121
1.09k
             ycbcrsubsampling[1] != 4) ||
122
37.1k
            (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))
123
69
        {
124
69
            TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
125
69
                          ycbcrsubsampling[0], ycbcrsubsampling[1]);
126
69
            return 0;
127
69
        }
128
37.1k
        samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
129
37.1k
        samplingblocks_hor =
130
37.1k
            TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
131
37.1k
        samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
132
37.1k
        samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
133
37.1k
                                              samplingblock_samples, module);
134
37.1k
        samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
135
37.1k
            tif, samplingrow_samples, td->td_bitspersample, module));
136
37.1k
        return (
137
37.1k
            _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
138
37.2k
    }
139
351k
    else
140
351k
        return (_TIFFMultiply64(tif, nrows, TIFFScanlineSize64(tif), module));
141
389k
}
142
tmsize_t TIFFVStripSize(TIFF *tif, uint32_t nrows)
143
134k
{
144
134k
    static const char module[] = "TIFFVStripSize";
145
134k
    uint64_t m;
146
134k
    m = TIFFVStripSize64(tif, nrows);
147
134k
    return _TIFFCastUInt64ToSSize(tif, m, module);
148
134k
}
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
236k
{
198
236k
    TIFFDirectory *td = &tif->tif_dir;
199
236k
    uint32_t rps = td->td_rowsperstrip;
200
236k
    if (rps > td->td_imagelength)
201
103k
        rps = td->td_imagelength;
202
236k
    return (TIFFVStripSize64(tif, rps));
203
236k
}
204
tmsize_t TIFFStripSize(TIFF *tif)
205
171k
{
206
171k
    static const char module[] = "TIFFStripSize";
207
171k
    uint64_t m;
208
171k
    m = TIFFStripSize64(tif);
209
171k
    return _TIFFCastUInt64ToSSize(tif, m, module);
210
171k
}
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
7.86k
{
220
7.86k
    return (*tif->tif_defstripsize)(tif, request);
221
7.86k
}
222
223
uint32_t _TIFFDefaultStripSize(TIFF *tif, uint32_t s)
224
7.86k
{
225
7.86k
    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
7.86k
    return (s);
245
7.86k
}
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
683k
{
259
683k
    static const char module[] = "TIFFScanlineSize64";
260
683k
    TIFFDirectory *td = &tif->tif_dir;
261
683k
    uint64_t scanline_size;
262
683k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
263
630k
    {
264
630k
        if ((td->td_photometric == PHOTOMETRIC_YCBCR) &&
265
63.5k
            (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
266
49.0k
        {
267
49.0k
            uint16_t ycbcrsubsampling[2];
268
49.0k
            uint16_t samplingblock_samples;
269
49.0k
            uint32_t samplingblocks_hor;
270
49.0k
            uint64_t samplingrow_samples;
271
49.0k
            uint64_t samplingrow_size;
272
49.0k
            if (td->td_samplesperpixel != 3)
273
0
            {
274
0
                TIFFErrorExtR(tif, module, "Invalid td_samplesperpixel value");
275
0
                return 0;
276
0
            }
277
49.0k
            TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
278
49.0k
                                  ycbcrsubsampling + 0, ycbcrsubsampling + 1);
279
49.0k
            if (((ycbcrsubsampling[0] != 1) && (ycbcrsubsampling[0] != 2) &&
280
2.35k
                 (ycbcrsubsampling[0] != 4)) ||
281
49.0k
                ((ycbcrsubsampling[1] != 1) && (ycbcrsubsampling[1] != 2) &&
282
1.29k
                 (ycbcrsubsampling[1] != 4)) ||
283
48.9k
                ((ycbcrsubsampling[0] == 0) || (ycbcrsubsampling[1] == 0)))
284
189
            {
285
189
                TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling");
286
189
                return 0;
287
189
            }
288
48.9k
            samplingblock_samples =
289
48.9k
                ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
290
48.9k
            samplingblocks_hor =
291
48.9k
                TIFFhowmany_32(td->td_imagewidth, ycbcrsubsampling[0]);
292
48.9k
            samplingrow_samples = _TIFFMultiply64(
293
48.9k
                tif, samplingblocks_hor, samplingblock_samples, module);
294
48.9k
            samplingrow_size =
295
48.9k
                TIFFhowmany_64(_TIFFMultiply64(tif, samplingrow_samples,
296
48.9k
                                               td->td_bitspersample, module),
297
48.9k
                               8);
298
48.9k
            scanline_size = (samplingrow_size / ycbcrsubsampling[1]);
299
48.9k
        }
300
581k
        else
301
581k
        {
302
581k
            uint64_t scanline_samples;
303
581k
            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
581k
            scanline_samples = _TIFFMultiply64(tif, scanline_width,
322
581k
                                               td->td_samplesperpixel, module);
323
581k
            scanline_size =
324
581k
                TIFFhowmany_64(_TIFFMultiply64(tif, scanline_samples,
325
581k
                                               td->td_bitspersample, module),
326
581k
                               8);
327
581k
        }
328
630k
    }
329
52.2k
    else
330
52.2k
    {
331
52.2k
        scanline_size =
332
52.2k
            TIFFhowmany_64(_TIFFMultiply64(tif, td->td_imagewidth,
333
52.2k
                                           td->td_bitspersample, module),
334
52.2k
                           8);
335
52.2k
    }
336
682k
    if (scanline_size == 0)
337
7.64k
    {
338
7.64k
        TIFFErrorExtR(tif, module, "Computed scanline size is zero");
339
7.64k
        return 0;
340
7.64k
    }
341
675k
    return (scanline_size);
342
682k
}
343
tmsize_t TIFFScanlineSize(TIFF *tif)
344
279k
{
345
279k
    static const char module[] = "TIFFScanlineSize";
346
279k
    uint64_t m;
347
279k
    m = TIFFScanlineSize64(tif);
348
279k
    return _TIFFCastUInt64ToSSize(tif, m, module);
349
279k
}
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
}