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

📄 dim_stk_format_io.cpp

📁 Digital Notebook Source Code v1.1.0 [
💻 CPP
📖 第 1 页 / 共 3 页
字号:
  stkInfo->metaData.AutoScaleLoInfo[0]     = 0;
  stkInfo->metaData.AutoScaleLoInfo[1]     = 1;
  stkInfo->metaData.AutoScaleHiInfo[0]     = 0;
  stkInfo->metaData.AutoScaleHiInfo[1]     = 1;
  stkInfo->metaData.Gamma                  = 0;
  stkInfo->metaData.GammaRed               = 0;
  stkInfo->metaData.GammaGreen             = 0;
  stkInfo->metaData.GammaBlue              = 0;
}

void stkClearInfo ( TDimTiffParams *tiffParams )
{
  if (tiffParams == NULL) return;
  if (tiffParams->subType != tstStk) return;

  TDimStkInfo *stkInfo = &tiffParams->stkInfo;

  stkClearOffsets( stkInfo );
 
  if (  stkInfo->metaData.CalibrationUnits != NULL )
    _TIFFfree(stkInfo->metaData.CalibrationUnits);

  if (  stkInfo->metaData.Name != NULL )
    _TIFFfree(stkInfo->metaData.Name);

  if (  stkInfo->metaData.grayUnitName != NULL )
    _TIFFfree(stkInfo->metaData.grayUnitName);

  if (  stkInfo->metaData.StagePositionX != NULL )
    _TIFFfree(stkInfo->metaData.StagePositionX);

  if (  stkInfo->metaData.StagePositionY != NULL )
    _TIFFfree(stkInfo->metaData.StagePositionY);

  if (  stkInfo->metaData.CameraChipOffsetX != NULL )
    _TIFFfree(stkInfo->metaData.CameraChipOffsetX);

  if (  stkInfo->metaData.CameraChipOffsetY != NULL )
    _TIFFfree(stkInfo->metaData.CameraChipOffsetY); 

  if (  stkInfo->metaData.StageLabel != NULL )  
    _TIFFfree(stkInfo->metaData.StageLabel);

  if (  stkInfo->metaData.AbsoluteZ != NULL )
    _TIFFfree(stkInfo->metaData.AbsoluteZ);

  if (  stkInfo->metaData.AbsoluteZValid != NULL )
    _TIFFfree(stkInfo->metaData.AbsoluteZValid);

  if (  stkInfo->metaData.wavelength != NULL )
    _TIFFfree(stkInfo->metaData.wavelength);

  if (  stkInfo->metaData.zDistance != NULL )
    _TIFFfree(stkInfo->metaData.zDistance);

  if (  stkInfo->metaData.creationDate != NULL )
    _TIFFfree(stkInfo->metaData.creationDate);

  if (  stkInfo->metaData.creationTime != NULL )
    _TIFFfree(stkInfo->metaData.creationTime);

  if (  stkInfo->metaData.modificationDate != NULL )
    _TIFFfree(stkInfo->metaData.modificationDate);

  if (  stkInfo->metaData.modificationTime != NULL )
    _TIFFfree(stkInfo->metaData.modificationTime);

  stkInitInfo( stkInfo );
  return;
}


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


DIM_UINT  stkReadPlane(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;

  TIFF *tif = tiffParams->dimTiff;
  uint16 photometric = PHOTOMETRIC_MINISWHITE;
  uint16 compress_tag;
  uint16 bitspersample = 8;
  uint16 samplesperpixel = 1;
  uint32 height = 0; 
  uint32 width = 0;
  toff_t old_pos = tif->tif_curoff;
  uint32 rowsperstrip   = 1;

  (void) TIFFGetFieldDefaulted(tif, TIFFTAG_COMPRESSION, &compress_tag); 
  TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &samplesperpixel);
  TIFFGetField(tif, TIFFTAG_BITSPERSAMPLE, &bitspersample);
  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);  
  TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &rowsperstrip); 


  DIM_UINT Bpp = (unsigned int) ceil( ((double) img->i.depth) / 8.0 );
  long size = img->i.width * img->i.height * Bpp;

  long file_plane_offset = tiffParams->stkInfo.strip_offsets[0] + (size * plane);

  if (compress_tag != COMPRESSION_NONE) 
    file_plane_offset = tiffParams->stkInfo.strip_offsets[plane * tiffParams->stkInfo.strips_per_plane ]; 



  // position file position into begining of the plane
  if (tif->tif_seekproc((thandle_t) tif->tif_fd, file_plane_offset, SEEK_SET) != 0)
  {
    
    //---------------------------------------------------
    // load the buffer with decompressed plane data
    //---------------------------------------------------
  
    if (compress_tag == COMPRESSION_NONE) 
    {
      
      int read_size = tif->tif_readproc((thandle_t) tif->tif_fd, img->bits[sample], size);
      if (read_size != size) return 1;
      // now swap bytes if needed
      tif->tif_postdecode(tif, (tidata_t) img->bits[sample], size); 
    }
    else
    {
      int strip_size = width * Bpp * samplesperpixel * rowsperstrip;
      DIM_UCHAR *buf = (DIM_UCHAR *) _TIFFmalloc( strip_size ); 

      // ---------------------------------------------------
      // let's tweak libtiff and change offsets and bytecounts for correct values for this plane
	    TIFFDirectory *td = &tif->tif_dir;

      uint32 *plane_strip_offsets = tiffParams->stkInfo.strip_offsets + (plane * tiffParams->stkInfo.strips_per_plane);
      //memcpy( td->td_stripoffset, plane_strip_offsets, tiffParams->stkInfo.strips_per_plane * sizeof(uint32) );
      _TIFFmemcpy( td->td_stripoffset, plane_strip_offsets, tiffParams->stkInfo.strips_per_plane * sizeof(uint32) );

      uint32 *plane_strip_bytecounts = tiffParams->stkInfo.strip_bytecounts + (plane * tiffParams->stkInfo.strips_per_plane);
      //memcpy( td->td_stripbytecount, plane_strip_bytecounts, tiffParams->stkInfo.strips_per_plane * sizeof(uint32) );
      _TIFFmemcpy( td->td_stripbytecount, plane_strip_bytecounts, tiffParams->stkInfo.strips_per_plane * sizeof(uint32) );
      // ---------------------------------------------------

      int strip_num = plane * tiffParams->stkInfo.strips_per_plane;
      int strip_offset = 0;
      while (strip_offset < tiffParams->stkInfo.strips_per_plane)
      {
        int read_size = TIFFReadEncodedStrip (tif, strip_offset, (tdata_t) buf, (tsize_t) strip_size);
        if (read_size == -1) return 1;
        //memcpy( ((unsigned char *) img->bits[sample])+strip_offset*strip_size, buf, read_size );
        _TIFFmemcpy( ((unsigned char *) img->bits[sample])+strip_offset*strip_size, buf, read_size );
        ++strip_offset;
      }

      _TIFFfree( buf );  
    }

    // invert it if we got negative
    TIFFGetField(tiffParams->dimTiff, TIFFTAG_PHOTOMETRIC, &photometric);
    if (photometric == PHOTOMETRIC_MINISWHITE)
      invertSample(img, 0);
  }

  tif->tif_seekproc((thandle_t) tif->tif_fd, old_pos, SEEK_SET);

  return 0;
}


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

//----------------------------------------------------------------------------
// META DATA PROC
//----------------------------------------------------------------------------

DIM_UINT stkAddOneTag (TDimFormatHandle *fmtHndl, int tag, const char* str)
{
  DIM_UCHAR *buf = NULL;
  DIM_UINT32 buf_size = strlen(str);
  DIM_UINT32 buf_type = DIM_TAG_ASCII;

  if ( (buf_size == 0) || (str == NULL) ) return 1;
  else
  {
    // now add tag into structure
    TDimTagItem item;

    buf = (unsigned char *) DimMalloc(buf_size + 1);
    strncpy((char *) buf, str, buf_size);
    buf[buf_size] = '\0';

    item.tagGroup  = DIM_META_STK;
    item.tagId     = tag;
    item.tagType   = buf_type;
    item.tagLength = buf_size;
    item.tagData   = buf;

    addMetaTag( &fmtHndl->metaData, item);
  }

  return 0;
}


DIM_UINT stkReadMetaMeta (TDimFormatHandle *fmtHndl, int group, int tag, int type)
{
  if (fmtHndl == NULL) return 1;
  if (fmtHndl->internalParams == NULL) return 1;
  TDimTiffParams *tiffParams = (TDimTiffParams *) fmtHndl->internalParams;
  TDimStkMetaData *meta = &tiffParams->stkInfo.metaData;
  
  if ( (group != DIM_META_STK) && (group != -1) ) return 1;


  // add tag UIC2Tag 33629, some info
  std::string str_uic2 = "";
  char text[1024];

  int i;
  for (i=0; i<meta->N; i++)
  {
    unsigned short y;
    unsigned char m, d;
    unsigned char h, mi, s;

    JulianToYMD(meta->creationDate[i], y, m, d);
    MiliMidnightToHMS(meta->creationTime[i], h, mi, s);

    sprintf(text, "Page %.3d: %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n", i, y, m, d, h, mi, s );
    str_uic2 += text;
  }
 
  if (str_uic2.size() > 0) 
    stkAddOneTag ( fmtHndl, 33629, str_uic2.c_str() );

  return 0;
}

void stkWriteTextMeta(TDimTiffParams *tiffParams, MemIOBuf *outIOBuf)
{
  if (tiffParams == NULL) return;
  if (tiffParams->subType != tstStk) return;
  if (outIOBuf == NULL) return;
  TDimStkMetaData *meta = &tiffParams->stkInfo.metaData;
  char text[1024];

/*
  //long CreateTime[2];
  //long LastSavedTime[2];
*/
  sprintf(text, "[MetaMorph Stack (STK) Fields]\n\n");
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );  
  
  sprintf(text, "Name: %s\n\n", meta->Name);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );
  
  sprintf(text, "AutoScale: %d\n\n", meta->AutoScale);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

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

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

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

  sprintf(text, "XCalibration: %d/%d\n\n", meta->XCalibration[0], meta->XCalibration[1]);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

  sprintf(text, "YCalibration: %d/%d\n\n", meta->YCalibration[0], meta->YCalibration[1]);
  MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 );

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

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

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

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

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

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

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

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

  // new fields
  int i;
  for (i=0; i<meta->N; i++)
  {
    unsigned short y;
    unsigned char m, d;
    unsigned char h, mi, s;

    JulianToYMD(meta->creationDate[i], y, m, d);
    MiliMidnightToHMS(meta->creationTime[i], h, mi, s);

    sprintf(text, "Page %3.3d: %.4d-%.2d-%.2d %.2d:%.2d:%.2d\n\n", i , y, m, d, h, mi, s );

    MemIO_WriteProc( (thandle_t) outIOBuf, text, strlen(text)-1 ); 
  }

}










⌨️ 快捷键说明

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