00001
00002
00003
00004
00005
00006
00007
00008
00009 #ifndef IMPEM_EM_READER_WRITER_H
00010 #define IMPEM_EM_READER_WRITER_H
00011
00012 #include "em_config.h"
00013 #include "MapReaderWriter.h"
00014 #include "DensityHeader.h"
00015 #include "def.h"
00016
00017 IMPEM_BEGIN_NAMESPACE
00018
00019 class IMPEMEXPORT EMHeader
00020 {
00021 public:
00022 #ifndef SWIG
00023 class EMHeaderParse
00024 {
00025 public:
00026 static const int COMMENT_SIZE = 80;
00027 static const int VOLTAGE_OFFSET = 0;
00028 static const int CS_OFFSET = 1;
00029 static const int APERTURE_OFFSET = 2;
00030 static const int MAGNIFICATION_OFFSET = 3;
00031 static const int POSTMAGNIFICATION_OFFSET = 4;
00032 static const int EXPOSURETIME_OFFSET = 5;
00033 static const int OBJECTPIXELSIZE_OFFSET = 6;
00034 static const int MICROSCOPE_OFFSET = 7;
00035 static const int PIXELSIZE_OFFSET = 8;
00036 static const int CCDAREA_OFFSET = 9;
00037 static const int DEFOCUS_OFFSET = 10;
00038 static const int ASTIGAMATISM_OFFSET = 11;
00039 static const int ASTIGAMATISMANGLE_OFFSET = 12;
00040 static const int FOCUSINCREMENT_OFFSET = 13;
00041 static const int COUNTSPERELECTRON_OFFSET = 14;
00042 static const int INTENSITY_OFFSET = 15;
00043 static const int ENERGYSLITWIDTH_OFFSET = 16;
00044 static const int ENERGYOFFSET_OFFSET = 17;
00045 static const int TILTANGLE_OFFSET = 18;
00046 static const int TILTAXIS_OFFSET = 19;
00047 static const int MARKERX_OFFSET = 20;
00048 static const int MARKERY_OFFSET = 21;
00049 static const int LSWAP_OFFSET = 22;
00050
00051 void InitEMHeader(EMHeader &header) {
00052 float scale = 1000.0;
00053 header.magic = (int)magic;
00054 header.type = (int)type;
00055 header.nx = nx;
00056 header.ny = ny;
00057 header.nz = nz;
00058 strncpy(header.comment, comment,COMMENT_SIZE);
00059 header.comment[COMMENT_SIZE] = '\0';
00060 header.voltage = (float)emdata[VOLTAGE_OFFSET];
00061 header.Cs = (float)emdata[CS_OFFSET]/scale;
00062 header.Aperture = (float)emdata[APERTURE_OFFSET];
00063 header.Magnification = (float)emdata[MAGNIFICATION_OFFSET];
00064 header.Postmagnification = (float)emdata[POSTMAGNIFICATION_OFFSET]
00065 /scale;
00066 header.Exposuretime = (float)emdata[EXPOSURETIME_OFFSET];
00067 header.Objectpixelsize = (float)emdata[OBJECTPIXELSIZE_OFFSET]
00068 /scale;
00069 header.Microscope = (float)emdata[MICROSCOPE_OFFSET];
00070 header.Pixelsize = (float)emdata[PIXELSIZE_OFFSET]/scale;
00071 header.CCDArea = (float)emdata[CCDAREA_OFFSET]/scale;
00072 header.Defocus = (float)emdata[DEFOCUS_OFFSET];
00073 header.Astigmatism = (float)emdata[ASTIGAMATISM_OFFSET]/scale;
00074 header.AstigmatismAngle = (float)emdata[ASTIGAMATISMANGLE_OFFSET];
00075 header.FocusIncrement = (float)emdata[FOCUSINCREMENT_OFFSET]
00076 /scale;
00077 header.CountsPerElectron = (float)emdata[COUNTSPERELECTRON_OFFSET]
00078 /scale;
00079 header.Intensity = (float)emdata[INTENSITY_OFFSET]/scale;
00080 header.EnergySlitwidth = (float)emdata[ENERGYSLITWIDTH_OFFSET];
00081 header.EnergyOffset = (float)emdata[ENERGYOFFSET_OFFSET];
00082 header.Tiltangle = (float)emdata[TILTANGLE_OFFSET]/scale;
00083 header.Tiltaxis = (float)emdata[TILTAXIS_OFFSET]/scale;
00084 header.MarkerX = (float)emdata[MARKERX_OFFSET];
00085 header.MarkerY = (float)emdata[MARKERY_OFFSET];
00086 header.lswap = emdata[LSWAP_OFFSET];
00087
00088 }
00089 void Init(const EMHeader &header) {
00090 magic = (unsigned char)header.magic;
00091 type = (unsigned char)header.type;
00092 nx = header.nx;
00093 ny = header.ny;
00094 nz = header.nz;
00095 strncpy(comment,header.comment, COMMENT_SIZE);
00096 float scale=1000.0;
00097 emdata[VOLTAGE_OFFSET] = (int)header.voltage;
00098 emdata[CS_OFFSET] = (int)(header.Cs*scale);
00099 emdata[APERTURE_OFFSET] = (int)header.Aperture;
00100 emdata[MAGNIFICATION_OFFSET] = (int)header.Magnification;
00101 emdata[POSTMAGNIFICATION_OFFSET] = (int)(header.Postmagnification
00102 * scale);
00103 emdata[EXPOSURETIME_OFFSET] = (int)header.Exposuretime ;
00104 emdata[OBJECTPIXELSIZE_OFFSET] = (int)(header.Objectpixelsize
00105 * scale);
00106 emdata[MICROSCOPE_OFFSET] = (int)header.Microscope;
00107 emdata[PIXELSIZE_OFFSET] = (int)(header.Pixelsize * scale);
00108 emdata[CCDAREA_OFFSET] = (int)(header.CCDArea * scale);
00109 emdata[DEFOCUS_OFFSET] = (int)header.Defocus;
00110 emdata[ASTIGAMATISM_OFFSET] = (int)(header.Astigmatism * scale);
00111 emdata[ASTIGAMATISMANGLE_OFFSET] = (int)header.AstigmatismAngle;
00112 emdata[FOCUSINCREMENT_OFFSET] = (int)(header.FocusIncrement
00113 * scale);
00114 emdata[COUNTSPERELECTRON_OFFSET] = (int)(header.FocusIncrement
00115 * scale);
00116 emdata[INTENSITY_OFFSET] = (int)(header.Intensity * scale);
00117 emdata[ENERGYSLITWIDTH_OFFSET] = (int)header.EnergySlitwidth;
00118 emdata[ENERGYOFFSET_OFFSET] = (int)header.EnergyOffset ;
00119 emdata[TILTANGLE_OFFSET] = (int)(header.Tiltangle *scale);
00120 emdata[TILTAXIS_OFFSET] = (int)(header.Tiltaxis * scale);
00121 emdata[MARKERX_OFFSET] = (int)header.MarkerX;
00122 emdata[MARKERY_OFFSET] = (int)header.MarkerY;
00123 emdata[LSWAP_OFFSET] = header.lswap;
00124
00125 }
00126
00127 unsigned char magic;
00128 unsigned char dummy[2];
00129 unsigned char type;
00130 int nx,ny,nz;
00131 char comment[80];
00132 int emdata[40];
00133 char dummy_long[256];
00134
00135 };
00136 #endif // SWIG
00137
00138
00139
00140 EMHeader(){}
00141 EMHeader(const DensityHeader &header);
00142 void generate_common_header(DensityHeader &header);
00143
00144
00145
00146 friend std::ostream& operator<<(std::ostream& s, const EMHeader &h) {
00147 return s <<"size : " << h.nx << " " << h.ny << " " << h.nz
00148 << std::endl << "magic: " << h.magic << std::endl <<
00149 "type: " << h.type << std::endl <<
00150 " comment : " << h.comment << std::endl <<
00151 " voltage : " << h.voltage << std::endl <<
00152 "cs: " << h.Cs << std::endl <<
00153 "aperture: " << h.Aperture<< std::endl <<
00154 "Magnification " << h.Magnification<< std::endl <<
00155 " Postmagnification "<< h.Postmagnification << std::endl <<
00156 " Exposuretime " << h.Exposuretime << std::endl <<
00157 "Objectpixelsize " << h.Objectpixelsize << std::endl <<
00158 "Microscope " << h.Microscope << std::endl <<
00159 "pixel-size: " << h.Pixelsize << std::endl;
00160 }
00161
00162
00163
00164 int magic;
00165 int type;
00166 int nx,ny,nz;
00167 char comment[81];
00168 float voltage;
00169 float Cs;
00170 float Aperture;
00171 float Magnification;
00172 float Postmagnification;
00173 float Exposuretime;
00174 float Objectpixelsize;
00175 float Microscope;
00176 float Pixelsize;
00177 float CCDArea;
00178 float Defocus;
00179 float Astigmatism;
00180 float AstigmatismAngle;
00181 float FocusIncrement;
00182 float CountsPerElectron;
00183 float Intensity;
00184 float EnergySlitwidth;
00185 float EnergyOffset;
00186 float Tiltangle;
00187 float Tiltaxis;
00188 float MarkerX;
00189 float MarkerY;
00190 int lswap;
00191 };
00192
00193
00194 class IMPEMEXPORT EMReaderWriter : public MapReaderWriter
00195 {
00196 public:
00197
00198
00199
00200
00201
00202
00203
00204 void Read(const char *filename, float **data, DensityHeader &header);
00205
00206
00207
00208
00209
00210
00211 void Write(const char* filename, const float *data,
00212 const DensityHeader &header);
00213 protected:
00214
00215
00216
00217
00218
00219
00220 void ReadHeader(std::ifstream &file, EMHeader &header);
00221
00222
00223
00224
00225
00226
00227
00228
00229 void ReadData(std::ifstream &file, float **data, const EMHeader &header);
00230
00231
00232
00233
00234
00235
00236 void WriteHeader(std::ostream& s, const EMHeader &header);
00237 };
00238
00239 IMPEM_END_NAMESPACE
00240
00241 #endif