1 /* $Header: /usr/people/sam/tiff/libtiff/RCS/tif_tile.c,v 1.18 1994/09/17 23:22:37 sam Exp $ */
2
3 /*
4 * Copyright (c) 1991, 1992, 1993, 1994 Sam Leffler
5 * Copyright (c) 1991, 1992, 1993, 1994 Silicon Graphics, Inc.
6 *
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.
14 *
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.
18 *
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
24 * OF THIS SOFTWARE.
25 */
26
27 /*
28 * TIFF Library.
29 *
30 * Tiled Image Support Routines.
31 */
32 #include "tiffiop.h"
33
34 /*
35 * Compute which tile an (x,y,z,s) value is in.
36 */
37 ttile_t
TIFFComputeTile(TIFF * tif,uint32 x,uint32 y,uint32 z,tsample_t s)38 TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
39 {
40 TIFFDirectory *td = &tif->tif_dir;
41 uint32 dx = td->td_tilewidth;
42 uint32 dy = td->td_tilelength;
43 uint32 dz = td->td_tiledepth;
44 ttile_t tile = 1;
45
46 if (td->td_imagedepth == 1)
47 z = 0;
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 = howmany(td->td_imagewidth, dx);
56 uint32 ypt = howmany(td->td_imagelength, dy);
57 uint32 zpt = howmany(td->td_imagedepth, dz);
58
59 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
60 tile = (xpt*ypt*zpt)*s +
61 (xpt*ypt)*(z/dz) +
62 xpt*(y/dy) +
63 x/dx;
64 else
65 tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx + s;
66 }
67 return (tile);
68 }
69
70 /*
71 * Check an (x,y,z,s) coordinate
72 * against the image bounds.
73 */
TIFFCheckTile(TIFF * tif,uint32 x,uint32 y,uint32 z,tsample_t s)74 TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, tsample_t s)
75 {
76 TIFFDirectory *td = &tif->tif_dir;
77
78 if (x >= td->td_imagewidth) {
79 TIFFError(tif->tif_name, "Col %ld out of range, max %lu",
80 (long) x, (u_long) td->td_imagewidth);
81 return (0);
82 }
83 if (y >= td->td_imagelength) {
84 TIFFError(tif->tif_name, "Row %ld out of range, max %lu",
85 (long) y, (u_long) td->td_imagelength);
86 return (0);
87 }
88 if (z >= td->td_imagedepth) {
89 TIFFError(tif->tif_name, "Depth %ld out of range, max %lu",
90 (long) z, (u_long) td->td_imagedepth);
91 return (0);
92 }
93 if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
94 s >= td->td_samplesperpixel) {
95 TIFFError(tif->tif_name, "Sample %d out of range, max %u",
96 (int) s, td->td_samplesperpixel);
97 return (0);
98 }
99 return (1);
100 }
101
102 /*
103 * Compute how many tiles are in an image.
104 */
105 ttile_t
TIFFNumberOfTiles(TIFF * tif)106 TIFFNumberOfTiles(TIFF* tif)
107 {
108 TIFFDirectory *td = &tif->tif_dir;
109 uint32 dx = td->td_tilewidth;
110 uint32 dy = td->td_tilelength;
111 uint32 dz = td->td_tiledepth;
112 ttile_t ntiles;
113
114 if (dx == (uint32) -1)
115 dx = td->td_imagewidth;
116 if (dy == (uint32) -1)
117 dy = td->td_imagelength;
118 if (dz == (uint32) -1)
119 dz = td->td_imagedepth;
120 ntiles = (dx != 0 && dy != 0 && dz != 0) ?
121 (howmany(td->td_imagewidth, dx) * howmany(td->td_imagelength, dy) *
122 howmany(td->td_imagedepth, dz)) :
123 0;
124 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
125 ntiles *= td->td_samplesperpixel;
126 return (ntiles);
127 }
128
129 /*
130 * Compute the # bytes in each row of a tile.
131 */
132 tsize_t
TIFFTileRowSize(TIFF * tif)133 TIFFTileRowSize(TIFF* tif)
134 {
135 TIFFDirectory *td = &tif->tif_dir;
136 tsize_t rowsize;
137
138 if (td->td_tilelength == 0 || td->td_tilewidth == 0)
139 return ((tsize_t) 0);
140 rowsize = td->td_bitspersample * td->td_tilewidth;
141 if (td->td_planarconfig == PLANARCONFIG_CONTIG)
142 rowsize *= td->td_samplesperpixel;
143 return (howmany(rowsize, 8));
144 }
145
146 /*
147 * Compute the # bytes in a variable length, row-aligned tile.
148 */
149 tsize_t
TIFFVTileSize(TIFF * tif,uint32 nrows)150 TIFFVTileSize(TIFF* tif, uint32 nrows)
151 {
152 TIFFDirectory *td = &tif->tif_dir;
153 tsize_t tilesize;
154
155 if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
156 td->td_tiledepth == 0)
157 return ((tsize_t) 0);
158 #ifdef YCBCR_SUPPORT
159 if (td->td_planarconfig == PLANARCONFIG_CONTIG &&
160 td->td_photometric == PHOTOMETRIC_YCBCR) {
161 /*
162 * Packed YCbCr data contain one Cb+Cr for every
163 * HorizontalSampling*VerticalSampling Y values.
164 * Must also roundup width and height when calculating
165 * since images that are not a multiple of the
166 * horizontal/vertical subsampling area include
167 * YCbCr data for the extended image.
168 */
169 tsize_t w =
170 roundup(td->td_tilewidth, td->td_ycbcrsubsampling[0]);
171 tsize_t rowsize = howmany(w*td->td_bitspersample, 8);
172 tsize_t samplingarea =
173 td->td_ycbcrsubsampling[0]*td->td_ycbcrsubsampling[1];
174 nrows = roundup(nrows, td->td_ycbcrsubsampling[1]);
175 /* NB: don't need howmany here 'cuz everything is rounded */
176 tilesize = nrows*rowsize + 2*(nrows*rowsize / samplingarea);
177 } else
178 #endif
179 tilesize = nrows * TIFFTileRowSize(tif);
180 return (tilesize * td->td_tiledepth);
181 }
182
183 /*
184 * Compute the # bytes in a row-aligned tile.
185 */
186 tsize_t
TIFFTileSize(TIFF * tif)187 TIFFTileSize(TIFF* tif)
188 {
189 return (TIFFVTileSize(tif, tif->tif_dir.td_tilelength));
190 }
191