EMAN2
Functions
emdata_io.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void _read_image (ImageIO *imageio, int img_index=0, bool header_only=false, const Region *region=0, bool is_3d=false)
 This file is a part of "emdata.h", to use functions in this file, you should "#include "emdata.h", NEVER directly include this file. More...
 
void _write_image (ImageIO *imageio, int img_index=0, EMUtil::ImageType imgtype=EMUtil::IMAGE_UNKNOWN, bool header_only=false, const Region *region=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)
 
void read_image (const string &filename, int img_index=0, bool header_only=false, const Region *region=0, bool is_3d=false, EMUtil::ImageType imgtype=EMUtil::IMAGE_UNKNOWN)
 read an image file and stores its information to this EMData object. More...
 
void read_binedimage (const string &filename, int img_index=0, int binfactor=0, bool fast=false, bool is_3d=false)
 read in a binned image, bin while reading. More...
 
void write_image (const string &filename, int img_index=0, EMUtil::ImageType imgtype=EMUtil::IMAGE_UNKNOWN, bool header_only=false, const Region *region=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)
 write the header and data out to an image. More...
 
void append_image (const string &filename, EMUtil::ImageType imgtype=EMUtil::IMAGE_UNKNOWN, bool header_only=false)
 append to an image file; If the file doesn't exist, create one. More...
 
void write_lst (const string &filename, const string &reffile="", int refn=-1, const string &comment="")
 Append data to a LST image file. More...
 
static vector< std::shared_ptr< EMData > > read_images (const string &filename, vector< int > img_indices=vector< int >(), EMUtil::ImageType imgtype=EMUtil::IMAGE_UNKNOWN, bool header_only=false)
 Read a set of images from file specified by 'filename'. More...
 
static bool write_images (const string &filename, vector< std::shared_ptr< EMData > > imgs, EMUtil::ImageType imgtype=EMUtil::IMAGE_UNKNOWN, bool header_only=false, const Region *region=nullptr, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)
 Write a set of images to file specified by 'filename'. More...
 
ostream & operator<< (ostream &out, const EMData &obj)
 

Function Documentation

◆ _read_image()

void _read_image ( ImageIO *  imageio,
int  img_index = 0,
bool  header_only = false,
const Region *  region = 0,
bool  is_3d = false 
)
private

This file is a part of "emdata.h", to use functions in this file, you should "#include "emdata.h", NEVER directly include this file.

◆ _write_image()

void _write_image ( ImageIO *  imageio,
int  img_index = 0,
EMUtil::ImageType  imgtype = EMUtil::IMAGE_UNKNOWN,
bool  header_only = false,
const Region *  region = 0,
EMUtil::EMDataType  filestoragetype = EMUtil::EM_FLOAT,
bool  use_host_endian = true 
)
private

◆ append_image()

void append_image ( const string &  filename,
EMUtil::ImageType  imgtype = EMUtil::IMAGE_UNKNOWN,
bool  header_only = false 
)

append to an image file; If the file doesn't exist, create one.

Parameters
filenameThe image file name.
imgtypeWrite to the given image format type. if not specified, use the 'filename' extension to decide.
header_onlyTo write only the header or both header and data.

◆ operator<<()

friend ostream & operator<< ( ostream &  out,
const EMData &  obj 
)

◆ read_binedimage()

void EMData::read_binedimage ( const string &  filename,
int  img_index = 0,
int  binfactor = 0,
bool  fast = false,
bool  is_3d = false 
)

read in a binned image, bin while reading.

For use in huge files(tomograms)

Parameters
filenameThe image file name.
img_indexThe nth image you want to read.
binfactorThe amout you want to bin by. Must be an integer
fastOnly bin xy slice every binfactor intervals, otherwise meanshrink z
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.
Exceptions
ImageFormatException
ImageReadException

Definition at line 124 of file emdata_io.cpp.

125{
126 ENTERFUNC;
127
128 ImageIO *imageio = EMUtil::get_imageio(filename, ImageIO::READ_ONLY);
129
130 if (!imageio) {
131 throw ImageFormatException("cannot create an image io");
132 }
133 else {
134 int err = imageio->read_header(attr_dict, img_index, 0, is_3d);
135 if (err) {
136 throw ImageReadException(filename, "imageio read header failed");
137 }
138 else {
139 attr_dict["source_path"] = filename;
140 attr_dict["source_n"] = img_index;
141 if (imageio->is_complex_mode()) {
142 set_complex(true);
143 set_fftpad(true);
144 }
145 if (attr_dict.has_key("is_fftodd") && (int)attr_dict["is_fftodd"] == 1) {
146 set_fftodd(true);
147 }
148 if ((int) attr_dict["is_complex_ri"] == 1) {
149 set_ri(true);
150 }
151 save_byteorder_to_dict(imageio);
152
153 int ori_nx = nx = attr_dict["nx"];
154 int ori_ny = ny = attr_dict["ny"];
155 int ori_nz = nz = attr_dict["nz"];
156 if (!fast) ori_nz-=ori_nz%binfactor; // makes sure Z is a multiple of binfactor, hack to fix the poor logic in the original routine
157 attr_dict.erase("nx");
158 attr_dict.erase("ny");
159 attr_dict.erase("nz");
160
161 // At this point nx, ny and nz are all reduced by binfactor
162 set_size(nx/binfactor, ny/binfactor, nz/binfactor);
163
164 //here is where we read in the binned data
165 EMData* tempdata = new EMData();
166 size_t sizeofslice = nx*ny*sizeof(float);
167
168 //zbin factor use 1 to speed binning(but don't benfit by averaging in Z)
169 int zbin = binfactor;
170 if(fast) zbin = 1;
171 //verbose
172 float percent = 0.1f;
173 for(int k = 0; k < ori_nz; k+=binfactor){
174 if(k > ori_nz*percent){
175 printf("%1.0f %% Done\n",100.0*float(k)/float(ori_nz));
176 percent+=0.1f;
177 }
178 // read in a slice region
179// printf("%d %d %d %d\n",k,ori_nx,ori_ny,zbin);
180 const Region* binregion = new Region(0,0,k,ori_nx,ori_ny,zbin);
181 tempdata->read_image(filename, 0, false, binregion);
182 // shrink the slice
183 if (binfactor > 1) tempdata->process_inplace("math.meanshrink",Dict("n",binfactor));
184 size_t offset = nx*ny*k/binfactor;
185 //add slice to total
186 EMUtil::em_memcpy(get_data()+offset,tempdata->get_data(),sizeofslice);
187 delete binregion;
188 }
189
190 delete tempdata;
191 update();
192 }
193 }
194
195 EMUtil::close_imageio(filename, imageio);
196 imageio = 0;
197 EXITFUNC;
198}
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
EMData stores an image's data and defines core image processing routines.
Definition: emdata.h:82
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Definition: imageio.h:127
virtual bool is_complex_mode()=0
Is this an complex image or not.
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.
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
Definition: geometry.h:497
void set_fftodd(bool is_fftodd)
Mark this image as having (real-space) odd nx.
void set_fftpad(bool is_fftpadded)
Mark this image as already extended along x for ffts.
void set_ri(bool is_ri)
Mark this image as a real/imaginary format complex image.
void set_size(int nx, int ny=1, int nz=1, bool noalloc=false)
Resize this EMData's main board memory pointer.
void update()
Mark EMData as changed, statistics, etc will be updated at need.
float * get_data() const
Get the image pixel density data in a 1D float array.
void set_complex(bool is_complex)
Mark this image as a complex image.
#define ImageReadException(filename, desc)
Definition: exception.h:204
#define ImageFormatException(desc)
Definition: exception.h:147
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49

References EMAN::EMData::attr_dict, EMAN::EMUtil::close_imageio(), EMAN::EMUtil::em_memcpy(), EMAN::EMData::EMData(), ENTERFUNC, EMAN::Dict::erase(), EXITFUNC, get_data(), EMAN::EMUtil::get_imageio(), EMAN::Dict::has_key(), ImageFormatException, ImageReadException, EMAN::ImageIO::is_complex_mode(), EMAN::EMData::nx, EMAN::EMData::ny, EMAN::EMData::nz, EMAN::ImageIO::read_header(), EMAN::ImageIO::READ_ONLY, EMAN::EMData::save_byteorder_to_dict(), set_complex(), set_fftodd(), set_fftpad(), set_ri(), set_size(), and update().

◆ read_image()

void read_image ( const string &  filename,
int  img_index = 0,
bool  header_only = false,
const Region *  region = 0,
bool  is_3d = false,
EMUtil::ImageType  imgtype = EMUtil::IMAGE_UNKNOWN 
)

read an image file and stores its information to this EMData object.

If a region is given, then only read a region of the image file. The region will be this EMData object. The given region must be inside the given image file. Otherwise, an error will be created.

Parameters
filenameThe image file name.
img_indexThe nth image you want to read.
header_onlyTo read only the header or both header and data.
regionTo read only a region of the image.
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.
Exceptions
ImageFormatException
ImageReadException

◆ read_images()

static vector< std::shared_ptr< EMData > > read_images ( const string &  filename,
vector< int >  img_indices = vector< int >(),
EMUtil::ImageType  imgtype = EMUtil::IMAGE_UNKNOWN,
bool  header_only = false 
)
static

Read a set of images from file specified by 'filename'.

Which images are read is set by 'img_indices'.

Parameters
filenameThe image file name.
img_indicesWhich images are read. If it is empty, all images are read. If it is not empty, only those in this array are read.
header_onlyIf true, only read image header. If false, read both data and header.
Returns
The set of images read from filename.

Referenced by EMAN::EMUtil::get_all_attributes().

◆ write_image()

void write_image ( const string &  filename,
int  img_index = 0,
EMUtil::ImageType  imgtype = EMUtil::IMAGE_UNKNOWN,
bool  header_only = false,
const Region *  region = 0,
EMUtil::EMDataType  filestoragetype = EMUtil::EM_FLOAT,
bool  use_host_endian = true 
)

write the header and data out to an image.

If the img_index = -1, append the image to the given image file.

If the given image file already exists, this image format only stores 1 image, and no region is given, then truncate the image file to zero length before writing data out. For header writing only, no truncation happens.

If a region is given, then write a region only.

Parameters
filenameThe image file name.
img_indexThe nth image to write as.
imgtypeWrite to the given image format type. if not specified, use the 'filename' extension to decide.
header_onlyTo write only the header or both header and data.
regionDefine the region to write to.
filestoragetypeThe image data type used in the output file.
use_host_endianTo write in the host computer byte order.
Exceptions
ImageFormatException
ImageWriteException

Referenced by bispecRotTransInvDirect(), bispecRotTransInvN(), EMAN::RotateInFSProcessor::process_inplace(), and write_lst().

◆ write_images()

static bool write_images ( const string &  filename,
vector< std::shared_ptr< EMData > >  imgs,
EMUtil::ImageType  imgtype = EMUtil::IMAGE_UNKNOWN,
bool  header_only = false,
const Region *  region = nullptr,
EMUtil::EMDataType  filestoragetype = EMUtil::EM_FLOAT,
bool  use_host_endian = true 
)
static

Write a set of images to file specified by 'filename'.

Which images are written is set by 'imgs'.

Parameters
filenameThe image file name.
imgsWhich images are written.
imgtypeWrite to the given image format type. if not specified, use the 'filename' extension to decide.
header_onlyTo write only the header or both header and data.
regionDefine the region to write to.
filestoragetypeThe image data type used in the output file.
use_host_endianTo write in the host computer byte order.
Returns
True if set of images are written successfully to filename.

◆ write_lst()

void EMData::write_lst ( const string &  filename,
const string &  reffile = "",
int  refn = -1,
const string &  comment = "" 
)

Append data to a LST image file.

Parameters
filenameThe LST image file name.
reffileReference file name.
refnThe reference file number.
commentThe comment to the added reference file.
See also
lstio.h

Definition at line 361 of file emdata_io.cpp.

363{
364 ENTERFUNC;
365 attr_dict["LST.reffile"] = reffile;
366 attr_dict["LST.refn"] = refn;
367 attr_dict["LST.comment"] = comment;
368 write_image(filename, -1, EMUtil::IMAGE_LST, false);
369 EXITFUNC;
370}
void write_image(const string &filename, int img_index=0, EMUtil::ImageType imgtype=EMUtil::IMAGE_UNKNOWN, bool header_only=false, const Region *region=0, EMUtil::EMDataType filestoragetype=EMUtil::EM_FLOAT, bool use_host_endian=true)
write the header and data out to an image.

References EMAN::EMData::attr_dict, ENTERFUNC, EXITFUNC, EMAN::EMUtil::IMAGE_LST, and write_image().