tmsize_t
TIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)
{
	static const char module[] = "TIFFReadEncodedStrip";
	TIFFDirectory *td = &tif->tif_dir;
	uint32 rowsperstrip;
	uint32 stripsperplane;
	uint32 stripinplane;
	uint16 plane;
	uint32 rows;
	tmsize_t stripsize;
	if (!TIFFCheckRead(tif,0))
		return((tmsize_t)(-1));
	if (strip>=td->td_nstrips)
	{
		TIFFErrorExt(tif->tif_clientdata,module,
		    "%lu: Strip out of range, max %lu",(unsigned long)strip,
		    (unsigned long)td->td_nstrips);
		return((tmsize_t)(-1));
	}
	rowsperstrip=td->td_rowsperstrip;
	if (rowsperstrip>td->td_imagelength)
		rowsperstrip=td->td_imagelength;
	stripsperplane=((td->td_imagelength+rowsperstrip-1)/rowsperstrip);
	stripinplane=(strip%stripsperplane);
	plane=(strip/stripsperplane);
	rows=td->td_imagelength-stripinplane*rowsperstrip;
	if (rows>rowsperstrip)
		rows=rowsperstrip;
	stripsize=TIFFVStripSize(tif,rows);
	if (stripsize==0)
		return((tmsize_t)(-1));
	if ((size!=(tmsize_t)(-1))&&(size<stripsize))
		stripsize=size;
	if (!TIFFFillStrip(tif,strip))
		return((tmsize_t)(-1));
	if ((*tif->tif_decodestrip)(tif,buf,stripsize,plane)<=0)
		return((tmsize_t)(-1));
	(*tif->tif_postdecode)(tif,buf,stripsize);
	return(stripsize);
}
['void TIFFBuildOverviews( TIFF *hTIFF, int nOverviews, int * panOvList,\n                         int bUseSubIFDs, const char *pszResampleMethod,\n                         int (*pfnProgress)( double, void * ),\n                         void * pProgressData )\n{\n    TIFFOvrCache\t**papoRawBIs;\n    uint32\t\tnXSize, nYSize, nBlockXSize, nBlockYSize;\n    uint16\t\tnBitsPerPixel, nPhotometric, nCompressFlag, nSamples,\n        nPlanarConfig, nSampleFormat;\n    int         bSubsampled;\n    uint16      nHorSubsampling, nVerSubsampling;\n    int\t\t\tbTiled, nSXOff, nSYOff, i;\n    unsigned char\t*pabySrcTile;\n    uint16\t\t*panRedMap, *panGreenMap, *panBlueMap;\n    TIFFErrorHandler    pfnWarning;\n    (void) pfnProgress;\n    (void) pProgressData;\n    TIFFGetField( hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize );\n    TIFFGetField( hTIFF, TIFFTAG_IMAGELENGTH, &nYSize );\n    TIFFGetField( hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerPixel );\n    TIFFGetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamples );\n    TIFFGetFieldDefaulted( hTIFF, TIFFTAG_PLANARCONFIG, &nPlanarConfig );\n    TIFFGetFieldDefaulted( hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric );\n    TIFFGetFieldDefaulted( hTIFF, TIFFTAG_COMPRESSION, &nCompressFlag );\n    TIFFGetFieldDefaulted( hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat );\n    if( nPhotometric == PHOTOMETRIC_YCBCR || nPhotometric == PHOTOMETRIC_ITULAB )\n    {\n        if( nBitsPerPixel != 8 || nSamples != 3 || nPlanarConfig != PLANARCONFIG_CONTIG ||\n            nSampleFormat != SAMPLEFORMAT_UINT)\n        {\n            TIFFErrorExt( TIFFClientdata(hTIFF), "TIFFBuildOverviews",\n                          "File `%s\' has an unsupported subsampling configuration.\\n",\n                          TIFFFileName(hTIFF) );\n            return;\n        }\n        bSubsampled = 1;\n        TIFFGetField( hTIFF, TIFFTAG_YCBCRSUBSAMPLING, &nHorSubsampling, &nVerSubsampling );\n    }\n    else\n    {\n        if( nBitsPerPixel < 8 )\n        {\n            TIFFErrorExt( TIFFClientdata(hTIFF), "TIFFBuildOverviews",\n                          "File `%s\' has samples of %d bits per sample.  Sample\\n"\n                          "sizes of less than 8 bits per sample are not supported.\\n",\n                          TIFFFileName(hTIFF), nBitsPerPixel );\n            return;\n        }\n        bSubsampled = 0;\n        nHorSubsampling = 1;\n        nVerSubsampling = 1;\n    }\n    pfnWarning = TIFFSetWarningHandler( NULL );\n    if( TIFFGetField( hTIFF, TIFFTAG_ROWSPERSTRIP, &(nBlockYSize) ) )\n    {\n        nBlockXSize = nXSize;\n        bTiled = FALSE;\n    }\n    else\n    {\n        TIFFGetField( hTIFF, TIFFTAG_TILEWIDTH, &nBlockXSize );\n        TIFFGetField( hTIFF, TIFFTAG_TILELENGTH, &nBlockYSize );\n        bTiled = TRUE;\n    }\n    if( TIFFGetField( hTIFF, TIFFTAG_COLORMAP,\n                      &panRedMap, &panGreenMap, &panBlueMap ) )\n    {\n        uint16\t\t*panRed2, *panGreen2, *panBlue2;\n        int             nColorCount = 1 << nBitsPerPixel;\n        panRed2 = (uint16 *) _TIFFmalloc(2*nColorCount);\n        panGreen2 = (uint16 *) _TIFFmalloc(2*nColorCount);\n        panBlue2 = (uint16 *) _TIFFmalloc(2*nColorCount);\n        memcpy( panRed2, panRedMap, 2 * nColorCount );\n        memcpy( panGreen2, panGreenMap, 2 * nColorCount );\n        memcpy( panBlue2, panBlueMap, 2 * nColorCount );\n        panRedMap = panRed2;\n        panGreenMap = panGreen2;\n        panBlueMap = panBlue2;\n    }\n    else\n    {\n        panRedMap = panGreenMap = panBlueMap = NULL;\n    }\n    papoRawBIs = (TIFFOvrCache **) _TIFFmalloc(nOverviews*sizeof(void*));\n    for( i = 0; i < nOverviews; i++ )\n    {\n        uint32  nOXSize, nOYSize, nOBlockXSize, nOBlockYSize;\n        toff_t  nDirOffset;\n        nOXSize = (nXSize + panOvList[i] - 1) / panOvList[i];\n        nOYSize = (nYSize + panOvList[i] - 1) / panOvList[i];\n        nOBlockXSize = MIN(nBlockXSize,nOXSize);\n        nOBlockYSize = MIN(nBlockYSize,nOYSize);\n        if( bTiled )\n        {\n            if( (nOBlockXSize % 16) != 0 )\n                nOBlockXSize = nOBlockXSize + 16 - (nOBlockXSize % 16);\n            if( (nOBlockYSize % 16) != 0 )\n                nOBlockYSize = nOBlockYSize + 16 - (nOBlockYSize % 16);\n        }\n        nDirOffset = TIFF_WriteOverview( hTIFF, nOXSize, nOYSize,\n                                         nBitsPerPixel, nPlanarConfig,\n                                         nSamples, nOBlockXSize, nOBlockYSize,\n                                         bTiled, nCompressFlag, nPhotometric,\n                                         nSampleFormat,\n                                         panRedMap, panGreenMap, panBlueMap,\n                                         bUseSubIFDs,\n                                         nHorSubsampling, nVerSubsampling );\n        papoRawBIs[i] = TIFFCreateOvrCache( hTIFF, nDirOffset );\n    }\n    if( panRedMap != NULL )\n    {\n        _TIFFfree( panRedMap );\n        _TIFFfree( panGreenMap );\n        _TIFFfree( panBlueMap );\n    }\n    if( bTiled )\n        pabySrcTile = (unsigned char *) _TIFFmalloc(TIFFTileSize(hTIFF));\n    else\n        pabySrcTile = (unsigned char *) _TIFFmalloc(TIFFStripSize(hTIFF));\n    for( nSYOff = 0; nSYOff < (int) nYSize; nSYOff += nBlockYSize )\n    {\n        for( nSXOff = 0; nSXOff < (int) nXSize; nSXOff += nBlockXSize )\n        {\n            TIFF_ProcessFullResBlock( hTIFF, nPlanarConfig,\n                                      bSubsampled,nHorSubsampling,nVerSubsampling,\n                                      nOverviews, panOvList,\n                                      nBitsPerPixel, nSamples, papoRawBIs,\n                                      nSXOff, nSYOff, pabySrcTile,\n                                      nBlockXSize, nBlockYSize,\n                                      nSampleFormat, pszResampleMethod );\n        }\n    }\n    _TIFFfree( pabySrcTile );\n    for( i = 0; i < nOverviews; i++ )\n    {\n        TIFFDestroyOvrCache( papoRawBIs[i] );\n    }\n    if( papoRawBIs != NULL )\n        _TIFFfree( papoRawBIs );\n    TIFFSetWarningHandler( pfnWarning );\n}', 'void TIFF_ProcessFullResBlock( TIFF *hTIFF, int nPlanarConfig,\n                               int bSubsampled,\n                               int nHorSubsampling, int nVerSubsampling,\n                               int nOverviews, int * panOvList,\n                               int nBitsPerPixel,\n                               int nSamples, TIFFOvrCache ** papoRawBIs,\n                               uint32 nSXOff, uint32 nSYOff,\n                               unsigned char *pabySrcTile,\n                               uint32 nBlockXSize, uint32 nBlockYSize,\n                               int nSampleFormat, const char * pszResampling )\n{\n    int\t\tiOverview, iSample;\n    for( iSample = 0; iSample < nSamples; iSample++ )\n    {\n        if( nPlanarConfig == PLANARCONFIG_SEPARATE || iSample == 0 )\n        {\n            if( TIFFIsTiled(hTIFF) )\n            {\n                TIFFReadEncodedTile( hTIFF,\n                                     TIFFComputeTile(hTIFF, nSXOff, nSYOff,\n                                                     0, (tsample_t)iSample ),\n                                     pabySrcTile,\n                                     TIFFTileSize(hTIFF));\n            }\n            else\n            {\n                TIFFReadEncodedStrip( hTIFF,\n                                      TIFFComputeStrip(hTIFF, nSYOff,\n                                                       (tsample_t) iSample),\n                                      pabySrcTile,\n                                      TIFFStripSize(hTIFF) );\n            }\n        }\n        for( iOverview = 0; iOverview < nOverviews; iOverview++ )\n        {\n            TIFFOvrCache *poRBI = papoRawBIs[iOverview];\n            unsigned char *pabyOTile;\n            uint32  nTXOff, nTYOff, nOXOff, nOYOff, nOMult;\n            uint32  nOBlockXSize = poRBI->nBlockXSize;\n            uint32  nOBlockYSize = poRBI->nBlockYSize;\n            int     nSkewBits, nSampleByteOffset;\n            nOMult = panOvList[iOverview];\n            nOXOff = (nSXOff/nOMult) / nOBlockXSize;\n            nOYOff = (nSYOff/nOMult) / nOBlockYSize;\n            if( bSubsampled )\n            {\n                pabyOTile = TIFFGetOvrBlock_Subsampled( poRBI, nOXOff, nOYOff );\n                nTXOff = (nSXOff - nOXOff*nOMult*nOBlockXSize) / nOMult;\n                nTYOff = (nSYOff - nOYOff*nOMult*nOBlockYSize) / nOMult;\n#ifdef DBMALLOC\n                malloc_chain_check( 1 );\n#endif\n                TIFF_DownSample_Subsampled( pabySrcTile, iSample,\n                                            nBlockXSize, nBlockYSize,\n                                            pabyOTile,\n                                            poRBI->nBlockXSize, poRBI->nBlockYSize,\n                                            nTXOff, nTYOff,\n                                            nOMult, pszResampling,\n                                            nHorSubsampling, nVerSubsampling );\n#ifdef DBMALLOC\n                malloc_chain_check( 1 );\n#endif\n            }\n            else\n            {\n                pabyOTile = TIFFGetOvrBlock( poRBI, nOXOff, nOYOff, iSample );\n                nTXOff = (nSXOff - nOXOff*nOMult*nOBlockXSize) / nOMult;\n                nTYOff = (nSYOff - nOYOff*nOMult*nOBlockYSize) / nOMult;\n                assert( (nBitsPerPixel % 8) == 0 );\n                if( nPlanarConfig == PLANARCONFIG_SEPARATE )\n                {\n                    nSkewBits = 0;\n                    nSampleByteOffset = 0;\n                }\n                else\n                {\n                    nSkewBits = nBitsPerPixel * (nSamples-1);\n                    nSampleByteOffset = (nBitsPerPixel/8) * iSample;\n                }\n#ifdef DBMALLOC\n                malloc_chain_check( 1 );\n#endif\n                TIFF_DownSample( pabySrcTile + nSampleByteOffset,\n                               nBlockXSize, nBlockYSize,\n                               nSkewBits, nBitsPerPixel, pabyOTile,\n                               poRBI->nBlockXSize, poRBI->nBlockYSize,\n                               nTXOff, nTYOff,\n                               nOMult, nSampleFormat, pszResampling );\n#ifdef DBMALLOC\n                malloc_chain_check( 1 );\n#endif\n            }\n        }\n    }\n}', 'tmsize_t\nTIFFStripSize(TIFF* tif)\n{\n\tstatic const char module[] = "TIFFStripSize";\n\tuint64 m;\n\ttmsize_t n;\n\tm=TIFFStripSize64(tif);\n\tn=(tmsize_t)m;\n\tif ((uint64)n!=m)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,"Integer overflow");\n\t\tn=0;\n\t}\n\treturn(n);\n}', 'uint64\nTIFFStripSize64(TIFF* tif)\n{\n\tTIFFDirectory* td = &tif->tif_dir;\n\tuint32 rps = td->td_rowsperstrip;\n\tif (rps > td->td_imagelength)\n\t\trps = td->td_imagelength;\n\treturn (TIFFVStripSize64(tif, rps));\n}', 'tmsize_t\nTIFFReadEncodedStrip(TIFF* tif, uint32 strip, void* buf, tmsize_t size)\n{\n\tstatic const char module[] = "TIFFReadEncodedStrip";\n\tTIFFDirectory *td = &tif->tif_dir;\n\tuint32 rowsperstrip;\n\tuint32 stripsperplane;\n\tuint32 stripinplane;\n\tuint16 plane;\n\tuint32 rows;\n\ttmsize_t stripsize;\n\tif (!TIFFCheckRead(tif,0))\n\t\treturn((tmsize_t)(-1));\n\tif (strip>=td->td_nstrips)\n\t{\n\t\tTIFFErrorExt(tif->tif_clientdata,module,\n\t\t    "%lu: Strip out of range, max %lu",(unsigned long)strip,\n\t\t    (unsigned long)td->td_nstrips);\n\t\treturn((tmsize_t)(-1));\n\t}\n\trowsperstrip=td->td_rowsperstrip;\n\tif (rowsperstrip>td->td_imagelength)\n\t\trowsperstrip=td->td_imagelength;\n\tstripsperplane=((td->td_imagelength+rowsperstrip-1)/rowsperstrip);\n\tstripinplane=(strip%stripsperplane);\n\tplane=(strip/stripsperplane);\n\trows=td->td_imagelength-stripinplane*rowsperstrip;\n\tif (rows>rowsperstrip)\n\t\trows=rowsperstrip;\n\tstripsize=TIFFVStripSize(tif,rows);\n\tif (stripsize==0)\n\t\treturn((tmsize_t)(-1));\n\tif ((size!=(tmsize_t)(-1))&&(size<stripsize))\n\t\tstripsize=size;\n\tif (!TIFFFillStrip(tif,strip))\n\t\treturn((tmsize_t)(-1));\n\tif ((*tif->tif_decodestrip)(tif,buf,stripsize,plane)<=0)\n\t\treturn((tmsize_t)(-1));\n\t(*tif->tif_postdecode)(tif,buf,stripsize);\n\treturn(stripsize);\n}']