00001 00005 /* 00006 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu) 00007 * Copyright (c) 2000-2006 Baylor College of Medicine 00008 * 00009 * This software is issued under a joint BSD/GNU license. You may use the 00010 * source code in this file under either license. However, note that the 00011 * complete EMAN2 and SPARX software packages have some GPL dependencies, 00012 * so you are responsible for compliance with the licenses of these packages 00013 * if you opt to use BSD licensing. The warranty disclaimer below holds 00014 * in either instance. 00015 * 00016 * This complete copyright notice must be included in any revised version of the 00017 * source code. Additional authorship citations may be added, but existing 00018 * author citations must be preserved. 00019 * 00020 * This program is free software; you can redistribute it and/or modify 00021 * it under the terms of the GNU General Public License as published by 00022 * the Free Software Foundation; either version 2 of the License, or 00023 * (at your option) any later version. 00024 * 00025 * This program is distributed in the hope that it will be useful, 00026 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00027 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00028 * GNU General Public License for more details. 00029 * 00030 * You should have received a copy of the GNU General Public License 00031 * along with this program; if not, write to the Free Software 00032 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00033 * 00034 * */ 00035 00036 #ifndef eman__mrcio_h__ 00037 #define eman__mrcio_h__ 1 00038 00039 #include "imageio.h" 00040 00041 namespace EMAN 00042 { 00048 class MrcIO:public ImageIO 00049 { 00050 public: 00051 explicit MrcIO(const string & filename, IOMode rw_mode = READ_ONLY); 00052 ~MrcIO(); 00053 00054 DEFINE_IMAGEIO_FUNC; 00055 00056 int read_ctf(Ctf & ctf, int image_index = 0); 00057 void write_ctf(const Ctf & ctf, int image_index = 0); 00058 00059 static bool is_valid(const void *first_block, off_t file_size = 0); 00060 static int get_mode_size(int mm); 00061 static int to_em_datatype(int mrcmode); 00062 static int to_mrcmode(int em_datatype, int is_complex); 00063 00064 int get_nimg(); 00065 00066 private: 00067 enum MrcMode { 00068 MRC_UCHAR = 0, 00069 MRC_SHORT, 00070 MRC_FLOAT, 00071 MRC_SHORT_COMPLEX, 00072 MRC_FLOAT_COMPLEX, 00073 MRC_USHORT = 6, //non-standard 00074 MRC_UCHAR3 = 16, //unsigned char * 3, for rgb data, non-standard 00075 MRC_UNKNOWN 00076 }; 00077 00078 enum { 00079 MRC_NUM_LABELS = 10, 00080 MRC_LABEL_SIZE = 80, 00081 NUM_4BYTES_PRE_MAP = 52, 00082 NUM_4BYTES_AFTER_MAP = 3 00083 }; 00084 00085 /* updated to MRC Image2000 format which is compatible with CCP4 format */ 00086 struct MrcHeader 00087 { 00088 int nx; /* number of columns */ 00089 int ny; /* number of rows */ 00090 int nz; /* number of sections */ 00091 00092 int mode; /* See modes above. */ 00093 00094 int nxstart; /* No. of first column in map, default 0. */ 00095 int nystart; /* No. of first row in map, default 0. */ 00096 int nzstart; /* No. of first section in map,default 0. */ 00097 00098 int mx; /* Number of intervals along X. */ 00099 int my; /* Number of intervals along Y. */ 00100 int mz; /* Number of intervals along Z. */ 00101 00102 /* Cell: treat a whole 2D image as a cell */ 00103 float xlen; /* Cell dimensions (Angstroms). */ 00104 float ylen; /* Cell dimensions (Angstroms). */ 00105 float zlen; /* Cell dimensions (Angstroms). */ 00106 00107 float alpha; /* Cell angles (Degrees). */ 00108 float beta; /* Cell angles (Degrees). */ 00109 float gamma; /* Cell angles (Degrees). */ 00110 00111 /* axis X => 1, Y => 2, Z => 3 */ 00112 int mapc; /* Which axis corresponds to Columns. */ 00113 int mapr; /* Which axis corresponds to Rows. */ 00114 int maps; /* Which axis corresponds to Sections. */ 00115 00116 float amin; /* Minimum density value. */ 00117 float amax; /* Maximum density value. */ 00118 float amean; /* Mean density value. */ 00119 00120 int ispg; /* Space group number (0 for images). */ 00121 00122 int nsymbt; /* Number of chars used for storing symmetry operators. */ 00123 00124 int user[25]; 00125 00126 float xorigin; /* X origin. */ 00127 float yorigin; /* Y origin. */ 00128 float zorigin; /* Z origin. */ 00129 00130 char map[4]; /* constant string "MAP " */ 00131 int machinestamp; /* machine stamp in CCP4 convention: 00132 big endian=0x11110000 little endian=0x44440000 */ 00133 /* There is an ambiguity in the specification, using 0x11111111 & 4 instead */ 00134 00135 float rms; /* rms deviation of map from mean density */ 00136 00137 int nlabels; /* Number of labels being used. */ 00138 char labels[MRC_NUM_LABELS][MRC_LABEL_SIZE]; 00139 }; 00140 00153 struct FeiMrcHeader 00154 { 00155 int nx; /* The number of pixels in the x direction of the image */ 00156 int ny; /* The number of pixels in the y direction of the image */ 00157 int nz; /* The number of pixels in the z direction of the image 00158 Effectively in the tomography tilt series this means the 00159 number of images in the tilt series. */ 00160 00161 int mode; /* Defines the data type. Should always be 1 (2-byte integer) 00162 in the case of tomography*/ 00163 00164 int nxstart; /* set to 0: not used; lower bound of columns */ 00165 int nystart; /* set to 0: not used; lower bound of rows */ 00166 int nzstart; /* set to 0: not used; lower bound of sections */ 00167 00168 int mx; /* set to nx: not used; grid size x */ 00169 int my; /* set to ny: not used; grid size y */ 00170 int mz; /* set to nz: not used; grid size z */ 00171 00172 float xlen; /* set to mx: not used; cell size in x Angstroms (pixel spacing=3Dxlen/mx) */ 00173 float ylen; /* set to mx: not used; cell size in y Angstroms (pixel spacing=3Dxlen/my) */ 00174 float zlen; /* set to mx: not used; cell size in z Angstroms (pixel spacing=3Dxlen/mz) */ 00175 00176 float alpha; /* set to 90: not used; cell angles in degrees */ 00177 float beta; /* set to 90: not used; cell angles in degrees */ 00178 float gamma; /* set to 90: not used; cell angles in degrees */ 00179 00180 /* axis X => 1, Y => 2, Z => 3 */ 00181 int mapc; /* set to 1: not used; mapping columns, rows, sections on axis (x=3D1, y=3D2, z=3D3) */ 00182 int mapr; /* set to 2: not used; mapping columns, rows, sections on axis (x=3D1, y=3D2, z=3D3) */ 00183 int maps; /* set to 3: not used; mapping columns, rows, sections on axis (x=3D1, y=3D2, z=3D3) */ 00184 00185 float amin; /* minimum pixel value of all images in file */ 00186 float amax; /* maximum pixel value of all images in file */ 00187 float amean; /* mean pixel value of all images in file */ 00188 00189 short ispg; /* set to 0: not used; space group number (0 for images) */ 00190 00191 short nsymbt; /* set to 0: not used; number of bytes used for storing symmetry operators */ 00192 00193 int next; /* This value gives the offset (in bytes) from the end 00194 of the file header to the first dataset (image). 00195 Thus you will find the first image at 1024 + next bytes. */ 00196 short dvid; /* set to 0: not used; creator id */ 00197 char extra[30]; /* set to 0: not used, extra 30 bytes data */ 00198 short numintegers; /* set to 0: not used */ 00199 short numfloats; /* set to 32; we always expect a extended header of 32 floats */ 00200 00201 short sub; 00202 short zfac; 00203 float min2; 00204 float max2; 00205 float min3; 00206 float max3; 00207 float min4; 00208 float max4; 00209 short idtype; 00210 short lens; 00211 short nd1; 00212 short nd2; 00213 short vd1; 00214 short vd2; 00215 float tiltangles[9]; /* set to 0; not used; used to rotate model to match rotated image */ 00216 00217 float zorg; /* set to 0: not used; origin of image */ 00218 float xorg; 00219 float yorg; 00220 00221 int nlabl; /* number of labels */ 00222 char labl[MRC_NUM_LABELS][MRC_LABEL_SIZE]; /* Arrays of characters that can be used for description. 00223 Label0 is used for copyright information, always start with "Fei"*/ 00224 }; 00225 00230 struct FeiMrcExtHeader 00231 { 00232 float a_tilt; /* Alpha tilt, in degrees */ 00233 float b_tilt; /* beta tilt, in degrees */ 00234 00235 float x_stage; /* Stage x position. Normally in SI units (meters), but some older files may be 00236 in micrometers. Check by looking at values for x,y,z. If one of these exceeds 1, 00237 it will be micrometers. */ 00238 float y_stage; /* Stage y position. For testing of units see x_stage */ 00239 float z_stage; /* Stage z position. For testing of units see x_stage */ 00240 00241 float x_shift; /* Image shift x. For testing of units see x_stage */ 00242 float y_shift; /* Image shift y. For testing of units see x_stage */ 00243 00244 float defocus; /* Defocus as read from microscope. For testing of units see x_stage */ 00245 float exp_time; /* Exposure time in seconds */ 00246 float mean_int; /* Mean value of image */ 00247 00248 float tilt_axis; /* The orientation of the tilt axis in the image in degrees. 00249 Vertical to the top is 0=B0, the direction of positive rotation is anti-clockwise */ 00250 float pixel_size; /* The pixel size of the images in SI units (meters) */ 00251 float magnification; /*The magnification used in SI units (volts) */ 00252 float ht; /* Value of the high tension in SI units (volts) */ 00253 float binning; /* The binning of the CCD or STEM acquisition */ 00254 float appliedDefocus; /* The intended application defocus in SI units (meters), 00255 as defined for example in the tomography parameters view. */ 00256 00257 float remainder[16]; /* not used */ 00258 }; 00259 00260 static const char *CTF_MAGIC; 00261 static const char *SHORT_CTF_MAGIC; 00262 00263 00264 private: 00265 string filename; 00266 IOMode rw_mode; 00267 FILE *mrcfile; 00268 int mode_size; 00269 00270 union { 00271 MrcHeader mrch; 00272 FeiMrcHeader feimrch; 00273 }; 00274 00275 /* the extended MRC format for tomography, used by FEI */ 00276 bool isFEI; 00277 00278 int is_ri; 00279 bool is_big_endian; 00280 bool is_new_file; 00281 bool initialized; 00282 bool is_transpose; 00283 00285 static int generate_machine_stamp(); 00286 void swap_header(MrcHeader& mrch); 00287 00292 void update_stat(void* data); 00293 00294 int read_mrc_header(Dict & dict, int image_index = 0, const Region * area = 0, bool is_3d = false); 00295 int read_fei_header(Dict & dict, int image_index = 0, const Region * area = 0, bool is_3d = false); 00296 00297 //utility funciton to tranpose x and y dimension in case the source mrc image is mapc=2,mapr=1 00298 int transpose(float *data, int nx, int ny, int nz) const; 00299 }; 00300 } 00301 00302 #endif //eman__mrcio_h__