#include <imagicio2.h>
Inheritance diagram for EMAN::ImagicIO2:
Public Member Functions | |
ImagicIO2 (string filename, IOMode rw_mode=READ_ONLY) | |
~ImagicIO2 () | |
int | init_test () |
bool | is_single_image_format () const |
If this file format is only for single iamge. | |
int | get_nimg () |
Get number of images in this file. | |
Static Public Member Functions | |
static bool | is_valid (const void *first_block) |
Public Attributes | |
DEFINE_IMAGEIO_FUNC | |
Static Public Attributes | |
static const char * | HED_EXT = "hed" |
static const char * | IMG_EXT = "img" |
Private Types | |
IMAGIC_CHAR | |
IMAGIC_SHORT | |
IMAGIC_FLOAT | |
IMAGIC_FLOAT_COMPLEX | |
IMAGIC_FFT_FLOAT_COMPLEX | |
IMAGIC_UNKNOWN_TYPE | |
NUM_4BYTES_PRE_IYLP = 14 | |
NUM_4BYTES_AFTER_IXOLD = 14 | |
NUM_4BYTES_AFTER_NAME = 150 | |
VAX_VMS = 16777216 | |
LINUX_WINDOWS = 33686018 | |
SGI_IBM = 67372036 | |
enum | DataType { IMAGIC_CHAR, IMAGIC_SHORT, IMAGIC_FLOAT, IMAGIC_FLOAT_COMPLEX, IMAGIC_FFT_FLOAT_COMPLEX, IMAGIC_UNKNOWN_TYPE } |
enum | { NUM_4BYTES_PRE_IYLP = 14, NUM_4BYTES_AFTER_IXOLD = 14, NUM_4BYTES_AFTER_NAME = 150 } |
enum | RealType { VAX_VMS = 16777216, LINUX_WINDOWS = 33686018, SGI_IBM = 67372036 } |
Private Member Functions | |
size_t | get_datatype_size (DataType t) const |
int | to_em_datatype (DataType t) const |
void | make_header_host_endian (Imagic4D &hed) const |
void | swap_header (Imagic4D &hed) const |
DataType | get_datatype_from_name (const char *name) const |
Ctf * | read_ctf (const Imagic4D &hed) const |
the Ctf object is a EMAN1Ctf object. | |
void | write_ctf (const Ctf *const ctf, int image_index=0) |
int | generate_machine_stamp () const |
Private Attributes | |
string | filename |
string | hed_filename |
string | img_filename |
IOMode | rw_mode |
FILE * | hed_file |
FILE * | img_file |
Imagic4D | imagich |
bool | is_big_endian |
bool | initialized |
bool | is_new_hed |
bool | is_new_img |
DataType | datatype |
int | nz |
Static Private Attributes | |
static const char * | REAL_TYPE_MAGIC = "REAL" |
static const char * | CTF_MAGIC = "!-" |
Classes | |
struct | Imagic4D |
IMAGIC-4D file format http://www.imagescience.de/formats/formats.htm. More... |
Renewed 4D version: http://www.imagescience.de/formats/formats.htm
An IMAGIC-5 file has 2 files: a) a header file with extension ".hed". It contains information for every image. b) an image file with extension ".img". It contains raw data.
The header file contains one (fixed-size) record per image stored. Every header record consists of 256 REAL/float for every image.
The image file contains only the raw data. Depending on the internal IMAGIC-5 format used, which can be REAL, INTG, PACK or COMP, the data is stored as REAL/float, INTEGER/int, INTEGER*1/byte or 2x REAL/float, respectively. The first pixel stored is the upper left one. The data is stored line by line, section by section, volume by volume.
3D imagic uses the same format to 2D. it is a bunch of 2D slices. use the 'hint' IS_3D to treat "2D slices" as 3D volume.
This renewed version support storing multiple 3D images in one file (header/data pair).
EMAN2 will read both old and new Imagic header, but write to new format from - Grant Tang.
Definition at line 73 of file imagicio2.h.
anonymous enum [private] |
Definition at line 119 of file imagicio2.h.
00120 { 00121 NUM_4BYTES_PRE_IYLP = 14, 00122 NUM_4BYTES_AFTER_IXOLD = 14, 00123 NUM_4BYTES_AFTER_NAME = 150 //before HISTORY 00124 };
enum EMAN::ImagicIO2::DataType [private] |
IMAGIC_CHAR | |
IMAGIC_SHORT | |
IMAGIC_FLOAT | |
IMAGIC_FLOAT_COMPLEX | |
IMAGIC_FFT_FLOAT_COMPLEX | |
IMAGIC_UNKNOWN_TYPE |
Definition at line 108 of file imagicio2.h.
00109 { 00110 IMAGIC_CHAR, 00111 IMAGIC_SHORT, 00112 IMAGIC_FLOAT, 00113 IMAGIC_FLOAT_COMPLEX, 00114 IMAGIC_FFT_FLOAT_COMPLEX, 00115 IMAGIC_UNKNOWN_TYPE 00116 };
enum EMAN::ImagicIO2::RealType [private] |
Definition at line 126 of file imagicio2.h.
00127 { 00128 VAX_VMS = 16777216, //for VAX/VMS 00129 LINUX_WINDOWS = 33686018, //for OSF, ULTRIX, LINUX, MS WINDOWS 00130 SGI_IBM = 67372036 //for SiliconGraphics, SUN, HP, IBM 00131 };
ImagicIO2::ImagicIO2 | ( | string | filename, | |
IOMode | rw_mode = READ_ONLY | |||
) | [explicit] |
Definition at line 57 of file imagicio2.cpp.
References EMAN::Util::change_filename_ext(), EMAN::ImagicIO2::Imagic4D::count, datatype, filename, HED_EXT, hed_filename, IMAGIC_UNKNOWN_TYPE, imagich, IMG_EXT, img_filename, is_big_endian, EMAN::ByteOrder::is_host_big_endian(), is_new_hed, is_new_img, and nz.
00058 : filename(file), rw_mode(rw), hed_file(0), img_file(0), initialized(false) 00059 { 00060 hed_filename = Util::change_filename_ext(filename, HED_EXT); 00061 img_filename = Util::change_filename_ext(filename, IMG_EXT); 00062 00063 is_big_endian = ByteOrder::is_host_big_endian(); 00064 is_new_hed = false; 00065 is_new_img = false; 00066 memset(&imagich, 0, sizeof(Imagic4D)); 00067 imagich.count = -1; 00068 datatype = IMAGIC_UNKNOWN_TYPE; 00069 nz = 0; 00070 }
ImagicIO2::~ImagicIO2 | ( | ) |
int ImagicIO2::generate_machine_stamp | ( | ) | const [private] |
Definition at line 642 of file imagicio2.cpp.
References LINUX_WINDOWS, SGI_IBM, and VAX_VMS.
00643 { 00644 int machinestamp; 00645 00646 #ifdef __sgi 00647 machinestamp = SGI_IBM; 00648 #elif defined __OPENVMS__ 00649 machinestamp = VAX_VMS; 00650 #else 00651 machinestamp = LINUX_WINDOWS; 00652 #endif 00653 00654 return machinestamp; 00655 }
ImagicIO2::DataType ImagicIO2::get_datatype_from_name | ( | const char * | name | ) | const [private] |
Definition at line 381 of file imagicio2.cpp.
References IMAGIC_CHAR, IMAGIC_FFT_FLOAT_COMPLEX, IMAGIC_FLOAT, IMAGIC_FLOAT_COMPLEX, IMAGIC_SHORT, IMAGIC_UNKNOWN_TYPE, REAL_TYPE_MAGIC, and t.
00382 { 00383 DataType t = IMAGIC_UNKNOWN_TYPE; 00384 00385 if (strncmp(name, "PACK",4) == 0) { 00386 t = IMAGIC_CHAR; 00387 } 00388 else if (strncmp(name, "INTG",4) == 0) { 00389 t = IMAGIC_SHORT; 00390 } 00391 else if (strncmp(name, REAL_TYPE_MAGIC,4) == 0) { 00392 t = IMAGIC_FLOAT; 00393 } 00394 else if (strncmp(name, "COMP",4) == 0) { 00395 t = IMAGIC_FLOAT_COMPLEX; 00396 } 00397 else if (strncmp(name, "RECO",4) == 0) { 00398 t = IMAGIC_FFT_FLOAT_COMPLEX; 00399 } 00400 return t; 00401 }
size_t EMAN::ImagicIO2::get_datatype_size | ( | DataType | t | ) | const [private] |
int ImagicIO2::get_nimg | ( | ) | [virtual] |
Get number of images in this file.
Reimplemented from EMAN::ImageIO.
Definition at line 745 of file imagicio2.cpp.
References EMAN::ImageIO::init().
int ImagicIO2::init_test | ( | ) |
Definition at line 129 of file imagicio2.cpp.
References data, ENTERFUNC, EXITFUNC, FileAccessException, filename, hed_filename, in, initialized, LOGERR, nx, and ny.
00130 { 00131 ENTERFUNC; 00132 00133 if (initialized) { 00134 return 1; 00135 } 00136 00137 FILE *in = fopen(hed_filename.c_str(), "rb"); 00138 if (!in) { 00139 throw FileAccessException(filename); 00140 } 00141 00142 char first_block[1024]; 00143 size_t n = fread(first_block, sizeof(char), sizeof(first_block), in); 00144 00145 if (n == 0) { 00146 LOGERR("file '%s' is an empty file", filename.c_str()); 00147 fclose(in); 00148 return -1; 00149 } 00150 fclose(in); 00151 00152 const int *data = reinterpret_cast <const int *>(first_block); 00153 int nx = data[13]; 00154 int ny = data[12]; 00155 int izold = data[11]; 00156 00157 if(izold==nx*ny) { 00158 EXITFUNC; 00159 return -1; //old style IMAGIC file 00160 } 00161 else { 00162 EXITFUNC; 00163 return 0; //new IMAGIC4D file 00164 } 00165 }
bool EMAN::ImagicIO2::is_single_image_format | ( | ) | const [inline, virtual] |
If this file format is only for single iamge.
Reimplemented from EMAN::ImageIO.
Definition at line 93 of file imagicio2.h.
bool ImagicIO2::is_valid | ( | const void * | first_block | ) | [static] |
Definition at line 167 of file imagicio2.cpp.
References data, ENTERFUNC, EXITFUNC, EMAN::ByteOrder::is_data_big_endian(), EMAN::ByteOrder::is_host_big_endian(), LINUX_WINDOWS, nx, ny, nz, SGI_IBM, EMAN::ByteOrder::swap_bytes(), and VAX_VMS.
00168 { 00169 ENTERFUNC; 00170 00171 if (!first_block) { 00172 return false; 00173 } 00174 00175 const int *data = static_cast < const int *>(first_block); 00176 int count = data[1]; 00177 int headrec = data[3]; 00178 int hour = data[7]; 00179 int minute = data[8]; 00180 int second = data[9]; 00181 int rsize = data[10]; 00182 int nx = data[13]; 00183 int ny = data[12]; 00184 int nz = data[60]; 00185 int realtype = data[68]; 00186 00187 bool data_big_endian = ByteOrder::is_data_big_endian(&headrec); 00188 00189 if (data_big_endian != ByteOrder::is_host_big_endian()) { 00190 ByteOrder::swap_bytes(&count); 00191 ByteOrder::swap_bytes(&headrec); 00192 ByteOrder::swap_bytes(&hour); 00193 ByteOrder::swap_bytes(&rsize); 00194 ByteOrder::swap_bytes(&nx); 00195 ByteOrder::swap_bytes(&ny); 00196 ByteOrder::swap_bytes(&nz); 00197 ByteOrder::swap_bytes(&realtype); 00198 } 00199 00200 const int max_dim = 1 << 20; 00201 bool result = false; 00202 00203 // this field realtype is unique to new Imagic-5 format 00204 if(realtype != VAX_VMS && realtype != LINUX_WINDOWS && realtype != SGI_IBM) { 00205 EXITFUNC; 00206 return result; 00207 } 00208 00209 if (headrec == 1 && 00210 count >= 0 && count < max_dim && 00211 nx > 0 && nx < max_dim && 00212 ny > 0 && ny < max_dim && 00213 nz > 0 && nz < max_dim && 00214 hour >= 0 && hour < 24 && 00215 minute >=0 && minute <60 && 00216 second >=0 && second <60) { 00217 result = true; 00218 } 00219 00220 EXITFUNC; 00221 return result; 00222 }
void ImagicIO2::make_header_host_endian | ( | Imagic4D & | hed | ) | const [private] |
Definition at line 657 of file imagicio2.cpp.
References is_big_endian, EMAN::ByteOrder::is_host_big_endian(), and swap_header().
00658 { 00659 if (is_big_endian != ByteOrder::is_host_big_endian()) { 00660 swap_header(hed); 00661 } 00662 }
the Ctf object is a EMAN1Ctf object.
Definition at line 422 of file imagicio2.cpp.
References CTF_MAGIC, ENTERFUNC, EXITFUNC, EMAN::Ctf::from_string(), and EMAN::ImagicIO2::Imagic4D::label.
00423 { 00424 ENTERFUNC; 00425 00426 Ctf * ctf_ = 0; 00427 size_t n = strlen(CTF_MAGIC); 00428 00429 if (strncmp(imagich.label, CTF_MAGIC, n) == 0) { 00430 ctf_ = new EMAN1Ctf(); 00431 string header_label(hed.label); 00432 // Note: this block was making things crash because it assumed the following if statement 00433 // was true - I added the if statement (d.woolford) 00434 if (header_label.size() > 2) { 00435 string sctf = "O" + header_label.substr(2); 00436 ctf_->from_string(sctf); 00437 } 00438 } 00439 00440 EXITFUNC; 00441 return ctf_; 00442 }
void ImagicIO2::swap_header | ( | Imagic4D & | hed | ) | const [private] |
Definition at line 664 of file imagicio2.cpp.
References NUM_4BYTES_AFTER_IXOLD, NUM_4BYTES_AFTER_NAME, NUM_4BYTES_PRE_IYLP, and EMAN::ByteOrder::swap_bytes().
Referenced by make_header_host_endian().
00665 { 00666 ByteOrder::swap_bytes((int *) &hed, NUM_4BYTES_PRE_IYLP); 00667 ByteOrder::swap_bytes(&hed.ixold, NUM_4BYTES_AFTER_IXOLD); 00668 ByteOrder::swap_bytes((int *) &hed.ccc3d, NUM_4BYTES_AFTER_NAME); 00669 }
int EMAN::ImagicIO2::to_em_datatype | ( | DataType | t | ) | const [private] |
void ImagicIO2::write_ctf | ( | const Ctf *const | ctf, | |
int | image_index = 0 | |||
) | [private] |
Definition at line 444 of file imagicio2.cpp.
References CTF_MAGIC, ENTERFUNC, EXITFUNC, hed_file, hed_filename, ImageWriteException, EMAN::ImageIO::init(), and EMAN::Ctf::to_string().
00445 { 00446 ENTERFUNC; 00447 init(); 00448 00449 size_t n = strlen(CTF_MAGIC); 00450 strcpy(imagich.label, CTF_MAGIC); 00451 string ctf_ = ctf->to_string().substr(1); 00452 00453 //pad ctf_ to 80 char 00454 if(ctf_.size()>80) { 00455 ctf_ = ctf_.substr(0, 80); 00456 } 00457 else { 00458 string padded(80 - ctf_.size(), ' '); 00459 ctf_ = ctf_ + padded; 00460 } 00461 00462 strncpy(&imagich.label[n], ctf_.c_str(), sizeof(imagich.label) - n); 00463 00464 rewind(hed_file); 00465 if (fwrite(&imagich, sizeof(Imagic4D), 1, hed_file) != 1) { 00466 throw ImageWriteException(hed_filename, "Imagic Header"); 00467 } 00468 00469 EXITFUNC; 00470 }
const char * ImagicIO2::CTF_MAGIC = "!-" [static, private] |
DataType EMAN::ImagicIO2::datatype [private] |
Definition at line 82 of file imagicio2.h.
string EMAN::ImagicIO2::filename [private] |
const char * ImagicIO2::HED_EXT = "hed" [static] |
FILE* EMAN::ImagicIO2::hed_file [private] |
string EMAN::ImagicIO2::hed_filename [private] |
Definition at line 253 of file imagicio2.h.
Referenced by ImagicIO2(), init_test(), and write_ctf().
Imagic4D EMAN::ImagicIO2::imagich [private] |
const char * ImagicIO2::IMG_EXT = "img" [static] |
FILE* EMAN::ImagicIO2::img_file [private] |
string EMAN::ImagicIO2::img_filename [private] |
bool EMAN::ImagicIO2::initialized [private] |
bool EMAN::ImagicIO2::is_big_endian [private] |
Definition at line 261 of file imagicio2.h.
Referenced by ImagicIO2(), and make_header_host_endian().
bool EMAN::ImagicIO2::is_new_hed [private] |
bool EMAN::ImagicIO2::is_new_img [private] |
int EMAN::ImagicIO2::nz [private] |
const char * ImagicIO2::REAL_TYPE_MAGIC = "REAL" [static, private] |
IOMode EMAN::ImagicIO2::rw_mode [private] |
Definition at line 256 of file imagicio2.h.