2012-08-28 09:29:40 +02:00
|
|
|
/* $Id: tif_tile.c,v 1.23 2012-06-06 05:33:55 fwarmerdam Exp $ */
|
2010-05-11 19:44:00 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 1991-1997 Sam Leffler
|
|
|
|
* Copyright (c) 1991-1997 Silicon Graphics, Inc.
|
|
|
|
*
|
2012-10-17 09:12:04 +02:00
|
|
|
* Permission to use, copy, modify, distribute, and sell this software and
|
2010-05-11 19:44:00 +02:00
|
|
|
* its documentation for any purpose is hereby granted without fee, provided
|
|
|
|
* that (i) the above copyright notices and this permission notice appear in
|
|
|
|
* all copies of the software and related documentation, and (ii) the names of
|
|
|
|
* Sam Leffler and Silicon Graphics may not be used in any advertising or
|
|
|
|
* publicity relating to the software without the specific, prior written
|
|
|
|
* permission of Sam Leffler and Silicon Graphics.
|
2012-10-17 09:12:04 +02:00
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
|
|
|
|
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*
|
2010-05-11 19:44:00 +02:00
|
|
|
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
|
|
|
|
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
|
2012-10-17 09:12:04 +02:00
|
|
|
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
|
|
|
|
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
|
2010-05-11 19:44:00 +02:00
|
|
|
* OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* TIFF Library.
|
|
|
|
*
|
|
|
|
* Tiled Image Support Routines.
|
|
|
|
*/
|
|
|
|
#include "tiffiop.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute which tile an (x,y,z,s) value is in.
|
|
|
|
*/
|
2012-02-29 16:01:28 +01:00
|
|
|
uint32
|
|
|
|
TIFFComputeTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
TIFFDirectory *td = &tif->tif_dir;
|
|
|
|
uint32 dx = td->td_tilewidth;
|
|
|
|
uint32 dy = td->td_tilelength;
|
|
|
|
uint32 dz = td->td_tiledepth;
|
|
|
|
uint32 tile = 1;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2012-10-17 09:12:04 +02:00
|
|
|
if (td->td_imagedepth == 1)
|
|
|
|
z = 0;
|
|
|
|
if (dx == (uint32) -1)
|
|
|
|
dx = td->td_imagewidth;
|
|
|
|
if (dy == (uint32) -1)
|
|
|
|
dy = td->td_imagelength;
|
|
|
|
if (dz == (uint32) -1)
|
|
|
|
dz = td->td_imagedepth;
|
|
|
|
if (dx != 0 && dy != 0 && dz != 0) {
|
|
|
|
uint32 xpt = TIFFhowmany_32(td->td_imagewidth, dx);
|
|
|
|
uint32 ypt = TIFFhowmany_32(td->td_imagelength, dy);
|
|
|
|
uint32 zpt = TIFFhowmany_32(td->td_imagedepth, dz);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2012-10-17 09:12:04 +02:00
|
|
|
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
|
|
|
|
tile = (xpt*ypt*zpt)*s +
|
|
|
|
(xpt*ypt)*(z/dz) +
|
|
|
|
xpt*(y/dy) +
|
|
|
|
x/dx;
|
|
|
|
else
|
|
|
|
tile = (xpt*ypt)*(z/dz) + xpt*(y/dy) + x/dx;
|
|
|
|
}
|
|
|
|
return (tile);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check an (x,y,z,s) coordinate
|
|
|
|
* against the image bounds.
|
|
|
|
*/
|
|
|
|
int
|
2012-02-29 16:01:28 +01:00
|
|
|
TIFFCheckTile(TIFF* tif, uint32 x, uint32 y, uint32 z, uint16 s)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
TIFFDirectory *td = &tif->tif_dir;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2012-10-17 09:12:04 +02:00
|
|
|
if (x >= td->td_imagewidth) {
|
|
|
|
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
|
|
|
|
"%lu: Col out of range, max %lu",
|
|
|
|
(unsigned long) x,
|
|
|
|
(unsigned long) (td->td_imagewidth - 1));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (y >= td->td_imagelength) {
|
|
|
|
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
|
|
|
|
"%lu: Row out of range, max %lu",
|
|
|
|
(unsigned long) y,
|
|
|
|
(unsigned long) (td->td_imagelength - 1));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (z >= td->td_imagedepth) {
|
|
|
|
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
|
|
|
|
"%lu: Depth out of range, max %lu",
|
|
|
|
(unsigned long) z,
|
|
|
|
(unsigned long) (td->td_imagedepth - 1));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
if (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
|
|
|
|
s >= td->td_samplesperpixel) {
|
|
|
|
TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
|
|
|
|
"%lu: Sample out of range, max %lu",
|
|
|
|
(unsigned long) s,
|
|
|
|
(unsigned long) (td->td_samplesperpixel - 1));
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
return (1);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute how many tiles are in an image.
|
|
|
|
*/
|
2012-02-29 16:01:28 +01:00
|
|
|
uint32
|
2010-05-11 19:44:00 +02:00
|
|
|
TIFFNumberOfTiles(TIFF* tif)
|
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
TIFFDirectory *td = &tif->tif_dir;
|
|
|
|
uint32 dx = td->td_tilewidth;
|
|
|
|
uint32 dy = td->td_tilelength;
|
|
|
|
uint32 dz = td->td_tiledepth;
|
|
|
|
uint32 ntiles;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2012-10-17 09:12:04 +02:00
|
|
|
if (dx == (uint32) -1)
|
|
|
|
dx = td->td_imagewidth;
|
|
|
|
if (dy == (uint32) -1)
|
|
|
|
dy = td->td_imagelength;
|
|
|
|
if (dz == (uint32) -1)
|
|
|
|
dz = td->td_imagedepth;
|
|
|
|
ntiles = (dx == 0 || dy == 0 || dz == 0) ? 0 :
|
|
|
|
_TIFFMultiply32(tif, _TIFFMultiply32(tif, TIFFhowmany_32(td->td_imagewidth, dx),
|
|
|
|
TIFFhowmany_32(td->td_imagelength, dy),
|
|
|
|
"TIFFNumberOfTiles"),
|
|
|
|
TIFFhowmany_32(td->td_imagedepth, dz), "TIFFNumberOfTiles");
|
|
|
|
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
|
|
|
|
ntiles = _TIFFMultiply32(tif, ntiles, td->td_samplesperpixel,
|
|
|
|
"TIFFNumberOfTiles");
|
|
|
|
return (ntiles);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the # bytes in each row of a tile.
|
|
|
|
*/
|
2012-02-29 16:01:28 +01:00
|
|
|
uint64
|
|
|
|
TIFFTileRowSize64(TIFF* tif)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
TIFFDirectory *td = &tif->tif_dir;
|
|
|
|
uint64 rowsize;
|
2012-02-29 16:01:28 +01:00
|
|
|
|
2012-10-17 09:12:04 +02:00
|
|
|
if (td->td_tilelength == 0 || td->td_tilewidth == 0)
|
|
|
|
return (0);
|
|
|
|
rowsize = _TIFFMultiply64(tif, td->td_bitspersample, td->td_tilewidth,
|
|
|
|
"TIFFTileRowSize");
|
|
|
|
if (td->td_planarconfig == PLANARCONFIG_CONTIG)
|
|
|
|
rowsize = _TIFFMultiply64(tif, rowsize, td->td_samplesperpixel,
|
|
|
|
"TIFFTileRowSize");
|
|
|
|
return (TIFFhowmany8_64(rowsize));
|
2012-02-29 16:01:28 +01:00
|
|
|
}
|
|
|
|
tmsize_t
|
|
|
|
TIFFTileRowSize(TIFF* tif)
|
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
static const char module[] = "TIFFTileRowSize";
|
|
|
|
uint64 m;
|
|
|
|
tmsize_t n;
|
|
|
|
m=TIFFTileRowSize64(tif);
|
|
|
|
n=(tmsize_t)m;
|
|
|
|
if ((uint64)n!=m)
|
|
|
|
{
|
|
|
|
TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
|
|
|
|
n=0;
|
|
|
|
}
|
|
|
|
return(n);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the # bytes in a variable length, row-aligned tile.
|
|
|
|
*/
|
2012-02-29 16:01:28 +01:00
|
|
|
uint64
|
|
|
|
TIFFVTileSize64(TIFF* tif, uint32 nrows)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
static const char module[] = "TIFFVTileSize64";
|
|
|
|
TIFFDirectory *td = &tif->tif_dir;
|
|
|
|
if (td->td_tilelength == 0 || td->td_tilewidth == 0 ||
|
|
|
|
td->td_tiledepth == 0)
|
|
|
|
return (0);
|
|
|
|
if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
|
|
|
|
(td->td_photometric==PHOTOMETRIC_YCBCR)&&
|
|
|
|
(td->td_samplesperpixel==3)&&
|
|
|
|
(!isUpSampled(tif)))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Packed YCbCr data contain one Cb+Cr for every
|
|
|
|
* HorizontalSampling*VerticalSampling Y values.
|
|
|
|
* Must also roundup width and height when calculating
|
|
|
|
* since images that are not a multiple of the
|
|
|
|
* horizontal/vertical subsampling area include
|
|
|
|
* YCbCr data for the extended image.
|
|
|
|
*/
|
|
|
|
uint16 ycbcrsubsampling[2];
|
|
|
|
uint16 samplingblock_samples;
|
|
|
|
uint32 samplingblocks_hor;
|
|
|
|
uint32 samplingblocks_ver;
|
|
|
|
uint64 samplingrow_samples;
|
|
|
|
uint64 samplingrow_size;
|
|
|
|
TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
|
|
|
|
ycbcrsubsampling+1);
|
|
|
|
if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
|
|
|
|
||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
|
|
|
|
{
|
|
|
|
TIFFErrorExt(tif->tif_clientdata,module,
|
|
|
|
"Invalid YCbCr subsampling (%dx%d)",
|
|
|
|
ycbcrsubsampling[0],
|
|
|
|
ycbcrsubsampling[1] );
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
|
|
|
|
samplingblocks_hor=TIFFhowmany_32(td->td_tilewidth,ycbcrsubsampling[0]);
|
|
|
|
samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
|
|
|
|
samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
|
|
|
|
samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
|
|
|
|
return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return(_TIFFMultiply64(tif,nrows,TIFFTileRowSize64(tif),module));
|
2012-02-29 16:01:28 +01:00
|
|
|
}
|
|
|
|
tmsize_t
|
|
|
|
TIFFVTileSize(TIFF* tif, uint32 nrows)
|
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
static const char module[] = "TIFFVTileSize";
|
|
|
|
uint64 m;
|
|
|
|
tmsize_t n;
|
|
|
|
m=TIFFVTileSize64(tif,nrows);
|
|
|
|
n=(tmsize_t)m;
|
|
|
|
if ((uint64)n!=m)
|
|
|
|
{
|
|
|
|
TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
|
|
|
|
n=0;
|
|
|
|
}
|
|
|
|
return(n);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute the # bytes in a row-aligned tile.
|
|
|
|
*/
|
2012-02-29 16:01:28 +01:00
|
|
|
uint64
|
|
|
|
TIFFTileSize64(TIFF* tif)
|
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
return (TIFFVTileSize64(tif, tif->tif_dir.td_tilelength));
|
2012-02-29 16:01:28 +01:00
|
|
|
}
|
|
|
|
tmsize_t
|
2010-05-11 19:44:00 +02:00
|
|
|
TIFFTileSize(TIFF* tif)
|
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
static const char module[] = "TIFFTileSize";
|
|
|
|
uint64 m;
|
|
|
|
tmsize_t n;
|
|
|
|
m=TIFFTileSize64(tif);
|
|
|
|
n=(tmsize_t)m;
|
|
|
|
if ((uint64)n!=m)
|
|
|
|
{
|
|
|
|
TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
|
|
|
|
n=0;
|
|
|
|
}
|
|
|
|
return(n);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute a default tile size based on the image
|
|
|
|
* characteristics and a requested value. If a
|
|
|
|
* request is <1 then we choose a size according
|
|
|
|
* to certain heuristics.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
|
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
(*tif->tif_deftilesize)(tif, tw, th);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_TIFFDefaultTileSize(TIFF* tif, uint32* tw, uint32* th)
|
|
|
|
{
|
2012-10-17 09:12:04 +02:00
|
|
|
(void) tif;
|
|
|
|
if (*(int32*) tw < 1)
|
|
|
|
*tw = 256;
|
|
|
|
if (*(int32*) th < 1)
|
|
|
|
*th = 256;
|
|
|
|
/* roundup to a multiple of 16 per the spec */
|
|
|
|
if (*tw & 0xf)
|
|
|
|
*tw = TIFFroundup_32(*tw, 16);
|
|
|
|
if (*th & 0xf)
|
|
|
|
*th = TIFFroundup_32(*th, 16);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=8 sts=8 sw=8 noet: */
|
2010-07-16 14:54:53 +02:00
|
|
|
/*
|
|
|
|
* Local Variables:
|
|
|
|
* mode: c
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* fill-column: 78
|
|
|
|
* End:
|
|
|
|
*/
|