Blame libtiff/tif_tile.c

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