roentgen b75cab
/******************************************************************************
roentgen b75cab
 * $Id: tif_ovrcache.c,v 1.9 2010-06-08 18:55:15 bfriesen Exp $
roentgen b75cab
 *
roentgen b75cab
 * Project:  TIFF Overview Builder
roentgen b75cab
 * Purpose:  Library functions to maintain two rows of tiles or two strips
roentgen b75cab
 *           of data for output overviews as an output cache. 
roentgen b75cab
 * Author:   Frank Warmerdam, warmerdam@pobox.com
roentgen b75cab
 *
roentgen b75cab
 ******************************************************************************
roentgen b75cab
 * Copyright (c) 2000, Frank Warmerdam
roentgen b75cab
 *
roentgen b75cab
 * Permission is hereby granted, free of charge, to any person obtaining a
roentgen b75cab
 * copy of this software and associated documentation files (the "Software"),
roentgen b75cab
 * to deal in the Software without restriction, including without limitation
roentgen b75cab
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
roentgen b75cab
 * and/or sell copies of the Software, and to permit persons to whom the
roentgen b75cab
 * Software is furnished to do so, subject to the following conditions:
roentgen b75cab
 *
roentgen b75cab
 * The above copyright notice and this permission notice shall be included
roentgen b75cab
 * in all copies or substantial portions of the Software.
roentgen b75cab
 *
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
roentgen b75cab
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
roentgen b75cab
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
roentgen b75cab
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
roentgen b75cab
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
roentgen b75cab
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
roentgen b75cab
 * DEALINGS IN THE SOFTWARE.
roentgen b75cab
 ******************************************************************************
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
#include "tif_ovrcache.h"
roentgen b75cab
#include <assert.h></assert.h>
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                         TIFFCreateOvrCache()                         */
roentgen b75cab
/*                                                                      */
roentgen b75cab
/*      Create an overview cache to hold two rows of blocks from an     */
roentgen b75cab
/*      existing TIFF directory.                                        */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
TIFFOvrCache *TIFFCreateOvrCache( TIFF *hTIFF, toff_t nDirOffset )
roentgen b75cab
roentgen b75cab
{
roentgen b75cab
    TIFFOvrCache	*psCache;
roentgen b75cab
    toff_t		nBaseDirOffset;
roentgen b75cab
roentgen b75cab
    psCache = (TIFFOvrCache *) _TIFFmalloc(sizeof(TIFFOvrCache));
roentgen b75cab
    psCache->nDirOffset = nDirOffset;
roentgen b75cab
    psCache->hTIFF = hTIFF;
roentgen b75cab
    
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Get definition of this raster from the TIFF file itself.        */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    nBaseDirOffset = TIFFCurrentDirOffset( psCache->hTIFF );
roentgen b75cab
    TIFFSetSubDirectory( hTIFF, nDirOffset );
roentgen b75cab
    
roentgen b75cab
    TIFFGetField( hTIFF, TIFFTAG_IMAGEWIDTH, &(psCache->nXSize) );
roentgen b75cab
    TIFFGetField( hTIFF, TIFFTAG_IMAGELENGTH, &(psCache->nYSize) );
roentgen b75cab
roentgen b75cab
    TIFFGetField( hTIFF, TIFFTAG_BITSPERSAMPLE, &(psCache->nBitsPerPixel) );
roentgen b75cab
    TIFFGetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, &(psCache->nSamples) );
roentgen b75cab
    TIFFGetField( hTIFF, TIFFTAG_PLANARCONFIG, &(psCache->nPlanarConfig) );
roentgen b75cab
roentgen b75cab
    if( !TIFFIsTiled( hTIFF ) )
roentgen b75cab
    {
roentgen b75cab
        TIFFGetField( hTIFF, TIFFTAG_ROWSPERSTRIP, &(psCache->nBlockYSize) );
roentgen b75cab
        psCache->nBlockXSize = psCache->nXSize;
roentgen b75cab
        psCache->nBytesPerBlock = TIFFStripSize(hTIFF);
roentgen b75cab
        psCache->bTiled = FALSE;
roentgen b75cab
    }
roentgen b75cab
    else
roentgen b75cab
    {
roentgen b75cab
        TIFFGetField( hTIFF, TIFFTAG_TILEWIDTH, &(psCache->nBlockXSize) );
roentgen b75cab
        TIFFGetField( hTIFF, TIFFTAG_TILELENGTH, &(psCache->nBlockYSize) );
roentgen b75cab
        psCache->nBytesPerBlock = TIFFTileSize(hTIFF);
roentgen b75cab
        psCache->bTiled = TRUE;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Compute some values from this.                                  */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
roentgen b75cab
    psCache->nBlocksPerRow = (psCache->nXSize + psCache->nBlockXSize - 1)
roentgen b75cab
        		/ psCache->nBlockXSize;
roentgen b75cab
    psCache->nBlocksPerColumn = (psCache->nYSize + psCache->nBlockYSize - 1)
roentgen b75cab
        		/ psCache->nBlockYSize;
roentgen b75cab
roentgen b75cab
    if (psCache->nPlanarConfig == PLANARCONFIG_SEPARATE)
roentgen b75cab
        psCache->nBytesPerRow = psCache->nBytesPerBlock
roentgen b75cab
            * psCache->nBlocksPerRow * psCache->nSamples;
roentgen b75cab
    else
roentgen b75cab
        psCache->nBytesPerRow =
roentgen b75cab
            psCache->nBytesPerBlock * psCache->nBlocksPerRow;
roentgen b75cab
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Allocate and initialize the data buffers.                       */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
roentgen b75cab
    psCache->pabyRow1Blocks =
roentgen b75cab
        (unsigned char *) _TIFFmalloc(psCache->nBytesPerRow);
roentgen b75cab
    psCache->pabyRow2Blocks =
roentgen b75cab
        (unsigned char *) _TIFFmalloc(psCache->nBytesPerRow);
roentgen b75cab
roentgen b75cab
    if( psCache->pabyRow1Blocks == NULL
roentgen b75cab
        || psCache->pabyRow2Blocks == NULL )
roentgen b75cab
    {
roentgen b75cab
		TIFFErrorExt( hTIFF->tif_clientdata, hTIFF->tif_name,
roentgen b75cab
					  "Can't allocate memory for overview cache." );
roentgen b75cab
        /* TODO: use of TIFFError is inconsistent with use of fprintf in addtiffo.c, sort out */
roentgen b75cab
        return NULL;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    _TIFFmemset( psCache->pabyRow1Blocks, 0, psCache->nBytesPerRow );
roentgen b75cab
    _TIFFmemset( psCache->pabyRow2Blocks, 0, psCache->nBytesPerRow );
roentgen b75cab
roentgen b75cab
    psCache->nBlockOffset = 0;
roentgen b75cab
roentgen b75cab
    TIFFSetSubDirectory( psCache->hTIFF, nBaseDirOffset );
roentgen b75cab
    
roentgen b75cab
    return psCache;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                          TIFFWriteOvrRow()                           */
roentgen b75cab
/*                                                                      */
roentgen b75cab
/*      Write one entire row of blocks (row 1) to the tiff file, and    */
roentgen b75cab
/*      then rotate the block buffers, essentially moving things        */
roentgen b75cab
/*      down by one block.                                              */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
static void TIFFWriteOvrRow( TIFFOvrCache * psCache )
roentgen b75cab
roentgen b75cab
{
roentgen b75cab
    int		nRet, iTileX, iTileY = psCache->nBlockOffset;
roentgen b75cab
    unsigned char *pabyData;
roentgen b75cab
    toff_t	nBaseDirOffset;
roentgen b75cab
    uint32      RowsInStrip;
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      If the output cache is multi-byte per sample, and the file      */
roentgen b75cab
/*      being written to is of a different byte order than the current  */
roentgen b75cab
/*      platform, we will need to byte swap the data.                   */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    if( TIFFIsByteSwapped(psCache->hTIFF) )
roentgen b75cab
    {
roentgen b75cab
        if( psCache->nBitsPerPixel == 16 )
roentgen b75cab
            TIFFSwabArrayOfShort( (uint16 *) psCache->pabyRow1Blocks,
roentgen b75cab
                      (psCache->nBytesPerBlock * psCache->nSamples) / 2 );
roentgen b75cab
roentgen b75cab
        else if( psCache->nBitsPerPixel == 32 )
roentgen b75cab
            TIFFSwabArrayOfLong( (uint32 *) psCache->pabyRow1Blocks,
roentgen b75cab
                         (psCache->nBytesPerBlock * psCache->nSamples) / 4 );
roentgen b75cab
roentgen b75cab
        else if( psCache->nBitsPerPixel == 64 )
roentgen b75cab
            TIFFSwabArrayOfDouble( (double *) psCache->pabyRow1Blocks,
roentgen b75cab
                         (psCache->nBytesPerBlock * psCache->nSamples) / 8 );
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Record original directory position, so we can restore it at     */
roentgen b75cab
/*      end.                                                            */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    nBaseDirOffset = TIFFCurrentDirOffset( psCache->hTIFF );
roentgen b75cab
    nRet = TIFFSetSubDirectory( psCache->hTIFF, psCache->nDirOffset );
roentgen b75cab
    assert( nRet == 1 );
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Write blocks to TIFF file.                                      */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
	for( iTileX = 0; iTileX < psCache->nBlocksPerRow; iTileX++ )
roentgen b75cab
	{
roentgen b75cab
		int nTileID;
roentgen b75cab
roentgen b75cab
		if (psCache->nPlanarConfig == PLANARCONFIG_SEPARATE)
roentgen b75cab
		{
roentgen b75cab
			int iSample;
roentgen b75cab
roentgen b75cab
			for( iSample = 0; iSample < psCache->nSamples; iSample++ )
roentgen b75cab
			{
roentgen b75cab
				pabyData = TIFFGetOvrBlock( psCache, iTileX, iTileY, iSample );
roentgen b75cab
roentgen b75cab
				if( psCache->bTiled )
roentgen b75cab
				{
roentgen b75cab
					nTileID = TIFFComputeTile( psCache->hTIFF,
roentgen b75cab
					    iTileX * psCache->nBlockXSize,
roentgen b75cab
					    iTileY * psCache->nBlockYSize,
roentgen b75cab
					    0, (tsample_t) iSample );
roentgen b75cab
					TIFFWriteEncodedTile( psCache->hTIFF, nTileID,
roentgen b75cab
					    pabyData,
roentgen b75cab
					    TIFFTileSize(psCache->hTIFF) );
roentgen b75cab
				}
roentgen b75cab
				else
roentgen b75cab
				{
roentgen b75cab
					nTileID = TIFFComputeStrip( psCache->hTIFF,
roentgen b75cab
					    iTileY * psCache->nBlockYSize,
roentgen b75cab
					    (tsample_t) iSample );
roentgen b75cab
					RowsInStrip=psCache->nBlockYSize;
roentgen b75cab
					if ((iTileY+1)*psCache->nBlockYSize>psCache->nYSize)
roentgen b75cab
						RowsInStrip=psCache->nYSize-iTileY*psCache->nBlockYSize;
roentgen b75cab
					TIFFWriteEncodedStrip( psCache->hTIFF, nTileID,
roentgen b75cab
					    pabyData,
roentgen b75cab
					    TIFFVStripSize(psCache->hTIFF,RowsInStrip) );
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
roentgen b75cab
		}
roentgen b75cab
		else
roentgen b75cab
		{
roentgen b75cab
			pabyData = TIFFGetOvrBlock( psCache, iTileX, iTileY, 0 );
roentgen b75cab
roentgen b75cab
			if( psCache->bTiled )
roentgen b75cab
			{
roentgen b75cab
				nTileID = TIFFComputeTile( psCache->hTIFF,
roentgen b75cab
				    iTileX * psCache->nBlockXSize,
roentgen b75cab
				    iTileY * psCache->nBlockYSize,
roentgen b75cab
				    0, 0 );
roentgen b75cab
				TIFFWriteEncodedTile( psCache->hTIFF, nTileID,
roentgen b75cab
				    pabyData,
roentgen b75cab
				    TIFFTileSize(psCache->hTIFF) );
roentgen b75cab
			}
roentgen b75cab
			else
roentgen b75cab
			{
roentgen b75cab
				nTileID = TIFFComputeStrip( psCache->hTIFF,
roentgen b75cab
				    iTileY * psCache->nBlockYSize,
roentgen b75cab
				    0 );
roentgen b75cab
				RowsInStrip=psCache->nBlockYSize;
roentgen b75cab
				if ((iTileY+1)*psCache->nBlockYSize>psCache->nYSize)
roentgen b75cab
					RowsInStrip=psCache->nYSize-iTileY*psCache->nBlockYSize;
roentgen b75cab
				TIFFWriteEncodedStrip( psCache->hTIFF, nTileID,
roentgen b75cab
				    pabyData,
roentgen b75cab
				    TIFFVStripSize(psCache->hTIFF,RowsInStrip) );
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	/* TODO: add checks on error status return of TIFFWriteEncodedTile and TIFFWriteEncodedStrip */
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Rotate buffers.                                                 */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    pabyData = psCache->pabyRow1Blocks;
roentgen b75cab
    psCache->pabyRow1Blocks = psCache->pabyRow2Blocks;
roentgen b75cab
    psCache->pabyRow2Blocks = pabyData;
roentgen b75cab
roentgen b75cab
    _TIFFmemset( pabyData, 0, psCache->nBytesPerRow );
roentgen b75cab
roentgen b75cab
    psCache->nBlockOffset++;
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Restore access to original directory.                           */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    TIFFFlush( psCache->hTIFF );
roentgen b75cab
    /* TODO: add checks on error status return of TIFFFlush */
roentgen b75cab
    TIFFSetSubDirectory( psCache->hTIFF, nBaseDirOffset );
roentgen b75cab
    /* TODO: add checks on error status return of TIFFSetSubDirectory */
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                          TIFFGetOvrBlock()                           */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
/* TODO: make TIFF_Downsample handle iSample offset, so that we can
roentgen b75cab
 * do with a single TIFFGetOvrBlock and no longer need TIFFGetOvrBlock_Subsampled */
roentgen b75cab
unsigned char *TIFFGetOvrBlock( TIFFOvrCache *psCache, int iTileX, int iTileY,
roentgen b75cab
                                int iSample )
roentgen b75cab
roentgen b75cab
{
roentgen b75cab
    int		nRowOffset;
roentgen b75cab
roentgen b75cab
    if( iTileY > psCache->nBlockOffset + 1 )
roentgen b75cab
        TIFFWriteOvrRow( psCache );
roentgen b75cab
roentgen b75cab
    assert( iTileX >= 0 && iTileX < psCache->nBlocksPerRow );
roentgen b75cab
    assert( iTileY >= 0 && iTileY < psCache->nBlocksPerColumn );
roentgen b75cab
    assert( iTileY >= psCache->nBlockOffset
roentgen b75cab
            && iTileY < psCache->nBlockOffset+2 );
roentgen b75cab
    assert( iSample >= 0 && iSample < psCache->nSamples );
roentgen b75cab
roentgen b75cab
    if (psCache->nPlanarConfig == PLANARCONFIG_SEPARATE)
roentgen b75cab
        nRowOffset = ((iTileX * psCache->nSamples) + iSample)
roentgen b75cab
            * psCache->nBytesPerBlock;
roentgen b75cab
    else
roentgen b75cab
        nRowOffset = iTileX * psCache->nBytesPerBlock +
roentgen b75cab
            (psCache->nBitsPerPixel + 7) / 8 * iSample;
roentgen b75cab
roentgen b75cab
    if( iTileY == psCache->nBlockOffset )
roentgen b75cab
        return psCache->pabyRow1Blocks + nRowOffset;
roentgen b75cab
    else
roentgen b75cab
        return psCache->pabyRow2Blocks + nRowOffset;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                     TIFFGetOvrBlock_Subsampled()                     */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
unsigned char *TIFFGetOvrBlock_Subsampled( TIFFOvrCache *psCache, 
roentgen b75cab
                                           int iTileX, int iTileY )
roentgen b75cab
roentgen b75cab
{
roentgen b75cab
    int		nRowOffset;
roentgen b75cab
roentgen b75cab
    if( iTileY > psCache->nBlockOffset + 1 )
roentgen b75cab
        TIFFWriteOvrRow( psCache );
roentgen b75cab
roentgen b75cab
    assert( iTileX >= 0 && iTileX < psCache->nBlocksPerRow );
roentgen b75cab
    assert( iTileY >= 0 && iTileY < psCache->nBlocksPerColumn );
roentgen b75cab
    assert( iTileY >= psCache->nBlockOffset
roentgen b75cab
            && iTileY < psCache->nBlockOffset+2 );
roentgen b75cab
    assert( psCache->nPlanarConfig != PLANARCONFIG_SEPARATE );
roentgen b75cab
roentgen b75cab
    nRowOffset = iTileX * psCache->nBytesPerBlock;
roentgen b75cab
roentgen b75cab
    if( iTileY == psCache->nBlockOffset )
roentgen b75cab
        return psCache->pabyRow1Blocks + nRowOffset;
roentgen b75cab
    else
roentgen b75cab
        return psCache->pabyRow2Blocks + nRowOffset;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                        TIFFDestroyOvrCache()                         */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
void TIFFDestroyOvrCache( TIFFOvrCache * psCache )
roentgen b75cab
roentgen b75cab
{
roentgen b75cab
    while( psCache->nBlockOffset < psCache->nBlocksPerColumn )
roentgen b75cab
        TIFFWriteOvrRow( psCache );
roentgen b75cab
roentgen b75cab
    _TIFFfree( psCache->pabyRow1Blocks );
roentgen b75cab
    _TIFFfree( psCache->pabyRow2Blocks );
roentgen b75cab
    _TIFFfree( psCache );
roentgen b75cab
}
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
 */