]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tif_tile.c
   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 summarize(TIFF
* tif
, size_t summand1
, size_t summand2
, const char* where
) 
  37         uint32  bytes 
= summand1 
+ summand2
; 
  39         if (bytes 
- summand1 
!= summand2
) { 
  40                 TIFFError(tif
->tif_name
, "Integer overflow in %s", where
); 
  48 multiply(TIFF
* tif
, size_t nmemb
, size_t elem_size
, const char* where
) 
  50         uint32  bytes 
= nmemb 
* elem_size
; 
  52         if (elem_size 
&& bytes 
/ elem_size 
!= nmemb
) { 
  53                 TIFFError(tif
->tif_name
, "Integer overflow in %s", where
); 
  61  * Compute which tile an (x,y,z,s) value is in. 
  64 TIFFComputeTile(TIFF
* tif
, uint32 x
, uint32 y
, uint32 z
, tsample_t s
) 
  66         TIFFDirectory 
*td 
= &tif
->tif_dir
; 
  67         uint32 dx 
= td
->td_tilewidth
; 
  68         uint32 dy 
= td
->td_tilelength
; 
  69         uint32 dz 
= td
->td_tiledepth
; 
  72         if (td
->td_imagedepth 
== 1) 
  74         if (dx 
== (uint32
) -1) 
  75                 dx 
= td
->td_imagewidth
; 
  76         if (dy 
== (uint32
) -1) 
  77                 dy 
= td
->td_imagelength
; 
  78         if (dz 
== (uint32
) -1) 
  79                 dz 
= td
->td_imagedepth
; 
  80         if (dx 
!= 0 && dy 
!= 0 && dz 
!= 0) { 
  81                 uint32 xpt 
= TIFFhowmany(td
->td_imagewidth
, dx
);  
  82                 uint32 ypt 
= TIFFhowmany(td
->td_imagelength
, dy
);  
  83                 uint32 zpt 
= TIFFhowmany(td
->td_imagedepth
, dz
);  
  85                 if (td
->td_planarconfig 
== PLANARCONFIG_SEPARATE
)  
  86                         tile 
= (xpt
*ypt
*zpt
)*s 
+ 
  91                         tile 
= (xpt
*ypt
)*(z
/dz
) + xpt
*(y
/dy
) + x
/dx
; 
  97  * Check an (x,y,z,s) coordinate 
  98  * against the image bounds. 
 101 TIFFCheckTile(TIFF
* tif
, uint32 x
, uint32 y
, uint32 z
, tsample_t s
) 
 103         TIFFDirectory 
*td 
= &tif
->tif_dir
; 
 105         if (x 
>= td
->td_imagewidth
) { 
 106                 TIFFError(tif
->tif_name
, "Col %ld out of range, max %lu", 
 107                     (long) x
, (u_long
) td
->td_imagewidth
); 
 110         if (y 
>= td
->td_imagelength
) { 
 111                 TIFFError(tif
->tif_name
, "Row %ld out of range, max %lu", 
 112                     (long) y
, (u_long
) td
->td_imagelength
); 
 115         if (z 
>= td
->td_imagedepth
) { 
 116                 TIFFError(tif
->tif_name
, "Depth %ld out of range, max %lu", 
 117                     (long) z
, (u_long
) td
->td_imagedepth
); 
 120         if (td
->td_planarconfig 
== PLANARCONFIG_SEPARATE 
&& 
 121             s 
>= td
->td_samplesperpixel
) { 
 122                 TIFFError(tif
->tif_name
, "Sample %d out of range, max %u", 
 123                     (int) s
, td
->td_samplesperpixel
); 
 130  * Compute how many tiles are in an image. 
 133 TIFFNumberOfTiles(TIFF
* tif
) 
 135         TIFFDirectory 
*td 
= &tif
->tif_dir
; 
 136         uint32 dx 
= td
->td_tilewidth
; 
 137         uint32 dy 
= td
->td_tilelength
; 
 138         uint32 dz 
= td
->td_tiledepth
; 
 141         if (dx 
== (uint32
) -1) 
 142                 dx 
= td
->td_imagewidth
; 
 143         if (dy 
== (uint32
) -1) 
 144                 dy 
= td
->td_imagelength
; 
 145         if (dz 
== (uint32
) -1) 
 146                 dz 
= td
->td_imagedepth
; 
 147         ntiles 
= (dx 
== 0 || dy 
== 0 || dz 
== 0) ? 0 : 
 148             multiply(tif
, multiply(tif
, TIFFhowmany(td
->td_imagewidth
, dx
), 
 149                                    TIFFhowmany(td
->td_imagelength
, dy
), 
 150                                    "TIFFNumberOfTiles"), 
 151                      TIFFhowmany(td
->td_imagedepth
, dz
), "TIFFNumberOfTiles"); 
 152         if (td
->td_planarconfig 
== PLANARCONFIG_SEPARATE
) 
 153                 ntiles 
= multiply(tif
, ntiles
, td
->td_samplesperpixel
, 
 154                                   "TIFFNumberOfTiles"); 
 159  * Compute the # bytes in each row of a tile. 
 162 TIFFTileRowSize(TIFF
* tif
) 
 164         TIFFDirectory 
*td 
= &tif
->tif_dir
; 
 167         if (td
->td_tilelength 
== 0 || td
->td_tilewidth 
== 0) 
 168                 return ((tsize_t
) 0); 
 169         rowsize 
= multiply(tif
, td
->td_bitspersample
, td
->td_tilewidth
, 
 171         if (td
->td_planarconfig 
== PLANARCONFIG_CONTIG
) 
 172                 rowsize 
= multiply(tif
, rowsize
, td
->td_samplesperpixel
, 
 174         return ((tsize_t
) TIFFhowmany8(rowsize
)); 
 178  * Compute the # bytes in a variable length, row-aligned tile. 
 181 TIFFVTileSize(TIFF
* tif
, uint32 nrows
) 
 183         TIFFDirectory 
*td 
= &tif
->tif_dir
; 
 186         if (td
->td_tilelength 
== 0 || td
->td_tilewidth 
== 0 || 
 187             td
->td_tiledepth 
== 0) 
 188                 return ((tsize_t
) 0); 
 190         if (td
->td_planarconfig 
== PLANARCONFIG_CONTIG 
&& 
 191             td
->td_photometric 
== PHOTOMETRIC_YCBCR 
&& 
 194                  * Packed YCbCr data contain one Cb+Cr for every 
 195                  * HorizontalSampling*VerticalSampling Y values. 
 196                  * Must also roundup width and height when calculating 
 197                  * since images that are not a multiple of the 
 198                  * horizontal/vertical subsampling area include 
 199                  * YCbCr data for the extended image. 
 202                     TIFFroundup(td
->td_tilewidth
, td
->td_ycbcrsubsampling
[0]); 
 204                     TIFFhowmany8(multiply(tif
, w
, td
->td_bitspersample
, 
 206                 tsize_t samplingarea 
= 
 207                     td
->td_ycbcrsubsampling
[0]*td
->td_ycbcrsubsampling
[1]; 
 208                 nrows 
= TIFFroundup(nrows
, td
->td_ycbcrsubsampling
[1]); 
 209                 /* NB: don't need TIFFhowmany here 'cuz everything is rounded */ 
 210                 tilesize 
= multiply(tif
, nrows
, rowsize
, "TIFFVTileSize"); 
 211                 tilesize 
= summarize(tif
, tilesize
, 
 212                                      multiply(tif
, 2, tilesize 
/ samplingarea
, 
 217                 tilesize 
= multiply(tif
, nrows
, TIFFTileRowSize(tif
), 
 220             multiply(tif
, tilesize
, td
->td_tiledepth
, "TIFFVTileSize")); 
 224  * Compute the # bytes in a row-aligned tile. 
 227 TIFFTileSize(TIFF
* tif
) 
 229         return (TIFFVTileSize(tif
, tif
->tif_dir
.td_tilelength
)); 
 233  * Compute a default tile size based on the image 
 234  * characteristics and a requested value.  If a 
 235  * request is <1 then we choose a size according 
 236  * to certain heuristics. 
 239 TIFFDefaultTileSize(TIFF
* tif
, uint32
* tw
, uint32
* th
) 
 241         (*tif
->tif_deftilesize
)(tif
, tw
, th
); 
 245 _TIFFDefaultTileSize(TIFF
* tif
, uint32
* tw
, uint32
* th
) 
 248         if (*(int32
*) tw 
< 1) 
 250         if (*(int32
*) th 
< 1) 
 252         /* roundup to a multiple of 16 per the spec */ 
 254                 *tw 
= TIFFroundup(*tw
, 16); 
 256                 *th 
= TIFFroundup(*th
, 16);