Coverage Report

Created: 2026-06-07 07:20

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