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

PIF(Portable Image Format for EM Data) is an image format from Purdue University. More...

#include <pifio.h>

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

Classes

struct  PifColorMap
 
struct  PifFileHeader
 
struct  PifImageHeader
 

Public Member Functions

 PifIO (const string &fname, IOMode rw_mode=READ_ONLY)
 
 ~PifIO ()
 
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
 

Private Types

enum  { PIF_MAGIC_NUM = 8 }
 
enum  PifDataMode {
  PIF_CHAR = 0 , PIF_SHORT = 1 , PIF_FLOAT_INT = 2 , PIF_SHORT_COMPLEX = 3 ,
  PIF_FLOAT_INT_COMPLEX = 4 , PIF_BOXED_DATA = 6 , PIF_SHORT_FLOAT = 7 , PIF_SHORT_FLOAT_COMPLEX = 8 ,
  PIF_FLOAT = 9 , PIF_FLOAT_COMPLEX = 10 , PIF_MAP_FLOAT_SHORT = 20 , PIF_MAP_FLOAT_INT = 21 ,
  PIF_MAP_FLOAT_INT_2 = 40 , PIF_BOXED_FLOAT_INT = 46 , PIF_INVALID
}
 

Private Member Functions

int get_mode_size (PifDataMode mode)
 
bool is_float_int (int mode)
 
void fseek_to (int image_index)
 
int to_em_datatype (int pif_datatype)
 
int to_pif_datatype (int em_datatype)
 

Private Attributes

string filename
 
PifFileHeader pfh
 
int mode_size
 
bool is_big_endian
 
bool is_new_file
 
float real_scale_factor
 

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

PIF(Portable Image Format for EM Data) is an image format from Purdue University.

A PIF file = file header + (image header + image data) + (image header + image data) ...

A PIF file has a overall file header followed by n images. Each image has a header and data block.

EMAN only supports homogeneous PIF file, which means all images n a PIF should have the same dimensions. We also assume the (nx,ny,nz) in PifFileHeader are equal to (nx,ny,nz) in each Image header.

Definition at line 52 of file pifio.h.

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
private
Enumerator
PIF_MAGIC_NUM 

Definition at line 68 of file pifio.h.

69 {
71 };
@ PIF_MAGIC_NUM
Definition: pifio.h:70

◆ PifDataMode

Enumerator
PIF_CHAR 
PIF_SHORT 
PIF_FLOAT_INT 
PIF_SHORT_COMPLEX 
PIF_FLOAT_INT_COMPLEX 
PIF_BOXED_DATA 
PIF_SHORT_FLOAT 
PIF_SHORT_FLOAT_COMPLEX 
PIF_FLOAT 
PIF_FLOAT_COMPLEX 
PIF_MAP_FLOAT_SHORT 
PIF_MAP_FLOAT_INT 
PIF_MAP_FLOAT_INT_2 
PIF_BOXED_FLOAT_INT 
PIF_INVALID 

Definition at line 73 of file pifio.h.

74 {
75 PIF_CHAR = 0,
76 PIF_SHORT = 1,
77 PIF_FLOAT_INT = 2,
80 PIF_BOXED_DATA = 6, // byte, read as 0
83 PIF_FLOAT = 9,
87 PIF_MAP_FLOAT_INT_2 = 40, // 4 byte floatint, read as 2
88 PIF_BOXED_FLOAT_INT = 46, // 4 byte floatint, read as 2
90 };
@ PIF_FLOAT_INT
Definition: pifio.h:77
@ PIF_SHORT
Definition: pifio.h:76
@ PIF_SHORT_COMPLEX
Definition: pifio.h:78
@ PIF_FLOAT_COMPLEX
Definition: pifio.h:84
@ PIF_MAP_FLOAT_SHORT
Definition: pifio.h:85
@ PIF_BOXED_FLOAT_INT
Definition: pifio.h:88
@ PIF_SHORT_FLOAT
Definition: pifio.h:81
@ PIF_FLOAT
Definition: pifio.h:83
@ PIF_BOXED_DATA
Definition: pifio.h:80
@ PIF_MAP_FLOAT_INT
Definition: pifio.h:86
@ PIF_CHAR
Definition: pifio.h:75
@ PIF_INVALID
Definition: pifio.h:89
@ PIF_MAP_FLOAT_INT_2
Definition: pifio.h:87
@ PIF_FLOAT_INT_COMPLEX
Definition: pifio.h:79
@ PIF_SHORT_FLOAT_COMPLEX
Definition: pifio.h:82

Constructor & Destructor Documentation

◆ PifIO()

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

Definition at line 44 of file pifio.cpp.

45: ImageIO(fname, rw)
46{
47 file = 0;
48 mode_size = 0;
51 is_new_file = false;
52 memset(&pfh, 0, sizeof(PifFileHeader));
53}
static bool is_host_big_endian()
Definition: byteorder.cpp:40
ImageIO(const string &fname, IOMode rw)
Definition: imageio.cpp:40
FILE * file
Definition: imageio.h:354
int mode_size
Definition: pifio.h:176
bool is_new_file
Definition: pifio.h:178
PifFileHeader pfh
Definition: pifio.h:175
bool is_big_endian
Definition: pifio.h:177
float real_scale_factor
Definition: pifio.h:179

References EMAN::ImageIO::file, is_big_endian, EMAN::ByteOrder::is_host_big_endian(), is_new_file, mode_size, pfh, and real_scale_factor.

◆ ~PifIO()

PifIO::~PifIO ( )

Definition at line 55 of file pifio.cpp.

56{
57 if (file) {
58 fclose(file);
59 file = 0;
60 }
61}

References EMAN::ImageIO::file.

Member Function Documentation

◆ fseek_to()

void PifIO::fseek_to ( int  image_index)
private

Definition at line 193 of file pifio.cpp.

194{
195 int pih_sz = sizeof(PifImageHeader);
196 int image_size = 0;
197
198#if 0
199 // this works for some images that PURDUE people gave to me.
200 // But those images don't follow the PIF specification. So
201 // I believe they are in wrong format.
202 if (pfh.nimg == 1) {
203 image_size = pfh.nx * pfh.ny * pfh.nz;
204 }
205 else {
206 image_size = pfh.nx * pfh.ny;
207 }
208#endif
209 image_size = pfh.nx * pfh.ny * pfh.nz;
210
211 size_t file_offset = sizeof(PifFileHeader) +
212 (pih_sz + image_size * mode_size) * image_index;
213
214 portable_fseek(file, file_offset, SEEK_SET);
215}
int portable_fseek(FILE *fp, off_t offset, int whence)

References EMAN::ImageIO::file, mode_size, EMAN::PifIO::PifFileHeader::nimg, EMAN::PifIO::PifFileHeader::nx, EMAN::PifIO::PifFileHeader::ny, EMAN::PifIO::PifFileHeader::nz, pfh, and portable_fseek().

◆ get_mode_size()

int PifIO::get_mode_size ( PifDataMode  mode)
private

Definition at line 64 of file pifio.cpp.

65{
66 int size = 0;
67
68 switch (mode) {
69 case PIF_CHAR:
70 case PIF_BOXED_DATA:
71 size = sizeof(char);
72 break;
73 case PIF_SHORT:
74 case PIF_SHORT_FLOAT:
78 size = sizeof(short);
79 break;
80 case PIF_FLOAT:
82 size = sizeof(float);
83 break;
84 case PIF_FLOAT_INT:
89 size = sizeof(int);
90 break;
91 default:
92 break;
93 }
94 return size;
95}

References PIF_BOXED_DATA, PIF_BOXED_FLOAT_INT, PIF_CHAR, PIF_FLOAT, PIF_FLOAT_COMPLEX, PIF_FLOAT_INT, PIF_FLOAT_INT_COMPLEX, PIF_MAP_FLOAT_INT, PIF_MAP_FLOAT_INT_2, PIF_MAP_FLOAT_SHORT, PIF_SHORT, PIF_SHORT_COMPLEX, PIF_SHORT_FLOAT, and PIF_SHORT_FLOAT_COMPLEX.

◆ get_nimg()

int PifIO::get_nimg ( )
virtual

Return the number of images in this image file.

Reimplemented from EMAN::ImageIO.

Definition at line 540 of file pifio.cpp.

541{
542 init();
543 return pfh.nimg;
544}
virtual void init()=0
Do some initialization before doing the read/write.

References EMAN::ImageIO::init(), EMAN::PifIO::PifFileHeader::nimg, and pfh.

◆ is_float_int()

bool PifIO::is_float_int ( int  mode)
private

Definition at line 97 of file pifio.cpp.

98{
99 PifDataMode mode = static_cast < PifDataMode > (m);
100 switch (mode) {
101 case PIF_SHORT_FLOAT:
103 //case PIF_FLOAT:
104 case PIF_FLOAT_INT:
105 //case PIF_FLOAT_COMPLEX:
111 return true;
112 default:
113 break;
114 }
115 return false;
116}
PifDataMode
Definition: pifio.h:74

References PIF_BOXED_FLOAT_INT, PIF_FLOAT_INT, PIF_FLOAT_INT_COMPLEX, PIF_MAP_FLOAT_INT, PIF_MAP_FLOAT_INT_2, PIF_MAP_FLOAT_SHORT, PIF_SHORT_FLOAT, and PIF_SHORT_FLOAT_COMPLEX.

◆ is_single_image_format()

bool EMAN::PifIO::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 61 of file pifio.h.

62 {
63 return false;
64 }

◆ is_valid()

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

Definition at line 164 of file pifio.cpp.

165{
166 ENTERFUNC;
167 bool result = false;
168
169 if (first_block) {
170 const int *data = static_cast < const int *>(first_block);
171 int m1 = data[0];
172 int m2 = data[1];
173 int endian = data[7];
174 bool data_big_endian = false;
175 if (endian) {
176 data_big_endian = true;
177 }
178
179 if (data_big_endian != ByteOrder::is_host_big_endian()) {
182 }
183
184 if (m1 == PIF_MAGIC_NUM && m2 == PIF_MAGIC_NUM) {
185 result = true;
186 }
187 }
188
189 EXITFUNC;
190 return result;
191}
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
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49

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

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

◆ to_em_datatype()

int PifIO::to_em_datatype ( int  pif_datatype)
private

Definition at line 547 of file pifio.cpp.

548{
549 PifDataMode mode = static_cast < PifDataMode > (p);
551
552 switch (mode) {
553 case PIF_CHAR:
554 case PIF_BOXED_DATA:
555 e = EMUtil::EM_CHAR;
556 break;
557
558 case PIF_SHORT:
559 case PIF_SHORT_FLOAT:
562 break;
563
567 break;
568
569 case PIF_FLOAT:
570 case PIF_FLOAT_INT:
575 break;
579 break;
580 case PIF_INVALID:
582 break;
583 }
584 return e;
585}
EMDataType
Image pixel data type used in EMAN.
Definition: emutil.h:92
@ EM_UNKNOWN
Definition: emutil.h:93
@ EM_FLOAT_COMPLEX
Definition: emutil.h:104
@ EM_SHORT_COMPLEX
Definition: emutil.h:102
@ EM_SHORT
Definition: emutil.h:96

References EMAN::EMUtil::EM_CHAR, EMAN::EMUtil::EM_FLOAT, EMAN::EMUtil::EM_FLOAT_COMPLEX, EMAN::EMUtil::EM_SHORT, EMAN::EMUtil::EM_SHORT_COMPLEX, EMAN::EMUtil::EM_UNKNOWN, PIF_BOXED_DATA, PIF_BOXED_FLOAT_INT, PIF_CHAR, PIF_FLOAT, PIF_FLOAT_COMPLEX, PIF_FLOAT_INT, PIF_FLOAT_INT_COMPLEX, PIF_INVALID, PIF_MAP_FLOAT_INT, PIF_MAP_FLOAT_INT_2, PIF_MAP_FLOAT_SHORT, PIF_SHORT, PIF_SHORT_COMPLEX, PIF_SHORT_FLOAT, and PIF_SHORT_FLOAT_COMPLEX.

◆ to_pif_datatype()

int PifIO::to_pif_datatype ( int  em_datatype)
private

Definition at line 587 of file pifio.cpp.

588{
590
591 switch (e) {
592 case EMUtil::EM_CHAR:
593 m = PIF_BOXED_DATA;
594 break;
595 case EMUtil::EM_SHORT:
596 m = PIF_SHORT;
597 break;
600 break;
601 case EMUtil::EM_FLOAT:
602 m = PIF_FLOAT_INT;
603 break;
606 break;
607 default:
608 LOGERR("unknown PIF mode: %d", e);
609 }
610
611 return m;
612}
#define LOGERR
Definition: log.h:51

References EMAN::EMUtil::EM_CHAR, EMAN::EMUtil::EM_FLOAT, EMAN::EMUtil::EM_FLOAT_COMPLEX, EMAN::EMUtil::EM_SHORT, EMAN::EMUtil::EM_SHORT_COMPLEX, LOGERR, PIF_BOXED_DATA, PIF_FLOAT_COMPLEX, PIF_FLOAT_INT, PIF_INVALID, PIF_SHORT, and PIF_SHORT_COMPLEX.

Member Data Documentation

◆ DEFINE_IMAGEIO_FUNC

EMAN::PifIO::DEFINE_IMAGEIO_FUNC

Definition at line 58 of file pifio.h.

◆ filename

string EMAN::PifIO::filename
private

Definition at line 174 of file pifio.h.

◆ is_big_endian

bool EMAN::PifIO::is_big_endian
private

Definition at line 177 of file pifio.h.

Referenced by PifIO().

◆ is_new_file

bool EMAN::PifIO::is_new_file
private

Definition at line 178 of file pifio.h.

Referenced by PifIO().

◆ mode_size

int EMAN::PifIO::mode_size
private

Definition at line 176 of file pifio.h.

Referenced by fseek_to(), and PifIO().

◆ pfh

PifFileHeader EMAN::PifIO::pfh
private

Definition at line 175 of file pifio.h.

Referenced by fseek_to(), get_nimg(), and PifIO().

◆ real_scale_factor

float EMAN::PifIO::real_scale_factor
private

Definition at line 179 of file pifio.h.

Referenced by PifIO().


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