Blame libtiff/tif_strip.c

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