⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tif_overview.c

📁 TIFF文件格式读取及生成的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************** * tif_overview.c,v 1.9 2005/05/25 09:03:16 dron Exp * * Project:  TIFF Overview Builder * Purpose:  Library function for building overviews in a TIFF file. * Author:   Frank Warmerdam, warmerdam@pobox.com * * 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. ****************************************************************************** */#include <stdio.h>#include <assert.h>#include <stdlib.h>#include <string.h>#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)#endifvoid TIFFBuildOverviews( TIFF *, int, int *, int, const char *,                         int (*)(double,void*), void * );/************************************************************************//*                         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.                                             *//************************************************************************/uint32 TIFF_WriteOverview( TIFF *hTIFF, int nXSize, int nYSize,                           int nBitsPerPixel, int nPlanarConfig, int nSamples,                            int nBlockXSize, int nBlockYSize,                           int bTiled, int nCompressFlag, int nPhotometric,                           int nSampleFormat,                           unsigned short *panRed,                           unsigned short *panGreen,                           unsigned short *panBlue,                           int bUseSubIFDs ){    uint32	nBaseDirOffset;    uint32	nOffset;    nBaseDirOffset = TIFFCurrentDirOffset( hTIFF );    TIFFCreateDirectory( hTIFF );    /* -------------------------------------------------------------------- *//*      Setup TIFF fields.                                              *//* -------------------------------------------------------------------- */    TIFFSetField( hTIFF, TIFFTAG_IMAGEWIDTH, nXSize );    TIFFSetField( hTIFF, TIFFTAG_IMAGELENGTH, nYSize );    if( nSamples == 1 )        TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );    else        TIFFSetField( hTIFF, TIFFTAG_PLANARCONFIG, nPlanarConfig );    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 );    /* -------------------------------------------------------------------- *//*	Write color table if one is present.				*//* -------------------------------------------------------------------- */    if( panRed != NULL )    {        TIFFSetField( hTIFF, TIFFTAG_COLORMAP, panRed, panGreen, panBlue );    }/* -------------------------------------------------------------------- *//*      Write directory, and return byte offset.                        *//* -------------------------------------------------------------------- */    if( TIFFWriteCheck( hTIFF, bTiled, "TIFFBuildOverviews" ) == 0 )        return 0;    TIFFWriteDirectory( hTIFF );    TIFFSetDirectory( hTIFF, (tdir_t) (TIFFNumberOfDirectories(hTIFF)-1) );    nOffset = TIFFCurrentDirOffset( hTIFF );    TIFFSetSubDirectory( hTIFF, nBaseDirOffset );    return nOffset;}/************************************************************************//*                       TIFF_GetSourceSamples()                        *//************************************************************************/static void TIFF_GetSourceSamples( double * padfSamples, unsigned char *pabySrc,                        int nPixelBytes, int nSampleFormat,                        int nXSize, int nYSize,                        int nPixelOffset, int nLineOffset ){    int  iXOff, iYOff, iSample;    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;    }}/************************************************************************//*                          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 nSampleFormat, const char * pszResampling ){    int		i, j, k, nPixelBytes = (nBitsPerPixel) / 8;    int		nPixelGroupBytes = (nBitsPerPixel+nPixelSkewBits)/8;    unsigned char *pabySrc, *pabyDst;    double      *padfSamples;    assert( nBitsPerPixel >= 8 );    padfSamples = (double *) malloc(sizeof(double) * nOMult * nOMult);/* ==================================================================== *//*      Loop over scanline chunks to process, establishing where the    *//*      data is going.                                                  *//* ==================================================================== */    for( j = 0; j*nOMult < nBlockYSize; j++ )    {        if( j + nTYOff >= nOBlockYSize )            break;                    pabyDst = pabyOTile + ((j+nTYOff)*nOBlockXSize + nTXOff)            * nPixelBytes * nPixelGroupBytes;/* -------------------------------------------------------------------- *//*      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 )        {            pabySrc = pabySrcTile + j*nOMult*nBlockXSize * nPixelGroupBytes;            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[k] = pabySrc[k];                pabyDst += nPixelBytes * nPixelGroupBytes;                pabySrc += nOMult * nPixelGroupBytes;            }        }/* -------------------------------------------------------------------- *//*      Handle the case of averaging.  For this we also have to         *//*      handle each sample format we are concerned with.                *//* -------------------------------------------------------------------- */        else if( strncmp(pszResampling,"averag",6) == 0                  || strncmp(pszResampling,"AVERAG",6) == 0 )        {            pabySrc = pabySrcTile + j*nOMult*nBlockXSize * nPixelGroupBytes;            for( i = 0; i*nOMult < nBlockXSize; i++ )            {                double   dfTotal;                int      iSample;                int      nXSize, nYSize;                if( i + nTXOff >= nOBlockXSize )                    break;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -