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