📄 tif_overview.cpp
字号:
/****************************************************************************** * $Id: tif_overview.cpp,v 1.1 1999/11/29 21:33:22 warmerda Exp $ * * Project: TIFF Overview Builder * Purpose: Library function for building overviews in a TIFF file. * Author: Frank Warmerdam, warmerda@home.com * * Notes: * o This module uses the RawBlockedImage class to hold the overviews as * they are being built since we can't easily be reading from one directory * in a TIFF file, and writing to a bunch of others. * * o RawBlockedImage will create temporary files in the current directory * to cache the overviews so it doesn't have to hold them all in memory. * If the application crashes these will not be deleted (*.rbi). * * 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. ****************************************************************************** * * $Log: tif_overview.cpp,v $ * Revision 1.1 1999/11/29 21:33:22 warmerda * New * * Revision 1.1 1999/08/17 01:47:59 warmerda * New * * Revision 1.7 1999/03/12 17:47:26 warmerda * made independent of CPL * * Revision 1.6 1999/02/24 16:24:00 warmerda * Don't include cpl_string.h * * Revision 1.5 1999/02/11 22:27:12 warmerda * Added multi-sample support * * Revision 1.4 1999/02/11 19:23:39 warmerda * Only fix on multiples of 16 in block size if it is a tiled file. * * Revision 1.3 1999/02/11 19:21:14 warmerda * Limit tile sizes to multiples of 16 * * Revision 1.2 1999/02/11 18:37:43 warmerda * Removed debugging malloc stuff. * * Revision 1.1 1999/02/11 18:12:30 warmerda * New * */#include <stdio.h>#include <assert.h>#include <stdlib.h>#include "tiffio.h"#include "rawblockedimage.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)#endifextern "C" { void TIFFBuildOverviews( const char *, int, int *, int );}/************************************************************************//* TIFF_WriteOverview() *//************************************************************************/staticvoid TIFF_WriteOverview( TIFF *hTIFF, int nSamples, RawBlockedImage **papoRBI, int bTiled, int nCompressFlag, int nPhotometric, unsigned short *panRed, unsigned short *panGreen, unsigned short *panBlue, int bUseSubIFDs ){ int iSample; RawBlockedImage *poRBI = papoRBI[0]; /* -------------------------------------------------------------------- *//* Setup TIFF fields. *//* -------------------------------------------------------------------- */ TIFFSetField( hTIFF, TIFFTAG_IMAGEWIDTH, poRBI->GetXSize() ); TIFFSetField( hTIFF, TIFFTAG_IMAGELENGTH, poRBI->GetYSize() ); TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_SEPARATE ); TIFFSetField( hTIFF, TIFFTAG_BITSPERSAMPLE, poRBI->GetBitsPerPixel() ); TIFFSetField( hTIFF, TIFFTAG_SAMPLESPERPIXEL, nSamples ); TIFFSetField( hTIFF, TIFFTAG_COMPRESSION, nCompressFlag ); TIFFSetField( hTIFF, TIFFTAG_PHOTOMETRIC, nPhotometric ); if( bTiled ) { TIFFSetField( hTIFF, TIFFTAG_TILEWIDTH, poRBI->GetBlockXSize() ); TIFFSetField( hTIFF, TIFFTAG_TILELENGTH, poRBI->GetBlockYSize() ); } else TIFFSetField( hTIFF, TIFFTAG_ROWSPERSTRIP, poRBI->GetBlockYSize() ); TIFFSetField( hTIFF, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE ); /* -------------------------------------------------------------------- *//* Write color table if one is present. *//* -------------------------------------------------------------------- */ if( panRed != NULL ) { TIFFSetField( hTIFF, TIFFTAG_COLORMAP, panRed, panGreen, panBlue ); }/* -------------------------------------------------------------------- *//* Write blocks to TIFF file. *//* -------------------------------------------------------------------- */ for( iSample = 0; iSample < nSamples; iSample++ ) { int iTileX, iTileY; poRBI = papoRBI[iSample]; for( iTileY = 0; iTileY*poRBI->GetBlockYSize() < poRBI->GetYSize(); iTileY++ ) { for( iTileX = 0; iTileX*poRBI->GetBlockXSize() < poRBI->GetXSize(); iTileX++ ) { unsigned char *pabyData = poRBI->GetTile( iTileX, iTileY ); int nTileID; if( bTiled ) { nTileID = TIFFComputeTile(hTIFF, iTileX * poRBI->GetBlockXSize(), iTileY * poRBI->GetBlockYSize(), 0, iSample ); TIFFWriteEncodedTile( hTIFF, nTileID, pabyData, TIFFTileSize(hTIFF) ); } else { nTileID = TIFFComputeStrip(hTIFF, iTileY*poRBI->GetBlockYSize(), iSample); TIFFWriteEncodedStrip( hTIFF, nTileID, pabyData, TIFFStripSize( hTIFF ) ); } } } } TIFFWriteDirectory( hTIFF );}/************************************************************************//* TIFF_DownSample() *//* *//* Down sample a tile of full res data into a window of a tile *//* of downsampled data. *//************************************************************************/staticvoid TIFF_DownSample( unsigned char *pabySrcTile, int nBlockXSize, int nBlockYSize, int nPixelSkewBits, int nBitsPerPixel, unsigned char * pabyOTile, int nOBlockXSize, int nOBlockYSize, int nTXOff, int nTYOff, int nOMult ){ int i, j, k, nPixelBytes = (nBitsPerPixel) / 8; int nPixelGroupBytes = (nBitsPerPixel+nPixelSkewBits)/8; unsigned char *pabySrc, *pabyDst; assert( nBitsPerPixel >= 8 );/* -------------------------------------------------------------------- *//* Handle case of one or more whole bytes per sample. *//* -------------------------------------------------------------------- */ for( j = 0; j*nOMult < nBlockYSize; j++ ) { if( j + nTYOff >= nOBlockYSize ) break; pabySrc = pabySrcTile + j*nOMult*nBlockXSize * nPixelGroupBytes; pabyDst = pabyOTile + ((j+nTYOff)*nOBlockXSize + nTXOff) * nPixelBytes; for( i = 0; i*nOMult < nBlockXSize; i++ ) { if( i + nTXOff >= nOBlockXSize ) break; /* * For now use simple subsampling, from the top left corner * of the source block of pixels. */ for( k = 0; k < nPixelBytes; k++ ) { *(pabyDst++) = pabySrc[k]; } pabySrc += nOMult * nPixelGroupBytes; } }}/************************************************************************//* TIFF_ProcessFullResBlock() *//* *//* Process one block of full res data, downsampling into each *//* of the overviews. *//************************************************************************/void TIFF_ProcessFullResBlock( TIFF *hTIFF, int nPlanarConfig, int nOverviews, int * panOvList, int nBitsPerPixel, int nSamples, RawBlockedImage ** papoRawBIs, int nSXOff, int nSYOff, unsigned char *pabySrcTile, int nBlockXSize, int nBlockYSize ){ 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,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -