3 * Copyright (c) 1991-1997 Sam Leffler
4 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
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.
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.
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
29 * 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 TIFFDirectory
*td
= &tif
->tif_dir
;
148 if (td
->td_tilelength
== 0 || td
->td_tilewidth
== 0)
150 rowsize
= _TIFFMultiply64(tif
, td
->td_bitspersample
, td
->td_tilewidth
,
152 if (td
->td_planarconfig
== PLANARCONFIG_CONTIG
)
153 rowsize
= _TIFFMultiply64(tif
, rowsize
, td
->td_samplesperpixel
,
155 return (TIFFhowmany8_64(rowsize
));
158 TIFFTileRowSize(TIFF
* tif
)
160 static const char module[] = "TIFFTileRowSize";
163 m
=TIFFTileRowSize64(tif
);
167 TIFFErrorExt(tif
->tif_clientdata
,module,"Integer overflow");
174 * Compute the # bytes in a variable length, row-aligned tile.
177 TIFFVTileSize64(TIFF
* tif
, uint32 nrows
)
179 static const char module[] = "TIFFVTileSize64";
180 TIFFDirectory
*td
= &tif
->tif_dir
;
181 if (td
->td_tilelength
== 0 || td
->td_tilewidth
== 0 ||
182 td
->td_tiledepth
== 0)
184 if ((td
->td_planarconfig
==PLANARCONFIG_CONTIG
)&&
185 (td
->td_photometric
==PHOTOMETRIC_YCBCR
)&&
186 (td
->td_samplesperpixel
==3)&&
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.
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,
205 if ((ycbcrsubsampling
[0] != 1 && ycbcrsubsampling
[0] != 2 && ycbcrsubsampling
[0] != 4)
206 ||(ycbcrsubsampling
[1] != 1 && ycbcrsubsampling
[1] != 2 && ycbcrsubsampling
[1] != 4))
208 TIFFErrorExt(tif
->tif_clientdata
,module,
209 "Invalid YCbCr subsampling (%dx%d)",
211 ycbcrsubsampling
[1] );
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: */