2000-01-28 10:03:32 -05:00
|
|
|
/******************************************************************************
|
2005-05-25 05:37:05 -04:00
|
|
|
* tif_overview.c,v 1.9 2005/05/25 09:03:16 dron Exp
|
2000-01-28 10:03:32 -05:00
|
|
|
*
|
|
|
|
* Project: TIFF Overview Builder
|
|
|
|
* Purpose: Library function for building overviews in a TIFF file.
|
2004-07-26 10:25:04 -04:00
|
|
|
* Author: Frank Warmerdam, warmerdam@pobox.com
|
2000-01-28 10:03:32 -05:00
|
|
|
*
|
|
|
|
* Notes:
|
|
|
|
* o Currently only images with bits_per_sample of a multiple of eight
|
|
|
|
* will work.
|
|
|
|
*
|
|
|
|
* o The downsampler currently just takes the top left pixel from the
|
|
|
|
* source rectangle. Eventually sampling options of averaging, mode, and
|
|
|
|
* ``center pixel'' should be offered.
|
|
|
|
*
|
|
|
|
* o The code will attempt to use the same kind of compression,
|
|
|
|
* photometric interpretation, and organization as the source image, but
|
|
|
|
* it doesn't copy geotiff tags to the reduced resolution images.
|
|
|
|
*
|
|
|
|
* o Reduced resolution overviews for multi-sample files will currently
|
|
|
|
* always be generated as PLANARCONFIG_SEPARATE. This could be fixed
|
|
|
|
* reasonable easily if needed to improve compatibility with other
|
|
|
|
* packages. Many don't properly support PLANARCONFIG_SEPARATE.
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
* Copyright (c) 1999, Frank Warmerdam
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
|
|
|
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
/* TODO: update notes in header above */
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdlib.h>
|
2004-07-26 10:25:04 -04:00
|
|
|
#include <string.h>
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
#include "tiffio.h"
|
|
|
|
#include "tif_ovrcache.h"
|
|
|
|
|
|
|
|
#ifndef FALSE
|
|
|
|
# define FALSE 0
|
|
|
|
# define TRUE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef MAX
|
|
|
|
# define MIN(a,b) ((a<b) ? a : b)
|
|
|
|
# define MAX(a,b) ((a>b) ? a : b)
|
|
|
|
#endif
|
|
|
|
|
2018-02-06 10:13:57 -05:00
|
|
|
#define TIFF_DIR_MAX 65534
|
|
|
|
|
2000-04-18 18:48:31 -04:00
|
|
|
void TIFFBuildOverviews( TIFF *, int, int *, int, const char *,
|
|
|
|
int (*)(double,void*), void * );
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* TIFF_WriteOverview() */
|
|
|
|
/* */
|
|
|
|
/* Create a new directory, without any image data for an overview. */
|
|
|
|
/* Returns offset of newly created overview directory, but the */
|
|
|
|
/* current directory is reset to be the one in used when this */
|
|
|
|
/* function is called. */
|
2004-07-26 10:25:04 -04:00
|
|
|
/************************************************************************/
|
2000-01-28 10:03:32 -05:00
|
|
|
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 TIFF_WriteOverview( TIFF *hTIFF, uint32 nXSize, uint32 nYSize,
|
2005-05-25 05:37:05 -04:00
|
|
|
int nBitsPerPixel, int nPlanarConfig, int nSamples,
|
2000-01-28 10:03:32 -05:00
|
|
|
int nBlockXSize, int nBlockYSize,
|
|
|
|
int bTiled, int nCompressFlag, int nPhotometric,
|
|
|
|
int nSampleFormat,
|
|
|
|
unsigned short *panRed,
|
|
|
|
unsigned short *panGreen,
|
|
|
|
unsigned short *panBlue,
|
2005-12-16 00:59:55 -05:00
|
|
|
int bUseSubIFDs,
|
|
|
|
int nHorSubsampling, int nVerSubsampling )
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
{
|
2007-07-13 10:57:37 -04:00
|
|
|
toff_t nBaseDirOffset;
|
|
|
|
toff_t nOffset;
|
2018-02-06 10:13:57 -05:00
|
|
|
tdir_t iNumDir;
|
2000-01-28 10:03:32 -05:00
|
|
|
|
2008-12-30 22:06:27 -05:00
|
|
|
(void) bUseSubIFDs;
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
nBaseDirOffset = TIFFCurrentDirOffset( hTIFF );
|
|
|
|
|
|
|
|
TIFFCreateDirectory( hTIFF );
|
2005-12-16 00:59:55 -05:00
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Setup TIFF fields. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_IMAGEWIDTH, nXSize );
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_IMAGELENGTH, nYSize );
|
|
|
|
if( nSamples == 1 )
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
|
|
|
|
else
|
2005-05-25 05:37:05 -04:00
|
|
|
TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, nPlanarConfig );
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_BITSPERSAMPLE, nBitsPerPixel );
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, nSamples );
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_COMPRESSION, nCompressFlag );
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_PHOTOMETRIC, nPhotometric );
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_SAMPLEFORMAT, nSampleFormat );
|
|
|
|
|
|
|
|
if( bTiled )
|
|
|
|
{
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_TILEWIDTH, nBlockXSize );
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_TILELENGTH, nBlockYSize );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_ROWSPERSTRIP, nBlockYSize );
|
|
|
|
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE );
|
2005-12-16 00:59:55 -05:00
|
|
|
|
|
|
|
if( nPhotometric == PHOTOMETRIC_YCBCR || nPhotometric == PHOTOMETRIC_ITULAB )
|
|
|
|
{
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_YCBCRSUBSAMPLING, nHorSubsampling, nVerSubsampling);
|
|
|
|
/* TODO: also write YCbCrPositioning and YCbCrCoefficients tag identical to source IFD */
|
|
|
|
}
|
|
|
|
/* TODO: add command-line parameter for selecting jpeg compression quality
|
|
|
|
* that gets ignored when compression isn't jpeg */
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Write color table if one is present. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
if( panRed != NULL )
|
|
|
|
{
|
|
|
|
TIFFSetField( hTIFF, TIFFTAG_COLORMAP, panRed, panGreen, panBlue );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Write directory, and return byte offset. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
2004-07-26 10:25:04 -04:00
|
|
|
if( TIFFWriteCheck( hTIFF, bTiled, "TIFFBuildOverviews" ) == 0 )
|
|
|
|
return 0;
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
TIFFWriteDirectory( hTIFF );
|
2018-02-06 10:13:57 -05:00
|
|
|
iNumDir = TIFFNumberOfDirectories(hTIFF);
|
|
|
|
if( iNumDir > TIFF_DIR_MAX )
|
|
|
|
{
|
|
|
|
TIFFErrorExt( TIFFClientdata(hTIFF),
|
|
|
|
"TIFF_WriteOverview",
|
|
|
|
"File `%s' has too many directories.\n",
|
|
|
|
TIFFFileName(hTIFF) );
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
TIFFSetDirectory( hTIFF, (tdir_t) (iNumDir - 1) );
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
nOffset = TIFFCurrentDirOffset( hTIFF );
|
|
|
|
|
|
|
|
TIFFSetSubDirectory( hTIFF, nBaseDirOffset );
|
|
|
|
|
|
|
|
return nOffset;
|
|
|
|
}
|
|
|
|
|
2000-04-18 18:48:31 -04:00
|
|
|
/************************************************************************/
|
|
|
|
/* TIFF_GetSourceSamples() */
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
static void
|
|
|
|
TIFF_GetSourceSamples( double * padfSamples, unsigned char *pabySrc,
|
|
|
|
int nPixelBytes, int nSampleFormat,
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nXSize, uint32 nYSize,
|
2000-04-18 18:48:31 -04:00
|
|
|
int nPixelOffset, int nLineOffset )
|
|
|
|
{
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 iXOff, iYOff;
|
|
|
|
int iSample;
|
2000-04-18 18:48:31 -04:00
|
|
|
|
|
|
|
iSample = 0;
|
|
|
|
|
|
|
|
for( iYOff = 0; iYOff < nYSize; iYOff++ )
|
|
|
|
{
|
|
|
|
for( iXOff = 0; iXOff < nXSize; iXOff++ )
|
|
|
|
{
|
|
|
|
unsigned char *pabyData;
|
|
|
|
|
|
|
|
pabyData = pabySrc + iYOff * nLineOffset + iXOff * nPixelOffset;
|
|
|
|
|
|
|
|
if( nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 1 )
|
|
|
|
{
|
|
|
|
padfSamples[iSample++] = *pabyData;
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 2 )
|
|
|
|
{
|
|
|
|
padfSamples[iSample++] = ((uint16 *) pabyData)[0];
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 4 )
|
|
|
|
{
|
|
|
|
padfSamples[iSample++] = ((uint32 *) pabyData)[0];
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_INT && nPixelBytes == 2 )
|
|
|
|
{
|
|
|
|
padfSamples[iSample++] = ((int16 *) pabyData)[0];
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_INT && nPixelBytes == 32 )
|
|
|
|
{
|
|
|
|
padfSamples[iSample++] = ((int32 *) pabyData)[0];
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_IEEEFP && nPixelBytes == 4 )
|
|
|
|
{
|
|
|
|
padfSamples[iSample++] = ((float *) pabyData)[0];
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_IEEEFP && nPixelBytes == 8 )
|
|
|
|
{
|
|
|
|
padfSamples[iSample++] = ((double *) pabyData)[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* TIFF_SetSample() */
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
static void
|
|
|
|
TIFF_SetSample( unsigned char * pabyData, int nPixelBytes, int nSampleFormat,
|
|
|
|
double dfValue )
|
|
|
|
|
|
|
|
{
|
|
|
|
if( nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 1 )
|
|
|
|
{
|
|
|
|
*pabyData = (unsigned char) MAX(0,MIN(255,dfValue));
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 2 )
|
|
|
|
{
|
|
|
|
*((uint16 *)pabyData) = (uint16) MAX(0,MIN(65535,dfValue));
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_UINT && nPixelBytes == 4 )
|
|
|
|
{
|
|
|
|
*((uint32 *)pabyData) = (uint32) dfValue;
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_INT && nPixelBytes == 2 )
|
|
|
|
{
|
|
|
|
*((int16 *)pabyData) = (int16) MAX(-32768,MIN(32767,dfValue));
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_INT && nPixelBytes == 32 )
|
|
|
|
{
|
|
|
|
*((int32 *)pabyData) = (int32) dfValue;
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_IEEEFP && nPixelBytes == 4 )
|
|
|
|
{
|
|
|
|
*((float *)pabyData) = (float) dfValue;
|
|
|
|
}
|
|
|
|
else if( nSampleFormat == SAMPLEFORMAT_IEEEFP && nPixelBytes == 8 )
|
|
|
|
{
|
|
|
|
*((double *)pabyData) = dfValue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
/************************************************************************/
|
|
|
|
/* TIFF_DownSample() */
|
|
|
|
/* */
|
|
|
|
/* Down sample a tile of full res data into a window of a tile */
|
|
|
|
/* of downsampled data. */
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
static
|
|
|
|
void TIFF_DownSample( unsigned char *pabySrcTile,
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nBlockXSize, uint32 nBlockYSize,
|
2000-01-28 10:03:32 -05:00
|
|
|
int nPixelSkewBits, int nBitsPerPixel,
|
|
|
|
unsigned char * pabyOTile,
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nOBlockXSize, uint32 nOBlockYSize,
|
|
|
|
uint32 nTXOff, uint32 nTYOff, int nOMult,
|
2000-04-18 18:48:31 -04:00
|
|
|
int nSampleFormat, const char * pszResampling )
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
{
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 i, j;
|
|
|
|
int k, nPixelBytes = (nBitsPerPixel) / 8;
|
2000-01-28 10:03:32 -05:00
|
|
|
int nPixelGroupBytes = (nBitsPerPixel+nPixelSkewBits)/8;
|
|
|
|
unsigned char *pabySrc, *pabyDst;
|
2000-04-18 18:48:31 -04:00
|
|
|
double *padfSamples;
|
2015-05-30 17:13:39 -04:00
|
|
|
size_t tpadfSamples_size, padfSamples_size;
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
assert( nBitsPerPixel >= 8 );
|
|
|
|
|
2015-05-30 17:13:39 -04:00
|
|
|
/* sizeof(double) * nOMult * nOMult */
|
|
|
|
tpadfSamples_size=nOMult*nOMult;
|
|
|
|
if ((nOMult != 0) && (tpadfSamples_size/nOMult == (size_t) nOMult)) {
|
|
|
|
padfSamples_size=tpadfSamples_size;
|
|
|
|
tpadfSamples_size=padfSamples_size*sizeof(double);
|
|
|
|
if ((tpadfSamples_size / padfSamples_size) == sizeof(double))
|
|
|
|
padfSamples_size=tpadfSamples_size;
|
|
|
|
else
|
|
|
|
padfSamples_size=0;
|
|
|
|
} else {
|
|
|
|
padfSamples_size=0;
|
|
|
|
}
|
|
|
|
if (padfSamples_size == 0) {
|
|
|
|
/* TODO: This is an error condition */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
padfSamples = (double *) malloc(padfSamples_size);
|
2000-04-18 18:48:31 -04:00
|
|
|
|
|
|
|
/* ==================================================================== */
|
|
|
|
/* Loop over scanline chunks to process, establishing where the */
|
|
|
|
/* data is going. */
|
|
|
|
/* ==================================================================== */
|
2000-01-28 10:03:32 -05:00
|
|
|
for( j = 0; j*nOMult < nBlockYSize; j++ )
|
|
|
|
{
|
|
|
|
if( j + nTYOff >= nOBlockYSize )
|
|
|
|
break;
|
2005-12-16 00:59:55 -05:00
|
|
|
|
2005-06-06 10:15:47 -04:00
|
|
|
pabyDst = pabyOTile + ((j+nTYOff)*nOBlockXSize + nTXOff)
|
|
|
|
* nPixelBytes * nPixelGroupBytes;
|
2000-01-28 10:03:32 -05:00
|
|
|
|
2000-04-18 18:48:31 -04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Handler nearest resampling ... we don't even care about the */
|
|
|
|
/* data type, we just do a bytewise copy. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
if( strncmp(pszResampling,"nearest",4) == 0
|
|
|
|
|| strncmp(pszResampling,"NEAR",4) == 0 )
|
2000-01-28 10:03:32 -05:00
|
|
|
{
|
2000-04-18 18:48:31 -04:00
|
|
|
pabySrc = pabySrcTile + j*nOMult*nBlockXSize * nPixelGroupBytes;
|
|
|
|
|
|
|
|
for( i = 0; i*nOMult < nBlockXSize; i++ )
|
|
|
|
{
|
|
|
|
if( i + nTXOff >= nOBlockXSize )
|
|
|
|
break;
|
2000-01-28 10:03:32 -05:00
|
|
|
|
2000-04-18 18:48:31 -04:00
|
|
|
/*
|
|
|
|
* For now use simple subsampling, from the top left corner
|
|
|
|
* of the source block of pixels.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for( k = 0; k < nPixelBytes; k++ )
|
2005-06-06 10:15:47 -04:00
|
|
|
pabyDst[k] = pabySrc[k];
|
|
|
|
|
|
|
|
pabyDst += nPixelBytes * nPixelGroupBytes;
|
2000-04-18 18:48:31 -04:00
|
|
|
pabySrc += nOMult * nPixelGroupBytes;
|
|
|
|
}
|
|
|
|
}
|
2000-01-28 10:03:32 -05:00
|
|
|
|
2000-04-18 18:48:31 -04:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Handle the case of averaging. For this we also have to */
|
|
|
|
/* handle each sample format we are concerned with. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
2005-12-16 00:59:55 -05:00
|
|
|
else if( strncmp(pszResampling,"averag",6) == 0
|
2000-04-18 18:48:31 -04:00
|
|
|
|| strncmp(pszResampling,"AVERAG",6) == 0 )
|
|
|
|
{
|
|
|
|
pabySrc = pabySrcTile + j*nOMult*nBlockXSize * nPixelGroupBytes;
|
|
|
|
|
|
|
|
for( i = 0; i*nOMult < nBlockXSize; i++ )
|
2000-01-28 10:03:32 -05:00
|
|
|
{
|
2000-04-18 18:48:31 -04:00
|
|
|
double dfTotal;
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nXSize, nYSize, iSample;
|
2000-04-18 18:48:31 -04:00
|
|
|
|
|
|
|
if( i + nTXOff >= nOBlockXSize )
|
|
|
|
break;
|
|
|
|
|
2007-07-13 10:57:37 -04:00
|
|
|
nXSize = MIN((uint32)nOMult,nBlockXSize-i);
|
|
|
|
nYSize = MIN((uint32)nOMult,nBlockYSize-j);
|
2000-04-18 18:48:31 -04:00
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
TIFF_GetSourceSamples( padfSamples, pabySrc,
|
|
|
|
nPixelBytes, nSampleFormat,
|
|
|
|
nXSize, nYSize,
|
|
|
|
nPixelGroupBytes,
|
2000-04-18 18:48:31 -04:00
|
|
|
nPixelGroupBytes * nBlockXSize );
|
2005-12-16 00:59:55 -05:00
|
|
|
|
2000-04-18 18:48:31 -04:00
|
|
|
dfTotal = 0;
|
|
|
|
for( iSample = 0; iSample < nXSize*nYSize; iSample++ )
|
|
|
|
{
|
|
|
|
dfTotal += padfSamples[iSample];
|
|
|
|
}
|
|
|
|
|
|
|
|
TIFF_SetSample( pabyDst, nPixelBytes, nSampleFormat,
|
|
|
|
dfTotal / (nXSize*nYSize) );
|
|
|
|
|
|
|
|
pabySrc += nOMult * nPixelGroupBytes;
|
|
|
|
pabyDst += nPixelBytes;
|
2000-01-28 10:03:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-04-18 18:48:31 -04:00
|
|
|
|
|
|
|
free( padfSamples );
|
2000-01-28 10:03:32 -05:00
|
|
|
}
|
|
|
|
|
2007-04-07 02:36:39 -04:00
|
|
|
/************************************************************************/
|
|
|
|
/* TIFF_DownSample_Subsampled() */
|
|
|
|
/************************************************************************/
|
2005-12-16 00:59:55 -05:00
|
|
|
static
|
|
|
|
void TIFF_DownSample_Subsampled( unsigned char *pabySrcTile, int nSample,
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nBlockXSize, uint32 nBlockYSize,
|
2005-12-16 00:59:55 -05:00
|
|
|
unsigned char * pabyOTile,
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nOBlockXSize, uint32 nOBlockYSize,
|
|
|
|
uint32 nTXOff, uint32 nTYOff, int nOMult,
|
|
|
|
const char *pszResampling,
|
2005-12-16 00:59:55 -05:00
|
|
|
int nHorSubsampling, int nVerSubsampling )
|
|
|
|
{
|
|
|
|
/* TODO: test with variety of subsampling values, and incovinient tile/strip sizes */
|
2007-04-07 02:36:39 -04:00
|
|
|
int nSampleBlockSize;
|
2005-12-16 00:59:55 -05:00
|
|
|
int nSourceSampleRowSize;
|
|
|
|
int nDestSampleRowSize;
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nSourceX, nSourceY;
|
|
|
|
uint32 nSourceXSec, nSourceYSec;
|
|
|
|
uint32 nSourceXSecEnd, nSourceYSecEnd;
|
|
|
|
uint32 nDestX, nDestY;
|
2005-12-16 00:59:55 -05:00
|
|
|
int nSampleOffsetInSampleBlock;
|
|
|
|
unsigned int nCummulator;
|
|
|
|
unsigned int nCummulatorCount;
|
|
|
|
|
|
|
|
nSampleBlockSize = nHorSubsampling * nVerSubsampling + 2;
|
2007-04-07 02:36:39 -04:00
|
|
|
nSourceSampleRowSize =
|
|
|
|
( ( nBlockXSize + nHorSubsampling - 1 ) / nHorSubsampling ) * nSampleBlockSize;
|
|
|
|
nDestSampleRowSize =
|
|
|
|
( ( nOBlockXSize + nHorSubsampling - 1 ) / nHorSubsampling ) * nSampleBlockSize;
|
2005-12-16 00:59:55 -05:00
|
|
|
|
|
|
|
if( strncmp(pszResampling,"nearest",4) == 0
|
2007-04-07 02:36:39 -04:00
|
|
|
|| strncmp(pszResampling,"NEAR",4) == 0 )
|
|
|
|
{
|
2005-12-16 00:59:55 -05:00
|
|
|
if( nSample == 0 )
|
|
|
|
{
|
2007-04-07 02:36:39 -04:00
|
|
|
for( nSourceY = 0, nDestY = nTYOff;
|
|
|
|
nSourceY < nBlockYSize;
|
|
|
|
nSourceY += nOMult, nDestY ++)
|
|
|
|
{
|
|
|
|
if( nDestY >= nOBlockYSize )
|
|
|
|
break;
|
|
|
|
|
|
|
|
for( nSourceX = 0, nDestX = nTXOff;
|
|
|
|
nSourceX < nBlockXSize;
|
|
|
|
nSourceX += nOMult, nDestX ++)
|
2005-12-16 00:59:55 -05:00
|
|
|
{
|
2007-04-07 02:36:39 -04:00
|
|
|
if( nDestX >= nOBlockXSize )
|
|
|
|
break;
|
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
* ( pabyOTile + ( nDestY / nVerSubsampling ) * nDestSampleRowSize
|
2007-04-07 02:36:39 -04:00
|
|
|
+ ( nDestY % nVerSubsampling ) * nHorSubsampling
|
|
|
|
+ ( nDestX / nHorSubsampling ) * nSampleBlockSize
|
|
|
|
+ ( nDestX % nHorSubsampling ) ) =
|
2005-12-16 00:59:55 -05:00
|
|
|
* ( pabySrcTile + ( nSourceY / nVerSubsampling ) * nSourceSampleRowSize
|
2007-04-07 02:36:39 -04:00
|
|
|
+ ( nSourceY % nVerSubsampling ) * nHorSubsampling
|
|
|
|
+ ( nSourceX / nHorSubsampling ) * nSampleBlockSize
|
|
|
|
+ ( nSourceX % nHorSubsampling ) );
|
2005-12-16 00:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nSampleOffsetInSampleBlock = nHorSubsampling * nVerSubsampling + nSample - 1;
|
2007-04-07 02:36:39 -04:00
|
|
|
for( nSourceY = 0, nDestY = ( nTYOff / nVerSubsampling );
|
|
|
|
nSourceY < ( nBlockYSize / nVerSubsampling );
|
|
|
|
nSourceY += nOMult, nDestY ++)
|
2005-12-16 00:59:55 -05:00
|
|
|
{
|
2007-04-07 02:36:39 -04:00
|
|
|
if( nDestY*nVerSubsampling >= nOBlockYSize )
|
|
|
|
break;
|
|
|
|
|
|
|
|
for( nSourceX = 0, nDestX = ( nTXOff / nHorSubsampling );
|
|
|
|
nSourceX < ( nBlockXSize / nHorSubsampling );
|
|
|
|
nSourceX += nOMult, nDestX ++)
|
2005-12-16 00:59:55 -05:00
|
|
|
{
|
2007-04-07 02:36:39 -04:00
|
|
|
if( nDestX*nHorSubsampling >= nOBlockXSize )
|
|
|
|
break;
|
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
* ( pabyOTile + nDestY * nDestSampleRowSize
|
2007-04-07 02:36:39 -04:00
|
|
|
+ nDestX * nSampleBlockSize
|
|
|
|
+ nSampleOffsetInSampleBlock ) =
|
2005-12-16 00:59:55 -05:00
|
|
|
* ( pabySrcTile + nSourceY * nSourceSampleRowSize
|
2007-04-07 02:36:39 -04:00
|
|
|
+ nSourceX * nSampleBlockSize
|
|
|
|
+ nSampleOffsetInSampleBlock );
|
2005-12-16 00:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( strncmp(pszResampling,"averag",6) == 0
|
2007-04-07 02:36:39 -04:00
|
|
|
|| strncmp(pszResampling,"AVERAG",6) == 0 )
|
2005-12-16 00:59:55 -05:00
|
|
|
{
|
|
|
|
if( nSample == 0 )
|
|
|
|
{
|
|
|
|
for( nSourceY = 0, nDestY = nTYOff; nSourceY < nBlockYSize; nSourceY += nOMult, nDestY ++)
|
|
|
|
{
|
2007-04-07 02:36:39 -04:00
|
|
|
if( nDestY >= nOBlockYSize )
|
|
|
|
break;
|
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
for( nSourceX = 0, nDestX = nTXOff; nSourceX < nBlockXSize; nSourceX += nOMult, nDestX ++)
|
|
|
|
{
|
2007-04-07 02:36:39 -04:00
|
|
|
if( nDestX >= nOBlockXSize )
|
|
|
|
break;
|
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
nSourceXSecEnd = nSourceX + nOMult;
|
|
|
|
if( nSourceXSecEnd > nBlockXSize )
|
|
|
|
nSourceXSecEnd = nBlockXSize;
|
|
|
|
nSourceYSecEnd = nSourceY + nOMult;
|
|
|
|
if( nSourceYSecEnd > nBlockYSize )
|
|
|
|
nSourceYSecEnd = nBlockYSize;
|
|
|
|
nCummulator = 0;
|
|
|
|
for( nSourceYSec = nSourceY; nSourceYSec < nSourceYSecEnd; nSourceYSec ++)
|
|
|
|
{
|
|
|
|
for( nSourceXSec = nSourceX; nSourceXSec < nSourceXSecEnd; nSourceXSec ++)
|
|
|
|
{
|
|
|
|
nCummulator += * ( pabySrcTile + ( nSourceYSec / nVerSubsampling ) * nSourceSampleRowSize
|
2007-04-07 02:36:39 -04:00
|
|
|
+ ( nSourceYSec % nVerSubsampling ) * nHorSubsampling
|
|
|
|
+ ( nSourceXSec / nHorSubsampling ) * nSampleBlockSize
|
|
|
|
+ ( nSourceXSec % nHorSubsampling ) );
|
2005-12-16 00:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nCummulatorCount = ( nSourceXSecEnd - nSourceX ) * ( nSourceYSecEnd - nSourceY );
|
|
|
|
* ( pabyOTile + ( nDestY / nVerSubsampling ) * nDestSampleRowSize
|
2007-04-07 02:36:39 -04:00
|
|
|
+ ( nDestY % nVerSubsampling ) * nHorSubsampling
|
|
|
|
+ ( nDestX / nHorSubsampling ) * nSampleBlockSize
|
|
|
|
+ ( nDestX % nHorSubsampling ) ) =
|
2005-12-16 00:59:55 -05:00
|
|
|
( ( nCummulator + ( nCummulatorCount >> 1 ) ) / nCummulatorCount );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nSampleOffsetInSampleBlock = nHorSubsampling * nVerSubsampling + nSample - 1;
|
|
|
|
for( nSourceY = 0, nDestY = ( nTYOff / nVerSubsampling ); nSourceY < ( nBlockYSize / nVerSubsampling );
|
2007-04-07 02:36:39 -04:00
|
|
|
nSourceY += nOMult, nDestY ++)
|
2005-12-16 00:59:55 -05:00
|
|
|
{
|
2007-04-07 02:36:39 -04:00
|
|
|
if( nDestY*nVerSubsampling >= nOBlockYSize )
|
|
|
|
break;
|
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
for( nSourceX = 0, nDestX = ( nTXOff / nHorSubsampling ); nSourceX < ( nBlockXSize / nHorSubsampling );
|
2007-04-07 02:36:39 -04:00
|
|
|
nSourceX += nOMult, nDestX ++)
|
2005-12-16 00:59:55 -05:00
|
|
|
{
|
2007-04-07 02:36:39 -04:00
|
|
|
if( nDestX*nHorSubsampling >= nOBlockXSize )
|
|
|
|
break;
|
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
nSourceXSecEnd = nSourceX + nOMult;
|
|
|
|
if( nSourceXSecEnd > ( nBlockXSize / nHorSubsampling ) )
|
|
|
|
nSourceXSecEnd = ( nBlockXSize / nHorSubsampling );
|
|
|
|
nSourceYSecEnd = nSourceY + nOMult;
|
|
|
|
if( nSourceYSecEnd > ( nBlockYSize / nVerSubsampling ) )
|
|
|
|
nSourceYSecEnd = ( nBlockYSize / nVerSubsampling );
|
|
|
|
nCummulator = 0;
|
|
|
|
for( nSourceYSec = nSourceY; nSourceYSec < nSourceYSecEnd; nSourceYSec ++)
|
|
|
|
{
|
|
|
|
for( nSourceXSec = nSourceX; nSourceXSec < nSourceXSecEnd; nSourceXSec ++)
|
|
|
|
{
|
|
|
|
nCummulator += * ( pabySrcTile + nSourceYSec * nSourceSampleRowSize
|
2007-04-07 02:36:39 -04:00
|
|
|
+ nSourceXSec * nSampleBlockSize
|
|
|
|
+ nSampleOffsetInSampleBlock );
|
2005-12-16 00:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nCummulatorCount = ( nSourceXSecEnd - nSourceX ) * ( nSourceYSecEnd - nSourceY );
|
|
|
|
* ( pabyOTile + nDestY * nDestSampleRowSize
|
2007-04-07 02:36:39 -04:00
|
|
|
+ nDestX * nSampleBlockSize
|
|
|
|
+ nSampleOffsetInSampleBlock ) =
|
2005-12-16 00:59:55 -05:00
|
|
|
( ( nCummulator + ( nCummulatorCount >> 1 ) ) / nCummulatorCount );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
/************************************************************************/
|
|
|
|
/* TIFF_ProcessFullResBlock() */
|
|
|
|
/* */
|
|
|
|
/* Process one block of full res data, downsampling into each */
|
|
|
|
/* of the overviews. */
|
|
|
|
/************************************************************************/
|
|
|
|
|
|
|
|
void TIFF_ProcessFullResBlock( TIFF *hTIFF, int nPlanarConfig,
|
2007-07-13 10:57:37 -04:00
|
|
|
int bSubsampled,
|
|
|
|
int nHorSubsampling, int nVerSubsampling,
|
2000-01-28 10:03:32 -05:00
|
|
|
int nOverviews, int * panOvList,
|
2005-12-16 00:59:55 -05:00
|
|
|
int nBitsPerPixel,
|
2000-01-28 10:03:32 -05:00
|
|
|
int nSamples, TIFFOvrCache ** papoRawBIs,
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nSXOff, uint32 nSYOff,
|
2000-01-28 10:03:32 -05:00
|
|
|
unsigned char *pabySrcTile,
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nBlockXSize, uint32 nBlockYSize,
|
2000-04-18 18:48:31 -04:00
|
|
|
int nSampleFormat, const char * pszResampling )
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
{
|
|
|
|
int iOverview, iSample;
|
|
|
|
|
|
|
|
for( iSample = 0; iSample < nSamples; iSample++ )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We have to read a tile/strip for each sample for
|
|
|
|
* PLANARCONFIG_SEPARATE. Otherwise, we just read all the samples
|
|
|
|
* at once when handling the first sample.
|
|
|
|
*/
|
|
|
|
if( nPlanarConfig == PLANARCONFIG_SEPARATE || iSample == 0 )
|
|
|
|
{
|
|
|
|
if( TIFFIsTiled(hTIFF) )
|
|
|
|
{
|
|
|
|
TIFFReadEncodedTile( hTIFF,
|
|
|
|
TIFFComputeTile(hTIFF, nSXOff, nSYOff,
|
2004-07-26 10:25:04 -04:00
|
|
|
0, (tsample_t)iSample ),
|
2000-01-28 10:03:32 -05:00
|
|
|
pabySrcTile,
|
|
|
|
TIFFTileSize(hTIFF));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TIFFReadEncodedStrip( hTIFF,
|
2004-07-26 10:25:04 -04:00
|
|
|
TIFFComputeStrip(hTIFF, nSYOff,
|
|
|
|
(tsample_t) iSample),
|
2000-01-28 10:03:32 -05:00
|
|
|
pabySrcTile,
|
|
|
|
TIFFStripSize(hTIFF) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop over destination overview layers
|
|
|
|
*/
|
|
|
|
for( iOverview = 0; iOverview < nOverviews; iOverview++ )
|
|
|
|
{
|
|
|
|
TIFFOvrCache *poRBI = papoRawBIs[iOverview];
|
|
|
|
unsigned char *pabyOTile;
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nTXOff, nTYOff, nOXOff, nOYOff, nOMult;
|
|
|
|
uint32 nOBlockXSize = poRBI->nBlockXSize;
|
|
|
|
uint32 nOBlockYSize = poRBI->nBlockYSize;
|
|
|
|
int nSkewBits, nSampleByteOffset;
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Fetch the destination overview tile
|
|
|
|
*/
|
|
|
|
nOMult = panOvList[iOverview];
|
|
|
|
nOXOff = (nSXOff/nOMult) / nOBlockXSize;
|
|
|
|
nOYOff = (nSYOff/nOMult) / nOBlockYSize;
|
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
if( bSubsampled )
|
2000-01-28 10:03:32 -05:00
|
|
|
{
|
2005-12-16 00:59:55 -05:00
|
|
|
pabyOTile = TIFFGetOvrBlock_Subsampled( poRBI, nOXOff, nOYOff );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Establish the offset into this tile at which we should
|
|
|
|
* start placing data.
|
|
|
|
*/
|
|
|
|
nTXOff = (nSXOff - nOXOff*nOMult*nOBlockXSize) / nOMult;
|
|
|
|
nTYOff = (nSYOff - nOYOff*nOMult*nOBlockYSize) / nOMult;
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DBMALLOC
|
|
|
|
malloc_chain_check( 1 );
|
|
|
|
#endif
|
|
|
|
TIFF_DownSample_Subsampled( pabySrcTile, iSample,
|
|
|
|
nBlockXSize, nBlockYSize,
|
|
|
|
pabyOTile,
|
|
|
|
poRBI->nBlockXSize, poRBI->nBlockYSize,
|
|
|
|
nTXOff, nTYOff,
|
|
|
|
nOMult, pszResampling,
|
|
|
|
nHorSubsampling, nVerSubsampling );
|
|
|
|
#ifdef DBMALLOC
|
|
|
|
malloc_chain_check( 1 );
|
|
|
|
#endif
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-12-16 00:59:55 -05:00
|
|
|
|
|
|
|
pabyOTile = TIFFGetOvrBlock( poRBI, nOXOff, nOYOff, iSample );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Establish the offset into this tile at which we should
|
|
|
|
* start placing data.
|
|
|
|
*/
|
|
|
|
nTXOff = (nSXOff - nOXOff*nOMult*nOBlockXSize) / nOMult;
|
|
|
|
nTYOff = (nSYOff - nOYOff*nOMult*nOBlockYSize) / nOMult;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Figure out the skew (extra space between ``our samples'') and
|
|
|
|
* the byte offset to the first sample.
|
|
|
|
*/
|
|
|
|
assert( (nBitsPerPixel % 8) == 0 );
|
|
|
|
if( nPlanarConfig == PLANARCONFIG_SEPARATE )
|
|
|
|
{
|
|
|
|
nSkewBits = 0;
|
|
|
|
nSampleByteOffset = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nSkewBits = nBitsPerPixel * (nSamples-1);
|
|
|
|
nSampleByteOffset = (nBitsPerPixel/8) * iSample;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform the downsampling.
|
|
|
|
*/
|
2000-01-28 10:03:32 -05:00
|
|
|
#ifdef DBMALLOC
|
2005-12-16 00:59:55 -05:00
|
|
|
malloc_chain_check( 1 );
|
2000-01-28 10:03:32 -05:00
|
|
|
#endif
|
2005-12-16 00:59:55 -05:00
|
|
|
TIFF_DownSample( pabySrcTile + nSampleByteOffset,
|
|
|
|
nBlockXSize, nBlockYSize,
|
|
|
|
nSkewBits, nBitsPerPixel, pabyOTile,
|
|
|
|
poRBI->nBlockXSize, poRBI->nBlockYSize,
|
|
|
|
nTXOff, nTYOff,
|
|
|
|
nOMult, nSampleFormat, pszResampling );
|
2000-01-28 10:03:32 -05:00
|
|
|
#ifdef DBMALLOC
|
2005-12-16 00:59:55 -05:00
|
|
|
malloc_chain_check( 1 );
|
|
|
|
#endif
|
|
|
|
}
|
2000-01-28 10:03:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************************************************************/
|
|
|
|
/* TIFF_BuildOverviews() */
|
|
|
|
/* */
|
|
|
|
/* Build the requested list of overviews. Overviews are */
|
|
|
|
/* maintained in a bunch of temporary files and then these are */
|
|
|
|
/* written back to the TIFF file. Only one pass through the */
|
|
|
|
/* source TIFF file is made for any number of output */
|
|
|
|
/* overviews. */
|
|
|
|
/************************************************************************/
|
|
|
|
|
2000-01-28 10:36:38 -05:00
|
|
|
void TIFFBuildOverviews( TIFF *hTIFF, int nOverviews, int * panOvList,
|
2000-04-18 18:48:31 -04:00
|
|
|
int bUseSubIFDs, const char *pszResampleMethod,
|
|
|
|
int (*pfnProgress)( double, void * ),
|
|
|
|
void * pProgressData )
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
{
|
|
|
|
TIFFOvrCache **papoRawBIs;
|
2005-12-21 11:09:21 -05:00
|
|
|
uint32 nXSize, nYSize, nBlockXSize, nBlockYSize;
|
2000-01-28 10:03:32 -05:00
|
|
|
uint16 nBitsPerPixel, nPhotometric, nCompressFlag, nSamples,
|
2005-12-21 11:09:21 -05:00
|
|
|
nPlanarConfig, nSampleFormat;
|
2005-12-16 00:59:55 -05:00
|
|
|
int bSubsampled;
|
|
|
|
uint16 nHorSubsampling, nVerSubsampling;
|
2000-01-28 10:03:32 -05:00
|
|
|
int bTiled, nSXOff, nSYOff, i;
|
|
|
|
unsigned char *pabySrcTile;
|
|
|
|
uint16 *panRedMap, *panGreenMap, *panBlueMap;
|
|
|
|
TIFFErrorHandler pfnWarning;
|
|
|
|
|
2008-12-30 22:06:27 -05:00
|
|
|
(void) pfnProgress;
|
|
|
|
(void) pProgressData;
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Get the base raster size. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
TIFFGetField( hTIFF, TIFFTAG_IMAGEWIDTH, &nXSize );
|
|
|
|
TIFFGetField( hTIFF, TIFFTAG_IMAGELENGTH, &nYSize );
|
|
|
|
|
|
|
|
TIFFGetField( hTIFF, TIFFTAG_BITSPERSAMPLE, &nBitsPerPixel );
|
2005-12-21 11:09:21 -05:00
|
|
|
/* TODO: nBitsPerPixel seems misnomer and may need renaming to nBitsPerSample */
|
2000-01-28 10:03:32 -05:00
|
|
|
TIFFGetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, &nSamples );
|
|
|
|
TIFFGetFieldDefaulted( hTIFF, TIFFTAG_PLANARCONFIG, &nPlanarConfig );
|
|
|
|
|
|
|
|
TIFFGetFieldDefaulted( hTIFF, TIFFTAG_PHOTOMETRIC, &nPhotometric );
|
|
|
|
TIFFGetFieldDefaulted( hTIFF, TIFFTAG_COMPRESSION, &nCompressFlag );
|
|
|
|
TIFFGetFieldDefaulted( hTIFF, TIFFTAG_SAMPLEFORMAT, &nSampleFormat );
|
|
|
|
|
2005-12-16 00:59:55 -05:00
|
|
|
if( nPhotometric == PHOTOMETRIC_YCBCR || nPhotometric == PHOTOMETRIC_ITULAB )
|
2000-01-28 10:03:32 -05:00
|
|
|
{
|
2005-12-16 00:59:55 -05:00
|
|
|
if( nBitsPerPixel != 8 || nSamples != 3 || nPlanarConfig != PLANARCONFIG_CONTIG ||
|
2005-12-21 11:09:21 -05:00
|
|
|
nSampleFormat != SAMPLEFORMAT_UINT)
|
2005-12-16 00:59:55 -05:00
|
|
|
{
|
2005-12-21 11:09:21 -05:00
|
|
|
/* TODO: use of TIFFError is inconsistent with use of fprintf in addtiffo.c, sort out */
|
|
|
|
TIFFErrorExt( TIFFClientdata(hTIFF), "TIFFBuildOverviews",
|
|
|
|
"File `%s' has an unsupported subsampling configuration.\n",
|
|
|
|
TIFFFileName(hTIFF) );
|
|
|
|
/* If you need support for this particular flavor, please contact either
|
2005-12-16 00:59:55 -05:00
|
|
|
* Frank Warmerdam warmerdam@pobox.com
|
|
|
|
* Joris Van Damme info@awaresystems.be
|
|
|
|
*/
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bSubsampled = 1;
|
|
|
|
TIFFGetField( hTIFF, TIFFTAG_YCBCRSUBSAMPLING, &nHorSubsampling, &nVerSubsampling );
|
2005-12-21 11:09:21 -05:00
|
|
|
/* TODO: find out if maybe TIFFGetFieldDefaulted is better choice for YCbCrSubsampling tag */
|
2000-01-28 10:03:32 -05:00
|
|
|
}
|
2005-12-16 00:59:55 -05:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if( nBitsPerPixel < 8 )
|
|
|
|
{
|
2005-12-21 11:09:21 -05:00
|
|
|
/* TODO: use of TIFFError is inconsistent with use of fprintf in addtiffo.c, sort out */
|
|
|
|
TIFFErrorExt( TIFFClientdata(hTIFF), "TIFFBuildOverviews",
|
|
|
|
"File `%s' has samples of %d bits per sample. Sample\n"
|
|
|
|
"sizes of less than 8 bits per sample are not supported.\n",
|
|
|
|
TIFFFileName(hTIFF), nBitsPerPixel );
|
2005-12-16 00:59:55 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bSubsampled = 0;
|
|
|
|
nHorSubsampling = 1;
|
|
|
|
nVerSubsampling = 1;
|
|
|
|
}
|
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Turn off warnings to avoid alot of repeated warnings while */
|
|
|
|
/* rereading directories. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
pfnWarning = TIFFSetWarningHandler( NULL );
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Get the base raster block size. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
if( TIFFGetField( hTIFF, TIFFTAG_ROWSPERSTRIP, &(nBlockYSize) ) )
|
|
|
|
{
|
|
|
|
nBlockXSize = nXSize;
|
|
|
|
bTiled = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
TIFFGetField( hTIFF, TIFFTAG_TILEWIDTH, &nBlockXSize );
|
|
|
|
TIFFGetField( hTIFF, TIFFTAG_TILELENGTH, &nBlockYSize );
|
|
|
|
bTiled = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Capture the pallette if there is one. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
if( TIFFGetField( hTIFF, TIFFTAG_COLORMAP,
|
|
|
|
&panRedMap, &panGreenMap, &panBlueMap ) )
|
|
|
|
{
|
|
|
|
uint16 *panRed2, *panGreen2, *panBlue2;
|
2005-05-25 05:37:05 -04:00
|
|
|
int nColorCount = 1 << nBitsPerPixel;
|
2000-01-28 10:03:32 -05:00
|
|
|
|
2005-05-25 05:37:05 -04:00
|
|
|
panRed2 = (uint16 *) _TIFFmalloc(2*nColorCount);
|
|
|
|
panGreen2 = (uint16 *) _TIFFmalloc(2*nColorCount);
|
|
|
|
panBlue2 = (uint16 *) _TIFFmalloc(2*nColorCount);
|
2000-01-28 10:03:32 -05:00
|
|
|
|
2005-05-25 05:37:05 -04:00
|
|
|
memcpy( panRed2, panRedMap, 2 * nColorCount );
|
|
|
|
memcpy( panGreen2, panGreenMap, 2 * nColorCount );
|
|
|
|
memcpy( panBlue2, panBlueMap, 2 * nColorCount );
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
panRedMap = panRed2;
|
|
|
|
panGreenMap = panGreen2;
|
|
|
|
panBlueMap = panBlue2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
panRedMap = panGreenMap = panBlueMap = NULL;
|
|
|
|
}
|
2005-12-16 00:59:55 -05:00
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Initialize overviews. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
papoRawBIs = (TIFFOvrCache **) _TIFFmalloc(nOverviews*sizeof(void*));
|
|
|
|
|
|
|
|
for( i = 0; i < nOverviews; i++ )
|
|
|
|
{
|
2007-07-13 10:57:37 -04:00
|
|
|
uint32 nOXSize, nOYSize, nOBlockXSize, nOBlockYSize;
|
|
|
|
toff_t nDirOffset;
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
nOXSize = (nXSize + panOvList[i] - 1) / panOvList[i];
|
|
|
|
nOYSize = (nYSize + panOvList[i] - 1) / panOvList[i];
|
|
|
|
|
2007-07-13 10:57:37 -04:00
|
|
|
nOBlockXSize = MIN(nBlockXSize,nOXSize);
|
|
|
|
nOBlockYSize = MIN(nBlockYSize,nOYSize);
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
if( bTiled )
|
|
|
|
{
|
|
|
|
if( (nOBlockXSize % 16) != 0 )
|
|
|
|
nOBlockXSize = nOBlockXSize + 16 - (nOBlockXSize % 16);
|
2005-12-16 00:59:55 -05:00
|
|
|
|
2000-01-28 10:03:32 -05:00
|
|
|
if( (nOBlockYSize % 16) != 0 )
|
|
|
|
nOBlockYSize = nOBlockYSize + 16 - (nOBlockYSize % 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
nDirOffset = TIFF_WriteOverview( hTIFF, nOXSize, nOYSize,
|
2005-05-25 05:37:05 -04:00
|
|
|
nBitsPerPixel, nPlanarConfig,
|
|
|
|
nSamples, nOBlockXSize, nOBlockYSize,
|
2000-01-28 10:03:32 -05:00
|
|
|
bTiled, nCompressFlag, nPhotometric,
|
|
|
|
nSampleFormat,
|
|
|
|
panRedMap, panGreenMap, panBlueMap,
|
2005-12-16 00:59:55 -05:00
|
|
|
bUseSubIFDs,
|
|
|
|
nHorSubsampling, nVerSubsampling );
|
2000-01-28 10:03:32 -05:00
|
|
|
|
|
|
|
papoRawBIs[i] = TIFFCreateOvrCache( hTIFF, nDirOffset );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( panRedMap != NULL )
|
|
|
|
{
|
|
|
|
_TIFFfree( panRedMap );
|
|
|
|
_TIFFfree( panGreenMap );
|
|
|
|
_TIFFfree( panBlueMap );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Allocate a buffer to hold a source block. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
if( bTiled )
|
|
|
|
pabySrcTile = (unsigned char *) _TIFFmalloc(TIFFTileSize(hTIFF));
|
|
|
|
else
|
|
|
|
pabySrcTile = (unsigned char *) _TIFFmalloc(TIFFStripSize(hTIFF));
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Loop over the source raster, applying data to the */
|
|
|
|
/* destination raster. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
for( nSYOff = 0; nSYOff < (int) nYSize; nSYOff += nBlockYSize )
|
|
|
|
{
|
|
|
|
for( nSXOff = 0; nSXOff < (int) nXSize; nSXOff += nBlockXSize )
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Read and resample into the various overview images.
|
|
|
|
*/
|
|
|
|
|
|
|
|
TIFF_ProcessFullResBlock( hTIFF, nPlanarConfig,
|
2005-12-16 00:59:55 -05:00
|
|
|
bSubsampled,nHorSubsampling,nVerSubsampling,
|
2000-01-28 10:03:32 -05:00
|
|
|
nOverviews, panOvList,
|
|
|
|
nBitsPerPixel, nSamples, papoRawBIs,
|
|
|
|
nSXOff, nSYOff, pabySrcTile,
|
2000-04-18 18:48:31 -04:00
|
|
|
nBlockXSize, nBlockYSize,
|
|
|
|
nSampleFormat, pszResampleMethod );
|
2000-01-28 10:03:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_TIFFfree( pabySrcTile );
|
|
|
|
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
/* Cleanup the rawblockedimage files. */
|
|
|
|
/* -------------------------------------------------------------------- */
|
|
|
|
for( i = 0; i < nOverviews; i++ )
|
|
|
|
{
|
|
|
|
TIFFDestroyOvrCache( papoRawBIs[i] );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( papoRawBIs != NULL )
|
|
|
|
_TIFFfree( papoRawBIs );
|
|
|
|
|
|
|
|
TIFFSetWarningHandler( pfnWarning );
|
|
|
|
}
|
2005-12-16 00:59:55 -05:00
|
|
|
|
|
|
|
|
2010-06-08 14:55:15 -04:00
|
|
|
/*
|
|
|
|
* Local Variables:
|
|
|
|
* mode: c
|
2015-05-30 17:13:39 -04:00
|
|
|
* c-basic-offset: 4
|
2010-06-08 14:55:15 -04:00
|
|
|
* fill-column: 78
|
|
|
|
* End:
|
|
|
|
*/
|