EMAN2
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | Static Private Attributes
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.

Classes

struct  Imagic4D
 IMAGIC-4D file format http://www.imagescience.de/formats/formats.htm. More...

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

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

static const char * REAL_TYPE_MAGIC = "REAL"
static 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]
Enumerator:
NUM_4BYTES_PRE_IYLP 
NUM_4BYTES_AFTER_IXOLD 
NUM_4BYTES_AFTER_NAME 

Definition at line 119 of file imagicio2.h.

                {
                        NUM_4BYTES_PRE_IYLP = 14,
                        NUM_4BYTES_AFTER_IXOLD = 14,
                        NUM_4BYTES_AFTER_NAME = 150     //before HISTORY
                };
enum EMAN::ImagicIO2::DataType [private]
Enumerator:
IMAGIC_CHAR 
IMAGIC_SHORT 
IMAGIC_FLOAT 
IMAGIC_FLOAT_COMPLEX 
IMAGIC_FFT_FLOAT_COMPLEX 
IMAGIC_UNKNOWN_TYPE 

Definition at line 108 of file imagicio2.h.

enum EMAN::ImagicIO2::RealType [private]
Enumerator:
VAX_VMS 
LINUX_WINDOWS 
SGI_IBM 

Definition at line 126 of file imagicio2.h.

                {
                        VAX_VMS = 16777216,                     //for VAX/VMS
                        LINUX_WINDOWS = 33686018,       //for OSF, ULTRIX, LINUX, MS WINDOWS
                        SGI_IBM = 67372036                      //for SiliconGraphics, SUN, HP, IBM
                };

Constructor & Destructor Documentation

ImagicIO2::ImagicIO2 ( string  filename,
IOMode  rw_mode = READ_ONLY 
) [explicit]
ImagicIO2::~ImagicIO2 ( )

Definition at line 72 of file imagicio2.cpp.

References hed_file, and img_file.

{
        if (hed_file) {
                fclose(hed_file);
                hed_file = 0;
        }

        if (img_file) {
                fclose(img_file);
                img_file = 0;
        }
}

Member Function Documentation

int ImagicIO2::generate_machine_stamp ( ) const [private]

Definition at line 642 of file imagicio2.cpp.

References LINUX_WINDOWS, SGI_IBM, and VAX_VMS.

{
        int machinestamp;

#ifdef __sgi
        machinestamp = SGI_IBM;
#elif defined __OPENVMS__
        machinestamp = VAX_VMS;
#else
        machinestamp = LINUX_WINDOWS;
#endif

        return machinestamp;
}
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.

{
        DataType t = IMAGIC_UNKNOWN_TYPE;

        if (strncmp(name, "PACK",4) == 0) {
                t = IMAGIC_CHAR;
        }
        else if (strncmp(name, "INTG",4) == 0) {
                t = IMAGIC_SHORT;
        }
        else if (strncmp(name, REAL_TYPE_MAGIC,4) == 0) {
                t = IMAGIC_FLOAT;
        }
        else if (strncmp(name, "COMP",4) == 0) {
                t = IMAGIC_FLOAT_COMPLEX;
        }
        else if (strncmp(name, "RECO",4) == 0) {
                t = IMAGIC_FFT_FLOAT_COMPLEX;
        }
        return t;
}
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.

{
        size_t s = 0;
        switch (t) {
        case IMAGIC_CHAR:
                s = sizeof(unsigned char);
                break;
        case IMAGIC_SHORT:
                s = sizeof(unsigned short);
                break;
        case IMAGIC_FLOAT:
        case IMAGIC_FLOAT_COMPLEX:
        case IMAGIC_FFT_FLOAT_COMPLEX:
                s = sizeof(float);
                break;
        default:
                s = 0;
        }

        return s;
}
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().

{
        init();
        return imagich.count + 1;
}
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.

{
        ENTERFUNC;

        if (initialized) {
                return 1;
        }

        FILE *in = fopen(hed_filename.c_str(), "rb");
        if (!in) {
                throw FileAccessException(filename);
        }

        char first_block[1024];
        size_t n = fread(first_block, sizeof(char), sizeof(first_block), in);

        if (n == 0) {
                LOGERR("file '%s' is an empty file", filename.c_str());
                fclose(in);
                return -1;
        }
        fclose(in);

        const int *data = reinterpret_cast <const int *>(first_block);
        int nx = data[13];
        int ny = data[12];
        int izold = data[11];

        if(izold==nx*ny) {
                EXITFUNC;
                return -1;      //old style IMAGIC file
        }
        else {
                EXITFUNC;
                return 0;       //new IMAGIC4D file
        }
}
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.

                {
                        return false;
                }
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.

{
        ENTERFUNC;

        if (!first_block) {
                return false;
        }

        const int *data = static_cast < const int *>(first_block);
        int count = data[1];
        int headrec = data[3];
        int hour = data[7];
        int minute = data[8];
        int second = data[9];
        int rsize = data[10];
        int nx = data[13];
        int ny = data[12];
        int nz = data[60];
        int realtype = data[68];

        bool data_big_endian = ByteOrder::is_data_big_endian(&headrec);

        if (data_big_endian != ByteOrder::is_host_big_endian()) {
                ByteOrder::swap_bytes(&count);
                ByteOrder::swap_bytes(&headrec);
                ByteOrder::swap_bytes(&hour);
                ByteOrder::swap_bytes(&rsize);
                ByteOrder::swap_bytes(&nx);
                ByteOrder::swap_bytes(&ny);
                ByteOrder::swap_bytes(&nz);
                ByteOrder::swap_bytes(&realtype);
        }

        const int max_dim = 1 << 20;
        bool result = false;

        // this field realtype is unique to new Imagic-5 format
        if(realtype != VAX_VMS && realtype != LINUX_WINDOWS && realtype != SGI_IBM) {
                EXITFUNC;
                return result;
        }
        
        if (headrec == 1 &&
                count >= 0 && count < max_dim &&
                nx > 0 && nx < max_dim &&
                ny > 0 && ny < max_dim && 
                nz > 0 && nz < max_dim &&
                hour >= 0 && hour < 24 &&
                minute >=0 && minute <60 &&
                second >=0 && second <60) {
                result = true;
        }

        EXITFUNC;
        return result;
}
void ImagicIO2::make_header_host_endian ( Imagic4D hed) const [private]
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, ENTERFUNC, EXITFUNC, EMAN::Ctf::from_string(), imagich, and EMAN::ImagicIO2::Imagic4D::label.

{
        ENTERFUNC;

        Ctf * ctf_ = 0;
        size_t n = strlen(CTF_MAGIC);

        if (strncmp(imagich.label, CTF_MAGIC, n) == 0) {
                ctf_ = new EMAN1Ctf();
                string header_label(hed.label);
                // Note: this block was making things crash because it assumed the following if statement
                // was true - I added the if statement (d.woolford)
                if (header_label.size() > 2) {
                        string sctf = "O" + header_label.substr(2);
                        ctf_->from_string(sctf);
                }
        }

        EXITFUNC;
        return ctf_;
}
void ImagicIO2::swap_header ( Imagic4D hed) const [private]
int 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, imagich, EMAN::ImageIO::init(), EMAN::ImagicIO2::Imagic4D::label, and EMAN::Ctf::to_string().

{
        ENTERFUNC;
        init();

        size_t n = strlen(CTF_MAGIC);
        strcpy(imagich.label, CTF_MAGIC);
        string ctf_ = ctf->to_string().substr(1);

        //pad ctf_ to 80 char
        if(ctf_.size()>80) {
                ctf_ = ctf_.substr(0, 80);
        }
        else {
                string padded(80 - ctf_.size(), ' ');
                ctf_ = ctf_ + padded;
        }

        strncpy(&imagich.label[n], ctf_.c_str(), sizeof(imagich.label) - n);

        rewind(hed_file);
        if (fwrite(&imagich, sizeof(Imagic4D), 1, hed_file) != 1) {
                throw ImageWriteException(hed_filename, "Imagic Header");
        }

        EXITFUNC;
}

Member Data Documentation

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

Definition at line 105 of file imagicio2.h.

Referenced by read_ctf(), and write_ctf().

Definition at line 266 of file imagicio2.h.

Referenced by ImagicIO2().

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 76 of file imagicio2.h.

Referenced by ImagicIO2().

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

Definition at line 257 of file imagicio2.h.

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

Definition at line 253 of file imagicio2.h.

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

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 77 of file imagicio2.h.

Referenced by ImagicIO2().

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

Definition at line 258 of file imagicio2.h.

Referenced by ~ImagicIO2().

Definition at line 254 of file imagicio2.h.

Referenced by ImagicIO2().

Definition at line 262 of file imagicio2.h.

Referenced by init_test().

Definition at line 261 of file imagicio2.h.

Referenced by ImagicIO2(), and make_header_host_endian().

Definition at line 263 of file imagicio2.h.

Referenced by ImagicIO2().

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 104 of file imagicio2.h.

Referenced by get_datatype_from_name().

Definition at line 256 of file imagicio2.h.


The documentation for this class was generated from the following files: