1 /* $Id: tif_tile.c,v 1.1 2005/06/17 13:54:52 vp153 Exp $ */
4 * Copyright (c) 1991-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 * Tiled Image Support Routines.
35 summarize(TIFF* tif, size_t summand1, size_t summand2, const char* where)
38 * XXX: We are using casting to uint32 here, bacause sizeof(size_t)
39 * may be larger than sizeof(uint32) on 64-bit architectures.
41 uint32 bytes = summand1 + summand2;
43 if (bytes - summand1 != summand2) {
44 TIFFError(tif->tif_name, "Integer overflow in %s", where);
52 multiply(TIFF* tif, size_t nmemb, size_t elem_size, const char* where)
54 uint32 bytes = nmemb * elem_size;
56 if (elem_size && bytes / elem_size != nmemb) {
57 TIFFError(tif->tif_name, "Integer overflow in %s", where);
65 * Compute which tile an (x,y,z,s) value is in.
68 TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
70 TIFFDirectory *td = &tif->tif_dir;
71 uint32 dx = td->td_tilewidth;
72 uint32 dy = td->td_tilelength;
73 uint32 dz = td->td_tiledepth;
76 if (td->td_imagedepth == 1)
78 if (dx == (uint32) -1)
79 dx = td->td_imagewidth;
80 if (dy == (uint32) -1)
81 dy = td->td_imagelength;
82 if (dz == (uint32) -1)
83 dz = td->td_imagedepth;
84 if (dx != 0 && dy != 0 && dz != 0) {
85 uint32 xpt = TIFFhowmany(td->td_imagewidth, dx);
86 uint32 ypt = TIFFhowmany(td->td_imagelength, dy);
87 uint32 zpt = TIFFhowmany(td->td_imagedepth, dz);
89 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
90 tile = (xpt*ypt*zpt)*s +
95 tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;
101 * Check an (x,y,z,s) coordinate
102 * against the image bounds.
105 TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
107 TIFFDirectory *td = &tif->tif_dir;
109 if (x >= td->td_imagewidth) {
110 TIFFError(tif->tif_name, "%lu: Col out of range, max %lu",
111 (unsigned long) x, (unsigned long) td->td_imagewidth);
114 if (y >= td->td_imagelength) {
115 TIFFError(tif->tif_name, "%lu: Row out of range, max %lu",
116 (unsigned long) y, (unsigned long) td->td_imagelength);
119 if (z >= td->td_imagedepth) {
120 TIFFError(tif->tif_name, "%lu: Depth out of range, max %lu",
121 (unsigned long) z, (unsigned long) td->td_imagedepth);
124 if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
125 s >= td->td_samplesperpixel) {
126 TIFFError(tif->tif_name, "%lu: Sample out of range, max %lu",
127 (unsigned long) s, (unsigned long) td->td_samplesperpixel);
134 * Compute how many tiles are in an image.
137 TIFFNumberOfTiles(TIFF* tif)
139 TIFFDirectory *td = &tif->tif_dir;
140 uint32 dx = td->td_tilewidth;
141 uint32 dy = td->td_tilelength;
142 uint32 dz = td->td_tiledepth;
145 if (dx == (uint32) -1)
146 dx = td->td_imagewidth;
147 if (dy == (uint32) -1)
148 dy = td->td_imagelength;
149 if (dz == (uint32) -1)
150 dz = td->td_imagedepth;
151 ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
152 multiply(tif, multiply(tif, TIFFhowmany(td->td_imagewidth, dx),
153 TIFFhowmany(td->td_imagelength, dy),
154 "TIFFNumberOfTiles"),
155 TIFFhowmany(td->td_imagedepth, dz), "TIFFNumberOfTiles");
156 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
157 ntiles = multiply(tif, ntiles, td->td_samplesperpixel,
158 "TIFFNumberOfTiles");
163 * Compute the # bytes in each row of a tile.
166 TIFFTileRowSize(TIFF* tif)
168 TIFFDirectory *td = &tif->tif_dir;
171 if (td->td_tilelength == 0 || td->td_tilewidth == 0)
172 return ((tsize_t) 0);
173 rowsize = multiply(tif, td->td_bitspersample, td->td_tilewidth,
175 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
176 rowsize = multiply(tif, rowsize, td->td_samplesperpixel,
178 return ((tsize_t) TIFFhowmany8(rowsize));
182 * Compute the # bytes in a variable length, row-aligned tile.
185 TIFFVTileSize(TIFF* tif, uint32 nrows)
187 TIFFDirectory *td = &tif->tif_dir;
190 if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
191 td->td_tiledepth == 0)
192 return ((tsize_t) 0);
193 if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
194 td->td_photometric == PHOTOMETRIC_YCBCR &&
197 * Packed YCbCr data contain one Cb+Cr for every
198 * HorizontalSampling*VerticalSampling Y values.
199 * Must also roundup width and height when calculating
200 * since images that are not a multiple of the
201 * horizontal/vertical subsampling area include
202 * YCbCr data for the extended image.
205 TIFFroundup(td->td_tilewidth, td->td_ycbcrsubsampling[0]);
207 TIFFhowmany8(multiply(tif, w, td->td_bitspersample,
209 tsize_t samplingarea =
210 td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
211 if (samplingarea == 0) {
212 TIFFError(tif->tif_name, "Invalid YCbCr subsampling");
215 nrows = TIFFroundup(nrows, td->td_ycbcrsubsampling[1]);
216 /* NB: don't need TIFFhowmany here 'cuz everything is rounded */
217 tilesize = multiply(tif, nrows, rowsize, "TIFFVTileSize");
218 tilesize = summarize(tif, tilesize,
219 multiply(tif, 2, tilesize / samplingarea,
223 tilesize = multiply(tif, nrows, TIFFTileRowSize(tif),
226 multiply(tif, tilesize, td->td_tiledepth, "TIFFVTileSize"));
230 * Compute the # bytes in a row-aligned tile.
233 TIFFTileSize(TIFF* tif)
235 return (TIFFVTileSize(tif, tif->tif_dir.td_tilelength));
239 * Compute a default tile size based on the image
240 * characteristics and a requested value. If a
241 * request is <1 then we choose a size according
242 * to certain heuristics.
245 TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
247 (*tif->tif_deftilesize)(tif, tw, th);
251 _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
254 if (*(int32*) tw < 1)
256 if (*(int32*) th < 1)
258 /* roundup to a multiple of 16 per the spec */
260 *tw = TIFFroundup(*tw, 16);
262 *th = TIFFroundup(*th, 16);
265 /* vim: set ts=8 sts=8 sw=8 noet: */