Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

EMAN::ImagicIO2 Class Reference

IMAGIC-5 Header File Format. More...

#include <imagicio2.h>

Inheritance diagram for EMAN::ImagicIO2:

Inheritance graph
[legend]
Collaboration diagram for EMAN::ImagicIO2:

Collaboration graph
[legend]
List of all members.

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

bool is_valid (const void *first_block)

Public Attributes

 DEFINE_IMAGEIO_FUNC

Static Public Attributes

const char * HED_EXT = "hed"
const char * IMG_EXT = "img"

Private Types

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
Ctfread_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

const char * REAL_TYPE_MAGIC = "REAL"
const char * CTF_MAGIC = "!-"

Detailed Description

IMAGIC-5 Header File Format.

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.


Member Enumeration Documentation

anonymous enum [private]
 

Enumeration values:
NUM_4BYTES_PRE_IYLP 
NUM_4BYTES_AFTER_IXOLD 
NUM_4BYTES_AFTER_NAME 

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]
 

Enumeration values:
IMAGIC_CHAR 
IMAGIC_SHORT 
IMAGIC_FLOAT 
IMAGIC_FLOAT_COMPLEX 
IMAGIC_FFT_FLOAT_COMPLEX 
IMAGIC_UNKNOWN_TYPE 

Definition at line 108 of file imagicio2.h.

Referenced by get_datatype_from_name().

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]
 

Enumeration values:
VAX_VMS 
LINUX_WINDOWS 
SGI_IBM 

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                 };


Constructor & Destructor Documentation

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, 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  ) 
 

Definition at line 72 of file imagicio2.cpp.

References hed_file, and img_file.

00073 {
00074         if (hed_file) {
00075                 fclose(hed_file);
00076                 hed_file = 0;
00077         }
00078 
00079         if (img_file) {
00080                 fclose(img_file);
00081                 img_file = 0;
00082         }
00083 }


Member Function Documentation

int ImagicIO2::generate_machine_stamp  )  const [private]
 

Definition at line 642 of file imagicio2.cpp.

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 DataType, 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 ImagicIO2::get_datatype_size DataType  t  )  const [private]
 

Definition at line 757 of file imagicio2.cpp.

References IMAGIC_CHAR, IMAGIC_FFT_FLOAT_COMPLEX, IMAGIC_FLOAT, IMAGIC_FLOAT_COMPLEX, and IMAGIC_SHORT.

00758 {
00759         size_t s = 0;
00760         switch (t) {
00761         case IMAGIC_CHAR:
00762                 s = sizeof(unsigned char);
00763                 break;
00764         case IMAGIC_SHORT:
00765                 s = sizeof(unsigned short);
00766                 break;
00767         case IMAGIC_FLOAT:
00768         case IMAGIC_FLOAT_COMPLEX:
00769         case IMAGIC_FFT_FLOAT_COMPLEX:
00770                 s = sizeof(float);
00771                 break;
00772         default:
00773                 s = 0;
00774         }
00775 
00776         return s;
00777 }

int ImagicIO2::get_nimg  )  [virtual]
 

Get number of images in this file.

Returns:
number of images

Reimplemented from EMAN::ImageIO.

Definition at line 745 of file imagicio2.cpp.

References EMAN::ImagicIO2::Imagic4D::count, imagich, and EMAN::ImageIO::init().

00746 {
00747         init();
00748         return imagich.count + 1;
00749 }

int ImagicIO2::init_test  ) 
 

Definition at line 129 of file imagicio2.cpp.

References data, FileAccessException, filename, hed_filename, in, 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.

Returns:
false this file format support stack

Reimplemented from EMAN::ImageIO.

Definition at line 93 of file imagicio2.h.

00094                 {
00095                         return false;
00096                 }

bool ImagicIO2::is_valid const void *  first_block  )  [static]
 

Definition at line 167 of file imagicio2.cpp.

References data, EMAN::ByteOrder::is_data_big_endian(), EMAN::ByteOrder::is_host_big_endian(), LINUX_WINDOWS, nx, ny, nz, 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, and swap_header().

00658 {
00659         if (is_big_endian != ByteOrder::is_host_big_endian()) {
00660                 swap_header(hed);
00661         }
00662 }

Ctf * ImagicIO2::read_ctf const Imagic4D hed  )  const [private]
 

the Ctf object is a EMAN1Ctf object.

Definition at line 422 of file imagicio2.cpp.

References CTF_MAGIC, EMAN::Ctf::from_string(), imagich, 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, and NUM_4BYTES_PRE_IYLP.

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 ImagicIO2::to_em_datatype DataType  t  )  const [private]
 

Definition at line 403 of file imagicio2.cpp.

References IMAGIC_CHAR, IMAGIC_FFT_FLOAT_COMPLEX, IMAGIC_FLOAT, IMAGIC_FLOAT_COMPLEX, and IMAGIC_SHORT.

00404 {
00405         switch (t) {
00406         case IMAGIC_CHAR:
00407                 return EMUtil::EM_CHAR;
00408         case IMAGIC_SHORT:
00409                 return EMUtil::EM_SHORT;
00410         case IMAGIC_FLOAT:
00411                 return EMUtil::EM_FLOAT;
00412         case IMAGIC_FLOAT_COMPLEX:
00413         case IMAGIC_FFT_FLOAT_COMPLEX:
00414                 return EMUtil::EM_FLOAT_COMPLEX;
00415         default:
00416                 break;
00417         }
00418 
00419         return EMUtil::EM_UNKNOWN;
00420 }

void ImagicIO2::write_ctf const Ctf *const   ctf,
int  image_index = 0
[private]
 

Definition at line 444 of file imagicio2.cpp.

References CTF_MAGIC, hed_file, hed_filename, ImageWriteException, imagich, EMAN::ImageIO::init(), EMAN::ImagicIO2::Imagic4D::label, 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 }


Member Data Documentation

const char * ImagicIO2::CTF_MAGIC = "!-" [static, private]
 

Definition at line 55 of file imagicio2.cpp.

Referenced by read_ctf(), and write_ctf().

DataType EMAN::ImagicIO2::datatype [private]
 

Definition at line 266 of file imagicio2.h.

Referenced by ImagicIO2().

EMAN::ImagicIO2::DEFINE_IMAGEIO_FUNC
 

Definition at line 82 of file imagicio2.h.

string EMAN::ImagicIO2::filename [private]
 

Definition at line 252 of file imagicio2.h.

Referenced by ImagicIO2(), and init_test().

const char * ImagicIO2::HED_EXT = "hed" [static]
 

Definition at line 52 of file imagicio2.cpp.

Referenced by ImagicIO2().

FILE* EMAN::ImagicIO2::hed_file [private]
 

Definition at line 257 of file imagicio2.h.

Referenced by write_ctf(), and ~ImagicIO2().

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]
 

Definition at line 260 of file imagicio2.h.

Referenced by get_nimg(), ImagicIO2(), read_ctf(), and write_ctf().

const char * ImagicIO2::IMG_EXT = "img" [static]
 

Definition at line 53 of file imagicio2.cpp.

Referenced by ImagicIO2().

FILE* EMAN::ImagicIO2::img_file [private]
 

Definition at line 258 of file imagicio2.h.

Referenced by ~ImagicIO2().

string EMAN::ImagicIO2::img_filename [private]
 

Definition at line 254 of file imagicio2.h.

Referenced by ImagicIO2().

bool EMAN::ImagicIO2::initialized [private]
 

Definition at line 262 of file imagicio2.h.

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]
 

Definition at line 263 of file imagicio2.h.

Referenced by ImagicIO2().

bool EMAN::ImagicIO2::is_new_img [private]
 

Definition at line 264 of file imagicio2.h.

Referenced by ImagicIO2().

int EMAN::ImagicIO2::nz [private]
 

Definition at line 267 of file imagicio2.h.

Referenced by ImagicIO2(), and is_valid().

const char * ImagicIO2::REAL_TYPE_MAGIC = "REAL" [static, private]
 

Definition at line 54 of file imagicio2.cpp.

Referenced by get_datatype_from_name().

IOMode EMAN::ImagicIO2::rw_mode [private]
 

Definition at line 256 of file imagicio2.h.


The documentation for this class was generated from the following files:
Generated on Tue Jun 11 13:42:05 2013 for EMAN2 by  doxygen 1.3.9.1