Coverage Report

Created: 2026-09-14 07:34

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