LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_tile.c
Go to the documentation of this file.
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 */
35#include "tiffiop.h"
36
37/*
38 * Compute which tile an (x,y,z,s) value is in.
39 */
40uint32_t TIFFComputeTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z,
41 uint16_t s)
42{
43 return _TIFFComputeTileExt(tif, x, y, z, s, NULL);
44}
45
46/*
47 * Same as TIFFComputeTile(), and also sets *pOverflowed to true when the tile
48 * index overflows. 0 is a valid tile index, so the return value alone cannot
49 * tell. Other errors, such as a sample out of range, do not set the flag:
50 * callers are expected to have checked the coordinates with TIFFCheckTile().
51 */
52uint32_t _TIFFComputeTileExt(TIFF *tif, uint32_t x, uint32_t y, uint32_t z,
53 uint16_t s, bool *pOverflowed)
54{
55 static const char module[] = "TIFFComputeTile";
56 TIFFDirectory *td = &tif->tif_dir;
57 uint32_t dx = td->td_tilewidth;
58 uint32_t dy = td->td_tilelength;
59 uint32_t dz = td->td_tiledepth;
60 uint32_t tile = 1;
61 bool overflow = false;
62
63 if (td->td_imagedepth == 1)
64 z = 0;
65 if (dx == (uint32_t)-1)
66 dx = td->td_imagewidth;
67 if (dy == (uint32_t)-1)
68 dy = td->td_imagelength;
69 if (dz == (uint32_t)-1)
70 dz = td->td_imagedepth;
71 if (dx != 0 && dy != 0 && dz != 0)
72 {
73 uint32_t xpt = TIFFhowmany_32(td->td_imagewidth, dx);
74 uint32_t ypt = TIFFhowmany_32(td->td_imagelength, dy);
75 uint32_t zpt = TIFFhowmany_32(td->td_imagedepth, dz);
76 uint32_t xpt_ypt = _TIFFMultiply32Ext(tif, xpt, ypt, module, &overflow);
77 uint32_t xpt_ypt_zpt =
78 _TIFFMultiply32Ext(tif, xpt_ypt, zpt, module, &overflow);
79 uint64_t tile64;
80
81 if (overflow)
82 goto overflowed;
83 /* A product of two uint32_t values cannot overflow uint64_t. */
84 tile64 = _TIFFAdd64Ext(tif, (uint64_t)xpt_ypt * (z / dz),
85 (uint64_t)xpt * (y / dy), module, &overflow);
86 tile64 = _TIFFAdd64Ext(tif, tile64, x / dx, module, &overflow);
87 if (overflow)
88 goto overflowed;
89 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
90 {
91 if (s >= td->td_samplesperpixel)
92 {
93 TIFFErrorExtR(tif, module, "%lu: Sample out of range, max %lu",
94 (unsigned long)s,
95 (unsigned long)td->td_samplesperpixel);
96 return (0);
97 }
98 /* A uint32_t times a uint16_t cannot overflow uint64_t. */
99 tile64 = _TIFFAdd64Ext(tif, (uint64_t)xpt_ypt_zpt * s, tile64,
100 module, &overflow);
101 }
102 tile = _TIFFCastUInt64ToUInt32Ext(tif, tile64, module, &overflow);
103 if (overflow)
104 goto overflowed;
105 }
106 return (tile);
107
108overflowed:
109 if (pOverflowed != NULL)
110 *pOverflowed = true;
111 return (0);
112}
113
114/*
115 * Check an (x,y,z,s) coordinate
116 * against the image bounds.
117 */
118int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
119{
120 TIFFDirectory *td = &tif->tif_dir;
121
122 if (x >= td->td_imagewidth)
123 {
124 TIFFErrorExtR(tif, tif->tif_name, "%lu: Col out of range, max %lu",
125 (unsigned long)x, (unsigned long)(td->td_imagewidth - 1));
126 return (0);
127 }
128 if (y >= td->td_imagelength)
129 {
130 TIFFErrorExtR(tif, tif->tif_name, "%lu: Row out of range, max %lu",
131 (unsigned long)y,
132 (unsigned long)(td->td_imagelength - 1));
133 return (0);
134 }
135 if (z >= td->td_imagedepth)
136 {
137 TIFFErrorExtR(tif, tif->tif_name, "%lu: Depth out of range, max %lu",
138 (unsigned long)z, (unsigned long)(td->td_imagedepth - 1));
139 return (0);
140 }
142 s >= td->td_samplesperpixel)
143 {
144 TIFFErrorExtR(tif, tif->tif_name, "%lu: Sample out of range, max %lu",
145 (unsigned long)s,
146 (unsigned long)(td->td_samplesperpixel - 1));
147 return (0);
148 }
149 return (1);
150}
151
152/*
153 * Compute how many tiles are in an image.
154 */
156{
157 TIFFDirectory *td = &tif->tif_dir;
158 uint32_t dx = td->td_tilewidth;
159 uint32_t dy = td->td_tilelength;
160 uint32_t dz = td->td_tiledepth;
161 uint32_t ntiles;
162
163 if (dx == (uint32_t)-1)
164 dx = td->td_imagewidth;
165 if (dy == (uint32_t)-1)
166 dy = td->td_imagelength;
167 if (dz == (uint32_t)-1)
168 dz = td->td_imagedepth;
169 ntiles =
170 (dx == 0 || dy == 0 || dz == 0)
171 ? 0
173 tif,
176 "TIFFNumberOfTiles"),
177 TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
179 ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
180 "TIFFNumberOfTiles");
181 return (ntiles);
182}
183
184/*
185 * Compute the # bytes in each row of a tile.
186 */
188{
189 static const char module[] = "TIFFTileRowSize64";
190 TIFFDirectory *td = &tif->tif_dir;
191 uint64_t rowsize;
192 uint64_t tilerowsize;
193
194 if (td->td_tilelength == 0)
195 {
196 TIFFErrorExtR(tif, module, "Tile length is zero");
197 return 0;
198 }
199 if (td->td_tilewidth == 0)
200 {
201 TIFFErrorExtR(tif, module, "Tile width is zero");
202 return (0);
203 }
204 /* Packed YCbCr data hold one Cb+Cr pair for every
205 * HorizontalSampling*VerticalSampling Y values, so the size of one tile
206 * row is not simply tilewidth*bps*spp/8. Mirror TIFFScanlineSize64():
207 * compute the size of one row of sampling blocks, which covers
208 * VerticalSampling image rows, and divide by VerticalSampling, so that
209 * the invariant
210 * TIFFTileSize(tif) == TIFFTileRowSize(tif) * td_tilelength
211 * holds for subsampled YCbCr as it does for every other layout (tile
212 * dimensions are multiples of 16, so the division is exact for the
213 * supported sampling factors). Without this, a consumer that allocates
214 * TIFFTileSize and strides rows by TIFFTileRowSize walks past the end
215 * of its buffer. As with TIFFScanlineSize64(), the result is not the
216 * byte size of one visual row of interleaved pixels; it only makes
217 * sense multiplied by a multiple of VerticalSampling. */
218 if ((td->td_planarconfig == PLANARCONFIG_CONTIG) &&
219 (td->td_photometric == PHOTOMETRIC_YCBCR) &&
220 (td->td_samplesperpixel == 3) && (!isUpSampled(tif)))
221 {
222 uint16_t ycbcrsubsampling[2];
223 uint16_t samplingblock_samples;
224 uint32_t samplingblocks_hor;
225 uint64_t samplingrow_samples;
226
228 ycbcrsubsampling + 0, ycbcrsubsampling + 1);
229 if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 &&
230 ycbcrsubsampling[0] != 4) ||
231 (ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 &&
232 ycbcrsubsampling[1] != 4) ||
233 (ycbcrsubsampling[0] == 0 || ycbcrsubsampling[1] == 0))
234 {
235 TIFFErrorExtR(tif, module, "Invalid YCbCr subsampling (%u*%u)",
236 ycbcrsubsampling[0], ycbcrsubsampling[1]);
237 return 0;
238 }
239 samplingblock_samples =
240 (uint16_t)(ycbcrsubsampling[0] * ycbcrsubsampling[1] + 2);
241 samplingblocks_hor =
242 TIFFhowmany_32(td->td_tilewidth, ycbcrsubsampling[0]);
243 samplingrow_samples = _TIFFMultiply64(tif, samplingblocks_hor,
244 samplingblock_samples, module);
245 tilerowsize =
246 TIFFhowmany8_64(_TIFFMultiply64(tif, samplingrow_samples,
247 td->td_bitspersample, module)) /
248 ycbcrsubsampling[1];
249 if (tilerowsize == 0)
250 {
251 TIFFErrorExtR(tif, module, "Computed tile row size is zero");
252 return 0;
253 }
254 return (tilerowsize);
255 }
256 rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
257 "TIFFTileRowSize");
258 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
259 {
260 if (td->td_samplesperpixel == 0)
261 {
262 TIFFErrorExtR(tif, module, "Samples per pixel is zero");
263 return 0;
264 }
265 rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
266 "TIFFTileRowSize");
267 }
268 tilerowsize = TIFFhowmany8_64(rowsize);
269 if (tilerowsize == 0)
270 {
271 TIFFErrorExtR(tif, module, "Computed tile row size is zero");
272 return 0;
273 }
274 return (tilerowsize);
275}
277{
278 static const char module[] = "TIFFTileRowSize";
279 uint64_t m;
280 m = TIFFTileRowSize64(tif);
281 return _TIFFCastUInt64ToSSize(tif, m, module);
282}
283
284/*
285 * Compute the # bytes in a variable length, row-aligned tile.
286 */
287uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows)
288{
289 return _TIFFStrileSize64(tif, nrows, /* isStrip = */ FALSE);
290}
291
292tmsize_t TIFFVTileSize(TIFF *tif, uint32_t nrows)
293{
294 static const char module[] = "TIFFVTileSize";
295 uint64_t m;
296 m = TIFFVTileSize64(tif, nrows);
297 return _TIFFCastUInt64ToSSize(tif, m, module);
298}
299
300/*
301 * Compute the # bytes in a row-aligned tile.
302 */
303uint64_t TIFFTileSize64(TIFF *tif)
304{
305 return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
306}
308{
309 static const char module[] = "TIFFTileSize";
310 uint64_t m;
311 m = TIFFTileSize64(tif);
312 return _TIFFCastUInt64ToSSize(tif, m, module);
313}
314
315/*
316 * Compute a default tile size based on the image
317 * characteristics and a requested value. If a
318 * request is <1 then we choose a size according
319 * to certain heuristics.
320 */
321void TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
322{
323 (*tif->tif_deftilesize)(tif, tw, th);
324}
325
326void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
327{
328 (void)tif;
329 if (*(int32_t *)tw < 1)
330 *tw = 256;
331 if (*(int32_t *)th < 1)
332 *th = 256;
333 /* roundup to a multiple of 16 per the spec */
334 if (*tw & 0xf)
335 *tw = TIFFroundup_32(*tw, 16);
336 if (*th & 0xf)
337 *th = TIFFroundup_32(*th, 16);
338}
uint32_t _TIFFMultiply32Ext(TIFF *tif, uint32_t first, uint32_t second, const char *where, bool *pOverflowed)
Multiply two unsigned values with an overflow check, and record an overflow in a caller-provided flag...
Definition tif_aux.c:166
uint32_t _TIFFMultiply32(TIFF *tif, uint32_t first, uint32_t second, const char *where)
Multiply two unsigned values with an overflow check.
Definition tif_aux.c:40
uint32_t _TIFFCastUInt64ToUInt32Ext(TIFF *tif, uint64_t val, const char *module, bool *pOverflowed)
Convert an unsigned 64-bit value after checking the destination range, and record a rejection in a ca...
Definition tif_aux.c:197
tmsize_t _TIFFCastUInt64ToSSize(TIFF *tif, uint64_t val, const char *module)
Convert an unsigned 64-bit value after checking the destination range.
Definition tif_aux.c:140
uint64_t _TIFFAdd64Ext(TIFF *tif, uint64_t first, uint64_t second, const char *where, bool *pOverflowed)
Check and add two unsigned operands, and record an overflow in a caller-provided flag.
Definition tif_aux.c:180
uint64_t _TIFFMultiply64(TIFF *tif, uint64_t first, uint64_t second, const char *where)
Multiply two unsigned values with an overflow check.
Definition tif_aux.c:46
void _TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
Supply default tile dimensions and round them to multiples of sixteen.
Definition tif_tile.c:326
uint64_t _TIFFStrileSize64(TIFF *tif, uint32_t nrows, int isStrip)
Compute row-aligned decoded storage for a strip or tile-height request.
Definition tif_strip.c:106
uint32_t _TIFFComputeTileExt(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s, bool *pOverflowed)
Compute a tile index as TIFFComputeTile() does, and record an index overflow in a caller-provided fla...
Definition tif_tile.c:52
#define isUpSampled(tif)
Test whether the codec reports upsampled output.
Definition tiffiop.h:869
#define TIFFroundup_32(x, y)
Round a 32-bit value up to a multiple using the guarded quotient.
Definition tiffiop.h:1102
#define FALSE
Represent the legacy internal false value.
Definition tiffiop.h:136
#define TIFFhowmany_32(x, y)
Compute a guarded 32-bit ceiling quotient.
Definition tiffiop.h:1056
#define TIFFhowmany8_64(x)
Compute the byte count needed to hold a 64-bit bit count.
Definition tiffiop.h:1127
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
tmsize_t TIFFTileSize(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:307
uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows)
Compute the decoded-size estimate for a requested tile height.
Definition tif_tile.c:287
tmsize_t TIFFTileRowSize(TIFF *tif)
Compute the packed byte count of one tile row.
Definition tif_tile.c:276
uint64_t TIFFTileSize64(TIFF *tif)
Compute the decoded-size estimate for the configured tile height.
Definition tif_tile.c:303
tmsize_t TIFFVTileSize(TIFF *tif, uint32_t nrows)
Compute the decoded-size estimate for a requested tile height.
Definition tif_tile.c:292
uint32_t TIFFComputeTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
Compute a tile index from pixel and sample coordinates.
Definition tif_tile.c:40
int TIFFCheckTile(TIFF *tif, uint32_t x, uint32_t y, uint32_t z, uint16_t s)
Check pixel and separate-sample coordinates against image bounds.
Definition tif_tile.c:118
uint32_t TIFFNumberOfTiles(TIFF *tif)
Compute the tile count across image depth and sample planes.
Definition tif_tile.c:155
void TIFFDefaultTileSize(TIFF *tif, uint32_t *tw, uint32_t *th)
Ask the installed sizing method to adjust tile width and height.
Definition tif_tile.c:321
uint64_t TIFFTileRowSize64(TIFF *tif)
Compute the packed byte count of one tile row.
Definition tif_tile.c:187
int TIFFGetFieldDefaulted(TIFF *tif, uint32_t tag,...)
Query a present tag or a supported default.
Definition tif_aux.c:591
#define PLANARCONFIG_SEPARATE
Separate planes of data.
Definition tiff.h:474
#define PHOTOMETRIC_YCBCR
CCIR 601 (introduced in TIFF 6.0).
Definition tiff.h:396
#define TIFFTAG_YCBCRSUBSAMPLING
TIFF tag: YCbCr subsampling factors (introduced in TIFF 6.0).
Definition tiff.h:729
#define PLANARCONFIG_CONTIG
Single image plane.
Definition tiff.h:472
TIFF_SSIZE_T tmsize_t
Represent a signed memory or I/O size in bytes.
Definition tiffio.h:89
Store the current directory values and image traversal state.
Definition tif_dir.h:143
uint32_t td_tiledepth
Tile depth in slices.
Definition tif_dir.h:164
uint32_t td_imagewidth
Image width in pixels.
Definition tif_dir.h:154
uint32_t td_tilewidth
Tile width in pixels.
Definition tif_dir.h:160
uint32_t td_imagedepth
Image depth in slices.
Definition tif_dir.h:158
uint16_t td_planarconfig
PLANARCONFIG_* sample-plane arrangement.
Definition tif_dir.h:200
uint16_t td_samplesperpixel
Samples per pixel, including extra samples.
Definition tif_dir.h:182
uint32_t td_imagelength
Image length in rows.
Definition tif_dir.h:156
uint32_t td_tilelength
Tile length in rows.
Definition tif_dir.h:162
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
TIFFTileMethod tif_deftilesize
Codec callback computing or constraining default tile dimensions.
Definition tiffiop.h:667
char * tif_name
Current filename pointer; may refer to inline or client-supplied storage.
Definition tiffiop.h:394
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
Declare private library state, callbacks, and implementation helpers.