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

EMAN::ImagicIO Class Reference

IMAGIC-5 Header File Format. More...

#include <imagicio.h>

Inheritance diagram for EMAN::ImagicIO:

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

Collaboration graph
[legend]
List of all members.

Public Member Functions

 ImagicIO (string filename, IOMode rw_mode=READ_ONLY)
 ~ImagicIO ()
bool is_single_image_format () const
 Is this image format only storing 1 image or not.
int get_nimg ()
 Return the number of images in this image 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_UCHAR, IMAGIC_USHORT, IMAGIC_FLOAT, IMAGIC_FLOAT_COMPLEX,
  IMAGIC_FFT_FLOAT_COMPLEX, IMAGIC_UNKNOWN_TYPE
}
enum  { NUM_4BYTES_PRE_IXOLD = 14, NUM_4BYTES_AFTER_IXOLD = 14, NUM_4BYTES_AFTER_SPACE = 207 }

Private Member Functions

size_t get_datatype_size (DataType t)
int to_em_datatype (DataType t)
void make_header_host_endian (ImagicHeader &hed)
void swap_header (ImagicHeader &hed)
DataType get_datatype_from_name (const char *name)
Ctfread_ctf (const ImagicHeader &hed) const
 the Ctf object is a EMAN1Ctf object.
void write_ctf (const Ctf *const ctf, int image_index=0)

Private Attributes

string filename
string hed_filename
string img_filename
IOMode rw_mode
FILE * hed_file
FILE * img_file
ImagicHeader 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.

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.

imagic doesn't store multiple 3D images in one file (header/data pair).

Definition at line 69 of file imagicio.h.


Member Enumeration Documentation

anonymous enum [private]
 

Enumeration values:
NUM_4BYTES_PRE_IXOLD 
NUM_4BYTES_AFTER_IXOLD 
NUM_4BYTES_AFTER_SPACE 

Definition at line 103 of file imagicio.h.

00104                 {
00105                         NUM_4BYTES_PRE_IXOLD = 14,
00106                         NUM_4BYTES_AFTER_IXOLD = 14,
00107                         NUM_4BYTES_AFTER_SPACE = 207
00108                 };

enum EMAN::ImagicIO::DataType [private]
 

Enumeration values:
IMAGIC_UCHAR 
IMAGIC_USHORT 
IMAGIC_FLOAT 
IMAGIC_FLOAT_COMPLEX 
IMAGIC_FFT_FLOAT_COMPLEX 
IMAGIC_UNKNOWN_TYPE 

Definition at line 93 of file imagicio.h.

Referenced by get_datatype_from_name().

00094                 {
00095                         IMAGIC_UCHAR,
00096                         IMAGIC_USHORT,
00097                         IMAGIC_FLOAT,
00098                         IMAGIC_FLOAT_COMPLEX,
00099                         IMAGIC_FFT_FLOAT_COMPLEX,
00100                         IMAGIC_UNKNOWN_TYPE
00101                 };


Constructor & Destructor Documentation

ImagicIO::ImagicIO string  filename,
IOMode  rw_mode = READ_ONLY
[explicit]
 

Definition at line 56 of file imagicio.cpp.

References EMAN::Util::change_filename_ext(), EMAN::ImagicIO::ImagicHeader::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.

00057 :       filename(file), rw_mode(rw), hed_file(0), img_file(0), initialized(false)
00058 {
00059         hed_filename = Util::change_filename_ext(filename, HED_EXT);
00060         img_filename = Util::change_filename_ext(filename, IMG_EXT);
00061 
00062         is_big_endian = ByteOrder::is_host_big_endian();
00063         is_new_hed = false;
00064         is_new_img = false;
00065         memset(&imagich, 0, sizeof(ImagicHeader));
00066         imagich.count = -1;
00067         datatype = IMAGIC_UNKNOWN_TYPE;
00068         nz = 0;
00069 }

ImagicIO::~ImagicIO  ) 
 

Definition at line 71 of file imagicio.cpp.

References hed_file, and img_file.

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


Member Function Documentation

ImagicIO::DataType ImagicIO::get_datatype_from_name const char *  name  )  [private]
 

Definition at line 634 of file imagicio.cpp.

References DataType, REAL_TYPE_MAGIC, and t.

00635 {
00636         DataType t = IMAGIC_UNKNOWN_TYPE;
00637 
00638         if (strncmp(name, "PACK",4) == 0) {
00639                 t = IMAGIC_UCHAR;
00640         }
00641         else if (strncmp(name, "INTG",4) == 0) {
00642                 t = IMAGIC_USHORT;
00643         }
00644         else if (strncmp(name, REAL_TYPE_MAGIC,4) == 0) {
00645                 t = IMAGIC_FLOAT;
00646         }
00647         else if (strncmp(name, "COMP",4) == 0) {
00648                 t = IMAGIC_FLOAT_COMPLEX;
00649         }
00650         else if (strncmp(name, "RECO",4) == 0) {
00651                 t = IMAGIC_FFT_FLOAT_COMPLEX;
00652         }
00653         return t;
00654 }

size_t ImagicIO::get_datatype_size DataType  t  )  [private]
 

Definition at line 656 of file imagicio.cpp.

References IMAGIC_FFT_FLOAT_COMPLEX, IMAGIC_FLOAT, IMAGIC_FLOAT_COMPLEX, IMAGIC_UCHAR, and IMAGIC_USHORT.

00657 {
00658         size_t s = 0;
00659         switch (t) {
00660         case IMAGIC_UCHAR:
00661                 s = sizeof(unsigned char);
00662                 break;
00663         case IMAGIC_USHORT:
00664                 s = sizeof(unsigned short);
00665                 break;
00666         case IMAGIC_FLOAT:
00667         case IMAGIC_FLOAT_COMPLEX:
00668         case IMAGIC_FFT_FLOAT_COMPLEX:
00669                 s = sizeof(float);
00670                 break;
00671         default:
00672                 s = 0;
00673         }
00674 
00675         return s;
00676 }

int ImagicIO::get_nimg  )  [virtual]
 

Return the number of images in this image file.

Reimplemented from EMAN::ImageIO.

Definition at line 628 of file imagicio.cpp.

References EMAN::ImagicIO::ImagicHeader::count, imagich, and EMAN::ImageIO::init().

00629 {
00630         init();
00631         return (imagich.count + 1);
00632 }

bool EMAN::ImagicIO::is_single_image_format  )  const [inline, virtual]
 

Is this image format only storing 1 image or not.

Some image formats like MRC only store 1 image in a file, so this function returns 'true' for them. Other image formats like IMAGIC/HDF5 may store mutliple images, so this function returns 'false' for them.

Reimplemented from EMAN::ImageIO.

Definition at line 81 of file imagicio.h.

00082                 {
00083                         return false;
00084                 }

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

Definition at line 127 of file imagicio.cpp.

References data, EMAN::ByteOrder::is_data_big_endian(), EMAN::ByteOrder::is_host_big_endian(), nx, ny, and EMAN::ByteOrder::swap_bytes().

Referenced by EMAN::EMUtil::fast_get_image_type(), and EMAN::EMUtil::get_image_type().

00128 {
00129         ENTERFUNC;
00130 
00131         if (!first_block) {
00132                 return false;
00133         }
00134 
00135         const int *data = static_cast < const int *>(first_block);
00136         int count = data[1];
00137         int headrec = data[3];
00138         int month = data[5];
00139         int hour = data[7];
00140         int nx = data[13];
00141         int ny = data[12];
00142         
00143         bool data_big_endian = ByteOrder::is_data_big_endian(&headrec);
00144 
00145         if (data_big_endian != ByteOrder::is_host_big_endian()) {
00146                 ByteOrder::swap_bytes(&count);
00147                 ByteOrder::swap_bytes(&headrec);
00148                 ByteOrder::swap_bytes(&month);
00149                 ByteOrder::swap_bytes(&hour);
00150                 ByteOrder::swap_bytes(&nx);
00151                 ByteOrder::swap_bytes(&ny);
00152         }
00153 
00154         const int max_dim = 1 << 20;
00155         bool result = false;
00156 
00157         if (headrec == 1 &&
00158                 count >= 0 && count < max_dim &&
00159                 nx > 0 && nx < max_dim &&
00160                 ny > 0 && ny < max_dim && month >= 0 && hour >= 0 && hour <= 24) {
00161                 result = true;
00162         }
00163 
00164         EXITFUNC;
00165         return result;
00166 }

void ImagicIO::make_header_host_endian ImagicHeader hed  )  [private]
 

Definition at line 696 of file imagicio.cpp.

References is_big_endian, and swap_header().

00697 {
00698         if (is_big_endian != ByteOrder::is_host_big_endian()) {
00699                 swap_header(hed);
00700         }
00701 }

Ctf * ImagicIO::read_ctf const ImagicHeader hed  )  const [private]
 

the Ctf object is a EMAN1Ctf object.

Definition at line 573 of file imagicio.cpp.

References CTF_MAGIC, EMAN::Ctf::from_string(), imagich, and EMAN::ImagicIO::ImagicHeader::label.

00574 {
00575         ENTERFUNC;
00576 
00577         Ctf * ctf_ = 0;
00578         size_t n = strlen(CTF_MAGIC);
00579 
00580         if (strncmp(imagich.label, CTF_MAGIC, n) == 0) {
00581                 ctf_ = new EMAN1Ctf();
00582                 string header_label(hed.label);
00583                 // Note: this block was making things crash because it assumed the following if statement
00584                 // was true - I added the if statement (d.woolford)
00585                 if (header_label.size() > 2) {
00586                         string sctf = "O" + header_label.substr(2);
00587                         ctf_->from_string(sctf);
00588                 }
00589         }
00590 
00591         EXITFUNC;
00592         return ctf_;
00593 }

void ImagicIO::swap_header ImagicHeader hed  )  [private]
 

Definition at line 704 of file imagicio.cpp.

References NUM_4BYTES_AFTER_IXOLD, NUM_4BYTES_AFTER_SPACE, and NUM_4BYTES_PRE_IXOLD.

Referenced by make_header_host_endian().

00705 {
00706         ByteOrder::swap_bytes((int *) &hed, NUM_4BYTES_PRE_IXOLD);
00707         ByteOrder::swap_bytes(&hed.ixold, NUM_4BYTES_AFTER_IXOLD);
00708         ByteOrder::swap_bytes((int *) &hed.space, NUM_4BYTES_AFTER_SPACE);
00709 }

int ImagicIO::to_em_datatype DataType  t  )  [private]
 

Definition at line 678 of file imagicio.cpp.

References IMAGIC_FLOAT, IMAGIC_FLOAT_COMPLEX, IMAGIC_UCHAR, and IMAGIC_USHORT.

00679 {
00680         switch (t) {
00681         case IMAGIC_UCHAR:
00682                 return EMUtil::EM_UCHAR;
00683         case IMAGIC_USHORT:
00684                 return EMUtil::EM_USHORT;
00685         case IMAGIC_FLOAT:
00686                 return EMUtil::EM_FLOAT;
00687         case IMAGIC_FLOAT_COMPLEX:
00688                 return EMUtil::EM_FLOAT_COMPLEX;
00689         default:
00690                 break;
00691         }
00692 
00693         return EMUtil::EM_UNKNOWN;
00694 }

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

Definition at line 595 of file imagicio.cpp.

References CTF_MAGIC, hed_file, hed_filename, ImageWriteException, imagich, EMAN::ImageIO::init(), EMAN::ImagicIO::ImagicHeader::label, and EMAN::Ctf::to_string().

00596 {
00597         ENTERFUNC;
00598         init();
00599 
00600         size_t n = strlen(CTF_MAGIC);
00601         strcpy(imagich.label, CTF_MAGIC);
00602         string ctf_ = ctf->to_string().substr(1);
00603         strncpy(&imagich.label[n], ctf_.c_str(), sizeof(imagich.label) - n);
00604 
00605         rewind(hed_file);
00606         if (fwrite(&imagich, sizeof(ImagicHeader), 1, hed_file) != 1) {
00607                 throw ImageWriteException(hed_filename, "Imagic Header");
00608         }
00609 
00610         EXITFUNC;
00611 }


Member Data Documentation

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

Definition at line 54 of file imagicio.cpp.

Referenced by read_ctf(), and write_ctf().

DataType EMAN::ImagicIO::datatype [private]
 

Definition at line 200 of file imagicio.h.

Referenced by ImagicIO().

EMAN::ImagicIO::DEFINE_IMAGEIO_FUNC
 

Definition at line 78 of file imagicio.h.

string EMAN::ImagicIO::filename [private]
 

Definition at line 186 of file imagicio.h.

Referenced by ImagicIO().

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

Definition at line 51 of file imagicio.cpp.

Referenced by ImagicIO().

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

Definition at line 191 of file imagicio.h.

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

string EMAN::ImagicIO::hed_filename [private]
 

Definition at line 187 of file imagicio.h.

Referenced by ImagicIO(), and write_ctf().

ImagicHeader EMAN::ImagicIO::imagich [private]
 

Definition at line 194 of file imagicio.h.

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

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

Definition at line 52 of file imagicio.cpp.

Referenced by ImagicIO().

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

Definition at line 192 of file imagicio.h.

Referenced by ~ImagicIO().

string EMAN::ImagicIO::img_filename [private]
 

Definition at line 188 of file imagicio.h.

Referenced by ImagicIO().

bool EMAN::ImagicIO::initialized [private]
 

Definition at line 196 of file imagicio.h.

bool EMAN::ImagicIO::is_big_endian [private]
 

Definition at line 195 of file imagicio.h.

Referenced by ImagicIO(), and make_header_host_endian().

bool EMAN::ImagicIO::is_new_hed [private]
 

Definition at line 197 of file imagicio.h.

Referenced by ImagicIO().

bool EMAN::ImagicIO::is_new_img [private]
 

Definition at line 198 of file imagicio.h.

Referenced by ImagicIO().

int EMAN::ImagicIO::nz [private]
 

Definition at line 201 of file imagicio.h.

Referenced by ImagicIO().

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

Definition at line 53 of file imagicio.cpp.

Referenced by get_datatype_from_name().

IOMode EMAN::ImagicIO::rw_mode [private]
 

Definition at line 190 of file imagicio.h.


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