Coverage Report

Created: 2026-11-12 08:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/gdal/frmts/gtiff/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
23.5k
{
38
23.5k
    TIFFDirectory *td = &tif->tif_dir;
39
23.5k
    uint32_t dx = td->td_tilewidth;
40
23.5k
    uint32_t dy = td->td_tilelength;
41
23.5k
    uint32_t dz = td->td_tiledepth;
42
23.5k
    uint32_t tile = 1;
43
44
23.5k
    if (td->td_imagedepth == 1)
45
23.4k
        z = 0;
46
23.5k
    if (dx == (uint32_t)-1)
47
0
        dx = td->td_imagewidth;
48
23.5k
    if (dy == (uint32_t)-1)
49
0
        dy = td->td_imagelength;
50
23.5k
    if (dz == (uint32_t)-1)
51
1.11k
        dz = td->td_imagedepth;
52
23.5k
    if (dx != 0 && dy != 0 && dz != 0)
53
23.5k
    {
54
23.5k
        uint32_t xpt = TIFFhowmany_32(td->td_imagewidth, dx);
55
23.5k
        uint32_t ypt = TIFFhowmany_32(td->td_imagelength, dy);
56
23.5k
        uint32_t zpt = TIFFhowmany_32(td->td_imagedepth, dz);
57
23.5k
        uint32_t xpt_ypt = _TIFFMultiply32(tif, xpt, ypt, "TIFFComputeTile");
58
23.5k
        uint32_t xpt_ypt_zpt =
59
23.5k
            _TIFFMultiply32(tif, xpt_ypt, zpt, "TIFFComputeTile");
60
23.5k
        uint64_t z_offset;
61
23.5k
        uint64_t y_offset;
62
23.5k
        uint64_t tile64;
63
64
23.5k
        if ((xpt_ypt == 0 && xpt != 0 && ypt != 0) ||
65
23.5k
            (xpt_ypt_zpt == 0 && xpt_ypt != 0 && zpt != 0))
66
0
            return (0);
67
68
23.5k
        z_offset = _TIFFMultiply64(tif, xpt_ypt, z / dz, "TIFFComputeTile");
69
23.5k
        y_offset = _TIFFMultiply64(tif, xpt, y / dy, "TIFFComputeTile");
70
23.5k
        if ((z_offset == 0 && xpt_ypt != 0 && (z / dz) != 0) ||
71
23.5k
            (y_offset == 0 && xpt != 0 && (y / dy) != 0))
72
0
            return (0);
73
23.5k
        tile64 = _TIFFAdd64(tif, z_offset, y_offset, "TIFFComputeTile");
74
23.5k
        if (tile64 == 0 && (z_offset != 0 || y_offset != 0))
75
0
            return (0);
76
23.5k
        tile64 = _TIFFAdd64(tif, tile64, x / dx, "TIFFComputeTile");
77
23.5k
        if (tile64 == 0 && (z_offset != 0 || y_offset != 0 || (x / dx) != 0))
78
0
            return (0);
79
23.5k
        if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
80
5.57k
        {
81
5.57k
            uint64_t sample_offset;
82
5.57k
            if (s >= td->td_samplesperpixel)
83
0
            {
84
0
                TIFFErrorExtR(
85
0
                    tif, "TIFFComputeTile", "%lu: Sample out of range, max %lu",
86
0
                    (unsigned long)s, (unsigned long)td->td_samplesperpixel);
87
0
                return (0);
88
0
            }
89
5.57k
            sample_offset =
90
5.57k
                _TIFFMultiply64(tif, xpt_ypt_zpt, s, "TIFFComputeTile");
91
5.57k
            if (sample_offset == 0 && xpt_ypt_zpt != 0 && s != 0)
92
0
                return (0);
93
5.57k
            tile64 = _TIFFAdd64(tif, sample_offset, tile64, "TIFFComputeTile");
94
5.57k
            if (tile64 == 0 && (sample_offset != 0 || z_offset != 0 ||
95
333
                                y_offset != 0 || (x / dx) != 0))
96
0
                return (0);
97
5.57k
        }
98
23.5k
        tile = _TIFFCastUInt64ToUInt32(tif, tile64, "TIFFComputeTile");
99
23.5k
        if (tile == 0 && tile64 != 0)
100
0
            return (0);
101
23.5k
    }
102
23.5k
    return (tile);
103
23.5k
}
104
105
/*
106
 * Check an (x,y,z,s) coordinate
107
 * against the image bounds.
108
 */
109
int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
110
23.5k
{
111
23.5k
    TIFFDirectory *td = &tif->tif_dir;
112
113
23.5k
    if (x >= td->td_imagewidth)
114
0
    {
115
0
        TIFFErrorExtR(tif, tif->tif_name, "%lu: Col out of range, max %lu",
116
0
                      (unsigned long)x, (unsigned long)(td->td_imagewidth - 1));
117
0
        return (0);
118
0
    }
119
23.5k
    if (y >= td->td_imagelength)
120
0
    {
121
0
        TIFFErrorExtR(tif, tif->tif_name, "%lu: Row out of range, max %lu",
122
0
                      (unsigned long)y,
123
0
                      (unsigned long)(td->td_imagelength - 1));
124
0
        return (0);
125
0
    }
126
23.5k
    if (z >= td->td_imagedepth)
127
0
    {
128
0
        TIFFErrorExtR(tif, tif->tif_name, "%lu: Depth out of range, max %lu",
129
0
                      (unsigned long)z, (unsigned long)(td->td_imagedepth - 1));
130
0
        return (0);
131
0
    }
132
23.5k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
133
5.57k
        s >= td->td_samplesperpixel)
134
6
    {
135
6
        TIFFErrorExtR(tif, tif->tif_name, "%lu: Sample out of range, max %lu",
136
6
                      (unsigned long)s,
137
6
                      (unsigned long)(td->td_samplesperpixel - 1));
138
6
        return (0);
139
6
    }
140
23.5k
    return (1);
141
23.5k
}
142
143
/*
144
 * Compute how many tiles are in an image.
145
 */
146
uint32_t TIFFNumberOfTiles(TIFF *tif)
147
189k
{
148
189k
    TIFFDirectory *td = &tif->tif_dir;
149
189k
    uint32_t dx = td->td_tilewidth;
150
189k
    uint32_t dy = td->td_tilelength;
151
189k
    uint32_t dz = td->td_tiledepth;
152
189k
    uint32_t ntiles;
153
154
189k
    if (dx == (uint32_t)-1)
155
380
        dx = td->td_imagewidth;
156
189k
    if (dy == (uint32_t)-1)
157
339
        dy = td->td_imagelength;
158
189k
    if (dz == (uint32_t)-1)
159
281
        dz = td->td_imagedepth;
160
189k
    ntiles =
161
189k
        (dx == 0 || dy == 0 || dz == 0)
162
189k
            ? 0
163
189k
            : _TIFFMultiply32(
164
189k
                  tif,
165
189k
                  _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
166
189k
                                  TIFFhowmany_32(td->td_imagelength, dy),
167
189k
                                  "TIFFNumberOfTiles"),
168
189k
                  TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
169
189k
    if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
170
6.68k
        ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
171
6.68k
                                 "TIFFNumberOfTiles");
172
189k
    return (ntiles);
173
189k
}
174
175
/*
176
 * Compute the # bytes in each row of a tile.
177
 */
178
uint64_t TIFFTileRowSize64(TIFF *tif)
179
2.25M
{
180
2.25M
    static const char module[] = "TIFFTileRowSize64";
181
2.25M
    TIFFDirectory *td = &tif->tif_dir;
182
2.25M
    uint64_t rowsize;
183
2.25M
    uint64_t tilerowsize;
184
185
2.25M
    if (td->td_tilelength == 0)
186
0
    {
187
0
        TIFFErrorExtR(tif, module, "Tile length is zero");
188
0
        return 0;
189
0
    }
190
2.25M
    if (td->td_tilewidth == 0)
191
0
    {
192
0
        TIFFErrorExtR(tif, module, "Tile width is zero");
193
0
        return (0);
194
0
    }
195
2.25M
    rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
196
2.25M
                              "TIFFTileRowSize");
197
2.25M
    if (td->td_planarconfig == PLANARCONFIG_CONTIG)
198
1.12M
    {
199
1.12M
        if (td->td_samplesperpixel == 0)
200
0
        {
201
0
            TIFFErrorExtR(tif, module, "Samples per pixel is zero");
202
0
            return 0;
203
0
        }
204
1.12M
        rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
205
1.12M
                                  "TIFFTileRowSize");
206
1.12M
    }
207
2.25M
    tilerowsize = TIFFhowmany8_64(rowsize);
208
2.25M
    if (tilerowsize == 0)
209
60
    {
210
60
        TIFFErrorExtR(tif, module, "Computed tile row size is zero");
211
60
        return 0;
212
60
    }
213
2.25M
    return (tilerowsize);
214
2.25M
}
215
tmsize_t TIFFTileRowSize(TIFF *tif)
216
19.7k
{
217
19.7k
    static const char module[] = "TIFFTileRowSize";
218
19.7k
    uint64_t m;
219
19.7k
    m = TIFFTileRowSize64(tif);
220
19.7k
    return _TIFFCastUInt64ToSSize(tif, m, module);
221
19.7k
}
222
223
/*
224
 * Compute the # bytes in a variable length, row-aligned tile.
225
 */
226
uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows)
227
2.26M
{
228
2.26M
    return _TIFFStrileSize64(tif, nrows, /* isStrip = */ FALSE);
229
2.26M
}
230
231
tmsize_t TIFFVTileSize(TIFF *tif, uint32_t nrows)
232
0
{
233
0
    static const char module[] = "TIFFVTileSize";
234
0
    uint64_t m;
235
0
    m = TIFFVTileSize64(tif, nrows);
236
0
    return _TIFFCastUInt64ToSSize(tif, m, module);
237
0
}
238
239
/*
240
 * Compute the # bytes in a row-aligned tile.
241
 */
242
uint64_t TIFFTileSize64(TIFF *tif)
243
1.91M
{
244
1.91M
    return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
245
1.91M
}
246
tmsize_t TIFFTileSize(TIFF *tif)
247
1.89M
{
248
1.89M
    static const char module[] = "TIFFTileSize";
249
1.89M
    uint64_t m;
250
1.89M
    m = TIFFTileSize64(tif);
251
1.89M
    return _TIFFCastUInt64ToSSize(tif, m, module);
252
1.89M
}
253
254
/*
255
 * Compute a default tile size based on the image
256
 * characteristics and a requested value.  If a
257
 * request is <1 then we choose a size according
258
 * to certain heuristics.
259
 */
260
void TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
261
0
{
262
0
    (*tif->tif_deftilesize)(tif, tw, th);
263
0
}
264
265
void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
266
0
{
267
0
    (void)tif;
268
0
    if (*(int32_t *)tw < 1)
269
0
        *tw = 256;
270
0
    if (*(int32_t *)th < 1)
271
0
        *th = 256;
272
    /* roundup to a multiple of 16 per the spec */
273
0
    if (*tw & 0xf)
274
0
        *tw = TIFFroundup_32(*tw, 16);
275
0
    if (*th & 0xf)
276
0
        *th = TIFFroundup_32(*th, 16);
277
0
}