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

Classes

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

Public Member Functions

 ImagicIO2 (const string &fname, IOMode rw_mode=READ_ONLY)
 
 ~ImagicIO2 ()
 
int init_test ()
 
bool is_single_image_format () const
 If this file format is only for single iamge. More...
 
int get_nimg ()
 Get number of images in this 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_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. More...
 
void write_ctf (const Ctf *const ctf, int image_index=0)
 
int generate_machine_stamp () const
 

Private Attributes

string hed_filename
 
string img_filename
 
FILE * hed_file
 
FILE * img_file
 
Imagic4D 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.

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

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
NUM_4BYTES_PRE_IYLP 
NUM_4BYTES_AFTER_IXOLD 
NUM_4BYTES_AFTER_NAME 

Definition at line 115 of file imagicio2.h.

116 {
119 NUM_4BYTES_AFTER_NAME = 150 //before HISTORY
120 };
@ NUM_4BYTES_AFTER_IXOLD
Definition: imagicio2.h:118

◆ DataType

Enumerator
IMAGIC_CHAR 
IMAGIC_SHORT 
IMAGIC_FLOAT 
IMAGIC_FLOAT_COMPLEX 
IMAGIC_FFT_FLOAT_COMPLEX 
IMAGIC_UNKNOWN_TYPE 

Definition at line 104 of file imagicio2.h.

◆ RealType

Enumerator
VAX_VMS 
LINUX_WINDOWS 
SGI_IBM 

Definition at line 122 of file imagicio2.h.

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

Constructor & Destructor Documentation

◆ ImagicIO2()

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

Definition at line 51 of file imagicio2.cpp.

52: ImageIO(fname, rw), hed_file(0), img_file(0)
53{
56
58 is_new_hed = false;
59 is_new_img = false;
60 memset(&imagich, 0, sizeof(Imagic4D));
61 imagich.count = -1;
63 nz = 0;
64}
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
DataType datatype
Definition: imagicio2.h:259
static const char * IMG_EXT
Definition: imagicio2.h:73
FILE * img_file
Definition: imagicio2.h:252
string hed_filename
Definition: imagicio2.h:248
bool is_big_endian
Definition: imagicio2.h:255
static const char * HED_EXT
Definition: imagicio2.h:72
Imagic4D imagich
Definition: imagicio2.h:254
string img_filename
Definition: imagicio2.h:249
FILE * hed_file
Definition: imagicio2.h:251
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::ImagicIO2::Imagic4D::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.

◆ ~ImagicIO2()

ImagicIO2::~ImagicIO2 ( )

Definition at line 66 of file imagicio2.cpp.

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

References hed_file, and img_file.

Member Function Documentation

◆ generate_machine_stamp()

int ImagicIO2::generate_machine_stamp ( ) const
private

Definition at line 636 of file imagicio2.cpp.

637{
638 int machinestamp;
639
640#ifdef __sgi
641 machinestamp = SGI_IBM;
642#elif defined __OPENVMS__
643 machinestamp = VAX_VMS;
644#else
645 machinestamp = LINUX_WINDOWS;
646#endif
647
648 return machinestamp;
649}

References LINUX_WINDOWS, SGI_IBM, and VAX_VMS.

◆ get_datatype_from_name()

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

Definition at line 375 of file imagicio2.cpp.

376{
378
379 if (strncmp(name, "PACK",4) == 0) {
380 t = IMAGIC_CHAR;
381 }
382 else if (strncmp(name, "INTG",4) == 0) {
383 t = IMAGIC_SHORT;
384 }
385 else if (strncmp(name, REAL_TYPE_MAGIC,4) == 0) {
386 t = IMAGIC_FLOAT;
387 }
388 else if (strncmp(name, "COMP",4) == 0) {
390 }
391 else if (strncmp(name, "RECO",4) == 0) {
393 }
394 return t;
395}
static const char * REAL_TYPE_MAGIC
Definition: imagicio2.h:100

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

◆ get_datatype_size()

size_t ImagicIO2::get_datatype_size ( DataType  t) const
private

Definition at line 751 of file imagicio2.cpp.

752{
753 size_t s = 0;
754 switch (t) {
755 case IMAGIC_CHAR:
756 s = sizeof(unsigned char);
757 break;
758 case IMAGIC_SHORT:
759 s = sizeof(unsigned short);
760 break;
761 case IMAGIC_FLOAT:
764 s = sizeof(float);
765 break;
766 default:
767 s = 0;
768 }
769
770 return s;
771}

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

◆ get_nimg()

int ImagicIO2::get_nimg ( )
virtual

Get number of images in this file.

Returns
number of images

Reimplemented from EMAN::ImageIO.

Definition at line 739 of file imagicio2.cpp.

740{
741 init();
742 return imagich.count + 1;
743}
virtual void init()=0
Do some initialization before doing the read/write.

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

◆ init_test()

int ImagicIO2::init_test ( )

Definition at line 123 of file imagicio2.cpp.

124{
125 ENTERFUNC;
126
127 if (initialized) {
128 return 1;
129 }
130
131 FILE *in = fopen(hed_filename.c_str(), "rb");
132 if (!in) {
134 }
135
136 char first_block[1024];
137 size_t n = fread(first_block, sizeof(char), sizeof(first_block), in);
138
139 if (n == 0) {
140 LOGERR("file '%s' is an empty file", filename.c_str());
141 fclose(in);
142 return -1;
143 }
144 fclose(in);
145
146 const int *data = reinterpret_cast <const int *>(first_block);
147 int nx = data[13];
148 int ny = data[12];
149 int izold = data[11];
150
151 if(izold==nx*ny) {
152 EXITFUNC;
153 return -1; //old style IMAGIC file
154 }
155 else {
156 EXITFUNC;
157 return 0; //new IMAGIC4D file
158 }
159}
bool initialized
Definition: imageio.h:355
#define FileAccessException(filename)
Definition: exception.h:187
#define LOGERR
Definition: log.h:51
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49

References ENTERFUNC, EXITFUNC, FileAccessException, EMAN::ImageIO::filename, hed_filename, EMAN::ImageIO::initialized, and LOGERR.

◆ is_single_image_format()

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

If this file format is only for single iamge.

Returns
false this file format support stack

Reimplemented from EMAN::ImageIO.

Definition at line 89 of file imagicio2.h.

90 {
91 return false;
92 }

◆ is_valid()

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

Definition at line 161 of file imagicio2.cpp.

162{
163 ENTERFUNC;
164
165 if (!first_block) {
166 return false;
167 }
168
169 const int *data = static_cast < const int *>(first_block);
170 int count = data[1];
171 int headrec = data[3];
172 int hour = data[7];
173 int minute = data[8];
174 int second = data[9];
175 int rsize = data[10];
176 int nx = data[13];
177 int ny = data[12];
178 int nz = data[60];
179 int realtype = data[68];
180
181 bool data_big_endian = ByteOrder::is_data_big_endian(&headrec);
182
183 if (data_big_endian != ByteOrder::is_host_big_endian()) {
184 ByteOrder::swap_bytes(&count);
185 ByteOrder::swap_bytes(&headrec);
187 ByteOrder::swap_bytes(&rsize);
191 ByteOrder::swap_bytes(&realtype);
192 }
193
194 const int max_dim = 1 << 20;
195 bool result = false;
196
197 // this field realtype is unique to new Imagic-5 format
198 if(realtype != VAX_VMS && realtype != LINUX_WINDOWS && realtype != SGI_IBM) {
199 EXITFUNC;
200 return result;
201 }
202
203 if (headrec == 1 &&
204 count >= 0 && count < max_dim &&
205 nx > 0 && nx < max_dim &&
206 ny > 0 && ny < max_dim &&
207 nz > 0 && nz < max_dim &&
208 hour >= 0 && hour < 24 &&
209 minute >=0 && minute <60 &&
210 second >=0 && second <60) {
211 result = true;
212 }
213
214 EXITFUNC;
215 return result;
216}
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

References ENTERFUNC, EXITFUNC, EMAN::ByteOrder::is_data_big_endian(), EMAN::ByteOrder::is_host_big_endian(), LINUX_WINDOWS, nz, SGI_IBM, EMAN::ByteOrder::swap_bytes(), and VAX_VMS.

◆ make_header_host_endian()

void ImagicIO2::make_header_host_endian ( Imagic4D hed) const
private

Definition at line 651 of file imagicio2.cpp.

652{
654 swap_header(hed);
655 }
656}
void swap_header(Imagic4D &hed) const
Definition: imagicio2.cpp:658

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

◆ read_ctf()

Ctf * ImagicIO2::read_ctf ( const Imagic4D hed) const
private

the Ctf object is a EMAN1Ctf object.

Definition at line 416 of file imagicio2.cpp.

417{
418 ENTERFUNC;
419
420 Ctf * ctf_ = 0;
421 size_t n = strlen(CTF_MAGIC);
422
423 if (strncmp(imagich.label, CTF_MAGIC, n) == 0) {
424 ctf_ = new EMAN1Ctf();
425 string header_label(hed.label);
426 // Note: this block was making things crash because it assumed the following if statement
427 // was true - I added the if statement (d.woolford)
428 if (header_label.size() > 2) {
429 string sctf = "O" + header_label.substr(2);
430 ctf_->from_string(sctf);
431 }
432 }
433
434 EXITFUNC;
435 return ctf_;
436}
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: imagicio2.h:101

References CTF_MAGIC, ENTERFUNC, EXITFUNC, EMAN::Ctf::from_string(), imagich, and EMAN::ImagicIO2::Imagic4D::label.

◆ swap_header()

void ImagicIO2::swap_header ( Imagic4D hed) const
private

◆ to_em_datatype()

int ImagicIO2::to_em_datatype ( DataType  t) const
private

◆ write_ctf()

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

Definition at line 438 of file imagicio2.cpp.

439{
440 ENTERFUNC;
441 init();
442
443 size_t n = strlen(CTF_MAGIC);
444 strcpy(imagich.label, CTF_MAGIC);
445 string ctf_ = ctf->to_string().substr(1);
446
447 //pad ctf_ to 80 char
448 if(ctf_.size()>80) {
449 ctf_ = ctf_.substr(0, 80);
450 }
451 else {
452 string padded(80 - ctf_.size(), ' ');
453 ctf_ = ctf_ + padded;
454 }
455
456 strncpy(&imagich.label[n], ctf_.c_str(), sizeof(imagich.label) - n);
457
458 rewind(hed_file);
459 if (fwrite(&imagich, sizeof(Imagic4D), 1, hed_file) != 1) {
460 throw ImageWriteException(hed_filename, "Imagic Header");
461 }
462
463 EXITFUNC;
464}
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::ImagicIO2::Imagic4D::label, and EMAN::Ctf::to_string().

Member Data Documentation

◆ CTF_MAGIC

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

Definition at line 101 of file imagicio2.h.

Referenced by read_ctf(), and write_ctf().

◆ datatype

DataType EMAN::ImagicIO2::datatype
private

Definition at line 259 of file imagicio2.h.

Referenced by ImagicIO2().

◆ DEFINE_IMAGEIO_FUNC

EMAN::ImagicIO2::DEFINE_IMAGEIO_FUNC

Definition at line 78 of file imagicio2.h.

◆ HED_EXT

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

Definition at line 72 of file imagicio2.h.

Referenced by ImagicIO2().

◆ hed_file

FILE* EMAN::ImagicIO2::hed_file
private

Definition at line 251 of file imagicio2.h.

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

◆ hed_filename

string EMAN::ImagicIO2::hed_filename
private

Definition at line 248 of file imagicio2.h.

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

◆ imagich

Imagic4D EMAN::ImagicIO2::imagich
private

Definition at line 254 of file imagicio2.h.

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

◆ IMG_EXT

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

Definition at line 73 of file imagicio2.h.

Referenced by ImagicIO2().

◆ img_file

FILE* EMAN::ImagicIO2::img_file
private

Definition at line 252 of file imagicio2.h.

Referenced by ~ImagicIO2().

◆ img_filename

string EMAN::ImagicIO2::img_filename
private

Definition at line 249 of file imagicio2.h.

Referenced by ImagicIO2().

◆ is_big_endian

bool EMAN::ImagicIO2::is_big_endian
private

Definition at line 255 of file imagicio2.h.

Referenced by ImagicIO2(), and make_header_host_endian().

◆ is_new_hed

bool EMAN::ImagicIO2::is_new_hed
private

Definition at line 256 of file imagicio2.h.

Referenced by ImagicIO2().

◆ is_new_img

bool EMAN::ImagicIO2::is_new_img
private

Definition at line 257 of file imagicio2.h.

Referenced by ImagicIO2().

◆ nz

int EMAN::ImagicIO2::nz
private

Definition at line 260 of file imagicio2.h.

Referenced by ImagicIO2(), and is_valid().

◆ REAL_TYPE_MAGIC

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

Definition at line 100 of file imagicio2.h.

Referenced by get_datatype_from_name().


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