EMAN2
Classes | Public Member Functions | Static Public Member Functions | Public Attributes | Private Types | Private Attributes | List of all members
EMAN::EmIO Class Reference

EmIO defines I/O operations on EM image format. More...

#include <emio.h>

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

Classes

struct  EMHeader
 

Public Member Functions

 EmIO (const string &fname, IOMode rw_mode=READ_ONLY)
 
 ~EmIO ()
 
- 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 int get_nimg ()
 Return the number of images in this image file. 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...
 
virtual bool is_single_image_format () const
 Is this image format only storing 1 image 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, off_t file_size=0)
 
static size_t get_mode_size (char data_type)
 
static int get_machine_type ()
 
static int to_em_datatype (char t)
 

Public Attributes

 DEFINE_IMAGEIO_FUNC
 

Private Types

enum  DataType {
  EM_EM_CHAR = 1 , EM_EM_SHORT = 2 , EM_EM_INT = 4 , EM_EM_FLOAT = 5 ,
  EM_EM_COMPLEX = 8 , EM_EM_DOUBLE = 9 , EM_EM_UNKNOWN
}
 
enum  MachineType {
  EM_OS8 = 0 , EM_VAX = 1 , EM_CONVEX = 2 , EM_SGI = 3 ,
  EM_MAC = 5 , EM_PC = 6 , EM_UNKNOWN_MACHINE
}
 

Private Attributes

EMHeader emh
 
size_t mode_size
 
DataType mode
 
bool is_big_endian
 
bool is_new_file
 

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

EmIO defines I/O operations on EM image format.

EM image = header + data with (data = nx * ny * nz).

An EM image file stores 1 single 2D or 3D image.

Definition at line 44 of file emio.h.

Member Enumeration Documentation

◆ DataType

enum EMAN::EmIO::DataType
private
Enumerator
EM_EM_CHAR 
EM_EM_SHORT 
EM_EM_INT 
EM_EM_FLOAT 
EM_EM_COMPLEX 
EM_EM_DOUBLE 
EM_EM_UNKNOWN 

Definition at line 73 of file emio.h.

74 {
75 EM_EM_CHAR = 1,
76 EM_EM_SHORT = 2,
77 EM_EM_INT = 4,
78 EM_EM_FLOAT = 5,
79 EM_EM_COMPLEX = 8,
80 EM_EM_DOUBLE = 9,
82 };
@ EM_EM_UNKNOWN
Definition: emio.h:81
@ EM_EM_CHAR
Definition: emio.h:75
@ EM_EM_FLOAT
Definition: emio.h:78
@ EM_EM_COMPLEX
Definition: emio.h:79
@ EM_EM_SHORT
Definition: emio.h:76
@ EM_EM_DOUBLE
Definition: emio.h:80
@ EM_EM_INT
Definition: emio.h:77

◆ MachineType

Enumerator
EM_OS8 
EM_VAX 
EM_CONVEX 
EM_SGI 
EM_MAC 
EM_PC 
EM_UNKNOWN_MACHINE 

Definition at line 84 of file emio.h.

85 {
86 EM_OS8 = 0,
87 EM_VAX = 1,
88 EM_CONVEX = 2,
89 EM_SGI = 3,
90 EM_MAC = 5,
91 EM_PC = 6,
93 };
@ EM_SGI
Definition: emio.h:89
@ EM_UNKNOWN_MACHINE
Definition: emio.h:92
@ EM_VAX
Definition: emio.h:87
@ EM_PC
Definition: emio.h:91
@ EM_OS8
Definition: emio.h:86
@ EM_MAC
Definition: emio.h:90
@ EM_CONVEX
Definition: emio.h:88

Constructor & Destructor Documentation

◆ EmIO()

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

Definition at line 38 of file emio.cpp.

39: ImageIO(fname, rw)
40{
41 mode_size = 0;
44 is_new_file = false;
45 memset(&emh, 0, sizeof(EMHeader));
46}
static bool is_host_big_endian()
Definition: byteorder.cpp:40
EMHeader emh
Definition: emio.h:96
size_t mode_size
Definition: emio.h:98
DataType mode
Definition: emio.h:99
bool is_new_file
Definition: emio.h:101
bool is_big_endian
Definition: emio.h:100
ImageIO(const string &fname, IOMode rw)
Definition: imageio.cpp:40

References EM_EM_UNKNOWN, emh, is_big_endian, EMAN::ByteOrder::is_host_big_endian(), is_new_file, mode, and mode_size.

◆ ~EmIO()

EmIO::~EmIO ( )

Definition at line 48 of file emio.cpp.

49{
50 if (file) {
51 fclose(file);
52 file = 0;
53 }
54}
FILE * file
Definition: imageio.h:354

References EMAN::ImageIO::file.

Member Function Documentation

◆ get_machine_type()

int EmIO::get_machine_type ( )
static

Definition at line 324 of file emio.cpp.

325{
326 int m = EM_UNKNOWN_MACHINE;
327#ifdef __sgi
328 m = EM_SGI;
329#elif defined __linux__
330 m = EM_PC;
331#elif defined __CYGWIN__
332 m = EM_PC;
333#elif defined WIN32
334 m = EM_PC;
335#elif defined MACOS
336 m = EM_MAC;
337#elif defined macintosh
338 m = EM_MAC;
339#elif defined __darwin__
340 m = EM_MAC;
341#elif defined __APPLE__
342 m = EM_MAC;
343#else
345#endif
346 return m;
347}

References EM_MAC, EM_PC, EM_SGI, and EM_UNKNOWN_MACHINE.

◆ get_mode_size()

size_t EmIO::get_mode_size ( char  data_type)
static

Definition at line 349 of file emio.cpp.

350{
351 int mode = (int) data_type;
352 switch (mode) {
353 case EM_EM_CHAR:
354 return sizeof(char);
355 case EM_EM_SHORT:
356 return sizeof(short);
357 case EM_EM_INT:
358 case EM_EM_FLOAT:
359 case EM_EM_COMPLEX:
360 return sizeof(int);
361 case EM_EM_DOUBLE:
362 return sizeof(double);
363 }
364 return 0;
365}

References EM_EM_CHAR, EM_EM_COMPLEX, EM_EM_DOUBLE, EM_EM_FLOAT, EM_EM_INT, EM_EM_SHORT, and mode.

Referenced by is_valid().

◆ is_valid()

bool EmIO::is_valid ( const void *  first_block,
off_t  file_size = 0 
)
static

Definition at line 95 of file emio.cpp.

96{
98
99 if (!first_block) {
100 return false;
101 }
102
103 const char *data = static_cast < const char *>(first_block);
104 char machine = data[0];
105 char is_new_ver = data[1];
106 char data_type = data[3];
107
108 const int *data1 = static_cast < const int *>(first_block);
109 int nx = data1[1];
110 int ny = data1[2];
111 int nz = data1[3];
112
113 bool data_big_endian = ByteOrder::is_data_big_endian(&nz);
114 if (data_big_endian != ByteOrder::is_host_big_endian()) {
118 }
119
120 const int max_dim = 1 << 20;
121
122 if (((int) machine >= EM_OS8 && machine <= EM_PC) &&
123 (is_new_ver == 0 || is_new_ver == 1) &&
124 (data_type >= EM_EM_CHAR && data_type <= EM_EM_DOUBLE) &&
125 (nx > 1 && nx < max_dim) && (ny > 0 && ny < max_dim) && (nz > 0 && nz < max_dim)) {
126 if (file_size > 0) {
127 off_t file_size1 = (off_t)nx * (off_t)ny * (off_t)nz * (off_t)get_mode_size(data_type) + (off_t)sizeof(EMHeader);
128 if (file_size == file_size1) {
129 return true;
130 }
131 }
132 else {
133 return true;
134 }
135 }
136
137 return false;
138}
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
static size_t get_mode_size(char data_type)
Definition: emio.cpp:349
#define ENTERFUNC
Definition: log.h:48

References EM_EM_CHAR, EM_EM_DOUBLE, EM_OS8, EM_PC, ENTERFUNC, get_mode_size(), 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().

◆ to_em_datatype()

int EmIO::to_em_datatype ( char  t)
static

Definition at line 302 of file emio.cpp.

303{
304 DataType type = static_cast < DataType > (t);
305 switch (type) {
306 case EM_EM_CHAR:
307 return EMUtil::EM_CHAR;
308 case EM_EM_SHORT:
309 return EMUtil::EM_SHORT;
310 case EM_EM_INT:
311 return EMUtil::EM_INT;
312 case EM_EM_FLOAT:
313 return EMUtil::EM_FLOAT;
314 case EM_EM_DOUBLE:
315 return EMUtil::EM_DOUBLE;
316 case EM_EM_COMPLEX:
318 default:
319 break;
320 }
321 return EMUtil::EM_UNKNOWN;
322}
@ EM_UNKNOWN
Definition: emutil.h:93
@ EM_FLOAT_COMPLEX
Definition: emutil.h:104
@ EM_SHORT
Definition: emutil.h:96
DataType
Definition: emio.h:74

References EMAN::EMUtil::EM_CHAR, EMAN::EMUtil::EM_DOUBLE, EM_EM_CHAR, EM_EM_COMPLEX, EM_EM_DOUBLE, EM_EM_FLOAT, EM_EM_INT, EM_EM_SHORT, EMAN::EMUtil::EM_FLOAT, EMAN::EMUtil::EM_FLOAT_COMPLEX, EMAN::EMUtil::EM_INT, EMAN::EMUtil::EM_SHORT, and EMAN::EMUtil::EM_UNKNOWN.

Member Data Documentation

◆ DEFINE_IMAGEIO_FUNC

EMAN::EmIO::DEFINE_IMAGEIO_FUNC

Definition at line 50 of file emio.h.

◆ emh

EMHeader EMAN::EmIO::emh
private

Definition at line 96 of file emio.h.

Referenced by EmIO().

◆ is_big_endian

bool EMAN::EmIO::is_big_endian
private

Definition at line 100 of file emio.h.

Referenced by EmIO().

◆ is_new_file

bool EMAN::EmIO::is_new_file
private

Definition at line 101 of file emio.h.

Referenced by EmIO().

◆ mode

DataType EMAN::EmIO::mode
private

Definition at line 99 of file emio.h.

Referenced by EmIO(), and get_mode_size().

◆ mode_size

size_t EMAN::EmIO::mode_size
private

Definition at line 98 of file emio.h.

Referenced by EmIO().


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