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
, tsample_t 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(td
->td_imagewidth
, dx
);
56 uint32 ypt
= TIFFhowmany(td
->td_imagelength
, dy
);
57 uint32 zpt
= TIFFhowmany(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
+ s
;
71 * Check an (x,y,z,s) coordinate
72 * against the image bounds.
75 TIFFCheckTile(TIFF
* tif
, uint32 x
, uint32 y
, uint32 z
, tsample_t s
)
77 TIFFDirectory
*td
= &tif
->tif_dir
;
79 if (x
>= td
->td_imagewidth
) {
80 TIFFError(tif
->tif_name
, "Col %ld out of range, max %lu",
81 (long) x
, (u_long
) td
->td_imagewidth
);
84 if (y
>= td
->td_imagelength
) {
85 TIFFError(tif
->tif_name
, "Row %ld out of range, max %lu",
86 (long) y
, (u_long
) td
->td_imagelength
);
89 if (z
>= td
->td_imagedepth
) {
90 TIFFError(tif
->tif_name
, "Depth %ld out of range, max %lu",
91 (long) z
, (u_long
) td
->td_imagedepth
);
94 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
&&
95 s
>= td
->td_samplesperpixel
) {
96 TIFFError(tif
->tif_name
, "Sample %d out of range, max %u",
97 (int) s
, td
->td_samplesperpixel
);
104 * Compute how many tiles are in an image.
107 TIFFNumberOfTiles(TIFF
* tif
)
109 TIFFDirectory
*td
= &tif
->tif_dir
;
110 uint32 dx
= td
->td_tilewidth
;
111 uint32 dy
= td
->td_tilelength
;
112 uint32 dz
= td
->td_tiledepth
;
115 if (dx
== (uint32
) -1)
116 dx
= td
->td_imagewidth
;
117 if (dy
== (uint32
) -1)
118 dy
= td
->td_imagelength
;
119 if (dz
== (uint32
) -1)
120 dz
= td
->td_imagedepth
;
121 ntiles
= (dx
== 0 || dy
== 0 || dz
== 0) ? 0 :
122 (TIFFhowmany(td
->td_imagewidth
, dx
) *
123 TIFFhowmany(td
->td_imagelength
, dy
) *
124 TIFFhowmany(td
->td_imagedepth
, dz
));
125 if (td
->td_planarconfig
== PLANARCONFIG_SEPARATE
)
126 ntiles
*= td
->td_samplesperpixel
;
131 * Compute the # bytes in each row of a tile.
134 TIFFTileRowSize(TIFF
* tif
)
136 TIFFDirectory
*td
= &tif
->tif_dir
;
139 if (td
->td_tilelength
== 0 || td
->td_tilewidth
== 0)
140 return ((tsize_t
) 0);
141 rowsize
= td
->td_bitspersample
* td
->td_tilewidth
;
142 if (td
->td_planarconfig
== PLANARCONFIG_CONTIG
)
143 rowsize
*= td
->td_samplesperpixel
;
144 return ((tsize_t
) TIFFhowmany(rowsize
, 8));
148 * Compute the # bytes in a variable length, row-aligned tile.
151 TIFFVTileSize(TIFF
* tif
, uint32 nrows
)
153 TIFFDirectory
*td
= &tif
->tif_dir
;
156 if (td
->td_tilelength
== 0 || td
->td_tilewidth
== 0 ||
157 td
->td_tiledepth
== 0)
158 return ((tsize_t
) 0);
160 if (td
->td_planarconfig
== PLANARCONFIG_CONTIG
&&
161 td
->td_photometric
== PHOTOMETRIC_YCBCR
&&
164 * Packed YCbCr data contain one Cb+Cr for every
165 * HorizontalSampling*VerticalSampling Y values.
166 * Must also roundup width and height when calculating
167 * since images that are not a multiple of the
168 * horizontal/vertical subsampling area include
169 * YCbCr data for the extended image.
172 TIFFroundup(td
->td_tilewidth
, td
->td_ycbcrsubsampling
[0]);
173 tsize_t rowsize
= TIFFhowmany(w
*td
->td_bitspersample
, 8);
174 tsize_t samplingarea
=
175 td
->td_ycbcrsubsampling
[0]*td
->td_ycbcrsubsampling
[1];
176 nrows
= TIFFroundup(nrows
, td
->td_ycbcrsubsampling
[1]);
177 /* NB: don't need TIFFhowmany here 'cuz everything is rounded */
178 tilesize
= nrows
*rowsize
+ 2*(nrows
*rowsize
/ samplingarea
);
181 tilesize
= nrows
* TIFFTileRowSize(tif
);
182 return ((tsize_t
)(tilesize
* td
->td_tiledepth
));
186 * Compute the # bytes in a row-aligned tile.
189 TIFFTileSize(TIFF
* tif
)
191 return (TIFFVTileSize(tif
, tif
->tif_dir
.td_tilelength
));
195 * Compute a default tile size based on the image
196 * characteristics and a requested value. If a
197 * request is <1 then we choose a size according
198 * to certain heuristics.
201 TIFFDefaultTileSize(TIFF
* tif
, uint32
* tw
, uint32
* th
)
203 (*tif
->tif_deftilesize
)(tif
, tw
, th
);
207 _TIFFDefaultTileSize(TIFF
* tif
, uint32
* tw
, uint32
* th
)
210 if (*(int32
*) tw
< 1)
212 if (*(int32
*) th
< 1)
214 /* roundup to a multiple of 16 per the spec */
216 *tw
= TIFFroundup(*tw
, 16);
218 *th
= TIFFroundup(*th
, 16);