📄 dim_biorad_pic_format_io.cpp
字号:
/*****************************************************************************
BIORAD PIC 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:
03/29/2004 22:23 - First creation
05/10/2004 14:55 - Big endian support
08/04/2004 22:25 - Update to FMT_IFS 1.2, support for io protorypes
Ver : 3
*****************************************************************************/
#include <string>
// Disables Visual Studio 2005 warnings for deprecated code#ifdef WIN32 #pragma warning(disable:4996)#endif
#include "dim_biorad_pic_format.h"
//#include "memio.h"
//extern void* DimMalloc(DIM_ULONG size);
//extern void* DimFree(void *p);
TDimBioRadPicParams initTDimBioRadPicParams()
{
TDimBioRadPicParams r;
r.i = initTDimImageInfo();
r.i.depth = 8;
r.page_size_bytes = 0;
r.data_offset = 76;
r.notes_offset = 0;
r.has_notes = 0;
r.metaData.magnification = 0;
sprintf(r.metaData.datetime, "0000-00-00 00:00:00");
return r;
}
//----------------------------------------------------------------------------
// READ PROC
//----------------------------------------------------------------------------
static int read_biorad_image(TDimFormatHandle *fmtHndl)
{
if (fmtHndl == NULL) return 1;
if (fmtHndl->internalParams == NULL) return 1;
TDimBioRadPicParams *picPar = (TDimBioRadPicParams *) fmtHndl->internalParams;
TDimImageInfo *info = &picPar->i;
TDimImageBitmap *img = fmtHndl->image;
// init the image
if ( allocImg( fmtHndl, info, img) != 0 ) return 1;
long offset = picPar->data_offset + fmtHndl->pageNumber * picPar->page_size_bytes;
if (fmtHndl->stream == NULL) return 1;
if ( dimSeek(fmtHndl, offset, SEEK_SET) != 0) return 1;
if (dimRead( fmtHndl, img->bits[0], picPar->page_size_bytes, 1 ) != 1) return 1;
if ( (img->i.depth == 16) && (dimBigendian) )
dimSwapArrayOfShort((DIM_UINT16*) img->bits[0], picPar->page_size_bytes/2);
if (info->imageMode == DIM_RGB)
{ // rgb mode, three pages are considered RGB channels
offset += picPar->page_size_bytes;
if (dimSeek( fmtHndl, offset, SEEK_SET) != 0) return 1;
if (dimRead( fmtHndl, img->bits[1], picPar->page_size_bytes, 1 ) != 1) return 1;
if ( (img->i.depth == 16) && (dimBigendian) )
dimSwapArrayOfShort((DIM_UINT16*) img->bits[1], picPar->page_size_bytes/2);
if (picPar->num_images > 2)
{
offset += picPar->page_size_bytes;
if (dimSeek( fmtHndl, offset, SEEK_SET) != 0) return 1;
if (dimRead( fmtHndl, img->bits[2], picPar->page_size_bytes, 1 ) != 1) return 1;
if ( (img->i.depth == 16) && (dimBigendian) )
dimSwapArrayOfShort((DIM_UINT16*) img->bits[2], picPar->page_size_bytes/2);
}
else
{
memset( img->bits[2], 0, picPar->page_size_bytes );
}
}
return 0;
}
//----------------------------------------------------------------------------
// META DATA PROC
//----------------------------------------------------------------------------
DIM_UINT 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_biorad_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;
tag; type;
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) {
short note_type = * (DIM_INT16 *) (note + 10);
if (dimBigendian) dimSwapShort ( (DIM_UINT16*) ¬e_type );
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;
}
char* read_text_biorad_metadata ( TDimFormatHandle *fmtHndl )
{
if (fmtHndl == NULL) return NULL;
if (fmtHndl->internalParams == NULL) return NULL;
TDimBioRadPicParams *picPar = (TDimBioRadPicParams *) fmtHndl->internalParams;
TDimImageInfo *info = &picPar->i;
char *buf = NULL;
std::string str = "";
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) {
short note_type = * (DIM_INT16 *) (note + 10);
if (dimBigendian) dimSwapShort ( (DIM_UINT16*) ¬e_type );
char *text = (char *) (note + 16);
if (note_type == 1) {
if (text[0] == '\0') continue;
note01 += text;
note01 += '\n';
}
if (note_type == 20) {
if (text[0] == '\0') continue;
note20 += text;
note20 += '\n';
}
if (note_type == 21) {
if (text[0] == '\0') continue;
note21 += text;
note21 += '\n';
}
}
str += "[BioRad PIC Metadata]\n\n";
sprintf((char*) note, "Number of frames: %d\n", info->number_pages );
str += (char*) note;
if ( picPar->metaData.pixel_size[0] > 0) {
sprintf((char*)note, "X Resolution: %.6f microns\n", picPar->metaData.pixel_size[0] );
str += (char*) note;
}
if ( picPar->metaData.pixel_size[1] > 0) {
sprintf((char*)note, "Y Resolution: %.6f microns\n", picPar->metaData.pixel_size[1] );
str += (char*) note;
}
if ( (picPar->metaData.pixel_size[2] > 0) && (info->number_pages > 1) ) {
sprintf((char*)note, "Z Resolution: %.6f microns\n", picPar->metaData.pixel_size[2] );
str += (char*) note;
}
str += "\n";
if (note01.size() > 0) {
str += "\n[NOTE 1]\n";
str += note01;
str += "\n";
}
if (note20.size() > 0) {
str += "\n[NOTE 20]\n";
str += note20;
str += "\n";
}
if (note21.size() > 0) {
str += "\n[NOTE 21]\n";
str += note21;
str += "\n";
}
buf = new char [str.size()+1];
buf[str.size()] = '\0';
memcpy( buf, str.c_str(), str.size() );
return buf;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -