2 * Copyright (c) 1991-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
28 * Tiled Image Support Routines.
34 * Compute which tile an (x,y,z,s) value is in.
37 TIFFComputeTile(TIFF
* tif
, uint32 x
, uint32 y
, uint32 z
, uint16 s
)
39 TIFFDirectory
*td
= &tif
->tif_dir
;
40 uint32 dx
= td
->td_tilewidth
;
41 uint32 dy
= td
->td_tilelength
;
42 uint32 dz
= td
->td_tiledepth
;
45 if (td
->td_imagedepth
== 1)
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
);
58 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
)
59 tile
= (xpt
*ypt
*zpt
)*s
+
64 tile
= (xpt
*ypt
)*(z
/dz
) + xpt
*(y
/dy
) + x
/dx
;
70 * Check an (x,y,z,s) coordinate
71 * against the image bounds.
74 TIFFCheckTile(TIFF
* tif
, uint32 x
, uint32 y
, uint32 z
, uint16 s
)
76 TIFFDirectory
*td
= &tif
->tif_dir
;
78 if (x
>= td
->td_imagewidth
) {
79 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
80 "%lu: Col out of range, max %lu",
82 (unsigned long) (td
->td_imagewidth
- 1));
85 if (y
>= td
->td_imagelength
) {
86 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
87 "%lu: Row out of range, max %lu",
89 (unsigned long) (td
->td_imagelength
- 1));
92 if (z
>= td
->td_imagedepth
) {
93 TIFFErrorExt(tif
->tif_clientdata
, tif
->tif_name
,
94 "%lu: Depth out of range, max %lu",
96 (unsigned long) (td
->td_imagedepth
- 1));
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",
104 (unsigned long) (td
->td_samplesperpixel
- 1));
111 * Compute how many tiles are in an image.
114 TIFFNumberOfTiles(TIFF
* tif
)
116 TIFFDirectory
*td
= &tif
->tif_dir
;
117 uint32 dx
= td
->td_tilewidth
;
118 uint32 dy
= td
->td_tilelength
;
119 uint32 dz
= td
->td_tiledepth
;
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");
140 * Compute the # bytes in each row of a tile.
143 TIFFTileRowSize64(TIFF
* tif
)
145 static const char module
[] = "TIFFTileRowSize64";
146 TIFFDirectory
*td
= &tif
->tif_dir
;
150 if (td
->td_tilelength
== 0)
152 TIFFErrorExt(tif
->tif_clientdata
,module
,"Tile length is zero");
155 if (td
->td_tilewidth
== 0)
157 TIFFErrorExt(tif
->tif_clientdata
,module
,"Tile width is zero");
160 rowsize
= _TIFFMultiply64(tif
, td
->td_bitspersample
, td
->td_tilewidth
,
162 if (td
->td_planarconfig
== PLANARCONFIG_CONTIG
)
164 if (td
->td_samplesperpixel
== 0)
166 TIFFErrorExt(tif
->tif_clientdata
,module
,"Samples per pixel is zero");
169 rowsize
= _TIFFMultiply64(tif
, rowsize
, td
->td_samplesperpixel
,
172 tilerowsize
=TIFFhowmany8_64(rowsize
);
173 if (tilerowsize
== 0)
175 TIFFErrorExt(tif
->tif_clientdata
,module
,"Computed tile row size is zero");
178 return (tilerowsize
);
181 TIFFTileRowSize(TIFF
* tif
)
183 static const char module
[] = "TIFFTileRowSize";
186 m
=TIFFTileRowSize64(tif
);
190 TIFFErrorExt(tif
->tif_clientdata
,module
,"Integer overflow");
197 * Compute the # bytes in a variable length, row-aligned tile.
200 TIFFVTileSize64(TIFF
* tif
, uint32 nrows
)
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)
207 if ((td
->td_planarconfig
==PLANARCONFIG_CONTIG
)&&
208 (td
->td_photometric
==PHOTOMETRIC_YCBCR
)&&
209 (td
->td_samplesperpixel
==3)&&
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.
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,
228 if ((ycbcrsubsampling
[0] != 1 && ycbcrsubsampling
[0] != 2 && ycbcrsubsampling
[0] != 4)
229 ||(ycbcrsubsampling
[1] != 1 && ycbcrsubsampling
[1] != 2 && ycbcrsubsampling
[1] != 4))
231 TIFFErrorExt(tif
->tif_clientdata
,module
,
232 "Invalid YCbCr subsampling (%dx%d)",
234 ycbcrsubsampling
[1] );
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
));
245 return(_TIFFMultiply64(tif
,nrows
,TIFFTileRowSize64(tif
),module
));
248 TIFFVTileSize(TIFF
* tif
, uint32 nrows
)
250 static const char module
[] = "TIFFVTileSize";
253 m
=TIFFVTileSize64(tif
,nrows
);
257 TIFFErrorExt(tif
->tif_clientdata
,module
,"Integer overflow");
264 * Compute the # bytes in a row-aligned tile.
267 TIFFTileSize64(TIFF
* tif
)
269 return (TIFFVTileSize64(tif
, tif
->tif_dir
.td_tilelength
));
272 TIFFTileSize(TIFF
* tif
)
274 static const char module
[] = "TIFFTileSize";
277 m
=TIFFTileSize64(tif
);
281 TIFFErrorExt(tif
->tif_clientdata
,module
,"Integer overflow");
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.
294 TIFFDefaultTileSize(TIFF
* tif
, uint32
* tw
, uint32
* th
)
296 (*tif
->tif_deftilesize
)(tif
, tw
, th
);
300 _TIFFDefaultTileSize(TIFF
* tif
, uint32
* tw
, uint32
* th
)
303 if (*(int32
*) tw
< 1)
305 if (*(int32
*) th
< 1)
307 /* roundup to a multiple of 16 per the spec */
309 *tw
= TIFFroundup_32(*tw
, 16);
311 *th
= TIFFroundup_32(*th
, 16);
314 /* vim: set ts=8 sts=8 sw=8 noet: */