Coverage Report

Created: 2026-05-16 07:22

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