roentgen b75cab
/* $Id: tif_strip.c,v 1.35 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
 * Strip-organized Image Support Routines.
roentgen b75cab
 */
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Compute which strip a (row,sample) value is in.
roentgen b75cab
 */
roentgen b75cab
uint32
roentgen b75cab
TIFFComputeStrip(TIFF* tif, uint32 row, uint16 sample)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFComputeStrip";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint32 strip;
roentgen b75cab
roentgen b75cab
	strip = row / td->td_rowsperstrip;
roentgen b75cab
	if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
roentgen b75cab
		if (sample >= td->td_samplesperpixel) {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "%lu: Sample out of range, max %lu",
roentgen b75cab
			    (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
roentgen b75cab
			return (0);
roentgen b75cab
		}
roentgen b75cab
		strip += (uint32)sample*td->td_stripsperimage;
roentgen b75cab
	}
roentgen b75cab
	return (strip);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Compute how many strips are in an image.
roentgen b75cab
 */
roentgen b75cab
uint32
roentgen b75cab
TIFFNumberOfStrips(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint32 nstrips;
roentgen b75cab
roentgen b75cab
	nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
roentgen b75cab
	     TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
roentgen b75cab
	if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
roentgen b75cab
		nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
roentgen b75cab
		    "TIFFNumberOfStrips");
roentgen b75cab
	return (nstrips);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Compute the # bytes in a variable height, row-aligned strip.
roentgen b75cab
 */
roentgen b75cab
uint64
roentgen b75cab
TIFFVStripSize64(TIFF* tif, uint32 nrows)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFVStripSize64";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	if (nrows==(uint32)(-1))
roentgen b75cab
		nrows=td->td_imagelength;
roentgen b75cab
	if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
roentgen b75cab
	    (td->td_photometric == PHOTOMETRIC_YCBCR)&&
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
		if(td->td_samplesperpixel!=3)
roentgen b75cab
		{
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata,module,
roentgen b75cab
			    "Invalid td_samplesperpixel value");
roentgen b75cab
			return 0;
roentgen b75cab
		}
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_imagewidth,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,TIFFScanlineSize64(tif),module));
roentgen b75cab
}
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFVStripSize(TIFF* tif, uint32 nrows)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFVStripSize";
roentgen b75cab
	uint64 m;
roentgen b75cab
	tmsize_t n;
roentgen b75cab
	m=TIFFVStripSize64(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 raw strip.
roentgen b75cab
 */
roentgen b75cab
uint64
roentgen b75cab
TIFFRawStripSize64(TIFF* tif, uint32 strip)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFRawStripSize64";
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
	uint64 bytecount = td->td_stripbytecount[strip];
roentgen b75cab
roentgen b75cab
	if (bytecount == 0)
roentgen b75cab
	{
roentgen b75cab
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			     "%I64u: Invalid strip byte count, strip %lu",
roentgen b75cab
			     (unsigned __int64) bytecount,
roentgen b75cab
			     (unsigned long) strip);
roentgen b75cab
#else
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			     "%llu: Invalid strip byte count, strip %lu",
roentgen b75cab
			     (unsigned long long) bytecount,
roentgen b75cab
			     (unsigned long) strip);
roentgen b75cab
#endif
roentgen b75cab
		bytecount = (uint64) -1;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	return bytecount;
roentgen b75cab
}
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFRawStripSize(TIFF* tif, uint32 strip)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFRawStripSize";
roentgen b75cab
	uint64 m;
roentgen b75cab
	tmsize_t n;
roentgen b75cab
	m=TIFFRawStripSize64(tif,strip);
roentgen b75cab
	if (m==(uint64)(-1))
roentgen b75cab
		n=(tmsize_t)(-1);
roentgen b75cab
	else
roentgen b75cab
	{
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
	}
roentgen b75cab
	return(n);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Compute the # bytes in a (row-aligned) strip.
roentgen b75cab
 *
roentgen b75cab
 * Note that if RowsPerStrip is larger than the
roentgen b75cab
 * recorded ImageLength, then the strip size is
roentgen b75cab
 * truncated to reflect the actual space required
roentgen b75cab
 * to hold the strip.
roentgen b75cab
 */
roentgen b75cab
uint64
roentgen b75cab
TIFFStripSize64(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFDirectory* td = &tif->tif_dir;
roentgen b75cab
	uint32 rps = td->td_rowsperstrip;
roentgen b75cab
	if (rps > td->td_imagelength)
roentgen b75cab
		rps = td->td_imagelength;
roentgen b75cab
	return (TIFFVStripSize64(tif, rps));
roentgen b75cab
}
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFStripSize(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFStripSize";
roentgen b75cab
	uint64 m;
roentgen b75cab
	tmsize_t n;
roentgen b75cab
	m=TIFFStripSize64(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 strip size based on the image
roentgen b75cab
 * characteristics and a requested value.  If the
roentgen b75cab
 * request is <1 then we choose a strip size according
roentgen b75cab
 * to certain heuristics.
roentgen b75cab
 */
roentgen b75cab
uint32
roentgen b75cab
TIFFDefaultStripSize(TIFF* tif, uint32 request)
roentgen b75cab
{
roentgen b75cab
	return (*tif->tif_defstripsize)(tif, request);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
uint32
roentgen b75cab
_TIFFDefaultStripSize(TIFF* tif, uint32 s)
roentgen b75cab
{
roentgen b75cab
	if ((int32) s < 1) {
roentgen b75cab
		/*
roentgen b75cab
		 * If RowsPerStrip is unspecified, try to break the
roentgen b75cab
		 * image up into strips that are approximately
roentgen b75cab
		 * STRIP_SIZE_DEFAULT bytes long.
roentgen b75cab
		 */
roentgen b75cab
		uint64 scanlinesize;
roentgen b75cab
		uint64 rows;
roentgen b75cab
		scanlinesize=TIFFScanlineSize64(tif);
roentgen b75cab
		if (scanlinesize==0)
roentgen b75cab
			scanlinesize=1;
roentgen b75cab
		rows=(uint64)STRIP_SIZE_DEFAULT/scanlinesize;
roentgen b75cab
		if (rows==0)
roentgen b75cab
			rows=1;
roentgen b75cab
		else if (rows>0xFFFFFFFF)
roentgen b75cab
			rows=0xFFFFFFFF;
roentgen b75cab
		s=(uint32)rows;
roentgen b75cab
	}
roentgen b75cab
	return (s);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Return the number of bytes to read/write in a call to
roentgen b75cab
 * one of the scanline-oriented i/o routines.  Note that
roentgen b75cab
 * this number may be 1/samples-per-pixel if data is
roentgen b75cab
 * stored as separate planes.
roentgen b75cab
 * The ScanlineSize in case of YCbCrSubsampling is defined as the
roentgen b75cab
 * strip size divided by the strip height, i.e. the size of a pack of vertical
roentgen b75cab
 * subsampling lines divided by vertical subsampling. It should thus make
roentgen b75cab
 * sense when multiplied by a multiple of vertical subsampling.
roentgen b75cab
 */
roentgen b75cab
uint64
roentgen b75cab
TIFFScanlineSize64(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFScanlineSize64";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint64 scanline_size;
roentgen b75cab
	if (td->td_planarconfig==PLANARCONFIG_CONTIG)
roentgen b75cab
	{
roentgen b75cab
		if ((td->td_photometric==PHOTOMETRIC_YCBCR)&&
roentgen b75cab
		    (td->td_samplesperpixel==3)&&
roentgen b75cab
		    (!isUpSampled(tif)))
roentgen b75cab
		{
roentgen b75cab
			uint16 ycbcrsubsampling[2];
roentgen b75cab
			uint16 samplingblock_samples;
roentgen b75cab
			uint32 samplingblocks_hor;
roentgen b75cab
			uint64 samplingrow_samples;
roentgen b75cab
			uint64 samplingrow_size;
roentgen b75cab
			if(td->td_samplesperpixel!=3)
roentgen b75cab
			{
roentgen b75cab
                            TIFFErrorExt(tif->tif_clientdata,module,
roentgen b75cab
                                         "Invalid td_samplesperpixel value");
roentgen b75cab
                            return 0;
roentgen b75cab
			}
roentgen b75cab
			TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,
roentgen b75cab
                                              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");
roentgen b75cab
                            return 0;
roentgen b75cab
			}
roentgen b75cab
			samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
roentgen b75cab
			samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
roentgen b75cab
			samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
roentgen b75cab
			samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
roentgen b75cab
			scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
roentgen b75cab
		}
roentgen b75cab
		else
roentgen b75cab
		{
roentgen b75cab
			uint64 scanline_samples;
roentgen b75cab
			scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
roentgen b75cab
			scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
		scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,td->td_imagewidth,td->td_bitspersample,module),8);
roentgen b75cab
	return(scanline_size);
roentgen b75cab
}
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFScanlineSize(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFScanlineSize";
roentgen b75cab
	uint64 m;
roentgen b75cab
	tmsize_t n;
roentgen b75cab
	m=TIFFScanlineSize64(tif);
roentgen b75cab
	n=(tmsize_t)m;
roentgen b75cab
	if ((uint64)n!=m)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
roentgen b75cab
		n=0;
roentgen b75cab
	}
roentgen b75cab
	return(n);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Return the number of bytes required to store a complete
roentgen b75cab
 * decoded and packed raster scanline (as opposed to the
roentgen b75cab
 * I/O size returned by TIFFScanlineSize which may be less
roentgen b75cab
 * if data is store as separate planes).
roentgen b75cab
 */
roentgen b75cab
uint64
roentgen b75cab
TIFFRasterScanlineSize64(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFRasterScanlineSize64";
roentgen b75cab
	TIFFDirectory *td = &tif->tif_dir;
roentgen b75cab
	uint64 scanline;
roentgen b75cab
roentgen b75cab
	scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
roentgen b75cab
	if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
roentgen b75cab
		scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
roentgen b75cab
		return (TIFFhowmany8_64(scanline));
roentgen b75cab
	} else
roentgen b75cab
		return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
roentgen b75cab
		    td->td_samplesperpixel, module));
roentgen b75cab
}
roentgen b75cab
tmsize_t
roentgen b75cab
TIFFRasterScanlineSize(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFRasterScanlineSize";
roentgen b75cab
	uint64 m;
roentgen b75cab
	tmsize_t n;
roentgen b75cab
	m=TIFFRasterScanlineSize64(tif);
roentgen b75cab
	n=(tmsize_t)m;
roentgen b75cab
	if ((uint64)n!=m)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
roentgen b75cab
		n=0;
roentgen b75cab
	}
roentgen b75cab
	return(n);
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
 */