EMAN2
lstio.cpp
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef WIN32
33#include <sys/param.h>
34#include <unistd.h>
35#else
36#include <direct.h>
37#include <windows.h>
38#define M_PI 3.14159265358979323846f
39#define MAXPATHLEN (MAX_PATH*4)
40#endif
41
42#include <cstdio>
43#include <cstring>
44#include "lstio.h"
45#include "util.h"
46
47
48using namespace EMAN;
49
50const char *LstIO::MAGIC = "#LST";
51
52LstIO::LstIO(const string & fname, IOMode rw)
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}
62
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}
75
76void LstIO::init()
77{
79 if (initialized) {
80 return ;
81 }
82
83 initialized = true;
84
85 bool is_new_file = false;
86 file = sfopen(filename, rw_mode, &is_new_file);
87
88 if (!is_new_file) {
89
90 char buf[MAXPATHLEN];
91
92 if (!fgets(buf, MAXPATHLEN, file)) {
93 throw ImageReadException(filename, "first block");
94 }
95
96 if (!is_valid(&buf)) {
97 throw ImageReadException(filename, "invalid LST file");
98 }
99
100 for (nimg = 0; fgets(buf, MAXPATHLEN, file) != 0; nimg++) {
101 if (buf[0] == '#') {
102 nimg--;
103 }
104 }
105 rewind(file);
106 }
107 EXITFUNC;
108}
109
110bool LstIO::is_valid(const void *first_block)
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}
125
126int LstIO::calc_ref_image_index(int image_index)
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}
198
199
200int LstIO::read_header(Dict & dict, int image_index, const Region * area, bool is_3d)
201{
202 ENTERFUNC;
203 init();
204 check_read_access(image_index);
205 int ref_image_index = calc_ref_image_index(image_index);
206 int err = imageio->read_header(dict, ref_image_index, area, is_3d);
207 dict["source_path"] = ref_filename;
208 EXITFUNC;
209 return err;
210}
211
212int LstIO::write_header(const Dict &, int, const Region* , EMUtil::EMDataType, bool)
213{
214 ENTERFUNC;
215 init();
216 fprintf(file, "%s\n", MAGIC);
217 EXITFUNC;
218 return 0;
219}
220
221int LstIO::read_data(float *data, int image_index, const Region * area, bool is_3d)
222{
223 ENTERFUNC;
224 check_read_access(image_index, data);
225 int ref_image_index = calc_ref_image_index(image_index);
226 int err = imageio->read_data(data, ref_image_index, area, is_3d);
227 EXITFUNC;
228 return err;
229}
230
231int LstIO::write_data(float *data, int, const Region* , EMUtil::EMDataType, bool)
232{
233 ENTERFUNC;
234 fprintf(file, "%s\n", (char*)data);
235 EXITFUNC;
236 return 0;
237}
238
239void LstIO::flush()
240{
241 fflush(file);
242}
243
245{
246 return false;
247}
248
250{
251 init();
252 return is_big_endian;
253}
254
256{
257 init();
258 return nimg;
259}
static bool is_host_big_endian()
Definition: byteorder.cpp:40
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
EMDataType
Image pixel data type used in EMAN.
Definition: emutil.h:92
static ImageIO * get_imageio(const string &filename, int rw_mode, ImageType image_type=IMAGE_UNKNOWN)
Get an ImageIO object.
Definition: emutil.cpp:558
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Definition: imageio.h:127
IOMode rw_mode
Definition: imageio.h:353
string filename
Definition: imageio.h:352
virtual void flush()=0
Flush the IO buffer.
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.
bool initialized
Definition: imageio.h:355
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.
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.
FILE * sfopen(const string &filename, IOMode mode, bool *is_new=0, bool overwrite=false)
Run fopen safely.
Definition: imageio.cpp:135
void check_read_access(int image_index)
Validate 'image_index' in file reading.
Definition: imageio.cpp:95
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.
FILE * file
Definition: imageio.h:354
virtual void init()=0
Do some initialization before doing the read/write.
virtual bool is_image_big_endian()=0
Is this image in big endian or not.
int last_lst_index
Definition: lstio.h:66
static bool is_valid(const void *first_block)
Definition: lstio.cpp:110
int get_nimg()
Return the number of images in this image file.
Definition: lstio.cpp:255
static const char * MAGIC
Definition: lstio.h:70
int calc_ref_image_index(int image_index)
Definition: lstio.cpp:126
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
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
Definition: geometry.h:497
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 ImageReadException(filename, desc)
Definition: exception.h:204
#define LOGERR
Definition: log.h:51
#define ENTERFUNC
Definition: log.h:48
#define EXITFUNC
Definition: log.h:49
E2Exception class.
Definition: aligner.h:40