/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 | 0 | { |
38 | 0 | TIFFDirectory *td = &tif->tif_dir; |
39 | 0 | uint32_t dx = td->td_tilewidth; |
40 | 0 | uint32_t dy = td->td_tilelength; |
41 | 0 | uint32_t dz = td->td_tiledepth; |
42 | 0 | uint32_t tile = 1; |
43 | |
|
44 | 0 | if (td->td_imagedepth == 1) |
45 | 0 | z = 0; |
46 | 0 | if (dx == (uint32_t)-1) |
47 | 0 | dx = td->td_imagewidth; |
48 | 0 | if (dy == (uint32_t)-1) |
49 | 0 | dy = td->td_imagelength; |
50 | 0 | if (dz == (uint32_t)-1) |
51 | 0 | dz = td->td_imagedepth; |
52 | 0 | if (dx != 0 && dy != 0 && dz != 0) |
53 | 0 | { |
54 | 0 | uint32_t xpt = TIFFhowmany_32(td->td_imagewidth, dx); |
55 | 0 | uint32_t ypt = TIFFhowmany_32(td->td_imagelength, dy); |
56 | 0 | uint32_t zpt = TIFFhowmany_32(td->td_imagedepth, dz); |
57 | 0 | uint32_t xpt_ypt = _TIFFMultiply32(tif, xpt, ypt, "TIFFComputeTile"); |
58 | 0 | uint32_t xpt_ypt_zpt = |
59 | 0 | _TIFFMultiply32(tif, xpt_ypt, zpt, "TIFFComputeTile"); |
60 | 0 | uint64_t z_offset; |
61 | 0 | uint64_t y_offset; |
62 | 0 | uint64_t tile64; |
63 | |
|
64 | 0 | if ((xpt_ypt == 0 && xpt != 0 && ypt != 0) || |
65 | 0 | (xpt_ypt_zpt == 0 && xpt_ypt != 0 && zpt != 0)) |
66 | 0 | return (0); |
67 | | |
68 | 0 | z_offset = _TIFFMultiply64(tif, xpt_ypt, z / dz, "TIFFComputeTile"); |
69 | 0 | y_offset = _TIFFMultiply64(tif, xpt, y / dy, "TIFFComputeTile"); |
70 | 0 | if ((z_offset == 0 && xpt_ypt != 0 && (z / dz) != 0) || |
71 | 0 | (y_offset == 0 && xpt != 0 && (y / dy) != 0)) |
72 | 0 | return (0); |
73 | 0 | tile64 = _TIFFAdd64(tif, z_offset, y_offset, "TIFFComputeTile"); |
74 | 0 | if (tile64 == 0 && (z_offset != 0 || y_offset != 0)) |
75 | 0 | return (0); |
76 | 0 | tile64 = _TIFFAdd64(tif, tile64, x / dx, "TIFFComputeTile"); |
77 | 0 | if (tile64 == 0 && (z_offset != 0 || y_offset != 0 || (x / dx) != 0)) |
78 | 0 | return (0); |
79 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
80 | 0 | { |
81 | 0 | uint64_t sample_offset; |
82 | 0 | 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 | 0 | sample_offset = |
90 | 0 | _TIFFMultiply64(tif, xpt_ypt_zpt, s, "TIFFComputeTile"); |
91 | 0 | if (sample_offset == 0 && xpt_ypt_zpt != 0 && s != 0) |
92 | 0 | return (0); |
93 | 0 | tile64 = _TIFFAdd64(tif, sample_offset, tile64, "TIFFComputeTile"); |
94 | 0 | if (tile64 == 0 && (sample_offset != 0 || z_offset != 0 || |
95 | 0 | y_offset != 0 || (x / dx) != 0)) |
96 | 0 | return (0); |
97 | 0 | } |
98 | 0 | tile = _TIFFCastUInt64ToUInt32(tif, tile64, "TIFFComputeTile"); |
99 | 0 | if (tile == 0 && tile64 != 0) |
100 | 0 | return (0); |
101 | 0 | } |
102 | 0 | return (tile); |
103 | 0 | } |
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 | 0 | { |
111 | 0 | TIFFDirectory *td = &tif->tif_dir; |
112 | |
|
113 | 0 | 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 | 0 | 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 | 0 | 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 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE && |
133 | 0 | s >= td->td_samplesperpixel) |
134 | 0 | { |
135 | 0 | TIFFErrorExtR(tif, tif->tif_name, "%lu: Sample out of range, max %lu", |
136 | 0 | (unsigned long)s, |
137 | 0 | (unsigned long)(td->td_samplesperpixel - 1)); |
138 | 0 | return (0); |
139 | 0 | } |
140 | 0 | return (1); |
141 | 0 | } |
142 | | |
143 | | /* |
144 | | * Compute how many tiles are in an image. |
145 | | */ |
146 | | uint32_t TIFFNumberOfTiles(TIFF *tif) |
147 | 0 | { |
148 | 0 | TIFFDirectory *td = &tif->tif_dir; |
149 | 0 | uint32_t dx = td->td_tilewidth; |
150 | 0 | uint32_t dy = td->td_tilelength; |
151 | 0 | uint32_t dz = td->td_tiledepth; |
152 | 0 | uint32_t ntiles; |
153 | |
|
154 | 0 | if (dx == (uint32_t)-1) |
155 | 0 | dx = td->td_imagewidth; |
156 | 0 | if (dy == (uint32_t)-1) |
157 | 0 | dy = td->td_imagelength; |
158 | 0 | if (dz == (uint32_t)-1) |
159 | 0 | dz = td->td_imagedepth; |
160 | 0 | ntiles = |
161 | 0 | (dx == 0 || dy == 0 || dz == 0) |
162 | 0 | ? 0 |
163 | 0 | : _TIFFMultiply32( |
164 | 0 | tif, |
165 | 0 | _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx), |
166 | 0 | TIFFhowmany_32(td->td_imagelength, dy), |
167 | 0 | "TIFFNumberOfTiles"), |
168 | 0 | TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles"); |
169 | 0 | if (td->td_planarconfig == PLANARCONFIG_SEPARATE) |
170 | 0 | ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel, |
171 | 0 | "TIFFNumberOfTiles"); |
172 | 0 | return (ntiles); |
173 | 0 | } |
174 | | |
175 | | /* |
176 | | * Compute the # bytes in each row of a tile. |
177 | | */ |
178 | | uint64_t TIFFTileRowSize64(TIFF *tif) |
179 | 0 | { |
180 | 0 | static const char module[] = "TIFFTileRowSize64"; |
181 | 0 | TIFFDirectory *td = &tif->tif_dir; |
182 | 0 | uint64_t rowsize; |
183 | 0 | uint64_t tilerowsize; |
184 | |
|
185 | 0 | if (td->td_tilelength == 0) |
186 | 0 | { |
187 | 0 | TIFFErrorExtR(tif, module, "Tile length is zero"); |
188 | 0 | return 0; |
189 | 0 | } |
190 | 0 | if (td->td_tilewidth == 0) |
191 | 0 | { |
192 | 0 | TIFFErrorExtR(tif, module, "Tile width is zero"); |
193 | 0 | return (0); |
194 | 0 | } |
195 | 0 | rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth, |
196 | 0 | "TIFFTileRowSize"); |
197 | 0 | if (td->td_planarconfig == PLANARCONFIG_CONTIG) |
198 | 0 | { |
199 | 0 | if (td->td_samplesperpixel == 0) |
200 | 0 | { |
201 | 0 | TIFFErrorExtR(tif, module, "Samples per pixel is zero"); |
202 | 0 | return 0; |
203 | 0 | } |
204 | 0 | rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel, |
205 | 0 | "TIFFTileRowSize"); |
206 | 0 | } |
207 | 0 | tilerowsize = TIFFhowmany8_64(rowsize); |
208 | 0 | if (tilerowsize == 0) |
209 | 0 | { |
210 | 0 | TIFFErrorExtR(tif, module, "Computed tile row size is zero"); |
211 | 0 | return 0; |
212 | 0 | } |
213 | 0 | return (tilerowsize); |
214 | 0 | } |
215 | | tmsize_t TIFFTileRowSize(TIFF *tif) |
216 | 0 | { |
217 | 0 | static const char module[] = "TIFFTileRowSize"; |
218 | 0 | uint64_t m; |
219 | 0 | m = TIFFTileRowSize64(tif); |
220 | 0 | return _TIFFCastUInt64ToSSize(tif, m, module); |
221 | 0 | } |
222 | | |
223 | | /* |
224 | | * Compute the # bytes in a variable length, row-aligned tile. |
225 | | */ |
226 | | uint64_t TIFFVTileSize64(TIFF *tif, uint32_t nrows) |
227 | 0 | { |
228 | 0 | return _TIFFStrileSize64(tif, nrows, /* isStrip = */ FALSE); |
229 | 0 | } |
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 | 0 | { |
244 | 0 | return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength)); |
245 | 0 | } |
246 | | tmsize_t TIFFTileSize(TIFF *tif) |
247 | 0 | { |
248 | 0 | static const char module[] = "TIFFTileSize"; |
249 | 0 | uint64_t m; |
250 | 0 | m = TIFFTileSize64(tif); |
251 | 0 | return _TIFFCastUInt64ToSSize(tif, m, module); |
252 | 0 | } |
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 | } |