]> git.saurik.com Git - wxWidgets.git/blame - src/tiff/libtiff/tif_tile.c
Added wxRichTextTableBlock class to help with table UI operations
[wxWidgets.git] / src / tiff / libtiff / tif_tile.c
CommitLineData
8414a40c
VZ
1
2/*
3 * Copyright (c) 1991-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
13 *
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 */
25
26/*
27 * TIFF Library.
28 *
29 * Tiled Image Support Routines.
30 */
31#include "tiffiop.h"
32
8414a40c
VZ
33/*
34 * Compute which tile an (x,y,z,s) value is in.
35 */
80ed523f
VZ
36uint32
37TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
8414a40c
VZ
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;
80ed523f 43 uint32 tile = 1;
8414a40c
VZ
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) {
80ed523f
VZ
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);
8414a40c
VZ
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 */
73int
80ed523f 74TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
8414a40c
VZ
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 */
80ed523f 113uint32
8414a40c
VZ
114TIFFNumberOfTiles(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;
80ed523f 120 uint32 ntiles;
8414a40c
VZ
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 :
80ed523f
VZ
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");
8414a40c 133 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
80ed523f
VZ
134 ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
135 "TIFFNumberOfTiles");
8414a40c
VZ
136 return (ntiles);
137}
138
139/*
140 * Compute the # bytes in each row of a tile.
141 */
80ed523f
VZ
142uint64
143TIFFTileRowSize64(TIFF* tif)
8414a40c
VZ
144{
145 TIFFDirectory *td = &tif->tif_dir;
80ed523f
VZ
146 uint64 rowsize;
147
8414a40c 148 if (td->td_tilelength == 0 || td->td_tilewidth == 0)
80ed523f
VZ
149 return (0);
150 rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
151 "TIFFTileRowSize");
8414a40c 152 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
80ed523f
VZ
153 rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
154 "TIFFTileRowSize");
155 return (TIFFhowmany8_64(rowsize));
156}
157tmsize_t
158TIFFTileRowSize(TIFF* tif)
159{
160 static const char module[] = "TIFFTileRowSize";
161 uint64 m;
162 tmsize_t n;
163 m=TIFFTileRowSize64(tif);
164 n=(tmsize_t)m;
165 if ((uint64)n!=m)
166 {
167 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
168 n=0;
169 }
170 return(n);
8414a40c
VZ
171}
172
173/*
174 * Compute the # bytes in a variable length, row-aligned tile.
175 */
80ed523f
VZ
176uint64
177TIFFVTileSize64(TIFF* tif, uint32 nrows)
8414a40c 178{
80ed523f 179 static const char module[] = "TIFFVTileSize64";
8414a40c 180 TIFFDirectory *td = &tif->tif_dir;
8414a40c
VZ
181 if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
182 td->td_tiledepth == 0)
80ed523f
VZ
183 return (0);
184 if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
185 (td->td_photometric==PHOTOMETRIC_YCBCR)&&
186 (td->td_samplesperpixel==3)&&
187 (!isUpSampled(tif)))
188 {
8414a40c
VZ
189 /*
190 * Packed YCbCr data contain one Cb+Cr for every
191 * HorizontalSampling*VerticalSampling Y values.
192 * Must also roundup width and height when calculating
193 * since images that are not a multiple of the
194 * horizontal/vertical subsampling area include
195 * YCbCr data for the extended image.
196 */
80ed523f
VZ
197 uint16 ycbcrsubsampling[2];
198 uint16 samplingblock_samples;
199 uint32 samplingblocks_hor;
200 uint32 samplingblocks_ver;
201 uint64 samplingrow_samples;
202 uint64 samplingrow_size;
203 TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
204 ycbcrsubsampling+1);
205 if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
206 ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
207 {
208 TIFFErrorExt(tif->tif_clientdata,module,
209 "Invalid YCbCr subsampling (%dx%d)",
210 ycbcrsubsampling[0],
211 ycbcrsubsampling[1] );
8414a40c
VZ
212 return 0;
213 }
80ed523f
VZ
214 samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
215 samplingblocks_hor=TIFFhowmany_32(td->td_tilewidth,ycbcrsubsampling[0]);
216 samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
217 samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
218 samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
219 return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
220 }
221 else
222 return(_TIFFMultiply64(tif,nrows,TIFFTileRowSize64(tif),module));
223}
224tmsize_t
225TIFFVTileSize(TIFF* tif, uint32 nrows)
226{
227 static const char module[] = "TIFFVTileSize";
228 uint64 m;
229 tmsize_t n;
230 m=TIFFVTileSize64(tif,nrows);
231 n=(tmsize_t)m;
232 if ((uint64)n!=m)
233 {
234 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
235 n=0;
236 }
237 return(n);
8414a40c
VZ
238}
239
240/*
241 * Compute the # bytes in a row-aligned tile.
242 */
80ed523f
VZ
243uint64
244TIFFTileSize64(TIFF* tif)
245{
246 return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
247}
248tmsize_t
8414a40c
VZ
249TIFFTileSize(TIFF* tif)
250{
80ed523f
VZ
251 static const char module[] = "TIFFTileSize";
252 uint64 m;
253 tmsize_t n;
254 m=TIFFTileSize64(tif);
255 n=(tmsize_t)m;
256 if ((uint64)n!=m)
257 {
258 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
259 n=0;
260 }
261 return(n);
8414a40c
VZ
262}
263
264/*
265 * Compute a default tile size based on the image
266 * characteristics and a requested value. If a
267 * request is <1 then we choose a size according
268 * to certain heuristics.
269 */
270void
271TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
272{
273 (*tif->tif_deftilesize)(tif, tw, th);
274}
275
276void
277_TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
278{
279 (void) tif;
280 if (*(int32*) tw < 1)
281 *tw = 256;
282 if (*(int32*) th < 1)
283 *th = 256;
284 /* roundup to a multiple of 16 per the spec */
285 if (*tw & 0xf)
80ed523f 286 *tw = TIFFroundup_32(*tw, 16);
8414a40c 287 if (*th & 0xf)
80ed523f 288 *th = TIFFroundup_32(*th, 16);
8414a40c
VZ
289}
290
291/* vim: set ts=8 sts=8 sw=8 noet: */
80ed523f
VZ
292/*
293 * Local Variables:
294 * mode: c
295 * c-basic-offset: 8
296 * fill-column: 78
297 * End:
298 */