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::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.

Classes

struct  ImagicHeader

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

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

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

Definition at line 103 of file imagicio.h.

enum EMAN::ImagicIO::DataType [private]
Enumerator:
IMAGIC_UCHAR 
IMAGIC_USHORT 
IMAGIC_FLOAT 
IMAGIC_FLOAT_COMPLEX 
IMAGIC_FFT_FLOAT_COMPLEX 
IMAGIC_UNKNOWN_TYPE 

Definition at line 93 of file imagicio.h.


Constructor & Destructor Documentation

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

Definition at line 71 of file imagicio.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

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

Definition at line 634 of file imagicio.cpp.

References IMAGIC_FFT_FLOAT_COMPLEX, IMAGIC_FLOAT, IMAGIC_FLOAT_COMPLEX, IMAGIC_UCHAR, IMAGIC_UNKNOWN_TYPE, IMAGIC_USHORT, REAL_TYPE_MAGIC, and t.

{
        DataType t = IMAGIC_UNKNOWN_TYPE;

        if (strncmp(name, "PACK",4) == 0) {
                t = IMAGIC_UCHAR;
        }
        else if (strncmp(name, "INTG",4) == 0) {
                t = IMAGIC_USHORT;
        }
        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 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.

{
        size_t s = 0;
        switch (t) {
        case IMAGIC_UCHAR:
                s = sizeof(unsigned char);
                break;
        case IMAGIC_USHORT:
                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 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().

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

                {
                        return false;
                }
bool ImagicIO::is_valid ( const void *  first_block) [static]

Definition at line 127 of file imagicio.cpp.

References data, ENTERFUNC, EXITFUNC, 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().

{
        ENTERFUNC;

        if (!first_block) {
                return false;
        }

        const int *data = static_cast < const int *>(first_block);
        int count = data[1];
        int headrec = data[3];
        int month = data[5];
        int hour = data[7];
        int nx = data[13];
        int ny = data[12];
        
        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(&month);
                ByteOrder::swap_bytes(&hour);
                ByteOrder::swap_bytes(&nx);
                ByteOrder::swap_bytes(&ny);
        }

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

        if (headrec == 1 &&
                count >= 0 && count < max_dim &&
                nx > 0 && nx < max_dim &&
                ny > 0 && ny < max_dim && month >= 0 && hour >= 0 && hour <= 24) {
                result = true;
        }

        EXITFUNC;
        return result;
}
void ImagicIO::make_header_host_endian ( ImagicHeader hed) [private]
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, ENTERFUNC, EXITFUNC, EMAN::Ctf::from_string(), imagich, and EMAN::ImagicIO::ImagicHeader::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 ImagicIO::swap_header ( ImagicHeader hed) [private]
int ImagicIO::to_em_datatype ( DataType  t) [private]
void ImagicIO::write_ctf ( const Ctf *const  ctf,
int  image_index = 0 
) [private]

Definition at line 595 of file imagicio.cpp.

References CTF_MAGIC, ENTERFUNC, EXITFUNC, hed_file, hed_filename, ImageWriteException, imagich, EMAN::ImageIO::init(), EMAN::ImagicIO::ImagicHeader::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);
        strncpy(&imagich.label[n], ctf_.c_str(), sizeof(imagich.label) - n);

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

        EXITFUNC;
}

Member Data Documentation

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

Definition at line 90 of file imagicio.h.

Referenced by read_ctf(), and write_ctf().

Definition at line 200 of file imagicio.h.

Referenced by ImagicIO().

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 72 of file imagicio.h.

Referenced by EMAN::EMUtil::get_image_type(), and 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().

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 73 of file imagicio.h.

Referenced by EMAN::EMUtil::get_image_type(), and 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().

Definition at line 196 of file imagicio.h.

Definition at line 195 of file imagicio.h.

Referenced by ImagicIO(), and make_header_host_endian().

Definition at line 197 of file imagicio.h.

Referenced by ImagicIO().

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 89 of file imagicio.h.

Referenced by get_datatype_from_name().

Definition at line 190 of file imagicio.h.


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