EMAN2
|
ImageIO classes are designed for reading/writing various electron micrography image formats, including MRC, IMAGIC, SPIDER, PIF, etc. More...
#include <imageio.h>
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 |
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.
ImageIO::ImageIO | ( | const string & | fname, |
IOMode | rw | ||
) |
Definition at line 40 of file imageio.cpp.
|
virtual |
Definition at line 44 of file imageio.cpp.
|
inline |
Convert data of this image into host endian format.
data | An array of data. It can be any type, short, int, float, double, etc. |
n | Array size. |
Definition at line 261 of file imageio.h.
References EMAN::ByteOrder::is_host_big_endian(), is_image_big_endian(), and EMAN::ByteOrder::swap_bytes().
|
protected |
Validate 'image_index' in file reading.
image_index | The 'image_index'th image. Valid value = [0, nimg-1]. |
OutofRangeException | If image_index is out of range. |
Definition at line 95 of file imageio.cpp.
References get_nimg(), init(), and OutofRangeException.
Referenced by check_read_access().
|
protected |
Validate 'image_index' and 'data' in file reading.
image_index | The 'image_index'th image. Valid value = [0, nimg-1]. |
data | The array used to store the image data in reading. |
NullPointerException | If 'data' is NULL. |
OutofRangeException | If image_index is out of range. |
Definition at line 105 of file imageio.cpp.
References check_read_access(), and NullPointerException.
|
protected |
Validate image I/O region.
area | The image I/O region. |
max_size | The upper limit of the region's size. The region must be within 'max_size'. |
is_new_file | Whether it is on a new file or not. |
inbounds_only | if true verifies that the region is inside the image, otherwise no check is performed |
ImageReadException | Any image reading problem. |
Definition at line 58 of file imageio.cpp.
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().
|
protected |
Validate image I/O region.
area | The image I/O region. |
max_size | The upper limit of the region's size. The region must be within 'max_size'. |
is_new_file | Whether it is on a new file or not. |
inbounds_only | if true verifies that the region is inside the image, otherwise no check is performed |
ImageReadException | Any image reading problem. |
Definition at line 88 of file imageio.cpp.
References check_region().
|
protected |
Validate rw_mode, image_index, and data pointer in file writing.
rw_mode | File Read/Write mode. |
image_index | The 'image_index'th image. Valid value = [0, max_nimg]. |
max_nimg | Maximum number of images in the file. If its value <= 0, don't check image_index againt max_nimg. |
data | The data array to be writing to the image file. |
ImageWriteException | Image is not opened for writing. |
OutofRangeException | If image_index is out of range. |
Definition at line 126 of file imageio.cpp.
References check_write_access(), and NullPointerException.
|
protected |
Validate rw_mode and image_index in file writing.
rw_mode | File Read/Write mode. |
image_index | The 'image_index'th image. Valid value = [0, max_nimg]. |
max_nimg | Maximum number of images in the file. If its value <= 0, don't check image_index against max_nimg. |
ImageWriteException | Image is not opened for writing. |
OutofRangeException | If image_index is out of range. |
Definition at line 113 of file imageio.cpp.
References ImageWriteException, init(), OutofRangeException, and READ_ONLY.
Referenced by check_write_access(), EMAN::SpiderIO::write_single_data(), and EMAN::SpiderIO::write_single_header().
|
pure virtual |
Flush the IO buffer.
|
inline |
|
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.
References init().
Referenced by check_read_access(), and EMAN::EMUtil::get_image_count().
|
protectedpure virtual |
Do some initialization before doing the read/write.
Referenced by check_read_access(), check_write_access(), EMAN::DM4IO::get_nimg(), get_nimg(), EMAN::ImagicIO::get_nimg(), EMAN::ImagicIO2::get_nimg(), EMAN::LstFastIO::get_nimg(), EMAN::LstIO::get_nimg(), EMAN::MrcIO::get_nimg(), EMAN::PifIO::get_nimg(), EMAN::SerIO::get_nimg(), EMAN::SpiderIO::get_nimg(), EMAN::XYZIO::get_nimg(), EMAN::FitsIO::read_ctf(), EMAN::MrcIO::read_ctf(), EMAN::MrcIO::read_fei_header(), EMAN::FitsIO::write_ctf(), EMAN::MrcIO::write_ctf(), EMAN::ImagicIO::write_ctf(), and EMAN::ImagicIO2::write_ctf().
|
pure virtual |
Is this an complex image or not.
Referenced by read_binedimage(), and EMAN::MrcIO::read_mrc_header().
|
pure virtual |
Is this image in big endian or not.
Referenced by become_host_endian(), and EMAN::EMData::save_byteorder_to_dict().
|
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.
|
virtual |
Read CTF data from this image.
ctf | Used to store the CTF data. |
image_index | The index of the image to read. |
Reimplemented in EMAN::FitsIO, and EMAN::MrcIO.
Definition at line 48 of file imageio.cpp.
|
pure virtual |
Read the data from an image.
data | An array to store the data. It should be created outside of this function. |
image_index | The index of the image to read. |
area | Define an image-region to read. |
is_3d | Whether 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. |
|
inlinevirtual |
Read the data from an image as an 8 bit array, regardless of format.
data | An array to store the data. It should be created outside of this function. |
image_index | The index of the image to read (image number in file). |
area | If provided reads only the specified region. |
is_3d | Whether 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. |
minval | image value to map to 0 in the returned unsigned char |
maxval | image value to map to 255 in the returned unsigned char. If minval==maxval, autoscaling will be used. |
Definition at line 193 of file imageio.h.
References ImageFormatException.
|
pure virtual |
Read the header from an image.
dict | A keyed-dictionary to store the header information. |
image_index | The index of the image to read. |
area | Define an image-region to read. |
is_3d | Whether 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. |
Referenced by read_binedimage().
|
protected |
Run fopen safely.
filename | The file name to be opened. |
mode | File open mode. |
is_new | Is this a new file? |
overwrite | If the file exists, should we truncate it? |
FileAccessException | The file has access error. |
Definition at line 135 of file imageio.cpp.
References FileAccessException, filename, READ_ONLY, READ_WRITE, and WRITE_ONLY.
|
virtual |
Write CTF data to this image.
ctf | Ctf instance storing the CTF data. |
image_index | The index of the image to write. |
Reimplemented in EMAN::FitsIO, and EMAN::MrcIO.
Definition at line 53 of file imageio.cpp.
|
pure virtual |
Write data to an image.
data | An array storing the data. |
image_index | The index of the image to write. |
area | The region to write data to. |
filestoragetype | The image data type used in the output file. |
use_host_endian | Whether to use the host machine endian to write out or not. If false, use the endian opposite to the host machine's endian. |
Implemented in EMAN::SingleSpiderIO.
|
pure virtual |
Write a header to an image.
dict | A keyed-dictionary storing the header information. |
image_index | The index of the image to write. |
area | The region to write data to. |
filestoragetype | The image data type used in the output file. |
use_host_endian | Whether to use the host machine endian to write out or not. If false, use the endian opposite to the host machine's endian. |
Implemented in EMAN::SingleSpiderIO.
|
protected |
Definition at line 354 of file imageio.h.
Referenced by EMAN::LstFastIO::calc_ref_image_index(), EMAN::LstIO::calc_ref_image_index(), EMAN::PifIO::fseek_to(), EMAN::DM4IO::get_nimg(), EMAN::SerIO::get_nimg(), EMAN::PifIO::PifIO(), EMAN::SerIO::read_data_element(), EMAN::SerIO::read_data_tag(), EMAN::VtkIO::read_dataset(), EMAN::SerIO::read_dim_arr(), EMAN::MrcIO::read_fei_header(), EMAN::MrcIO::update_stats(), EMAN::MrcIO::write_ctf(), EMAN::SpiderIO::write_single_data(), EMAN::SpiderIO::write_single_header(), EMAN::AmiraIO::~AmiraIO(), EMAN::Df3IO::~Df3IO(), EMAN::DM3IO::~DM3IO(), EMAN::DM4IO::~DM4IO(), EMAN::EmIO::~EmIO(), EMAN::FitsIO::~FitsIO(), EMAN::Gatan2IO::~Gatan2IO(), EMAN::IcosIO::~IcosIO(), EMAN::LstFastIO::~LstFastIO(), EMAN::LstIO::~LstIO(), EMAN::MrcIO::~MrcIO(), EMAN::OmapIO::~OmapIO(), EMAN::PgmIO::~PgmIO(), EMAN::PifIO::~PifIO(), EMAN::SalIO::~SalIO(), EMAN::SerIO::~SerIO(), EMAN::SitusIO::~SitusIO(), EMAN::SpiderIO::~SpiderIO(), EMAN::VtkIO::~VtkIO(), and EMAN::XplorIO::~XplorIO().
|
protected |
Definition at line 352 of file imageio.h.
Referenced by EMAN::LstFastIO::calc_ref_image_index(), EMAN::LstIO::calc_ref_image_index(), EMAN::EerIO::EerIO(), get_filename(), EMAN::SerIO::get_nimg(), EMAN::ImagicIO::ImagicIO(), EMAN::ImagicIO2::ImagicIO2(), EMAN::ImagicIO2::init_test(), EMAN::SerIO::read_data_element(), EMAN::SerIO::read_data_tag(), EMAN::VtkIO::read_dataset(), EMAN::SerIO::read_dim_arr(), EMAN::MrcIO::read_fei_header(), EMAN::MrcIO::read_mrc_header(), sfopen(), EMAN::SpiderIO::SpiderIO(), EMAN::MrcIO::update_stats(), EMAN::MrcIO::write_ctf(), EMAN::SpiderIO::write_single_data(), and EMAN::SpiderIO::write_single_header().
|
protected |
Definition at line 355 of file imageio.h.
Referenced by EMAN::VtkIO::get_datasettype_from_name(), EMAN::VtkIO::get_datatype_from_name(), and EMAN::ImagicIO2::init_test().
|
protected |
Definition at line 353 of file imageio.h.
Referenced by EMAN::LstFastIO::calc_ref_image_index(), EMAN::LstIO::calc_ref_image_index(), EMAN::SpiderIO::write_single_data(), and EMAN::SpiderIO::write_single_header().