38const int SitusIO::SITUS_HEADER_LINES=2;
39const int SitusIO::FLOAT_SIZE = 12;
40const int SitusIO::NFLOAT_PER_LINE = 10;
41const char * SitusIO::OUTFORMAT =
"%12.6f";
42const int SitusIO::LINE_LENGTH = 1024;
44SitusIO::SitusIO(
const string & fname,
IOMode rw) :
47 apix(0.0f), origx(0.0f), origy(0.0f), origz(0.0f),
71 char first_block[1024];
72 fread(&first_block,
sizeof(
char),
sizeof(first_block),
file);
77 char * buf = (
char *)first_block;
98 int nitems_in_line = 0;
99 for (
int i=0; i<number_lines; ++i) {
101 if (!fgets(line,
sizeof(line),
file)) {
102 printf(
"read situs file failed\n");
105 nitems_in_line = (int) (strlen(line) /
FLOAT_SIZE);
107 for (
int j=0; j<nitems_in_line; ++j) {
108 sscanf(pline,
"%f", &data[index]);
127 dict[
"apix_x"] =
apix;
128 dict[
"apix_y"] =
apix;
129 dict[
"apix_z"] =
apix;
131 dict[
"origin_x"] =
origx;
132 dict[
"origin_y"] =
origy;
133 dict[
"origin_z"] =
origz;
144 apix = (float)dict[
"apix_x"];
145 origx = (float)dict[
"origin_x"];
146 origy = (float)dict[
"origin_y"];
147 origz = (float)dict[
"origin_z"];
148 nx = (int)dict[
"nx"];
149 ny = (int)dict[
"ny"];
150 nz = (int)dict[
"nz"];
155 if(!fputs(headerline,
file)) {
156 printf(
"Write situs header failed\n");
159 if(!fputs(
"\n\n",
file)) {
160 printf(
"Write situs header failed\n");
171 for (
size_t index=0; index<(size_t)
nx*
ny*
nz; ++index) {
189 char *buf = (
char *)(first_block);
192 if(line1.size()==0)
return false;
198 if(sscanf(line1.c_str(),
"%f %f %f %f %d %d %d %d", &
apix, &
origx, &
origy, &
origz, &
nx, &
ny, &
nz,&missing) != 7)
return false;
200 if(apix<0.01 || apix>100)
return false;
201 if(
nx<=0 ||
ny<0 ||
nz<0)
return false;
static bool is_host_big_endian()
Dict is a dictionary to store <string, EMObject> pair.
EMDataType
Image pixel data type used in EMAN.
static void jump_lines(FILE *file, int nlines)
ImageIO classes are designed for reading/writing various electron micrography image formats,...
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.
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.
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.
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.
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
static const int SITUS_HEADER_LINES
static bool is_valid(const void *first_block)
static const int LINE_LENGTH
static const char * OUTFORMAT
static const int NFLOAT_PER_LINE
static const int FLOAT_SIZE
static string get_line_from_string(char **str)
Extract a single line from a multi-line string.
#define ImageReadException(filename, desc)
int portable_fseek(FILE *fp, off_t offset, int whence)