Coverage Report

Created: 2023-12-08 06:53

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