EMAN2
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all 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]

Classes

struct  ImagicHeader
 

Public Member Functions

 ImagicIO (const string &fname, IOMode rw_mode=READ_ONLY)
 
 ~ImagicIO ()
 
bool is_single_image_format () const
 Is this image format only storing 1 image or not. More...
 
int get_nimg ()
 Return the number of images in this image file. More...
 
- Public Member Functions inherited from EMAN::ImageIO
 ImageIO (const string &fname, IOMode rw)
 
virtual ~ImageIO ()
 
virtual int read_header (Dict &dict, int image_index=0, const Region *area=0, bool is_3d=false)=0
 Read the header from an image. More...
 
virtual int write_header (const Dict &dict, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
 Write a header to an image. More...
 
virtual int read_data (float *data, int image_index=0, const Region *area=0, bool is_3d=false)=0
 Read the data from an image. More...
 
virtual int read_data_8bit (unsigned char *data, int image_index=0, const Region *area=0, bool is_3d=false, float minval=0.0f, float maxval=0.0f)
 Read the data from an image as an 8 bit array, regardless of format. More...
 
virtual int write_data (float *data, int image_index=0, const Region *area=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)=0
 Write data to an image. More...
 
virtual int read_ctf (Ctf &ctf, int image_index=0)
 Read CTF data from this image. More...
 
virtual void write_ctf (const Ctf &ctf, int image_index=0)
 Write CTF data to this image. More...
 
virtual void flush ()=0
 Flush the IO buffer. More...
 
virtual bool is_complex_mode ()=0
 Is this an complex image or not. More...
 
virtual bool is_image_big_endian ()=0
 Is this image in big endian or not. More...
 
template<class T >
void become_host_endian (T *data, size_t n=1)
 Convert data of this image into host endian format. More...
 
string get_filename () const
 

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. More...
 
void write_ctf (const Ctf *const ctf, int image_index=0)
 

Private Attributes

string hed_filename
 
string img_filename
 
FILE * hed_file
 
FILE * img_file
 
ImagicHeader imagich
 
bool is_big_endian
 
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 = "!-"
 

Additional Inherited Members

- Public Types inherited from EMAN::ImageIO
enum  IOMode { READ_ONLY = 1 , READ_WRITE = 2 , WRITE_ONLY = 3 }
 
- Protected Member Functions inherited from EMAN::ImageIO
virtual void init ()=0
 Do some initialization before doing the read/write. More...
 
void check_read_access (int image_index)
 Validate 'image_index' in file reading. More...
 
void check_read_access (int image_index, const float *data)
 Validate 'image_index' and 'data' in file reading. More...
 
void check_write_access (IOMode rw_mode, int image_index, int max_nimg=0)
 Validate rw_mode and image_index in file writing. More...
 
void check_write_access (IOMode rw_mode, int image_index, int max_nimg, const float *data)
 Validate rw_mode, image_index, and data pointer in file writing. More...
 
void check_region (const Region *area, const FloatSize &max_size, bool is_new_file=false, bool inbounds_only=true)
 Validate image I/O region. More...
 
void check_region (const Region *area, const IntSize &max_size, bool is_new_file=false, bool inbounds_only=true)
 Validate image I/O region. More...
 
FILE * sfopen (const string &filename, IOMode mode, bool *is_new=0, bool overwrite=false)
 Run fopen safely. More...
 
- Protected Attributes inherited from EMAN::ImageIO
string filename
 
IOMode rw_mode
 
FILE * file = nullptr
 
bool initialized = false
 

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

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
NUM_4BYTES_PRE_IXOLD 
NUM_4BYTES_AFTER_IXOLD 
NUM_4BYTES_AFTER_SPACE 

Definition at line 99 of file imagicio.h.

100 {
104 };
@ NUM_4BYTES_AFTER_SPACE
Definition: imagicio.h:103
@ NUM_4BYTES_AFTER_IXOLD
Definition: imagicio.h:102
@ NUM_4BYTES_PRE_IXOLD
Definition: imagicio.h:101

◆ DataType

Enumerator
IMAGIC_UCHAR 
IMAGIC_USHORT 
IMAGIC_FLOAT 
IMAGIC_FLOAT_COMPLEX 
IMAGIC_FFT_FLOAT_COMPLEX 
IMAGIC_UNKNOWN_TYPE 

Definition at line 89 of file imagicio.h.

Constructor & Destructor Documentation

◆ ImagicIO()

ImagicIO::ImagicIO ( const string &  fname,
IOMode  rw_mode = READ_ONLY 
)
explicit

Definition at line 50 of file imagicio.cpp.

51: ImageIO(fname, rw), hed_file(0), img_file(0)
52{
55
57 is_new_hed = false;
58 is_new_img = false;
59 memset(&imagich, 0, sizeof(ImagicHeader));
60 imagich.count = -1;
62 nz = 0;
63}
static bool is_host_big_endian()
Definition: byteorder.cpp:40
string filename
Definition: imageio.h:352
ImageIO(const string &fname, IOMode rw)
Definition: imageio.cpp:40
bool is_big_endian
Definition: imagicio.h:189
FILE * hed_file
Definition: imagicio.h:185
FILE * img_file
Definition: imagicio.h:186
ImagicHeader imagich
Definition: imagicio.h:188
bool is_new_img
Definition: imagicio.h:191
bool is_new_hed
Definition: imagicio.h:190
static const char * HED_EXT
Definition: imagicio.h:68
DataType datatype
Definition: imagicio.h:193
string img_filename
Definition: imagicio.h:183
static const char * IMG_EXT
Definition: imagicio.h:69
string hed_filename
Definition: imagicio.h:182
static string change_filename_ext(const string &old_filename, const string &new_ext)
Change a file's extension and return the new filename.
Definition: util.cpp:485

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

◆ ~ImagicIO()

ImagicIO::~ImagicIO ( )

Definition at line 65 of file imagicio.cpp.

66{
67 if (hed_file) {
68 fclose(hed_file);
69 hed_file = 0;
70 }
71
72 if (img_file) {
73 fclose(img_file);
74 img_file = 0;
75 }
76}

References hed_file, and img_file.

Member Function Documentation

◆ get_datatype_from_name()

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

Definition at line 628 of file imagicio.cpp.

629{
631
632 if (strncmp(name, "PACK",4) == 0) {
633 t = IMAGIC_UCHAR;
634 }
635 else if (strncmp(name, "INTG",4) == 0) {
636 t = IMAGIC_USHORT;
637 }
638 else if (strncmp(name, REAL_TYPE_MAGIC,4) == 0) {
639 t = IMAGIC_FLOAT;
640 }
641 else if (strncmp(name, "COMP",4) == 0) {
643 }
644 else if (strncmp(name, "RECO",4) == 0) {
646 }
647 return t;
648}
static const char * REAL_TYPE_MAGIC
Definition: imagicio.h:85

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

◆ get_datatype_size()

size_t ImagicIO::get_datatype_size ( DataType  t)
private

Definition at line 650 of file imagicio.cpp.

651{
652 size_t s = 0;
653 switch (t) {
654 case IMAGIC_UCHAR:
655 s = sizeof(unsigned char);
656 break;
657 case IMAGIC_USHORT:
658 s = sizeof(unsigned short);
659 break;
660 case IMAGIC_FLOAT:
663 s = sizeof(float);
664 break;
665 default:
666 s = 0;
667 }
668
669 return s;
670}

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

◆ get_nimg()

int ImagicIO::get_nimg ( )
virtual

Return the number of images in this image file.

Reimplemented from EMAN::ImageIO.

Definition at line 622 of file imagicio.cpp.

623{
624 init();
625 return (imagich.count + 1);
626}
virtual void init()=0
Do some initialization before doing the read/write.

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

◆ is_single_image_format()

bool EMAN::ImagicIO::is_single_image_format ( ) const
inlinevirtual

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

78 {
79 return false;
80 }

◆ is_valid()

bool ImagicIO::is_valid ( const void *  first_block)
static

Definition at line 121 of file imagicio.cpp.

122{
123 ENTERFUNC;
124
125 if (!first_block) {
126 return false;
127 }
128
129 const int *data = static_cast < const int *>(first_block);
130 int count = data[1];
131 int headrec = data[3];
132 int month = data[5];
133 int hour = data[7];
134 int nx = data[13];
135 int ny = data[12];
136
137 bool data_big_endian = ByteOrder::is_data_big_endian(&headrec);
138
139 if (data_big_endian != ByteOrder::is_host_big_endian()) {
140 ByteOrder::swap_bytes(&count);
141 ByteOrder::swap_bytes(&headrec);
142 ByteOrder::swap_bytes(&month);
146 }
147
148 const int max_dim = 1 << 20;
149 bool result = false;
150
151 if (headrec == 1 &&
152 count >= 0 && count < max_dim &&
153 nx > 0 && nx < max_dim &&
154 ny > 0 && ny < max_dim && month >= 0 && hour >= 0 && hour <= 24) {
155 result = true;
156 }
157
158 EXITFUNC;
159 return result;
160}
static void swap_bytes(T *data, size_t n=1)
swap the byte order of data with 'n' T-type elements.
Definition: byteorder.h:131
static bool is_data_big_endian(const T *small_num_addr)
given a pointer to a reasonable small integer number, return whether the number is big endian or not.
Definition: byteorder.h:76
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49

References ENTERFUNC, EXITFUNC, EMAN::ByteOrder::is_data_big_endian(), EMAN::ByteOrder::is_host_big_endian(), and EMAN::ByteOrder::swap_bytes().

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

◆ make_header_host_endian()

void ImagicIO::make_header_host_endian ( ImagicHeader hed)
private

Definition at line 690 of file imagicio.cpp.

691{
693 swap_header(hed);
694 }
695}
void swap_header(ImagicHeader &hed)
Definition: imagicio.cpp:698

References is_big_endian, EMAN::ByteOrder::is_host_big_endian(), and swap_header().

◆ read_ctf()

Ctf * ImagicIO::read_ctf ( const ImagicHeader hed) const
private

the Ctf object is a EMAN1Ctf object.

Definition at line 567 of file imagicio.cpp.

568{
569 ENTERFUNC;
570
571 Ctf * ctf_ = 0;
572 size_t n = strlen(CTF_MAGIC);
573
574 if (strncmp(imagich.label, CTF_MAGIC, n) == 0) {
575 ctf_ = new EMAN1Ctf();
576 string header_label(hed.label);
577 // Note: this block was making things crash because it assumed the following if statement
578 // was true - I added the if statement (d.woolford)
579 if (header_label.size() > 2) {
580 string sctf = "O" + header_label.substr(2);
581 ctf_->from_string(sctf);
582 }
583 }
584
585 EXITFUNC;
586 return ctf_;
587}
Ctf is the base class for all CTF model.
Definition: ctf.h:60
virtual int from_string(const string &ctf)=0
EMAN1Ctf is the CTF model used in EMAN1.
Definition: ctf.h:120
static const char * CTF_MAGIC
Definition: imagicio.h:86

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

◆ swap_header()

void ImagicIO::swap_header ( ImagicHeader hed)
private

◆ to_em_datatype()

int ImagicIO::to_em_datatype ( DataType  t)
private

Definition at line 672 of file imagicio.cpp.

673{
674 switch (t) {
675 case IMAGIC_UCHAR:
676 return EMUtil::EM_UCHAR;
677 case IMAGIC_USHORT:
678 return EMUtil::EM_USHORT;
679 case IMAGIC_FLOAT:
680 return EMUtil::EM_FLOAT;
683 default:
684 break;
685 }
686
687 return EMUtil::EM_UNKNOWN;
688}
@ EM_UCHAR
Definition: emutil.h:95
@ EM_UNKNOWN
Definition: emutil.h:93
@ EM_USHORT
Definition: emutil.h:97
@ EM_FLOAT_COMPLEX
Definition: emutil.h:104

References EMAN::EMUtil::EM_FLOAT, EMAN::EMUtil::EM_FLOAT_COMPLEX, EMAN::EMUtil::EM_UCHAR, EMAN::EMUtil::EM_UNKNOWN, EMAN::EMUtil::EM_USHORT, IMAGIC_FLOAT, IMAGIC_FLOAT_COMPLEX, IMAGIC_UCHAR, and IMAGIC_USHORT.

◆ write_ctf()

void ImagicIO::write_ctf ( const Ctf *const  ctf,
int  image_index = 0 
)
private

Definition at line 589 of file imagicio.cpp.

590{
591 ENTERFUNC;
592 init();
593
594 size_t n = strlen(CTF_MAGIC);
595 strcpy(imagich.label, CTF_MAGIC);
596 string ctf_ = ctf->to_string().substr(1);
597 strncpy(&imagich.label[n], ctf_.c_str(), sizeof(imagich.label) - n);
598
599 rewind(hed_file);
600 if (fwrite(&imagich, sizeof(ImagicHeader), 1, hed_file) != 1) {
601 throw ImageWriteException(hed_filename, "Imagic Header");
602 }
603
604 EXITFUNC;
605}
virtual string to_string() const =0
#define ImageWriteException(imagename, desc)
Definition: exception.h:223

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

Member Data Documentation

◆ CTF_MAGIC

const char * ImagicIO::CTF_MAGIC = "!-"
staticprivate

Definition at line 86 of file imagicio.h.

Referenced by read_ctf(), and write_ctf().

◆ datatype

DataType EMAN::ImagicIO::datatype
private

Definition at line 193 of file imagicio.h.

Referenced by ImagicIO().

◆ DEFINE_IMAGEIO_FUNC

EMAN::ImagicIO::DEFINE_IMAGEIO_FUNC

Definition at line 74 of file imagicio.h.

◆ HED_EXT

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

Definition at line 68 of file imagicio.h.

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

◆ hed_file

FILE* EMAN::ImagicIO::hed_file
private

Definition at line 185 of file imagicio.h.

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

◆ hed_filename

string EMAN::ImagicIO::hed_filename
private

Definition at line 182 of file imagicio.h.

Referenced by ImagicIO(), and write_ctf().

◆ imagich

ImagicHeader EMAN::ImagicIO::imagich
private

Definition at line 188 of file imagicio.h.

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

◆ IMG_EXT

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

Definition at line 69 of file imagicio.h.

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

◆ img_file

FILE* EMAN::ImagicIO::img_file
private

Definition at line 186 of file imagicio.h.

Referenced by ~ImagicIO().

◆ img_filename

string EMAN::ImagicIO::img_filename
private

Definition at line 183 of file imagicio.h.

Referenced by ImagicIO().

◆ is_big_endian

bool EMAN::ImagicIO::is_big_endian
private

Definition at line 189 of file imagicio.h.

Referenced by ImagicIO(), and make_header_host_endian().

◆ is_new_hed

bool EMAN::ImagicIO::is_new_hed
private

Definition at line 190 of file imagicio.h.

Referenced by ImagicIO().

◆ is_new_img

bool EMAN::ImagicIO::is_new_img
private

Definition at line 191 of file imagicio.h.

Referenced by ImagicIO().

◆ nz

int EMAN::ImagicIO::nz
private

Definition at line 194 of file imagicio.h.

Referenced by ImagicIO().

◆ REAL_TYPE_MAGIC

const char * ImagicIO::REAL_TYPE_MAGIC = "REAL"
staticprivate

Definition at line 85 of file imagicio.h.

Referenced by get_datatype_from_name().


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