35#define MAXPATHLEN (MAX_PATH*4)
50const string XplorIO::SECTION_MODE =
"ZYX";
51const int XplorIO::NFLOAT_PER_LINE = 6;
52const int XplorIO::INTEGER_SIZE = 8;
53const int XplorIO::FLOAT_SIZE = 12;
54const char * XplorIO::OUTFORMAT =
"%12.5E";
56XplorIO::XplorIO(
const string & fname,
IOMode rw)
95 char first_block[1024];
96 fread(&first_block,
sizeof(
char),
sizeof(first_block),
file);
116 while(fgets(line,
sizeof(line),
file)) {
117 line[strlen(line)-1] =
'\0';
121 else if (i == (ntitle+3)) {
122 if (sscanf(line,
"%8d%8d%8d%8d%8d%8d%8d%8d%8d", &
nx, &xmin, &xmax,
123 &
ny, &ymin, &ymax, &
nz, &zmin, &zmax) != 9) {
127 else if (i == (ntitle+4)) {
128 if(sscanf(line,
"%f %f %f %f %f %f",
133 else if (i == (ntitle+5)) {
154 char *buf = (
char *)(first_block);
158 if (line1.size() != 0) {
169 ntitle = atoi(line2.c_str());
170 if (ntitle < 0 || ntitle > 50) {
175 for (
int i = 0; i < ntitle+2; i++) {
196 if(image_index == -1) {
199 if(image_index != 0) {
200 throw ImageReadException(
filename,
"no stack allowed for MRC image. For take 2D slice out of 3D image, read the 3D image first, then use get_clip().");
206 int xlen = 0, ylen = 0, zlen = 0;
230 if(image_index == -1) {
233 if(image_index != 0) {
252 struct tm *t = localtime(&t0);
256 fprintf(
file,
"%8d\n", 1);
257 fprintf(
file,
"\"%s\" written by EMAN at %s",
filename.c_str(), asctime(t));
260 int z1 = (
nz - 1) / 2;
262 if (2 *
nz - 1 ==
nx && 2 *
nz - 1 ==
ny) {
267 fprintf(
file,
"%8d%8d%8d%8d%8d%8d%8d%8d%8d\n",
269 ny % 2 ?
ny / 2 :
ny / 2 - 1,
nz, z0, z1);
272 sprintf(fformat,
"%s%s%s%s%s%s\n",
275 fprintf(
file, fformat,
277 fprintf(
file,
"ZYX\n");
302 sprintf(desc,
"Region box %s is outside image area (%d,%d,%d)",
304 (
int)max_size[1], (
int)max_size[2]);
328 int nlines = nxy / step;
329 int nleft = nxy - nlines * step;
331 for (
int k = 0; k <
nz; k++) {
332 fgets(line,
sizeof(line),
file);
334 sscanf(line,
"%d", &kk);
336 LOGERR(
"section index = %d. It should be %d\n", kk, (k+1));
341 for (
int i = 0; i < nlines; i++) {
342 fgets(line,
sizeof(line),
file);
343 int i2 = k2 + i * step;
344 sscanf(line,
"%f %f %f %f %f %f",
345 &data[i2], &data[i2+1], &data[i2+2],
346 &data[i2+3], &data[i2+4], &data[i2+5]);
350 int i2 = k2 + nlines * step;
351 fgets(line,
sizeof(line),
file);
353 for (
int j = 0; j < nleft; j++) {
354 sscanf(pline,
"%f", &data[i2+j]);
386 for (
int k = 0; k <
nz; ++k) {
387 fprintf(
file,
"%8d\n", (k+1));
389 for (
int i = 0; i < nsecs - step; i += step) {
390 for (
int j = 0; j < step; j++) {
396 for (
int l = (nsecs - 1) / step * step; l < nsecs; l++) {
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 process_ascii_region_io(float *data, FILE *file, int rw_mode, int image_index, size_t mode_size, int nx, int ny, int nz, const Region *area, bool has_index_line, int nitems_per_line, const char *outformat)
Works for regions that are outside the image data dimension area.
static void get_region_dims(const Region *area, int nx, int *area_x, int ny, int *area_y, int nz=1, int *area_z=0)
Get a region's dimensions.
static void jump_lines(FILE *file, int nlines)
FloatSize is used to describe a 1D, 2D or 3D rectangular size in floating numbers.
ImageIO classes are designed for reading/writing various electron micrography image formats,...
void check_region(const Region *area, const FloatSize &max_size, bool is_new_file=false, bool inbounds_only=true)
Validate image I/O region.
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.
void check_read_access(int image_index)
Validate 'image_index' in file reading.
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.
void check_write_access(IOMode rw_mode, int image_index, int max_nimg=0)
Validate rw_mode and image_index in file writing.
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...
string get_string() const
Get the description of this region in a string.
bool is_region_in_box(const FloatSize &box) const
To check whether 'this' region is inside a given box assuming the box's origins are (0,...
static string get_line_from_string(char **str)
Extract a single line from a multi-line string.
static bool is_valid(const void *first_block)
static const char * OUTFORMAT
static const int FLOAT_SIZE
static const string SECTION_MODE
static const int NFLOAT_PER_LINE
static const int INTEGER_SIZE
#define Assert(s)
Define Assert() function that is effective only when -DDEBUG is used.
#define ImageReadException(filename, desc)
#define ImageWriteException(imagename, desc)
int portable_fseek(FILE *fp, off_t offset, int whence)