41FitsIO::FitsIO(
const string & fname,
IOMode rw)
85 if (strncmp(
"SIMPLE ",(
const char *)first_block,8)==0)
return true;
98 if(image_index == -1) {
102 if(image_index != 0) {
103 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().");
113 char s[81],lbl[9],val[80];
118 for (nr = fread(s,80,1,
file); strncmp(
"END",s,3); nr = fread(s,80,1,
file)) {
120 sscanf(s,
"%8s = %[^/]",lbl,val);
122 if (strncmp(
"SIMPLE ",s,8)==0)
continue;
123 else if (strncmp(
"END ",s,8)==0)
break;
126 else if (strncmp(
"NAXIS",s,5)==0) {
127 if (s[5]==
'1') dict[
"nx"]=atoi(val);
128 if (s[5]==
'2') dict[
"ny"]=atoi(val);
129 if (s[5]==
'3') dict[
"nz"]=atoi(val);
132 dict[(string)
"FITS."+lbl]=val;
138 int xlen = 0, ylen = 0, zlen = 0;
139 dtype=atoi(dict[
"FITS.BITPIX"]);
142 dict[
"nx"] =
nx=xlen;
143 dict[
"ny"] =
ny=ylen;
144 dict[
"nz"] =
nz=zlen;
154 LOGWARN(
"FITS write is not supported.");
164 size_t size = (size_t)
nx*
ny*
nz;
171 unsigned char *cdata=(
unsigned char *)
rdata;
172 short *sdata=(
short *)
rdata;
173 int *idata=(
int *)
rdata;
179 for (i=size-1; i<size; i--)
rdata[i]=cdata[i];
184 for (i=size-1; i<size; i--)
rdata[i]=sdata[i];
189 for (i=0; i<size; i++) rdata[i]=static_cast<float>(idata[i]);
196 ddata=(
double *)malloc(size*8);
199 for (i=0; i<size; i++) rdata[i]=static_cast<float>(ddata[i]);
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.
Ctf is the base class for all CTF model.
Dict is a dictionary to store <string, EMObject> pair.
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 bool is_valid(const void *first_block, off_t file_size=0)
int read_ctf(Ctf &ctf, int image_index=0)
Read CTF data from this image.
void write_ctf(const Ctf &ctf, int image_index=0)
Write CTF data to this image.
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.
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...
#define ImageReadException(filename, desc)
int portable_fseek(FILE *fp, off_t offset, int whence)