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

A LST file is an ASCII file that contains a list of image file names. More...

#include <lstio.h>

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

Public Member Functions

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

int calc_ref_image_index (int image_index)
 

Private Attributes

bool is_big_endian
 
int nimg
 
ImageIOimageio
 
string ref_filename
 
int last_lst_index
 
int last_ref_index
 

Static Private Attributes

static const char * MAGIC = "#LST"
 

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

A LST file is an ASCII file that contains a list of image file names.

Each line of a LST file has the following format: reference_image_index reference-image-filename comments

Definition at line 45 of file lstio.h.

Constructor & Destructor Documentation

◆ LstIO()

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

Definition at line 52 of file lstio.cpp.

53: ImageIO(fname, rw)
54{
56 nimg = 0;
57 imageio = 0;
58 ref_filename = "";
59 last_lst_index = -1;
60 last_ref_index = -1;
61}
static bool is_host_big_endian()
Definition: byteorder.cpp:40
ImageIO(const string &fname, IOMode rw)
Definition: imageio.cpp:40
int last_lst_index
Definition: lstio.h:66
int last_ref_index
Definition: lstio.h:67
int nimg
Definition: lstio.h:61
ImageIO * imageio
Definition: lstio.h:63
bool is_big_endian
Definition: lstio.h:60
string ref_filename
Definition: lstio.h:64

References imageio, is_big_endian, EMAN::ByteOrder::is_host_big_endian(), last_lst_index, last_ref_index, nimg, and ref_filename.

◆ ~LstIO()

LstIO::~LstIO ( )

Definition at line 63 of file lstio.cpp.

64{
65 if (file) {
66 fclose(file);
67 file = 0;
68 }
69 ref_filename = "";
70 if(imageio) {
71 delete imageio;
72 imageio = 0;
73 }
74}
FILE * file
Definition: imageio.h:354

References EMAN::ImageIO::file, imageio, and ref_filename.

Member Function Documentation

◆ calc_ref_image_index()

int LstIO::calc_ref_image_index ( int  image_index)
private

Definition at line 126 of file lstio.cpp.

127{
128 if (image_index == last_lst_index) {
129 return last_ref_index;
130 }
131 else {
132 char buf[MAXPATHLEN];
133 int step = image_index - last_lst_index;
134
135 if (step < 0) {
136 rewind(file);
137 step = image_index + 1;
138 }
139
140 for (int i = 0; i < step; i++) {
141 if (!fgets(buf, MAXPATHLEN, file)) {
142 LOGERR("reach EOF in file '%s' before reading %dth image",
143 filename.c_str(), image_index);
144 return 1;
145 }
146 if (buf[0] == '#') {
147 i--;
148 }
149 }
150 int ref_image_index = 0;
151 char ref_image_path[MAXPATHLEN];
152 char unused[256];
153 sscanf(buf, " %d %s %[ .,0-9-]", &ref_image_index, ref_image_path, unused);
154
155 char fullpath[MAXPATHLEN];
156
157 char sep = '/';
158#ifdef WIN32
159 sep = '\\';
160#endif
161// if (ref_image_path[0] == sep) {
162 strcpy(fullpath, ref_image_path);
163// }
164// else {
165// if (strrchr(filename.c_str(), sep)) {
166// strcpy(fullpath, filename.c_str());
167// }
168// else {
169// #ifndef WIN32
170// getcwd(fullpath, MAXPATHLEN);
171// #else
172// //GetCurrentDirectory(MAXPATHLEN, fullpath);
173// #endif
174// }
175//
176// char *p_basename = strrchr(fullpath, sep);
177// if (p_basename) {
178// //p_basename++;
179// //*p_basename = '\0';
180// char ssep[2];
181// ssep[0] = sep;
182// ssep[1] = '\0';
183// strcat(fullpath, ssep);
184// strcat(fullpath, ref_image_path);
185// }
186// }
187
188 ref_filename = string(fullpath);
190
191 last_ref_index = ref_image_index;
192 }
193
194 last_lst_index = image_index;
195
196 return last_ref_index;
197}
static ImageIO * get_imageio(const string &filename, int rw_mode, ImageType image_type=IMAGE_UNKNOWN)
Get an ImageIO object.
Definition: emutil.cpp:558
IOMode rw_mode
Definition: imageio.h:353
string filename
Definition: imageio.h:352
#define LOGERR
Definition: log.h:51

References EMAN::ImageIO::file, EMAN::ImageIO::filename, EMAN::EMUtil::get_imageio(), imageio, last_lst_index, last_ref_index, LOGERR, ref_filename, and EMAN::ImageIO::rw_mode.

◆ get_nimg()

int LstIO::get_nimg ( )
virtual

Return the number of images in this image file.

Reimplemented from EMAN::ImageIO.

Definition at line 255 of file lstio.cpp.

256{
257 init();
258 return nimg;
259}
virtual void init()=0
Do some initialization before doing the read/write.

References EMAN::ImageIO::init(), and nimg.

◆ is_single_image_format()

bool EMAN::LstIO::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 54 of file lstio.h.

55 {
56 return false;
57 }

◆ is_valid()

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

Definition at line 110 of file lstio.cpp.

111{
112 ENTERFUNC;
113 bool result = false;
114
115 if (!first_block) {
116 result = false;
117 }
118 else {
119 result = Util::check_file_by_magic(first_block, MAGIC);
120 }
121
122 EXITFUNC;
123 return result;
124}
static const char * MAGIC
Definition: lstio.h:70
static bool check_file_by_magic(const void *first_block, const char *magic)
check whether a file starts with certain magic string.
Definition: util.cpp:239
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49

References EMAN::Util::check_file_by_magic(), ENTERFUNC, EXITFUNC, and MAGIC.

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

Member Data Documentation

◆ DEFINE_IMAGEIO_FUNC

EMAN::LstIO::DEFINE_IMAGEIO_FUNC

Definition at line 51 of file lstio.h.

◆ imageio

ImageIO* EMAN::LstIO::imageio
private

Definition at line 63 of file lstio.h.

Referenced by calc_ref_image_index(), LstIO(), and ~LstIO().

◆ is_big_endian

bool EMAN::LstIO::is_big_endian
private

Definition at line 60 of file lstio.h.

Referenced by LstIO().

◆ last_lst_index

int EMAN::LstIO::last_lst_index
private

Definition at line 66 of file lstio.h.

Referenced by calc_ref_image_index(), and LstIO().

◆ last_ref_index

int EMAN::LstIO::last_ref_index
private

Definition at line 67 of file lstio.h.

Referenced by calc_ref_image_index(), and LstIO().

◆ MAGIC

const char * LstIO::MAGIC = "#LST"
staticprivate

Definition at line 70 of file lstio.h.

Referenced by is_valid().

◆ nimg

int EMAN::LstIO::nimg
private

Definition at line 61 of file lstio.h.

Referenced by get_nimg(), and LstIO().

◆ ref_filename

string EMAN::LstIO::ref_filename
private

Definition at line 64 of file lstio.h.

Referenced by calc_ref_image_index(), LstIO(), and ~LstIO().


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