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

📄 dim_psia_format_io.cpp

📁 Digital Notebook Source Code v1.1.0 [
💻 CPP
字号:
/*****************************************************************************
  TIFF PSIA IO 
  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
    10/10/2005 16:23 - image allocation fixed
        
  Ver : 2
*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "memio.h"
#include "dim_tiff_format.h"

//----------------------------------------------------------------------------
// PSIA MISC FUNCTIONS
//----------------------------------------------------------------------------

bool psiaIsTiffValid(TDimTiffParams *tiffParams)
{
  if (tiffParams == NULL) return FALSE;

  if (isTagPresentInFirstIFD( &tiffParams->ifds, 50434 ) == TRUE) return TRUE;
  if (isTagPresentInFirstIFD( &tiffParams->ifds, 50435 ) == TRUE) return TRUE;

  return FALSE;
}

void wstr2charcpy (char *trg, char *src, unsigned int n)
{
  unsigned int i;

  for (i=0; i<n; i++)
    //if (src[i*2] == '\0') return;
    //else
      trg[i] = src[i*2];
}

int psiaGetInfo (TDimTiffParams *tiffParams)
{
  if (tiffParams == NULL) return 1;
  if (tiffParams->dimTiff == NULL) return 1;
  if (tiffParams->ifds.count <= 0) return 1;

  DIM_UCHAR *buf = NULL;
  uint32 size, type;
  psiaInfoHeader *psiaInfo = &tiffParams->psiaInfo;

  if (!isTagPresentInFirstIFD( &tiffParams->ifds, 50435 )) return 1;

  readTiffTag (tiffParams->dimTiff, &tiffParams->ifds.ifds[0], 50435, size, type, &buf);
  if ( (size <= 0) || (buf == NULL) ) return 1;

  psiaInfo->dfLPFStrength = * (double *)     (buf + DIM_PSIA_OFFSET_LPFSSTRENGTH); 
  psiaInfo->bAutoFlatten  = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_AUTOFLATTEN);      
  psiaInfo->bACTrack      = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_ACTRACK);          
  psiaInfo->nWidth        = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_WIDTH);            
  psiaInfo->nHeight       = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_HEIGHT);           
  psiaInfo->dfAngle       = * (double *)     (buf + DIM_PSIA_OFFSET_ANGLE);
  psiaInfo->bSineScan     = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_SINESCAN);         
  psiaInfo->dfOverScan    = * (double *)     (buf + DIM_PSIA_OFFSET_OVERSCAN);        
  psiaInfo->bFastScanDir  = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_FASTSCANDIR);      
  psiaInfo->bSlowScanDir  = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_SLOWSCANDIR);      
  psiaInfo->bXYSwap       = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_XYSWAP);           
  psiaInfo->dfXScanSize   = * (double *)     (buf + DIM_PSIA_OFFSET_XSCANSIZE);       
  psiaInfo->dfYScanSize   = * (double *)     (buf + DIM_PSIA_OFFSET_YSCANSIZE);
  psiaInfo->dfXOffset     = * (double *)     (buf + DIM_PSIA_OFFSET_XOFFSET);         
  psiaInfo->dfYOffset     = * (double *)     (buf + DIM_PSIA_OFFSET_YOFFSET);
  psiaInfo->dfScanRate    = * (double *)     (buf + DIM_PSIA_OFFSET_SCANRATE);        
  psiaInfo->dfSetPoint    = * (double *)     (buf + DIM_PSIA_OFFSET_SETPOINT);        
  psiaInfo->dtTipBias     = * (double *)     (buf + DIM_PSIA_OFFSET_TIPBIAS);         
  psiaInfo->dfSampleBias  = * (double *)     (buf + DIM_PSIA_OFFSET_SAMPLEBIAS);      
  psiaInfo->dfDataGain    = * (double *)     (buf + DIM_PSIA_OFFSET_DATAGAIN);        
  psiaInfo->dfZScale      = * (double *)     (buf + DIM_PSIA_OFFSET_ZSCALE);          
  psiaInfo->dfZOffset     = * (double *)     (buf + DIM_PSIA_OFFSET_ZOFFSET);         
  psiaInfo->nDataMin      = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_DATAMIN);
  psiaInfo->nDataMax      = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_DATAMAX);
  psiaInfo->nDataAvg      = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_DATAAVG);
  psiaInfo->ncompression  = * (DIM_UINT32 *) (buf + DIM_PSIA_OFFSET_NCOMPRESSION);

  // if running the MSB machine (motorola, power pc) then swap
  //if (swabflag) 
  if (bigendian)
  {
    TIFFSwabDouble ( &psiaInfo->dfLPFStrength );  
    TIFFSwabLong   ( (uint32 *) &psiaInfo->bAutoFlatten );
    TIFFSwabLong   ( (uint32 *) &psiaInfo->bACTrack );
    TIFFSwabLong   ( (uint32 *) &psiaInfo->nWidth );
    TIFFSwabLong   ( (uint32 *) &psiaInfo->nHeight );
    TIFFSwabDouble ( &psiaInfo->dfAngle );  
    TIFFSwabLong   ( (uint32 *) &psiaInfo->bSineScan );
    TIFFSwabDouble ( &psiaInfo->dfOverScan );  
    TIFFSwabLong   ( (uint32 *) &psiaInfo->bFastScanDir ); 
    TIFFSwabLong   ( (uint32 *) &psiaInfo->bSlowScanDir );
    TIFFSwabLong   ( (uint32 *) &psiaInfo->bXYSwap );  
    TIFFSwabDouble ( &psiaInfo->dfXScanSize ); 
    TIFFSwabDouble ( &psiaInfo->dfYScanSize ); 
    TIFFSwabDouble ( &psiaInfo->dfXOffset ); 
    TIFFSwabDouble ( &psiaInfo->dfYOffset );   
    TIFFSwabDouble ( &psiaInfo->dfScanRate );
    TIFFSwabDouble ( &psiaInfo->dfSetPoint ); 
    TIFFSwabDouble ( &psiaInfo->dtTipBias ); 
    TIFFSwabDouble ( &psiaInfo->dfSampleBias );   
    TIFFSwabDouble ( &psiaInfo->dfZScale ); 
    TIFFSwabDouble ( &psiaInfo->dfZOffset ); 
    TIFFSwabLong   ( (uint32 *) &psiaInfo->nDataMin );
    TIFFSwabLong   ( (uint32 *) &psiaInfo->nDataMax );
    TIFFSwabLong   ( (uint32 *) &psiaInfo->nDataAvg );
    TIFFSwabLong   ( (uint32 *) &psiaInfo->ncompression );
  }

  wstr2charcpy (psiaInfo->szSourceNameW,   (char *) (buf + DIM_PSIA_OFFSET_SOURCENAME), 32);
  wstr2charcpy (psiaInfo->szImageModeW,    (char *) (buf + DIM_PSIA_OFFSET_IMAGEMODE), 8);
  wstr2charcpy (psiaInfo->szSetPointUnitW, (char *) (buf + DIM_PSIA_OFFSET_SETPOINTUNIT), 8);
  wstr2charcpy (psiaInfo->szUnitW,         (char *) (buf + DIM_PSIA_OFFSET_UNIT), 8);

  freeTiffTagBuf( &buf );

  return 0;
}


//----------------------------------------------------------------------------
// READ/WRITE FUNCTIONS
//----------------------------------------------------------------------------

DIM_UINT psiaReadPlane(TDimFormatHandle *fmtHndl, TDimTiffParams *tiffParams, int plane, TDimImageBitmap *img)
{
  
  if (tiffParams == 0) return 1;
  if (img        == 0) return 1;
  if (tiffParams->dimTiff == 0) return 1;
  
  DIM_UINT sample = 0;
  DIM_UCHAR *buf = NULL;
  uint32 size, type;
  register unsigned int y;
  DIM_UCHAR *p, *p2;

  img->i.depth   = 16;
  img->i.pixelType = DIM_TAG_SHORT;
  img->i.samples = 1;
  img->i.width   = tiffParams->psiaInfo.nWidth;
  img->i.height  = tiffParams->psiaInfo.nHeight;
  if ( allocImg( fmtHndl, &img->i, img) != 0 ) return 1;

  //--------------------------------------------------------------------
  // read actual image 
  //--------------------------------------------------------------------
  size = getImgSizeInBytes( img );
  readTiffTag (tiffParams->dimTiff, &tiffParams->ifds.ifds[0], 50434, size, type, &buf);
  if ( (size <= 0) || (buf == NULL) ) return 1;

  
  DIM_UINT32 line_size = img->i.width*2;
  p = ((DIM_UCHAR *) img->bits[0]) + (line_size * (img->i.height-1) );
  p2 = buf;
  for (y=0; y<img->i.height; y++ )
  {
    _TIFFmemcpy(p, p2, line_size);
    p  -= line_size;
    p2 += line_size;
  }

  if (bigendian) //if (swabflag) 
    TIFFSwabArrayOfShort( (DIM_UINT16 *) img->bits[0], size/2 );
  freeTiffTagBuf( &buf );

  // psia data is stored inverted
  invertImg( img );

  return 0;
}

//----------------------------------------------------------------------------
// METADATA TEXT FUNCTIONS
//----------------------------------------------------------------------------

void psiaWriteTextMeta(TDimTiffParams *tiffParams, MemIOBuf *outIOBuf)
{
  if (tiffParams == NULL) return;
  if (tiffParams->subType != tstPsia) return; 
  psiaInfoHeader *meta = &tiffParams->psiaInfo;
  char text[1024];

  sprintf(text, "[PSIA Metadata]\n\n");
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Source:          %s\n\n", meta->szSourceNameW);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );
  
  sprintf(text, "Head Mode:       %s\n\n", meta->szImageModeW);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Low Pass Filter: %.2f\n\n", meta->dfLPFStrength);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  if (meta->bAutoFlatten == 0)
    sprintf(text, "Auto Flatten:    Off\n\n");
  else
    sprintf(text, "Auto Flatten:    On\n\n");
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  if (meta->bACTrack == 0)
    sprintf(text, "AC Track:        Off\n\n");
  else
    sprintf(text, "AC Track:        On\n\n");
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Data Width:      %d (pixels)\n\n", meta->nWidth);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Data Height:     %d (pixels)\n\n", meta->nHeight);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Rotation:        %.2f (deg)\n\n", meta->dfAngle);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  if (meta->bSineScan == 0)  
    sprintf(text, "Sine Scan:       Off\n\n");
  else
    sprintf(text, "Sine Scan:       On\n\n");
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Over Scan:       %.2f (%%)\n\n", meta->dfOverScan);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );


  if (meta->bFastScanDir == 0)    
    sprintf(text, "Fast Scan Dir:   Right to Left\n\n");
  else
    sprintf(text, "Fast Scan Dir:   Left to Right\n\n");
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );


  if (meta->bSlowScanDir == 0) 
    sprintf(text, "Slow Scan Dir:   Top to Bototm\n\n");
  else
    sprintf(text, "Slow Scan Dir:   Bottom to Top\n\n");
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );


  if (meta->bXYSwap == 0)
    sprintf(text, "Fast Scan Axis:  X\n\n");
  else
    sprintf(text, "Fast Scan Axis:  Y\n\n");
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );


  sprintf(text, "X Scan Size:     %.2f (%s)\n\n", meta->dfXScanSize, meta->szUnitW);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Y Scan Size:     %.2f (%s)\n\n", meta->dfYScanSize, meta->szUnitW);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "X Scan Offset:   %.2f (%s)\n\n", meta->dfXOffset, meta->szUnitW);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Y Scan Offset:   %.2f (%s)\n\n", meta->dfYOffset, meta->szUnitW);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Scan Rate:       %.2f (Hz)\n\n", meta->dfScanRate);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Set Point:       %.4f (%s)\n\n", meta->dfSetPoint, meta->szSetPointUnitW);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  //sprintf(text, "SetPointUnitW: %s\n\n", meta->szSetPointUnitW);
  //MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Tip Bias:        %.2f (V)\n\n", meta->dtTipBias);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Sample Bias:     %.2f (V)\n\n", meta->dfSampleBias);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Data Gain:       %.4E (%s/step)\n\n", meta->dfDataGain, meta->szUnitW);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  double dval = meta->nDataMin * meta->dfDataGain;
  sprintf(text, "Data Min:        %.4G (%s)\n\n", dval, meta->szUnitW );
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  dval = meta->nDataMax * meta->dfDataGain;
  sprintf(text, "Data Max:        %.4G (%s)\n\n", dval, meta->szUnitW );
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Data Avg:        %d\n\n", meta->nDataAvg);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Z Scale:         %.2f\n\n", meta->dfZScale);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "Z Offset:        %.2f\n\n", meta->dfZOffset);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  //sprintf(text, "UnitW: %s\n\n", meta->szUnitW);
  //MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "NCompression:    %d\n\n", meta->ncompression);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );


  if (tiffParams == NULL) return;
  if (tiffParams->dimTiff == NULL) return;
  if (tiffParams->ifds.count <= 0) return;

  unsigned char *buf = NULL;
  uint32 size, type;

  if (!isTagPresentInFirstIFD( &tiffParams->ifds, 50436 )) return;
  readTiffTag (tiffParams->dimTiff, &tiffParams->ifds.ifds[0], 50436, size, type, &buf);
  if ( (size <= 0) || (buf == NULL) ) return;

  //MemIO_WriteProc( (thandle_t) outIOBuf, buf, size-1 );   
  sprintf(text, "Comments: %s\n\n", buf );
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );


  freeTiffTagBuf( &buf );

}


















⌨️ 快捷键说明

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