40Df3IO::Df3IO(
const string & fname,
IOMode rw)
42 is_new_file(false), rendermin(0.0), rendermax(0.0), renderbits(16)
75 if (fread(&
nx,
sizeof(
unsigned short), 1,
file) != 1) {
79 nr = fread(&
ny,
sizeof(
unsigned short), 1,
file); nr++;
80 nr = fread(&
nz,
sizeof(
unsigned short), 1,
file); nr++;
102 nx = (
unsigned short)((
int)dict[
"nx"]);
103 ny = (
unsigned short)((
int)dict[
"ny"]);
104 nz = (
unsigned short)((
int)dict[
"nz"]);
108 unsigned short df3header[3];
116 if(fwrite(df3header,
sizeof(
unsigned short), 3,
file) != 3) {
128 size_t image_size = (size_t)
nx*
ny*
nz;
133 size_t fsize = ftell (
file);
136 unsigned int * uidata = 0;
137 unsigned short * usdata = 0;
138 unsigned char * ucdata = 0;
141 switch(fsize/image_size) {
142 case sizeof(
unsigned int):
143 uidata =
new unsigned int[image_size];
144 nr = fread(uidata,
sizeof(
unsigned int), image_size,
file); nr++;
145 become_host_endian < unsigned int >(uidata, image_size);
147 if(uidata) {
delete [] uidata; uidata=0;}
149 case sizeof(
unsigned short):
150 usdata =
new unsigned short[image_size];
151 nr = fread(usdata,
sizeof(
unsigned short), image_size,
file); nr++;
152 become_host_endian < unsigned short >(usdata, image_size);
154 if(usdata) {
delete [] usdata; usdata=0;}
156 case sizeof(
unsigned char):
157 ucdata =
new unsigned char[image_size];
158 nr = fread(ucdata,
sizeof(
unsigned char), image_size,
file); nr++;
160 if(ucdata) {
delete [] ucdata; ucdata=0;}
175 size_t img_size = (size_t)
nx*
ny*
nz;
176 unsigned int * uidata = 0;
177 unsigned short * usdata = 0;
178 unsigned char * ucdata = 0;
186 uidata =
new unsigned int[img_size];
187 for (
size_t i = 0; i < img_size; ++i) {
192 uidata[i] = UINT_MAX;
199 if(fwrite(uidata,
sizeof(
unsigned int), img_size,
file) != img_size) {
202 if(uidata) {
delete [] uidata; uidata=0;}
205 usdata =
new unsigned short[img_size];
206 for (
size_t i = 0; i < img_size; ++i) {
211 usdata[i] = USHRT_MAX;
218 if(fwrite(usdata,
sizeof(
unsigned short), img_size,
file) != img_size) {
221 if(usdata) {
delete [] usdata; usdata=0;}
224 ucdata =
new unsigned char[img_size];
225 for (
size_t i = 0; i < img_size; ++i) {
230 ucdata[i] = UCHAR_MAX;
236 if(fwrite(ucdata,
sizeof(
unsigned char), img_size,
file) != img_size) {
239 if(ucdata) {
delete [] ucdata; ucdata=0;}
272 const unsigned short *data =
static_cast < const unsigned short *
>(first_block);
273 unsigned short nx = data[0];
274 unsigned short ny = data[1];
275 unsigned short nz = data[2];
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.
static void become_big_endian(T *data, size_t n=1)
convert data from host byte order to big endian order.
static bool is_valid(const void *first_block, off_t file_size=0)
Dict is a dictionary to store <string, EMObject> pair.
EMDataType
Image pixel data type used in EMAN.
static void getRenderMinMax(float *data, const int nx, const int ny, float &rendermin, float &rendermax, int &renderbits, const int nz=1)
Calculate the min and max pixel value accepted for image nomalization, if we did not get them from im...
static void getRenderLimits(const Dict &dict, float &rendermin, float &rendermax, int &renderbits)
Get the min and max pixel value accepted for image nomalization from image attribute dictionary,...
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...
EMData * copy() const
This file is a part of "emdata.h", to use functions in this file, you should "#include "emdata....
#define ImageReadException(filename, desc)
#define ImageWriteException(imagename, desc)
const int EMDataTypeBits[]
int portable_fseek(FILE *fp, off_t offset, int whence)