Coverage Report

Created: 2026-06-30 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/opencv/3rdparty/libtiff/tif_tile.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
 * Tiled Image Support Routines.
29
 */
30
#include "tiffiop.h"
31
32
/*
33
 * Compute which tile an (x,y,z,s) value is in.
34
 */
35
uint32_t TIFFComputeTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z,
36
                         uint16_t s)
37
236k
{
38
236k
    TIFFDirectory *td = &tif->tif_dir;
39
236k
    uint32_t dx = td->td_tilewidth;
40
236k
    uint32_t dy = td->td_tilelength;
41
236k
    uint32_t dz = td->td_tiledepth;
42
236k
    uint32_t tile = 1;
43
44
236k
    if (td->td_imagedepth == 1)
45
236k
        z = 0;
46
236k
    if (dx == (uint32_t)-1)
47
0
        dx = td->td_imagewidth;
48
236k
    if (dy == (uint32_t)-1)
49
0
        dy = td->td_imagelength;
50
236k
    if (dz == (uint32_t)-1)
51
991
        dz = td->td_imagedepth;
52
236k
    if (dx != 0 && dy != 0 && dz != 0)
53
236k
    {
54
236k
        uint32_t xpt = TIFFhowmany_32(td->td_imagewidth, dx);
55
236k
        uint32_t ypt = TIFFhowmany_32(td->td_imagelength, dy);
56
236k
        uint32_t zpt = TIFFhowmany_32(td->td_imagedepth, dz);
57
58
236k
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
59
215k
            tile = (xpt * ypt * zpt) * s + (xpt * ypt) * (z / dz) +
60
215k
                   xpt * (y / dy) + x / dx;
61
21.2k
        else
62
21.2k
            tile = (xpt * ypt) * (z / dz) + xpt * (y / dy) + x / dx;
63
236k
    }
64
236k
    return (tile);
65
236k
}
66
67
/*
68
 * Check an (x,y,z,s) coordinate
69
 * against the image bounds.
70
 */
71
int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
72
243k
{
73
243k
    TIFFDirectory *td = &tif->tif_dir;
74
75
243k
    if (x >= td->td_imagewidth)
76
0
    {
77
0
        TIFFErrorExtR(tif, tif->tif_name, "%lu: Col out of range, max %lu",
78
0
                      (unsigned long)x, (unsigned long)(td->td_imagewidth - 1));
79
0
        return (0);
80
0
    }
81
243k
    if (y >= td->td_imagelength)
82
0
    {
83
0
        TIFFErrorExtR(tif, tif->tif_name, "%lu: Row out of range, max %lu",
84
0
                      (unsigned long)y,
85
0
                      (unsigned long)(td->td_imagelength - 1));
86
0
        return (0);
87
0
    }
88
243k
    if (z >= td->td_imagedepth)
89
0
    {
90
0
        TIFFErrorExtR(tif, tif->tif_name, "%lu: Depth out of range, max %lu",
91
0
                      (unsigned long)z, (unsigned long)(td->td_imagedepth - 1));
92
0
        return (0);
93
0
    }
94
243k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
95
222k
        s >= td->td_samplesperpixel)
96
7.14k
    {
97
7.14k
        TIFFErrorExtR(tif, tif->tif_name, "%lu: Sample out of range, max %lu",
98
7.14k
                      (unsigned long)s,
99
7.14k
                      (unsigned long)(td->td_samplesperpixel - 1));
100
7.14k
        return (0);
101
7.14k
    }
102
236k
    return (1);
103
243k
}
104
105
/*
106
 * Compute how many tiles are in an image.
107
 */
108
uint32_t TIFFNumberOfTiles(TIFF *tif)
109
1.31k
{
110
1.31k
    TIFFDirectory *td = &tif->tif_dir;
111
1.31k
    uint32_t dx = td->td_tilewidth;
112
1.31k
    uint32_t dy = td->td_tilelength;
113
1.31k
    uint32_t dz = td->td_tiledepth;
114
1.31k
    uint32_t ntiles;
115
116
1.31k
    if (dx == (uint32_t)-1)
117
0
        dx = td->td_imagewidth;
118
1.31k
    if (dy == (uint32_t)-1)
119
0
        dy = td->td_imagelength;
120
1.31k
    if (dz == (uint32_t)-1)
121
23
        dz = td->td_imagedepth;
122
1.31k
    ntiles =
123
1.31k
        (dx == 0 || dy == 0 || dz == 0)
124
1.31k
            ? 0
125
1.31k
            : _TIFFMultiply32(
126
1.30k
                  tif,
127
1.30k
                  _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
128
1.30k
                                  TIFFhowmany_32(td->td_imagelength, dy),
129
1.30k
                                  "TIFFNumberOfTiles"),
130
1.30k
                  TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
131
1.31k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
132
297
        ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
133
297
                                 "TIFFNumberOfTiles");
134
1.31k
    return (ntiles);
135
1.31k
}
136
137
/*
138
 * Compute the # bytes in each row of a tile.
139
 */
140
uint64_t TIFFTileRowSize64(TIFF *tif)
141
180k
{
142
180k
    static const char module[] = "TIFFTileRowSize64";
143
180k
    TIFFDirectory *td = &tif->tif_dir;
144
180k
    uint64_t rowsize;
145
180k
    uint64_t tilerowsize;
146
147
180k
    if (td->td_tilelength == 0)
148
0
    {
149
0
        TIFFErrorExtR(tif, module, "Tile length is zero");
150
0
        return 0;
151
0
    }
152
180k
    if (td->td_tilewidth == 0)
153
0
    {
154
0
        TIFFErrorExtR(tif, module, "Tile width is zero");
155
0
        return (0);
156
0
    }
157
180k
    rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
158
180k
                              "TIFFTileRowSize");
159
180k
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
160
25.8k
    {
161
25.8k
        if (td->td_samplesperpixel == 0)
162
0
        {
163
0
            TIFFErrorExtR(tif, module, "Samples per pixel is zero");
164
0
            return 0;
165
0
        }
166
25.8k
        rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
167
25.8k
                                  "TIFFTileRowSize");
168
25.8k
    }
169
180k
    tilerowsize = TIFFhowmany8_64(rowsize);
170
180k
    if (tilerowsize == 0)
171
0
    {
172
0
        TIFFErrorExtR(tif, module, "Computed tile row size is zero");
173
0
        return 0;
174
0
    }
175
180k
    return (tilerowsize);
176
180k
}
177
tmsize_t TIFFTileRowSize(TIFF *tif)
178
97.8k
{
179
97.8k
    static const char module[] = "TIFFTileRowSize";
180
97.8k
    uint64_t m;
181
97.8k
    m = TIFFTileRowSize64(tif);
182
97.8k
    return _TIFFCastUInt64ToSSize(tif, m, module);
183
97.8k
}
184
185
/*
186
 * Compute the # bytes in a variable length, row-aligned tile.
187
 */
188
uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows)
189
101k
{
190
101k
    static const char module[] = "TIFFVTileSize64";
191
101k
    TIFFDirectory *td = &tif->tif_dir;
192
101k
    if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
193
101k
        td->td_tiledepth == 0)
194
0
        return (0);
195
101k
    if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
196
23.8k
        (td->td_photometric == PHOTOMETRIC_YCBCR) &&
197
19.6k
        (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
198
19.0k
    {
199
        /*
200
         * Packed YCbCr data contain one Cb+Cr for every
201
         * HorizontalSampling*VerticalSampling Y values.
202
         * Must also roundup width and height when calculating
203
         * since images that are not a multiple of the
204
         * horizontal/vertical subsampling area include
205
         * YCbCr data for the extended image.
206
         */
207
19.0k
        uint16_t ycbcrsubsampling[2];
208
19.0k
        uint16_t samplingblock_samples;
209
19.0k
        uint32_t samplingblocks_hor;
210
19.0k
        uint32_t samplingblocks_ver;
211
19.0k
        uint64_t samplingrow_samples;
212
19.0k
        uint64_t samplingrow_size;
213
19.0k
        TIFFGetFieldDefaulted(tif, TIFFTAG_YCBCRSUBSAMPLING,
214
19.0k
                              ycbcrsubsampling + 0, ycbcrsubsampling + 1);
215
19.0k
        if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
216
10.0k
             ycbcrsubsampling[0] != 4) ||
217
19.0k
            (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
218
1.17k
             ycbcrsubsampling[1] != 4))
219
0
        {
220
0
            TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%dx%d)",
221
0
                          ycbcrsubsampling[0], ycbcrsubsampling[1]);
222
0
            return 0;
223
0
        }
224
19.0k
        samplingblock_samples = ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2;
225
19.0k
        samplingblocks_hor =
226
19.0k
            TIFFhowmany_32(td->td_tilewidth, ycbcrsubsampling[0]);
227
19.0k
        samplingblocks_ver = TIFFhowmany_32(nrows, ycbcrsubsampling[1]);
228
19.0k
        samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
229
19.0k
                                              samplingblock_samples, module);
230
19.0k
        samplingrow_size = TIFFhowmany8_64(_TIFFMultiply64(
231
19.0k
            tif, samplingrow_samples, td->td_bitspersample, module));
232
19.0k
        return (
233
19.0k
            _TIFFMultiply64(tif, samplingrow_size, samplingblocks_ver, module));
234
19.0k
    }
235
82.5k
    else
236
82.5k
        return (_TIFFMultiply64(tif, nrows, TIFFTileRowSize64(tif), module));
237
101k
}
238
tmsize_t TIFFVTileSize(TIFF *tif, uint32_t nrows)
239
0
{
240
0
    static const char module[] = "TIFFVTileSize";
241
0
    uint64_t m;
242
0
    m = TIFFVTileSize64(tif, nrows);
243
0
    return _TIFFCastUInt64ToSSize(tif, m, module);
244
0
}
245
246
/*
247
 * Compute the # bytes in a row-aligned tile.
248
 */
249
uint64_t TIFFTileSize64(TIFF *tif)
250
101k
{
251
101k
    return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
252
101k
}
253
tmsize_t TIFFTileSize(TIFF *tif)
254
101k
{
255
101k
    static const char module[] = "TIFFTileSize";
256
101k
    uint64_t m;
257
101k
    m = TIFFTileSize64(tif);
258
101k
    return _TIFFCastUInt64ToSSize(tif, m, module);
259
101k
}
260
261
/*
262
 * Compute a default tile size based on the image
263
 * characteristics and a requested value.  If a
264
 * request is <1 then we choose a size according
265
 * to certain heuristics.
266
 */
267
void TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
268
0
{
269
0
    (*tif->tif_deftilesize)(tif, tw, th);
270
0
}
271
272
void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
273
0
{
274
0
    (void)tif;
275
0
    if (*(int32_t *)tw < 1)
276
0
        *tw = 256;
277
0
    if (*(int32_t *)th < 1)
278
0
        *th = 256;
279
    /* roundup to a multiple of 16 per the spec */
280
0
    if (*tw & 0xf)
281
0
        *tw = TIFFroundup_32(*tw, 16);
282
0
    if (*th & 0xf)
283
0
        *th = TIFFroundup_32(*th, 16);
284
0
}