EMAN2
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
EMAN::ImageIO Class Referenceabstract

ImageIO classes are designed for reading/writing various electron micrography image formats, including MRC, IMAGIC, SPIDER, PIF, etc. More...

#include <imageio.h>

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

Public Types

enum  IOMode { READ_ONLY = 1 , READ_WRITE = 2 , WRITE_ONLY = 3 }
 

Public Member Functions

 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
 

Protected Member Functions

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

string filename
 
IOMode rw_mode
 
FILE * file = nullptr
 
bool initialized = false
 

Detailed Description

ImageIO classes are designed for reading/writing various electron micrography image formats, including MRC, IMAGIC, SPIDER, PIF, etc.

ImageIO class is the base class for all image io classes. Each subclass defines the IO routines for reading/writing a type of image format. For example, MrcIO is for reading/writing MRC images.

A subclass should implement functions declared in DEFINE_IMAGEIO_FUNC macro.

Image header I/O is separated from image data I/O.

Some image formats (e.g., MRC, DM3, Gatan2, TIFF, PNG, EM, ICOS) only store a single 2D or 3D images. For them, image_index should always be 0. To read/write part of the image, use a region.

Some image formats (e.g., Imagic) can store either a stack of 2D images or a single 3D image. From the physical image file itself, there is no difference between them. Only at reading time, they may be treated as either a stack of 2D images, or a single 3D image. A boolean hint should be given for these formats at reading time.

Some image formats (e.g. HDF, PIF) can store a stack of images. Each image can be 2D or 3D.

For image formats storing multiple images, valid image_index = [0, n].

For image formats storing multiple images, the image append and insertion policy is:

Image region writing follows the following principles:

Each ImageIO subclass XYZ must define a static function to determine whether a given image is a valid XYZ image or not. It looks like: static bool is_valid(const void *first_block); 'first_block' is the first block of binary data in that image file.

The typical way to use an ImageIO instance is: a) To read: ImageIO *imageio = EMUtil::get_imageio(filename, ImageIO::READ_ONLY); int err = imageio->read_header(dict, img_index, region, is_3d); err = imageio->read_ctf(ctf, img_index); err = imageio->read_data(data, img_index, region, is_3d);

b) To write: similar to read.

Definition at line 126 of file imageio.h.

Member Enumeration Documentation

◆ IOMode

Enumerator
READ_ONLY 
READ_WRITE 
WRITE_ONLY 

Definition at line 129 of file imageio.h.

130{ READ_ONLY = 1, READ_WRITE = 2, WRITE_ONLY = 3 };

Constructor & Destructor Documentation

◆ ImageIO()

ImageIO::ImageIO ( const string &  fname,
IOMode  rw 
)

Definition at line 40 of file imageio.cpp.

41 : filename(fname), rw_mode(rw)
42{}
IOMode rw_mode
Definition: imageio.h:353
string filename
Definition: imageio.h:352

◆ ~ImageIO()

ImageIO::~ImageIO ( )
virtual

Definition at line 44 of file imageio.cpp.

45{
46}

Member Function Documentation

◆ become_host_endian()

template<class T >
void EMAN::ImageIO::become_host_endian ( T *  data,
size_t  n = 1 
)
inline

Convert data of this image into host endian format.

Parameters
dataAn array of data. It can be any type, short, int, float, double, etc.
nArray size.

Definition at line 261 of file imageio.h.

262 {
264 ByteOrder::swap_bytes(data, n);
265 }
266 }
static bool is_host_big_endian()
Definition: byteorder.cpp:40
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
virtual bool is_image_big_endian()=0
Is this image in big endian or not.

References EMAN::ByteOrder::is_host_big_endian(), is_image_big_endian(), and EMAN::ByteOrder::swap_bytes().

◆ check_read_access() [1/2]

void ImageIO::check_read_access ( int  image_index)
protected

Validate 'image_index' in file reading.

Parameters
image_indexThe 'image_index'th image. Valid value = [0, nimg-1].
Exceptions
OutofRangeExceptionIf image_index is out of range.

Definition at line 95 of file imageio.cpp.

96{
97 init();
98
99 int nimg = get_nimg();
100 if (image_index < 0 || image_index >= nimg) {
101 throw OutofRangeException(0, nimg-1, image_index, "image index");
102 }
103}
virtual int get_nimg()
Return the number of images in this image file.
Definition: imageio.cpp:176
virtual void init()=0
Do some initialization before doing the read/write.
#define OutofRangeException(low, high, input, objname)
Definition: exception.h:334

References get_nimg(), init(), and OutofRangeException.

Referenced by check_read_access().

◆ check_read_access() [2/2]

void ImageIO::check_read_access ( int  image_index,
const float *  data 
)
protected

Validate 'image_index' and 'data' in file reading.

Parameters
image_indexThe 'image_index'th image. Valid value = [0, nimg-1].
dataThe array used to store the image data in reading.
Exceptions
NullPointerExceptionIf 'data' is NULL.
OutofRangeExceptionIf image_index is out of range.

Definition at line 105 of file imageio.cpp.

106{
107 check_read_access(image_index);
108 if (!data) {
109 throw NullPointerException("image data is NULL");
110 }
111}
void check_read_access(int image_index)
Validate 'image_index' in file reading.
Definition: imageio.cpp:95
#define NullPointerException(desc)
Definition: exception.h:241

References check_read_access(), and NullPointerException.

◆ check_region() [1/2]

void ImageIO::check_region ( const Region area,
const FloatSize max_size,
bool  is_new_file = false,
bool  inbounds_only = true 
)
protected

Validate image I/O region.

Parameters
areaThe image I/O region.
max_sizeThe upper limit of the region's size. The region must be within 'max_size'.
is_new_fileWhether it is on a new file or not.
inbounds_onlyif true verifies that the region is inside the image, otherwise no check is performed
Exceptions
ImageReadExceptionAny image reading problem.

Definition at line 58 of file imageio.cpp.

60{
61 if (area) {
62 if (is_new_file) {
63 throw ImageReadException("", "file must exist before accessing its region");
64 }
65 int img_ndim = max_size.get_ndim();
66 int area_ndim = area->get_ndim();
67
68 if (area_ndim > img_ndim) {
69 char desc[256];
70 sprintf(desc, "Image is %dD. Cannot read %dD region", img_ndim, area_ndim);
71 throw ImageReadException("", desc);
72 }
73
74 // EMUtil::process_region_io handles regions that are outside the image. So some image types don't mind if the
75 // region is beyond the boundary. It would be ideal if they all could do this, but it would take some work.
76 if (inbounds_only ){
77 if (!area->is_region_in_box(max_size)) {
78 char desc[1024];
79 sprintf(desc, "Region box %s is outside image area (%d,%d,%d)",
80 area->get_string().c_str(), (int)max_size[0],
81 (int)max_size[1], (int)max_size[2]);
82 throw ImageReadException("", desc);
83 }
84 }
85 }
86}
int get_ndim() const
Get its dimension, 1D, 2D, or 3D.
Definition: geometry.h:147
int get_ndim() const
Get the region's dimension.
Definition: geometry.h:644
string get_string() const
Get the description of this region in a string.
Definition: geometry.cpp:138
bool is_region_in_box(const FloatSize &box) const
To check whether 'this' region is inside a given box assuming the box's origins are (0,...
Definition: geometry.cpp:123
#define ImageReadException(filename, desc)
Definition: exception.h:204

References EMAN::FloatSize::get_ndim(), EMAN::Region::get_ndim(), EMAN::Region::get_string(), ImageReadException, and EMAN::Region::is_region_in_box().

Referenced by check_region(), EMAN::MrcIO::read_fei_header(), EMAN::MrcIO::read_mrc_header(), EMAN::SpiderIO::write_single_data(), and EMAN::SpiderIO::write_single_header().

◆ check_region() [2/2]

void ImageIO::check_region ( const Region area,
const IntSize max_size,
bool  is_new_file = false,
bool  inbounds_only = true 
)
protected

Validate image I/O region.

Parameters
areaThe image I/O region.
max_sizeThe upper limit of the region's size. The region must be within 'max_size'.
is_new_fileWhether it is on a new file or not.
inbounds_onlyif true verifies that the region is inside the image, otherwise no check is performed
Exceptions
ImageReadExceptionAny image reading problem.

Definition at line 88 of file imageio.cpp.

90{
91 check_region(area, FloatSize(max_size[0], max_size[1], max_size[2]),
92 is_new_file,inbounds_only);
93}
FloatSize is used to describe a 1D, 2D or 3D rectangular size in floating numbers.
Definition: geometry.h:105
void check_region(const Region *area, const FloatSize &max_size, bool is_new_file=false, bool inbounds_only=true)
Validate image I/O region.
Definition: imageio.cpp:58

References check_region().

◆ check_write_access() [1/2]

void ImageIO::check_write_access ( IOMode  rw_mode,
int  image_index,
int  max_nimg,
const float *  data 
)
protected

Validate rw_mode, image_index, and data pointer in file writing.

Parameters
rw_modeFile Read/Write mode.
image_indexThe 'image_index'th image. Valid value = [0, max_nimg].
max_nimgMaximum number of images in the file. If its value <= 0, don't check image_index againt max_nimg.
dataThe data array to be writing to the image file.
Exceptions
ImageWriteExceptionImage is not opened for writing.
OutofRangeExceptionIf image_index is out of range.

Definition at line 126 of file imageio.cpp.

128{
129 check_write_access(iomode, image_index, max_nimg);
130 if (!data) {
131 throw NullPointerException("image data is NULL");
132 }
133}
void check_write_access(IOMode rw_mode, int image_index, int max_nimg=0)
Validate rw_mode and image_index in file writing.
Definition: imageio.cpp:113

References check_write_access(), and NullPointerException.

◆ check_write_access() [2/2]

void ImageIO::check_write_access ( IOMode  rw_mode,
int  image_index,
int  max_nimg = 0 
)
protected

Validate rw_mode and image_index in file writing.

Parameters
rw_modeFile Read/Write mode.
image_indexThe 'image_index'th image. Valid value = [0, max_nimg].
max_nimgMaximum number of images in the file. If its value <= 0, don't check image_index against max_nimg.
Exceptions
ImageWriteExceptionImage is not opened for writing.
OutofRangeExceptionIf image_index is out of range.

Definition at line 113 of file imageio.cpp.

114{
115 init();
116
117 if (iomode == READ_ONLY) {
118 throw ImageWriteException("", "File is not opened to write");
119 }
120
121 if ((image_index < -1) || (max_nimg > 0 && image_index >= max_nimg)) {
122 throw OutofRangeException(-1, max_nimg - 1, image_index, "image index");
123 }
124}
#define ImageWriteException(imagename, desc)
Definition: exception.h:223

References ImageWriteException, init(), OutofRangeException, and READ_ONLY.

Referenced by check_write_access(), EMAN::SpiderIO::write_single_data(), and EMAN::SpiderIO::write_single_header().

◆ flush()

virtual void EMAN::ImageIO::flush ( )
pure virtual

Flush the IO buffer.

◆ get_filename()

string EMAN::ImageIO::get_filename ( ) const
inline

Definition at line 268 of file imageio.h.

268 {
269 return filename;
270 }

References filename.

◆ get_nimg()

int ImageIO::get_nimg ( )
virtual

Return the number of images in this image file.

Reimplemented in EMAN::DM4IO, EMAN::EerIO, EMAN::ImagicIO, EMAN::ImagicIO2, EMAN::LstFastIO, EMAN::LstIO, EMAN::MrcIO, EMAN::PifIO, EMAN::SerIO, EMAN::SpiderIO, and EMAN::XYZIO.

Definition at line 176 of file imageio.cpp.

177{
178 init();
179 return 1;
180}

References init().

Referenced by check_read_access(), and EMAN::EMUtil::get_image_count().

◆ init()

virtual void EMAN::ImageIO::init ( )
protectedpure virtual

◆ is_complex_mode()

virtual bool EMAN::ImageIO::is_complex_mode ( )
pure virtual

Is this an complex image or not.

Referenced by read_binedimage(), and EMAN::MrcIO::read_mrc_header().

◆ is_image_big_endian()

virtual bool EMAN::ImageIO::is_image_big_endian ( )
pure virtual

Is this image in big endian or not.

Referenced by become_host_endian(), and EMAN::EMData::save_byteorder_to_dict().

◆ is_single_image_format()

virtual bool EMAN::ImageIO::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 in EMAN::ImagicIO, EMAN::ImagicIO2, EMAN::LstFastIO, EMAN::LstIO, EMAN::PifIO, EMAN::SpiderIO, EMAN::SingleSpiderIO, and EMAN::EerIO.

Definition at line 250 of file imageio.h.

251 {
252 return true;
253 }

◆ read_ctf()

int ImageIO::read_ctf ( Ctf ctf,
int  image_index = 0 
)
virtual

Read CTF data from this image.

Parameters
ctfUsed to store the CTF data.
image_indexThe index of the image to read.
Returns
0 if OK; 1 if error.

Reimplemented in EMAN::FitsIO, and EMAN::MrcIO.

Definition at line 48 of file imageio.cpp.

49{
50 return 1;
51}

◆ read_data()

virtual int EMAN::ImageIO::read_data ( float *  data,
int  image_index = 0,
const Region area = 0,
bool  is_3d = false 
)
pure virtual

Read the data from an image.

Parameters
dataAn array to store the data. It should be created outside of this function.
image_indexThe index of the image to read.
areaDefine an image-region to read.
is_3dWhether to treat the image as a single 3D or a set of 2Ds. This is a hint for certain image formats which has no difference between 3D image and set of 2Ds.
Returns
0 if OK; 1 if error.

◆ read_data_8bit()

virtual int EMAN::ImageIO::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 
)
inlinevirtual

Read the data from an image as an 8 bit array, regardless of format.

Parameters
dataAn array to store the data. It should be created outside of this function.
image_indexThe index of the image to read (image number in file).
areaIf provided reads only the specified region.
is_3dWhether to treat the image as a single 3D or a set of 2Ds. This is a hint for certain image formats which has no difference between 3D image and set of 2Ds.
minvalimage value to map to 0 in the returned unsigned char
maxvalimage value to map to 255 in the returned unsigned char. If minval==maxval, autoscaling will be used.
Returns
0 if OK; 1 if error.

Definition at line 193 of file imageio.h.

194 {
195 throw ImageFormatException("8 bit reading not supported for this format");
196 }
#define ImageFormatException(desc)
Definition: exception.h:147

References ImageFormatException.

◆ read_header()

virtual int EMAN::ImageIO::read_header ( Dict dict,
int  image_index = 0,
const Region area = 0,
bool  is_3d = false 
)
pure virtual

Read the header from an image.

Parameters
dictA keyed-dictionary to store the header information.
image_indexThe index of the image to read.
areaDefine an image-region to read.
is_3dWhether to treat the image as a single 3D or a set of 2Ds. This is a hint for certain image formats which has no difference between 3D image and set of 2Ds.
Returns
0 if OK; 1 if error.

Referenced by read_binedimage().

◆ sfopen()

FILE * ImageIO::sfopen ( const string &  filename,
IOMode  mode,
bool *  is_new = 0,
bool  overwrite = false 
)
protected

Run fopen safely.

Parameters
filenameThe file name to be opened.
modeFile open mode.
is_newIs this a new file?
overwriteIf the file exists, should we truncate it?
Exceptions
FileAccessExceptionThe file has access error.
Returns
The opened file pointer.

Definition at line 135 of file imageio.cpp.

137{
138 FILE *f = 0;
139 if (mode == READ_ONLY)
140 f = fopen(filename.c_str(), "rb");
141 else if (mode == READ_WRITE) {
142 if (overwrite) {
143 f = fopen(filename.c_str(), "wb");
144 if (is_new)
145 *is_new = true;
146 }
147 else {
148 f = fopen(filename.c_str(), "r+b");
149 if (!f) {
150 FILE *f1 = fopen(filename.c_str(), "wb");
151 if (!f1)
153 else {
154 if (is_new)
155 *is_new = true;
156 fclose(f1);
157 f1 = 0;
158 f = fopen(filename.c_str(), "r+b");
159 }
160 }
161 }
162 }
163 else if (mode == WRITE_ONLY) {
164 f = fopen(filename.c_str(), "wb");
165 if (is_new)
166 *is_new = true;
167 }
168
169 if (!f)
171
172 return f;
173}
#define FileAccessException(filename)
Definition: exception.h:187

References FileAccessException, filename, READ_ONLY, READ_WRITE, and WRITE_ONLY.

◆ write_ctf()

void ImageIO::write_ctf ( const Ctf ctf,
int  image_index = 0 
)
virtual

Write CTF data to this image.

Parameters
ctfCtf instance storing the CTF data.
image_indexThe index of the image to write.
Returns
0 if OK; 1 if error.

Reimplemented in EMAN::FitsIO, and EMAN::MrcIO.

Definition at line 53 of file imageio.cpp.

54{
55
56}

◆ write_data()

virtual int EMAN::ImageIO::write_data ( float *  data,
int  image_index = 0,
const Region area = 0,
EMUtil::EMDataType  filestoragetype = EMUtil::EM_FLOAT,
bool  use_host_endian = true 
)
pure virtual

Write data to an image.

Parameters
dataAn array storing the data.
image_indexThe index of the image to write.
areaThe region to write data to.
filestoragetypeThe image data type used in the output file.
use_host_endianWhether to use the host machine endian to write out or not. If false, use the endian opposite to the host machine's endian.
Returns
0 if OK; 1 if error.

Implemented in EMAN::SingleSpiderIO.

◆ write_header()

virtual int EMAN::ImageIO::write_header ( const Dict dict,
int  image_index = 0,
const Region area = 0,
EMUtil::EMDataType  filestoragetype = EMUtil::EM_FLOAT,
bool  use_host_endian = true 
)
pure virtual

Write a header to an image.

Parameters
dictA keyed-dictionary storing the header information.
image_indexThe index of the image to write.
areaThe region to write data to.
filestoragetypeThe image data type used in the output file.
use_host_endianWhether to use the host machine endian to write out or not. If false, use the endian opposite to the host machine's endian.
Returns
0 if OK; 1 if error.

Implemented in EMAN::SingleSpiderIO.

Member Data Documentation

◆ file

FILE* EMAN::ImageIO::file = nullptr
protected

◆ filename

string EMAN::ImageIO::filename
protected

◆ initialized

bool EMAN::ImageIO::initialized = false
protected

◆ rw_mode

IOMode EMAN::ImageIO::rw_mode
protected

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