Blame thirdparty/libtiff/tif_strip.c

Packit caffb5
/* $Id: tif_strip.c,v 1.36 2015-06-07 22:35:40 bfriesen Exp $ */
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * Copyright (c) 1991-1997 Sam Leffler
Packit caffb5
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Packit caffb5
 *
Packit caffb5
 * Permission to use, copy, modify, distribute, and sell this software and 
Packit caffb5
 * its documentation for any purpose is hereby granted without fee, provided
Packit caffb5
 * that (i) the above copyright notices and this permission notice appear in
Packit caffb5
 * all copies of the software and related documentation, and (ii) the names of
Packit caffb5
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Packit caffb5
 * publicity relating to the software without the specific, prior written
Packit caffb5
 * permission of Sam Leffler and Silicon Graphics.
Packit caffb5
 * 
Packit caffb5
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
Packit caffb5
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
Packit caffb5
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
Packit caffb5
 * 
Packit caffb5
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Packit caffb5
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Packit caffb5
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit caffb5
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
Packit caffb5
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
Packit caffb5
 * OF THIS SOFTWARE.
Packit caffb5
 */
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * TIFF Library.
Packit caffb5
 *
Packit caffb5
 * Strip-organized Image Support Routines.
Packit caffb5
 */
Packit caffb5
#include "tiffiop.h"
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * Compute which strip a (row,sample) value is in.
Packit caffb5
 */
Packit caffb5
uint32
Packit caffb5
TIFFComputeStrip(TIFF* tif, uint32 row, uint16 sample)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFComputeStrip";
Packit caffb5
	TIFFDirectory *td = &tif->tif_dir;
Packit caffb5
	uint32 strip;
Packit caffb5
Packit caffb5
	strip = row / td->td_rowsperstrip;
Packit caffb5
	if (td->td_planarconfig == PLANARCONFIG_SEPARATE) {
Packit caffb5
		if (sample >= td->td_samplesperpixel) {
Packit caffb5
			TIFFErrorExt(tif->tif_clientdata, module,
Packit caffb5
			    "%lu: Sample out of range, max %lu",
Packit caffb5
			    (unsigned long) sample, (unsigned long) td->td_samplesperpixel);
Packit caffb5
			return (0);
Packit caffb5
		}
Packit caffb5
		strip += (uint32)sample*td->td_stripsperimage;
Packit caffb5
	}
Packit caffb5
	return (strip);
Packit caffb5
}
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * Compute how many strips are in an image.
Packit caffb5
 */
Packit caffb5
uint32
Packit caffb5
TIFFNumberOfStrips(TIFF* tif)
Packit caffb5
{
Packit caffb5
	TIFFDirectory *td = &tif->tif_dir;
Packit caffb5
	uint32 nstrips;
Packit caffb5
Packit caffb5
	nstrips = (td->td_rowsperstrip == (uint32) -1 ? 1 :
Packit caffb5
	     TIFFhowmany_32(td->td_imagelength, td->td_rowsperstrip));
Packit caffb5
	if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
Packit caffb5
		nstrips = _TIFFMultiply32(tif, nstrips, (uint32)td->td_samplesperpixel,
Packit caffb5
		    "TIFFNumberOfStrips");
Packit caffb5
	return (nstrips);
Packit caffb5
}
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * Compute the # bytes in a variable height, row-aligned strip.
Packit caffb5
 */
Packit caffb5
uint64
Packit caffb5
TIFFVStripSize64(TIFF* tif, uint32 nrows)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFVStripSize64";
Packit caffb5
	TIFFDirectory *td = &tif->tif_dir;
Packit caffb5
	if (nrows==(uint32)(-1))
Packit caffb5
		nrows=td->td_imagelength;
Packit caffb5
	if ((td->td_planarconfig==PLANARCONFIG_CONTIG)&&
Packit caffb5
	    (td->td_photometric == PHOTOMETRIC_YCBCR)&&
Packit caffb5
	    (!isUpSampled(tif)))
Packit caffb5
	{
Packit caffb5
		/*
Packit caffb5
		 * Packed YCbCr data contain one Cb+Cr for every
Packit caffb5
		 * HorizontalSampling*VerticalSampling Y values.
Packit caffb5
		 * Must also roundup width and height when calculating
Packit caffb5
		 * since images that are not a multiple of the
Packit caffb5
		 * horizontal/vertical subsampling area include
Packit caffb5
		 * YCbCr data for the extended image.
Packit caffb5
		 */
Packit caffb5
		uint16 ycbcrsubsampling[2];
Packit caffb5
		uint16 samplingblock_samples;
Packit caffb5
		uint32 samplingblocks_hor;
Packit caffb5
		uint32 samplingblocks_ver;
Packit caffb5
		uint64 samplingrow_samples;
Packit caffb5
		uint64 samplingrow_size;
Packit caffb5
		if(td->td_samplesperpixel!=3)
Packit caffb5
		{
Packit caffb5
			TIFFErrorExt(tif->tif_clientdata,module,
Packit caffb5
			    "Invalid td_samplesperpixel value");
Packit caffb5
			return 0;
Packit caffb5
		}
Packit caffb5
		TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,ycbcrsubsampling+0,
Packit caffb5
		    ycbcrsubsampling+1);
Packit caffb5
		if ((ycbcrsubsampling[0] != 1 && ycbcrsubsampling[0] != 2 && ycbcrsubsampling[0] != 4)
Packit caffb5
		    ||(ycbcrsubsampling[1] != 1 && ycbcrsubsampling[1] != 2 && ycbcrsubsampling[1] != 4))
Packit caffb5
		{
Packit caffb5
			TIFFErrorExt(tif->tif_clientdata,module,
Packit caffb5
				     "Invalid YCbCr subsampling (%dx%d)", 
Packit caffb5
				     ycbcrsubsampling[0], 
Packit caffb5
				     ycbcrsubsampling[1] );
Packit caffb5
			return 0;
Packit caffb5
		}
Packit caffb5
		samplingblock_samples=ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
Packit caffb5
		samplingblocks_hor=TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
Packit caffb5
		samplingblocks_ver=TIFFhowmany_32(nrows,ycbcrsubsampling[1]);
Packit caffb5
		samplingrow_samples=_TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
Packit caffb5
		samplingrow_size=TIFFhowmany8_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module));
Packit caffb5
		return(_TIFFMultiply64(tif,samplingrow_size,samplingblocks_ver,module));
Packit caffb5
	}
Packit caffb5
	else
Packit caffb5
		return(_TIFFMultiply64(tif,nrows,TIFFScanlineSize64(tif),module));
Packit caffb5
}
Packit caffb5
tmsize_t
Packit caffb5
TIFFVStripSize(TIFF* tif, uint32 nrows)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFVStripSize";
Packit caffb5
	uint64 m;
Packit caffb5
	tmsize_t n;
Packit caffb5
	m=TIFFVStripSize64(tif,nrows);
Packit caffb5
	n=(tmsize_t)m;
Packit caffb5
	if ((uint64)n!=m)
Packit caffb5
	{
Packit caffb5
		TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
Packit caffb5
		n=0;
Packit caffb5
	}
Packit caffb5
	return(n);
Packit caffb5
}
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * Compute the # bytes in a raw strip.
Packit caffb5
 */
Packit caffb5
uint64
Packit caffb5
TIFFRawStripSize64(TIFF* tif, uint32 strip)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFRawStripSize64";
Packit caffb5
	TIFFDirectory* td = &tif->tif_dir;
Packit caffb5
	uint64 bytecount = td->td_stripbytecount[strip];
Packit caffb5
Packit caffb5
	if (bytecount == 0)
Packit caffb5
	{
Packit caffb5
#if defined(__WIN32__) && (defined(_MSC_VER) || defined(__MINGW32__))
Packit caffb5
		TIFFErrorExt(tif->tif_clientdata, module,
Packit caffb5
			     "%I64u: Invalid strip byte count, strip %lu",
Packit caffb5
			     (unsigned __int64) bytecount,
Packit caffb5
			     (unsigned long) strip);
Packit caffb5
#else
Packit caffb5
		TIFFErrorExt(tif->tif_clientdata, module,
Packit caffb5
			     "%llu: Invalid strip byte count, strip %lu",
Packit caffb5
			     (unsigned long long) bytecount,
Packit caffb5
			     (unsigned long) strip);
Packit caffb5
#endif
Packit caffb5
		bytecount = (uint64) -1;
Packit caffb5
	}
Packit caffb5
Packit caffb5
	return bytecount;
Packit caffb5
}
Packit caffb5
tmsize_t
Packit caffb5
TIFFRawStripSize(TIFF* tif, uint32 strip)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFRawStripSize";
Packit caffb5
	uint64 m;
Packit caffb5
	tmsize_t n;
Packit caffb5
	m=TIFFRawStripSize64(tif,strip);
Packit caffb5
	if (m==(uint64)(-1))
Packit caffb5
		n=(tmsize_t)(-1);
Packit caffb5
	else
Packit caffb5
	{
Packit caffb5
		n=(tmsize_t)m;
Packit caffb5
		if ((uint64)n!=m)
Packit caffb5
		{
Packit caffb5
			TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
Packit caffb5
			n=0;
Packit caffb5
		}
Packit caffb5
	}
Packit caffb5
	return(n);
Packit caffb5
}
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * Compute the # bytes in a (row-aligned) strip.
Packit caffb5
 *
Packit caffb5
 * Note that if RowsPerStrip is larger than the
Packit caffb5
 * recorded ImageLength, then the strip size is
Packit caffb5
 * truncated to reflect the actual space required
Packit caffb5
 * to hold the strip.
Packit caffb5
 */
Packit caffb5
uint64
Packit caffb5
TIFFStripSize64(TIFF* tif)
Packit caffb5
{
Packit caffb5
	TIFFDirectory* td = &tif->tif_dir;
Packit caffb5
	uint32 rps = td->td_rowsperstrip;
Packit caffb5
	if (rps > td->td_imagelength)
Packit caffb5
		rps = td->td_imagelength;
Packit caffb5
	return (TIFFVStripSize64(tif, rps));
Packit caffb5
}
Packit caffb5
tmsize_t
Packit caffb5
TIFFStripSize(TIFF* tif)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFStripSize";
Packit caffb5
	uint64 m;
Packit caffb5
	tmsize_t n;
Packit caffb5
	m=TIFFStripSize64(tif);
Packit caffb5
	n=(tmsize_t)m;
Packit caffb5
	if ((uint64)n!=m)
Packit caffb5
	{
Packit caffb5
		TIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");
Packit caffb5
		n=0;
Packit caffb5
	}
Packit caffb5
	return(n);
Packit caffb5
}
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * Compute a default strip size based on the image
Packit caffb5
 * characteristics and a requested value.  If the
Packit caffb5
 * request is <1 then we choose a strip size according
Packit caffb5
 * to certain heuristics.
Packit caffb5
 */
Packit caffb5
uint32
Packit caffb5
TIFFDefaultStripSize(TIFF* tif, uint32 request)
Packit caffb5
{
Packit caffb5
	return (*tif->tif_defstripsize)(tif, request);
Packit caffb5
}
Packit caffb5
Packit caffb5
uint32
Packit caffb5
_TIFFDefaultStripSize(TIFF* tif, uint32 s)
Packit caffb5
{
Packit caffb5
	if ((int32) s < 1) {
Packit caffb5
		/*
Packit caffb5
		 * If RowsPerStrip is unspecified, try to break the
Packit caffb5
		 * image up into strips that are approximately
Packit caffb5
		 * STRIP_SIZE_DEFAULT bytes long.
Packit caffb5
		 */
Packit caffb5
		uint64 scanlinesize;
Packit caffb5
		uint64 rows;
Packit caffb5
		scanlinesize=TIFFScanlineSize64(tif);
Packit caffb5
		if (scanlinesize==0)
Packit caffb5
			scanlinesize=1;
Packit caffb5
		rows=(uint64)STRIP_SIZE_DEFAULT/scanlinesize;
Packit caffb5
		if (rows==0)
Packit caffb5
			rows=1;
Packit caffb5
		else if (rows>0xFFFFFFFF)
Packit caffb5
			rows=0xFFFFFFFF;
Packit caffb5
		s=(uint32)rows;
Packit caffb5
	}
Packit caffb5
	return (s);
Packit caffb5
}
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * Return the number of bytes to read/write in a call to
Packit caffb5
 * one of the scanline-oriented i/o routines.  Note that
Packit caffb5
 * this number may be 1/samples-per-pixel if data is
Packit caffb5
 * stored as separate planes.
Packit caffb5
 * The ScanlineSize in case of YCbCrSubsampling is defined as the
Packit caffb5
 * strip size divided by the strip height, i.e. the size of a pack of vertical
Packit caffb5
 * subsampling lines divided by vertical subsampling. It should thus make
Packit caffb5
 * sense when multiplied by a multiple of vertical subsampling.
Packit caffb5
 */
Packit caffb5
uint64
Packit caffb5
TIFFScanlineSize64(TIFF* tif)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFScanlineSize64";
Packit caffb5
	TIFFDirectory *td = &tif->tif_dir;
Packit caffb5
	uint64 scanline_size;
Packit caffb5
	if (td->td_planarconfig==PLANARCONFIG_CONTIG)
Packit caffb5
	{
Packit caffb5
		if ((td->td_photometric==PHOTOMETRIC_YCBCR)&&
Packit caffb5
		    (td->td_samplesperpixel==3)&&
Packit caffb5
		    (!isUpSampled(tif)))
Packit caffb5
		{
Packit caffb5
			uint16 ycbcrsubsampling[2];
Packit caffb5
			uint16 samplingblock_samples;
Packit caffb5
			uint32 samplingblocks_hor;
Packit caffb5
			uint64 samplingrow_samples;
Packit caffb5
			uint64 samplingrow_size;
Packit caffb5
			if(td->td_samplesperpixel!=3)
Packit caffb5
			{
Packit caffb5
                            TIFFErrorExt(tif->tif_clientdata,module,
Packit caffb5
                                         "Invalid td_samplesperpixel value");
Packit caffb5
                            return 0;
Packit caffb5
			}
Packit caffb5
			TIFFGetFieldDefaulted(tif,TIFFTAG_YCBCRSUBSAMPLING,
Packit caffb5
                                              ycbcrsubsampling+0,
Packit caffb5
                                              ycbcrsubsampling+1);
Packit caffb5
			if (((ycbcrsubsampling[0]!=1)&&(ycbcrsubsampling[0]!=2)&&(ycbcrsubsampling[0]!=4)) ||
Packit caffb5
			    ((ycbcrsubsampling[1]!=1)&&(ycbcrsubsampling[1]!=2)&&(ycbcrsubsampling[1]!=4)))
Packit caffb5
			{
Packit caffb5
                            TIFFErrorExt(tif->tif_clientdata,module,
Packit caffb5
                                         "Invalid YCbCr subsampling");
Packit caffb5
                            return 0;
Packit caffb5
			}
Packit caffb5
			samplingblock_samples = ycbcrsubsampling[0]*ycbcrsubsampling[1]+2;
Packit caffb5
			samplingblocks_hor = TIFFhowmany_32(td->td_imagewidth,ycbcrsubsampling[0]);
Packit caffb5
			samplingrow_samples = _TIFFMultiply64(tif,samplingblocks_hor,samplingblock_samples,module);
Packit caffb5
			samplingrow_size = TIFFhowmany_64(_TIFFMultiply64(tif,samplingrow_samples,td->td_bitspersample,module),8);
Packit caffb5
			scanline_size = (samplingrow_size/ycbcrsubsampling[1]);
Packit caffb5
		}
Packit caffb5
		else
Packit caffb5
		{
Packit caffb5
			uint64 scanline_samples;
Packit caffb5
			scanline_samples=_TIFFMultiply64(tif,td->td_imagewidth,td->td_samplesperpixel,module);
Packit caffb5
			scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,scanline_samples,td->td_bitspersample,module),8);
Packit caffb5
		}
Packit caffb5
	}
Packit caffb5
	else
Packit caffb5
        {
Packit caffb5
		scanline_size=TIFFhowmany_64(_TIFFMultiply64(tif,td->td_imagewidth,td->td_bitspersample,module),8);
Packit caffb5
        }
Packit caffb5
        if (scanline_size == 0)
Packit caffb5
        {
Packit caffb5
                TIFFErrorExt(tif->tif_clientdata,module,"Computed scanline size is zero");
Packit caffb5
                return 0;
Packit caffb5
        }
Packit caffb5
	return(scanline_size);
Packit caffb5
}
Packit caffb5
tmsize_t
Packit caffb5
TIFFScanlineSize(TIFF* tif)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFScanlineSize";
Packit caffb5
	uint64 m;
Packit caffb5
	tmsize_t n;
Packit caffb5
	m=TIFFScanlineSize64(tif);
Packit caffb5
	n=(tmsize_t)m;
Packit caffb5
	if ((uint64)n!=m) {
Packit caffb5
		TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
Packit caffb5
		n=0;
Packit caffb5
	}
Packit caffb5
	return(n);
Packit caffb5
}
Packit caffb5
Packit caffb5
/*
Packit caffb5
 * Return the number of bytes required to store a complete
Packit caffb5
 * decoded and packed raster scanline (as opposed to the
Packit caffb5
 * I/O size returned by TIFFScanlineSize which may be less
Packit caffb5
 * if data is store as separate planes).
Packit caffb5
 */
Packit caffb5
uint64
Packit caffb5
TIFFRasterScanlineSize64(TIFF* tif)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFRasterScanlineSize64";
Packit caffb5
	TIFFDirectory *td = &tif->tif_dir;
Packit caffb5
	uint64 scanline;
Packit caffb5
Packit caffb5
	scanline = _TIFFMultiply64(tif, td->td_bitspersample, td->td_imagewidth, module);
Packit caffb5
	if (td->td_planarconfig == PLANARCONFIG_CONTIG) {
Packit caffb5
		scanline = _TIFFMultiply64(tif, scanline, td->td_samplesperpixel, module);
Packit caffb5
		return (TIFFhowmany8_64(scanline));
Packit caffb5
	} else
Packit caffb5
		return (_TIFFMultiply64(tif, TIFFhowmany8_64(scanline),
Packit caffb5
		    td->td_samplesperpixel, module));
Packit caffb5
}
Packit caffb5
tmsize_t
Packit caffb5
TIFFRasterScanlineSize(TIFF* tif)
Packit caffb5
{
Packit caffb5
	static const char module[] = "TIFFRasterScanlineSize";
Packit caffb5
	uint64 m;
Packit caffb5
	tmsize_t n;
Packit caffb5
	m=TIFFRasterScanlineSize64(tif);
Packit caffb5
	n=(tmsize_t)m;
Packit caffb5
	if ((uint64)n!=m)
Packit caffb5
	{
Packit caffb5
		TIFFErrorExt(tif->tif_clientdata,module,"Integer arithmetic overflow");
Packit caffb5
		n=0;
Packit caffb5
	}
Packit caffb5
	return(n);
Packit caffb5
}
Packit caffb5
Packit caffb5
/* vim: set ts=8 sts=8 sw=8 noet: */
Packit caffb5
/*
Packit caffb5
 * Local Variables:
Packit caffb5
 * mode: c
Packit caffb5
 * c-basic-offset: 8
Packit caffb5
 * fill-column: 78
Packit caffb5
 * End:
Packit caffb5
 */