📄 dim_tiff_format.cpp
字号:
/*****************************************************************************
TIFF support
Copyright (c) 2004 by Dmitry V. Fedorov <www.dimin.net> <dima@dimin.net>
IMPLEMENTATION
Programmer: Dima V. Fedorov <mailto:dima@dimin.net> <http://www.dimin.net/>
History:
03/29/2004 22:23 - First creation
Ver : 2
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dim_xtiffio.h"
#include "dim_tiff_format.h"
void getCurrentPageInfo(TDimTiffParams *tiffParams);
void getImageInfo(TDimTiffParams *tiffParams);
TDimImageInfo initTDimImageInfo();
void* DimMalloc(DIM_ULONG size);
void* DimFree(void *p);
#include "dim_tiff_format_io.cpp"
//****************************************************************************
// STATIC FUNCTIONS THAT MIGHT BE PROVIDED BY HOST AND CALLING STUBS FOR THEM
//****************************************************************************
static TDimProgressProc hostProgressProc = NULL;
static TDimErrorProc hostErrorProc = NULL;
static TDimTestAbortProc hostTestAbortProc = NULL;
static TDimMallocProc hostMallocProc = NULL;
static TDimFreeProc hostFreeProc = NULL;
static void localTiffProgressProc(long done, long total, char *descr)
{
if (hostProgressProc != NULL) hostProgressProc( done, total, descr );
}
static void localTiffErrorProc(int val, char *descr)
{
if (hostErrorProc != NULL) hostErrorProc( val, descr );
}
static bool localTiffTestAbortProc ( void )
{
if (hostTestAbortProc != NULL)
{
if (hostTestAbortProc() == 1) return true; else return false;
}
else
return FALSE;
}
static void* localTiffMallocProc (DIM_ULONG size)
{
if (hostMallocProc != NULL)
return hostMallocProc( size );
else
{
//void *p = (void *) new char[size];
void *p = (void *) _TIFFmalloc(size);
return p;
}
}
static void* localTiffFreeProc (void *p)
{
if (hostFreeProc != NULL)
return hostFreeProc( p );
else
{
//unsigned char *pu = (unsigned char*) p;
//if (p != NULL) delete pu;
if (p != NULL) _TIFFfree( p );
return NULL;
}
}
static void setLocalTiffFunctions ( TDimFormatHandle *fmtHndl )
{
hostProgressProc = fmtHndl->showProgressProc;
hostErrorProc = fmtHndl->showErrorProc;
hostTestAbortProc = fmtHndl->testAbortProc;
hostMallocProc = fmtHndl->mallocProc;
hostFreeProc = fmtHndl->freeProc;
}
static void resetLocalTiffFunctions ( )
{
hostProgressProc = NULL;
hostErrorProc = NULL;
hostTestAbortProc = NULL;
hostMallocProc = NULL;
hostFreeProc = NULL;
}
void* DimMalloc(DIM_ULONG size)
{
return localTiffMallocProc ( size );
}
void* DimFree(void *p)
{
return localTiffFreeProc (p);
}
//****************************************************************************
// CALLBACKS
//****************************************************************************
static tsize_t tiff_read(thandle_t handle, tdata_t data, tsize_t size)
{
TDimFormatHandle *fmtHndl = (TDimFormatHandle *) handle;
//if ( fmtHndl->io_mode != DIM_IO_READ ) return 0;
return (tsize_t) dimRead( fmtHndl, data, 1, size );
}
static tsize_t tiff_write(thandle_t handle,tdata_t data,tsize_t size)
{
TDimFormatHandle *fmtHndl = (TDimFormatHandle *) handle;
if ( fmtHndl->io_mode != DIM_IO_WRITE ) return 0;
tsize_t res = (tsize_t) dimWrite( fmtHndl, data, 1, size );
return res;
//return size;
}
static toff_t tiff_seek(thandle_t handle, toff_t offset, int whence)
{
TDimFormatHandle *fmtHndl = (TDimFormatHandle *) handle;
return (tsize_t) dimSeek( fmtHndl, offset, whence );
}
static int tiff_close(thandle_t handle) {
TDimFormatHandle *fmtHndl = (TDimFormatHandle *) handle;
dimFlush( fmtHndl );
return dimClose( fmtHndl );
}
static toff_t tiff_size(thandle_t handle) {
TDimFormatHandle *fmtHndl = (TDimFormatHandle *) handle;
return dimSize( fmtHndl );
}
static int tiff_mmap(thandle_t handle,tdata_t* data,toff_t* size) {
handle=handle; data=data; size=size;
return 1;
}
static void tiff_unmap(thandle_t handle, tdata_t data, toff_t size) {
handle=handle; data=data; size=size;
}
//****************************************************************************
//
// FORMAT DEMANDED FUNTIONS
//
//****************************************************************************
//----------------------------------------------------------------------------
// UTILITARY FUNCTIONS
//----------------------------------------------------------------------------
unsigned int tiffGetNumberOfPages( TDimTiffParams *tiffpar )
{
unsigned int i=0;
TIFF *tif = tiffpar->dimTiff;
if (tif == NULL) return i;
// if STK then get number of pages in special way
if (tiffpar->subType == tstStk)
{
return stkGetNumPlanes( tif );
}
TIFFSetDirectory(tif, 0);
while (TIFFLastDirectory(tif) == 0)
{
if ( TIFFReadDirectory(tif) == 0) break;
i++;
}
i++;
//if (tiffpar->subType == tstPsia) i =* 2;
return i;
}
void tiffReadResolution( TIFF *tif, DIM_UINT &units, double &xRes, double &yRes)
{
if (tif == NULL) return;
float xresolution = 0;
float yresolution = 0;
short resolutionunit = 0;
units = 0;
xRes = 0;
yRes = 0;
if ( ( TIFFGetField(tif, TIFFTAG_RESOLUTIONUNIT , &resolutionunit) )
&&
( TIFFGetField(tif, TIFFTAG_XRESOLUTION , &xresolution) )
&&
( TIFFGetField(tif, TIFFTAG_YRESOLUTION , &yresolution) )
)
{
units = resolutionunit;
xRes = xresolution;
yRes = yresolution;
}
// here we need to read specific info here to define resolution correctly
}
DIM_UINT getTiffMode( TIFF *tif)
{
if (tif == NULL) return DIM_GRAYSCALE;
uint16 photometric = PHOTOMETRIC_MINISWHITE;
uint16 samplesperpixel = 1;
TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &photometric);
if (photometric == PHOTOMETRIC_RGB) return DIM_RGB;
if (photometric == PHOTOMETRIC_PALETTE) return DIM_INDEXED;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
if (samplesperpixel > 1) return DIM_MULTI;
return DIM_GRAYSCALE;
}
void getCurrentPageInfo(TDimTiffParams *tiffParams)
{
if (tiffParams == NULL) return;
TIFF *tif = tiffParams->dimTiff;
TDimImageInfo *info = &tiffParams->info;
if (tif == NULL) return;
uint32 height = 0;
uint32 width = 0;
uint16 bitspersample = 1;
uint16 samplesperpixel = 1;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
info->width = width;
info->height = height;
info->samples = samplesperpixel;
info->depth = bitspersample;
if (bitspersample != 16)
info->pixelType = DIM_TAG_BYTE;
else
info->pixelType = DIM_TAG_SHORT;
if( !TIFFIsTiled(tif) )
{
info->tileWidth = 0;
info->tileHeight = 0;
}
else
{
uint32 columns, rows;
TIFFGetField(tif, TIFFTAG_TILEWIDTH, &columns);
TIFFGetField(tif, TIFFTAG_TILELENGTH, &rows);
info->tileWidth = columns;
info->tileHeight = rows;
}
info->transparentIndex = 0;
info->transparencyMatting = 0;
if (tiffParams->subType != tstFluoview)
init_image_palette( tif, info );
else
fluoviewInitPalette( tiffParams, info);
info->imageMode = getTiffMode( tif );
tiffReadResolution( tif, info->resUnits, info->xRes, info->yRes);
if ( tiffParams->subType == tstFluoview )
{
TDimFluoviewInfo *fvi = &tiffParams->fluoviewInfo;
info->resUnits = DIM_RES_um;
info->xRes = fvi->xR;
info->yRes = fvi->yR;
}
if ( tiffParams->subType == tstPsia )
{
psiaInfoHeader *meta = &tiffParams->psiaInfo;
info->resUnits = DIM_RES_um;
info->xRes = meta->dfXScanSize / meta->nWidth;
info->yRes = meta->dfYScanSize / meta->nHeight;
}
}
void getImageInfo(TDimTiffParams *tiffParams)
{
if (tiffParams == NULL) return;
TIFF *tif = tiffParams->dimTiff;
TDimImageInfo *info = &tiffParams->info;
if (tif == NULL) return;
info->ver = sizeof(TDimImageInfo);
// read to which tiff sub type image pertence
tiffParams->subType = tstGeneric;
if (stkIsTiffValid( tiffParams ) == TRUE )
{
tiffParams->subType = tstStk;
stkGetInfo( tiffParams );
}
if (psiaIsTiffValid( tiffParams ) == TRUE )
{
tiffParams->subType = tstPsia;
psiaGetInfo ( tiffParams );
}
info->number_pages = tiffGetNumberOfPages( tiffParams );
// we need to get fluoview parameters with the knoweledge of number of pages
if (fluoviewIsTiffValid( tiffParams ) == TRUE )
{
tiffParams->subType = tstFluoview;
fluoviewGetInfo ( tiffParams );
}
// set dimensions
if (info->number_pages > 1)
{
if (tiffParams->subType == tstStk)
{
info->number_dims = 4;
info->dimensions[3].dim = DIM_DIM_Z;
info->number_t = info->number_pages;
}
if (tiffParams->subType == tstFluoview)
{
TDimFluoviewInfo *fvi = &tiffParams->fluoviewInfo;
info->number_z = fvi->z_slices;
info->number_t = fvi->t_frames;
if (fvi->z_slices > 1)
{
info->number_dims = 4;
info->dimensions[3].dim = DIM_DIM_Z;
}
if (fvi->t_frames > 1)
{
info->number_dims = 4;
info->dimensions[3].dim = DIM_DIM_T;
}
if ((fvi->z_slices > 1) && (fvi->t_frames > 1))
{
info->number_dims = 5;
info->dimensions[3].dim = DIM_DIM_Z;
info->dimensions[4].dim = DIM_DIM_T;
}
}
}
getCurrentPageInfo( tiffParams );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -