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

A LSX file is a high performance ASCII file that contains a list of image numbers and file names. More...

#include <lstfastio.h>

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

Public Member Functions

 LstFastIO (const string &fname, IOMode rw_mode=READ_ONLY)
 
 ~LstFastIO ()
 
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
 
unsigned int line_length
 
unsigned int head_length
 
ImageIOimageio
 
string ref_filename
 
int last_lst_index
 
int last_ref_index
 

Static Private Attributes

static const char * MAGIC = "#LSX"
 

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 LSX file is a high performance ASCII file that contains a list of image numbers and file names.

Each line of a LSX file has the following format: after the #LSX magic number are 2 lines, the second line containing

comment

line-length

Denoting the length of each line in the file. If writing additional lines to the file, this length must not be exceeded.

Additional '#' comment lines are NOT permitted within the file

lines have the form: reference_image_index reference-image-filename comments

Definition at line 54 of file lstfastio.h.

Constructor & Destructor Documentation

◆ LstFastIO()

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

Definition at line 52 of file lstfastio.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
bool is_big_endian
Definition: lstfastio.h:69
string ref_filename
Definition: lstfastio.h:75
int last_lst_index
Definition: lstfastio.h:77
ImageIO * imageio
Definition: lstfastio.h:74
int last_ref_index
Definition: lstfastio.h:78

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

◆ ~LstFastIO()

LstFastIO::~LstFastIO ( )

Definition at line 63 of file lstfastio.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 LstFastIO::calc_ref_image_index ( int  image_index)
private

Definition at line 138 of file lstfastio.cpp.

139{
140 if (image_index == last_lst_index) {
141 return last_ref_index;
142 }
143 else {
144 vector<char> buf(line_length+1);
145
146 fseek(file,head_length+line_length*image_index,SEEK_SET);
147 if (!fgets(buf.data(), line_length, file)) {
148 LOGERR("reach EOF in file '%s' before reading %dth image",
149 filename.c_str(), image_index);
150 return 1;
151 }
152
153 int ref_image_index = 0;
154 char ref_image_path[MAXPATHLEN];
155 vector<char> unused(line_length);
156 sscanf(buf.data(), " %d %s %[ .,0-9-]", &ref_image_index, ref_image_path, unused.data());
157 string newrefname = string(ref_image_path);
158 if (newrefname.compare(ref_filename)==0) { // use the existing imageio when possible, TODO: cache a few?
160 } else {
161 ref_filename = newrefname;
163 if (imageio!=0) delete imageio;
165 }
166 last_ref_index = ref_image_index;
167 }
168// printf("%d\t%d\t%s\n",image_index,last_ref_index,ref_filename.c_str());
169
170 last_lst_index = image_index;
171
172 return last_ref_index;
173}
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
unsigned int line_length
Definition: lstfastio.h:71
unsigned int head_length
Definition: lstfastio.h:72
static bool is_file_exist(const string &filename)
check whether a file exists or not
Definition: util.cpp:253
#define FileAccessException(filename)
Definition: exception.h:187
#define LOGERR
Definition: log.h:51

References EMAN::ImageIO::file, FileAccessException, EMAN::ImageIO::filename, EMAN::EMUtil::get_imageio(), head_length, imageio, EMAN::Util::is_file_exist(), last_lst_index, last_ref_index, line_length, LOGERR, ref_filename, and EMAN::ImageIO::rw_mode.

◆ get_nimg()

int LstFastIO::get_nimg ( )
virtual

Return the number of images in this image file.

Reimplemented from EMAN::ImageIO.

Definition at line 235 of file lstfastio.cpp.

236{
237 init();
238 return nimg;
239}
virtual void init()=0
Do some initialization before doing the read/write.

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

◆ is_single_image_format()

bool EMAN::LstFastIO::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 63 of file lstfastio.h.

64 {
65 return false;
66 }

◆ is_valid()

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

Definition at line 111 of file lstfastio.cpp.

112{
113 ENTERFUNC;
114 bool result = false;
115
116 if (!first_block) {
117 result = false;
118 }
119 else {
120 result = Util::check_file_by_magic(first_block, MAGIC);
121 }
122
123 if (result) {
124 for (int i=0; i<1024; i++) {
125 char c = ((const char *)first_block)[i];
126 if (c==0) break;
127 if (c==13) {
128// printf("%1024s\n",(const char *)first_block);
129 printf("ERROR: .lst file contains \\r at pos %d. This should never happen. (If you edit a .lst file with a text editor on Windows, it will corrupt the file). Aborting program.\n",i);
130 exit(1);
131 }
132 }
133 }
134 EXITFUNC;
135 return result;
136}
static const char * MAGIC
Definition: lstfastio.h:81
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::LstFastIO::DEFINE_IMAGEIO_FUNC

Definition at line 60 of file lstfastio.h.

◆ head_length

unsigned int EMAN::LstFastIO::head_length
private

Definition at line 72 of file lstfastio.h.

Referenced by calc_ref_image_index().

◆ imageio

ImageIO* EMAN::LstFastIO::imageio
private

Definition at line 74 of file lstfastio.h.

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

◆ is_big_endian

bool EMAN::LstFastIO::is_big_endian
private

Definition at line 69 of file lstfastio.h.

Referenced by LstFastIO().

◆ last_lst_index

int EMAN::LstFastIO::last_lst_index
private

Definition at line 77 of file lstfastio.h.

Referenced by calc_ref_image_index(), and LstFastIO().

◆ last_ref_index

int EMAN::LstFastIO::last_ref_index
private

Definition at line 78 of file lstfastio.h.

Referenced by calc_ref_image_index(), and LstFastIO().

◆ line_length

unsigned int EMAN::LstFastIO::line_length
private

Definition at line 71 of file lstfastio.h.

Referenced by calc_ref_image_index().

◆ MAGIC

const char * LstFastIO::MAGIC = "#LSX"
staticprivate

Definition at line 81 of file lstfastio.h.

Referenced by is_valid().

◆ nimg

int EMAN::LstFastIO::nimg
private

Definition at line 70 of file lstfastio.h.

Referenced by get_nimg(), and LstFastIO().

◆ ref_filename

string EMAN::LstFastIO::ref_filename
private

Definition at line 75 of file lstfastio.h.

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


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