[LIBTIFF] Update to version 4.0.10. CORE-15854
[reactos.git] / dll / 3rdparty / libtiff / tif_tile.c
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
31 #include <precomp.h>
32
33 /*
34 * Compute which tile an (x,y,z,s) value is in.
35 */
36 uint32
37 TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
38 {
39 TIFFDirectory *td = &tif->tif_dir;
40 uint32 dx = td->td_tilewidth;
41 uint32 dy = td->td_tilelength;
42 uint32 dz = td->td_tiledepth;
43 uint32 tile = 1;
44
45 if (td->td_imagedepth == 1)
46 z = 0;
47 if (dx == (uint32) -1)
48 dx = td->td_imagewidth;
49 if (dy == (uint32) -1)
50 dy = td->td_imagelength;
51 if (dz == (uint32) -1)
52 dz = td->td_imagedepth;
53 if (dx != 0 && dy != 0 && dz != 0) {
54 uint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);
55 uint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);
56 uint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);
57
58 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
59 tile = (xpt*ypt*zpt)*s +
60 (xpt*ypt)*(z/dz) +
61 xpt*(y/dy) +
62 x/dx;
63 else
64 tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;
65 }
66 return (tile);
67 }
68
69 /*
70 * Check an (x,y,z,s) coordinate
71 * against the image bounds.
72 */
73 int
74 TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
75 {
76 TIFFDirectory *td = &tif->tif_dir;
77
78 if (x >= td->td_imagewidth) {
79 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
80 "%lu: Col out of range, max %lu",
81 (unsigned long) x,
82 (unsigned long) (td->td_imagewidth - 1));
83 return (0);
84 }
85 if (y >= td->td_imagelength) {
86 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
87 "%lu: Row out of range, max %lu",
88 (unsigned long) y,
89 (unsigned long) (td->td_imagelength - 1));
90 return (0);
91 }
92 if (z >= td->td_imagedepth) {
93 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
94 "%lu: Depth out of range, max %lu",
95 (unsigned long) z,
96 (unsigned long) (td->td_imagedepth - 1));
97 return (0);
98 }
99 if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
100 s >= td->td_samplesperpixel) {
101 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
102 "%lu: Sample out of range, max %lu",
103 (unsigned long) s,
104 (unsigned long) (td->td_samplesperpixel - 1));
105 return (0);
106 }
107 return (1);
108 }
109
110 /*
111 * Compute how many tiles are in an image.
112 */
113 uint32
114 TIFFNumberOfTiles(TIFF* tif)
115 {
116 TIFFDirectory *td = &tif->tif_dir;
117 uint32 dx = td->td_tilewidth;
118 uint32 dy = td->td_tilelength;
119 uint32 dz = td->td_tiledepth;
120 uint32 ntiles;
121
122 if (dx == (uint32) -1)
123 dx = td->td_imagewidth;
124 if (dy == (uint32) -1)
125 dy = td->td_imagelength;
126 if (dz == (uint32) -1)
127 dz = td->td_imagedepth;
128 ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
129 _TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
130 TIFFhowmany_32(td->td_imagelength, dy),
131 "TIFFNumberOfTiles"),
132 TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
133 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
134 ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
135 "TIFFNumberOfTiles");
136 return (ntiles);
137 }
138
139 /*
140 * Compute the # bytes in each row of a tile.
141 */
142 uint64
143 TIFFTileRowSize64(TIFF* tif)
144 {
145 static const char module[] = "TIFFTileRowSize64";
146 TIFFDirectory *td = &tif->tif_dir;
147 uint64 rowsize;
148 uint64 tilerowsize;
149
150 if (td->td_tilelength == 0)
151 {
152 TIFFErrorExt(tif->tif_clientdata,module,"Tile length is zero");
153 return 0;
154 }
155 if (td->td_tilewidth == 0)
156 {
157 TIFFErrorExt(tif->tif_clientdata,module,"Tile width is zero");
158 return (0);
159 }
160 rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
161 "TIFFTileRowSize");
162 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
163 {
164 if (td->td_samplesperpixel == 0)
165 {
166 TIFFErrorExt(tif->tif_clientdata,module,"Samples per pixel is zero");
167 return 0;
168 }
169 rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
170 "TIFFTileRowSize");
171 }
172 tilerowsize=TIFFhowmany8_64(rowsize);
173 if (tilerowsize == 0)
174 {
175 TIFFErrorExt(tif->tif_clientdata,module,"Computed tile row size is zero");
176 return 0;
177 }
178 return (tilerowsize);
179 }
180 tmsize_t
181 TIFFTileRowSize(TIFF* tif)
182 {
183 static const char module[] = "TIFFTileRowSize";
184 uint64 m;
185 tmsize_t n;
186 m=TIFFTileRowSize64(tif);
187 n=(tmsize_t)m;
188 if ((uint64)n!=m)
189 {
190 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
191 n=0;
192 }
193 return(n);
194 }
195
196 /*
197 * Compute the # bytes in a variable length, row-aligned tile.
198 */
199 uint64
200 TIFFVTileSize64(TIFF* tif, uint32 nrows)
201 {
202 static const char module[] = "TIFFVTileSize64";
203 TIFFDirectory *td = &tif->tif_dir;
204 if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
205 td->td_tiledepth == 0)
206 return (0);
207 if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
208 (td->td_photometric==PHOTOMETRIC_YCBCR)&&
209 (td->td_samplesperpixel==3)&&
210 (!isUpSampled(tif)))
211 {
212 /*
213 * Packed YCbCr data contain one Cb+Cr for every
214 * HorizontalSampling*VerticalSampling Y values.
215 * Must also roundup width and height when calculating
216 * since images that are not a multiple of the
217 * horizontal/vertical subsampling area include
218 * YCbCr data for the extended image.
219 */
220 uint16 ycbcrsubsampling[2];
221 uint16 samplingblock_samples;
222 uint32 samplingblocks_hor;
223 uint32 samplingblocks_ver;
224 uint64 samplingrow_samples;
225 uint64 samplingrow_size;
226 TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
227 ycbcrsubsampling+1);
228 if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
229 ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
230 {
231 TIFFErrorExt(tif->tif_clientdata,module,
232 "Invalid YCbCr subsampling (%dx%d)",
233 ycbcrsubsampling[0],
234 ycbcrsubsampling[1] );
235 return 0;
236 }
237 samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
238 samplingblocks_hor=TIFFhowmany_32(td->td_tilewidth,ycbcrsubsampling[0]);
239 samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
240 samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
241 samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
242 return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
243 }
244 else
245 return(_TIFFMultiply64(tif,nrows,TIFFTileRowSize64(tif),module));
246 }
247 tmsize_t
248 TIFFVTileSize(TIFF* tif, uint32 nrows)
249 {
250 static const char module[] = "TIFFVTileSize";
251 uint64 m;
252 tmsize_t n;
253 m=TIFFVTileSize64(tif,nrows);
254 n=(tmsize_t)m;
255 if ((uint64)n!=m)
256 {
257 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
258 n=0;
259 }
260 return(n);
261 }
262
263 /*
264 * Compute the # bytes in a row-aligned tile.
265 */
266 uint64
267 TIFFTileSize64(TIFF* tif)
268 {
269 return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
270 }
271 tmsize_t
272 TIFFTileSize(TIFF* tif)
273 {
274 static const char module[] = "TIFFTileSize";
275 uint64 m;
276 tmsize_t n;
277 m=TIFFTileSize64(tif);
278 n=(tmsize_t)m;
279 if ((uint64)n!=m)
280 {
281 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
282 n=0;
283 }
284 return(n);
285 }
286
287 /*
288 * Compute a default tile size based on the image
289 * characteristics and a requested value. If a
290 * request is <1 then we choose a size according
291 * to certain heuristics.
292 */
293 void
294 TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
295 {
296 (*tif->tif_deftilesize)(tif, tw, th);
297 }
298
299 void
300 _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
301 {
302 (void) tif;
303 if (*(int32*) tw < 1)
304 *tw = 256;
305 if (*(int32*) th < 1)
306 *th = 256;
307 /* roundup to a multiple of 16 per the spec */
308 if (*tw & 0xf)
309 *tw = TIFFroundup_32(*tw, 16);
310 if (*th & 0xf)
311 *th = TIFFroundup_32(*th, 16);
312 }
313
314 /* vim: set ts=8 sts=8 sw=8 noet: */
315 /*
316 * Local Variables:
317 * mode: c
318 * c-basic-offset: 8
319 * fill-column: 78
320 * End:
321 */