📄 dim_nanoscope_format_io.cpp
字号:
/*****************************************************************************
NANOSCOPE IO
UCSB/BioITR property
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:
01/10/2005 12:17 - First creation
02/08/2005 22:30 - Support for incomplete image sections
Ver : 2
*****************************************************************************/
#include <string>
#include "dim_nanoscope_format.h"
// Disables Visual Studio 2005 warnings for deprecated code#ifdef WIN32 #pragma warning(disable:4996)#endif
#include <math.h>
#if defined(WIN32)
#include <limits.h>
#endif
#ifndef USHRT_MAX
#define USHRT_MAX 0xffff
#endif
//----------------------------------------------------------------------------
// READ PROC
//----------------------------------------------------------------------------
static int read_nanoscope_image(TDimFormatHandle *fmtHndl)
{
if (fmtHndl == NULL) return 1;
if (fmtHndl->internalParams == NULL) return 1;
TDimNanoscopeParams *nanoPar = (TDimNanoscopeParams *) fmtHndl->internalParams;
TDimImageInfo *info = &nanoPar->i;
if (fmtHndl->stream == NULL) return 1;
TDimNanoscopeImg nimg = nanoPar->imgs.at(0);
if (fmtHndl->pageNumber < nanoPar->imgs.size())
nimg = nanoPar->imgs.at( fmtHndl->pageNumber );
TDimImageBitmap *img = fmtHndl->image;
info->width = nimg.width;
info->height = nimg.height;
info->imageMode = DIM_GRAYSCALE;
info->samples = 1;
info->depth = 16;
unsigned long img_size_bytes = nimg.width * nimg.height * 2;
// init the image
if ( allocImg( fmtHndl, info, img) != 0 ) return 1;
if ( dimSeek(fmtHndl, nimg.data_offset, SEEK_SET) != 0) return 1;
if (dimRead( fmtHndl, img->bits[0], img_size_bytes, 1 ) != 1) return 1;
if ( (img->i.depth == 16) && (dimBigendian) )
dimSwapArrayOfShort((DIM_UINT16*) img->bits[0], img_size_bytes/2);
// now convert from SHORT to USHORT
if (img->i.depth == 16)
{
DIM_INT16 *pS = (DIM_INT16 *) img->bits[0];
DIM_UINT16 *pU = (DIM_UINT16 *) img->bits[0];
unsigned int x;
for (x=0; x<img_size_bytes/2; x++)
pU[x] = (unsigned short) (pS[x] + SHRT_MAX);
}
// now convert from bottom-top to top-bottom
register int y;
int line_size = getLineSizeInBytes(img);
unsigned char *pbuf = new unsigned char [ line_size ];
unsigned char *pt = ((unsigned char*) img->bits[0] ) + (line_size * (nimg.height-1));
unsigned char *pb = (unsigned char*) img->bits[0];
for (y=0; y<floor(info->height/2.0); y++)
{
memcpy( pbuf, pb, line_size );
memcpy( pb, pt, line_size );
memcpy( pt, pbuf, line_size );
pb+=line_size;
pt-=line_size;
}
delete [] pbuf;
return 0;
}
//----------------------------------------------------------------------------
// META DATA PROC
//----------------------------------------------------------------------------
DIM_UINT nanoscope_add_one_tag (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(fmtHndl, buf_size + 1);
strncpy((char *) buf, str, buf_size);
buf[buf_size] = '\0';
item.tagGroup = DIM_META_BIORAD;
item.tagId = tag;
item.tagType = buf_type;
item.tagLength = buf_size;
item.tagData = buf;
addMetaTag(&fmtHndl->metaData, item);
}
return 0;
}
DIM_UINT read_nanoscope_metadata (TDimFormatHandle *fmtHndl, int group, int tag, int type)
{
if (fmtHndl == NULL) return 1;
if (fmtHndl->internalParams == NULL) return 1;
/*
TDimBioRadPicParams *picPar = (TDimBioRadPicParams *) fmtHndl->internalParams;
if ( (group != DIM_META_BIORAD) && (group != -1) ) return 1;
std::string note01 = "";
std::string note20 = "";
std::string note21 = "";
unsigned char note[96];
// print biorad notes into the file
dimSeek(fmtHndl, picPar->notes_offset, SEEK_SET);
while ( dimRead( fmtHndl, note, 1, 96 ) == 96)
{
//int disp_level = * (DIM_INT16 *) (note + 0);
//int is_last = * (DIM_INT32 *) (note + 2);
int note_type = * (DIM_INT16 *) (note + 10);
char *text = (char *) (note + 16);
if (text[0] == '\0') continue;
if (note_type == 01) { note01 += text; note01 += "\n"; }
if (note_type == 20) { note20 += text; note20 += "\n"; }
if (note_type == 21) { note21 += text; note21 += "\n"; }
}
if (note01.size() > 0)
add_one_tag ( fmtHndl, 01, note01.c_str() );
if (note20.size() > 0)
add_one_tag ( fmtHndl, 20, note20.c_str() );
if (note21.size() > 0)
add_one_tag ( fmtHndl, 21, note21.c_str() );
*/
return 0;
group; tag; type;
}
char* read_text_nanoscope_metadata ( TDimFormatHandle *fmtHndl )
{
if (fmtHndl == NULL) return NULL;
if (fmtHndl->internalParams == NULL) return NULL;
TDimNanoscopeParams *nanoPar = (TDimNanoscopeParams *) fmtHndl->internalParams;
TDimNanoscopeImg nimg = nanoPar->imgs.at(0);
if (fmtHndl->pageNumber < nanoPar->imgs.size())
nimg = nanoPar->imgs.at( fmtHndl->pageNumber );
char *buf = NULL;
std::string str = "";
char cstr[1024], *line, *l2, p1[1024], p2[1024];
str += "[Nanoscope page properties]\n";
sprintf( cstr, "Width: %.2f um\n", nimg.width * nimg.xR );
str += cstr;
sprintf( cstr, "Height: %.2f um\n", nimg.height * nimg.yR );
str += cstr;
// Microscope mode
// \@MicroscopeList: S [TMMode] "Tapping"
// \@MicroscopeList: S [AFMMode] "Contact"
line = strstr( (char*) nanoPar->metaText.c_str(), "@MicroscopeList: S [" );
if (line != NULL) {
sscanf( line, "@MicroscopeList: S [%s ""%s""", p1, p2 );
sprintf( cstr, "Microscope mode: %s\n", p2 );
if (cstr != NULL) str += cstr;
}
// Depth
// Deflection
// \Date: 06:32:07 PM Thu Dec 09 2004
line = strstr( (char*) nanoPar->metaText.c_str(), "Date: " );
if (line != NULL) {
l2 = strstr( line, "\n" );
memset( p1, 0, 1024 );
strncpy( p1, line, l2-line );
if (l2 != NULL) str += p1;
str += "\n";
}
str += "\n";
str += nimg.metaText;
str += "\n";
str += nanoPar->metaText;
buf = new char [str.size()+1];
buf[str.size()] = '\0';
memcpy( buf, str.c_str(), str.size() );
return buf;
return NULL;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -