38EmIO::EmIO(
const string & fname,
IOMode rw)
69 if (fread(&
emh,
sizeof(EMHeader), 1,
file) != 1) {
103 const char *data =
static_cast < const char *
>(first_block);
104 char machine = data[0];
105 char is_new_ver = data[1];
106 char data_type = data[3];
108 const int *data1 =
static_cast < const int *
>(first_block);
120 const int max_dim = 1 << 20;
122 if (((
int) machine >=
EM_OS8 && machine <=
EM_PC) &&
123 (is_new_ver == 0 || is_new_ver == 1) &&
125 (nx > 1 && nx < max_dim) && (ny > 0 && ny < max_dim) && (nz > 0 && nz < max_dim)) {
127 off_t file_size1 = (off_t)nx * (off_t)ny * (off_t)nz * (off_t)
get_mode_size(data_type) + (off_t)
sizeof(
EMHeader);
128 if (file_size == file_size1) {
144 if(image_index == -1) {
148 if(image_index != 0) {
149 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().");
155 int xlen = 0, ylen = 0, zlen = 0;
170 if(image_index == -1) {
173 if(image_index != 0) {
190 if (fwrite(&
emh,
sizeof(EMHeader), 1,
file) != 1) {
209 unsigned char *cdata = (
unsigned char *) data;
213 int xlen = 0, ylen = 0, zlen = 0;
216 int total_sz = xlen * ylen * zlen;
228 for (
int k = total_sz - 1; k >= 0; k--) {
232 curr_data =
static_cast < float >(cdata[k]);
235 curr_data =
static_cast < float >(((
short *) cdata)[k]);
238 curr_data =
static_cast < float >(((
int *) cdata)[k]);
241 curr_data = ((
float *) cdata)[k];
263 image_index,
sizeof(
float),
267 int row_size =
sizeof(float) *
emh.
nx;
269 for (
int i = 0; i <
emh.
nz; i++) {
270 int k = i * sec_size;
271 for (
int j = 0; j <
emh.
ny; j++) {
272 fwrite(&data[k + j *
emh.
nx], row_size, 1,
file);
329#elif defined __linux__
331#elif defined __CYGWIN__
337#elif defined macintosh
339#elif defined __darwin__
341#elif defined __APPLE__
351 int mode = (int) data_type;
356 return sizeof(short);
362 return sizeof(double);
static bool is_host_big_endian()
static void swap_bytes(T *data, size_t n=1)
swap the byte order of data with 'n' T-type elements.
static bool is_data_big_endian(const T *small_num_addr)
given a pointer to a reasonable small integer number, return whether the number is big endian or not.
Dict is a dictionary to store <string, EMObject> pair.
static void process_region_io(void *cdata, FILE *file, int rw_mode, int image_index, size_t mode_size, int nx, int ny, int nz=1, const Region *area=0, bool need_flip=false, ImageType imgtype=IMAGE_UNKNOWN, int pre_row=0, int post_row=0)
Process image region IO.
EMDataType
Image pixel data type used in EMAN.
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 size_t get_mode_size(char data_type)
static int get_machine_type()
static bool is_valid(const void *first_block, off_t file_size=0)
static int to_em_datatype(char t)
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.
void become_host_endian(T *data, size_t n=1)
Convert data of this image into host endian format.
virtual bool is_image_big_endian()=0
Is this image in big endian or not.
IntSize is used to describe a 1D, 2D or 3D rectangular size in integers.
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
#define ImageReadException(filename, desc)
#define ImageWriteException(imagename, desc)
int portable_fseek(FILE *fp, off_t offset, int whence)