1 /* $Id: tif_tile.c,v 1.22 2010-07-01 15:33:28 dron 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 * Compute which tile an (x,y,z,s) value is in.
38 TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
40 TIFFDirectory *td = &tif->tif_dir;
41 uint32 dx = td->td_tilewidth;
42 uint32 dy = td->td_tilelength;
43 uint32 dz = td->td_tiledepth;
46 if (td->td_imagedepth == 1)
48 if (dx == (uint32) -1)
49 dx = td->td_imagewidth;
50 if (dy == (uint32) -1)
51 dy = td->td_imagelength;
52 if (dz == (uint32) -1)
53 dz = td->td_imagedepth;
54 if (dx != 0 && dy != 0 && dz != 0) {
55 uint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);
56 uint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);
57 uint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);
59 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
60 tile = (xpt*ypt*zpt)*s +
65 tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;
71 * Check an (x,y,z,s) coordinate
72 * against the image bounds.
75 TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
77 TIFFDirectory *td = &tif->tif_dir;
79 if (x >= td->td_imagewidth) {
80 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
81 "%lu: Col out of range, max %lu",
83 (unsigned long) (td->td_imagewidth - 1));
86 if (y >= td->td_imagelength) {
87 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
88 "%lu: Row out of range, max %lu",
90 (unsigned long) (td->td_imagelength - 1));
93 if (z >= td->td_imagedepth) {
94 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
95 "%lu: Depth out of range, max %lu",
97 (unsigned long) (td->td_imagedepth - 1));
100 if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
101 s >= td->td_samplesperpixel) {
102 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
103 "%lu: Sample out of range, max %lu",
105 (unsigned long) (td->td_samplesperpixel - 1));
112 * Compute how many tiles are in an image.
115 TIFFNumberOfTiles(TIFF* tif)
117 TIFFDirectory *td = &tif->tif_dir;
118 uint32 dx = td->td_tilewidth;
119 uint32 dy = td->td_tilelength;
120 uint32 dz = td->td_tiledepth;
123 if (dx == (uint32) -1)
124 dx = td->td_imagewidth;
125 if (dy == (uint32) -1)
126 dy = td->td_imagelength;
127 if (dz == (uint32) -1)
128 dz = td->td_imagedepth;
129 ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
130 _TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
131 TIFFhowmany_32(td->td_imagelength, dy),
132 "TIFFNumberOfTiles"),
133 TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
134 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
135 ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
136 "TIFFNumberOfTiles");
141 * Compute the # bytes in each row of a tile.
144 TIFFTileRowSize64(TIFF* tif)
146 TIFFDirectory *td = &tif->tif_dir;
149 if (td->td_tilelength == 0 || td->td_tilewidth == 0)
151 rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
153 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
154 rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
156 return (TIFFhowmany8_64(rowsize));
159 TIFFTileRowSize(TIFF* tif)
161 static const char module[] = "TIFFTileRowSize";
164 m=TIFFTileRowSize64(tif);
168 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
175 * Compute the # bytes in a variable length, row-aligned tile.
178 TIFFVTileSize64(TIFF* tif, uint32 nrows)
180 static const char module[] = "TIFFVTileSize64";
181 TIFFDirectory *td = &tif->tif_dir;
182 if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
183 td->td_tiledepth == 0)
185 if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
186 (td->td_photometric==PHOTOMETRIC_YCBCR)&&
187 (td->td_samplesperpixel==3)&&
191 * Packed YCbCr data contain one Cb+Cr for every
192 * HorizontalSampling*VerticalSampling Y values.
193 * Must also roundup width and height when calculating
194 * since images that are not a multiple of the
195 * horizontal/vertical subsampling area include
196 * YCbCr data for the extended image.
198 uint16 ycbcrsubsampling[2];
199 uint16 samplingblock_samples;
200 uint32 samplingblocks_hor;
201 uint32 samplingblocks_ver;
202 uint64 samplingrow_samples;
203 uint64 samplingrow_size;
204 TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
206 assert((ycbcrsubsampling[0]==1)||(ycbcrsubsampling[0]==2)||(ycbcrsubsampling[0]==4));
207 assert((ycbcrsubsampling[1]==1)||(ycbcrsubsampling[1]==2)||(ycbcrsubsampling[1]==4));
208 if (ycbcrsubsampling[0]*ycbcrsubsampling[1]==0)
210 TIFFErrorExt(tif->tif_clientdata,module,
211 "Invalid YCbCr subsampling");
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));
222 return(_TIFFMultiply64(tif,nrows,TIFFTileRowSize64(tif),module));
225 TIFFVTileSize(TIFF* tif, uint32 nrows)
227 static const char module[] = "TIFFVTileSize";
230 m=TIFFVTileSize64(tif,nrows);
234 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
241 * Compute the # bytes in a row-aligned tile.
244 TIFFTileSize64(TIFF* tif)
246 return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
249 TIFFTileSize(TIFF* tif)
251 static const char module[] = "TIFFTileSize";
254 m=TIFFTileSize64(tif);
258 TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
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.
271 TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
273 (*tif->tif_deftilesize)(tif, tw, th);
277 _TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
280 if (*(int32*) tw < 1)
282 if (*(int32*) th < 1)
284 /* roundup to a multiple of 16 per the spec */
286 *tw = TIFFroundup_32(*tw, 16);
288 *th = TIFFroundup_32(*th, 16);
291 /* vim: set ts=8 sts=8 sw=8 noet: */