EMAN2
mrcio.h
Go to the documentation of this file.
1/*
2 * Author: Steven Ludtke, 04/10/2003 (sludtke@bcm.edu)
3 * Copyright (c) 2000-2006 Baylor College of Medicine
4 *
5 * This software is issued under a joint BSD/GNU license. You may use the
6 * source code in this file under either license. However, note that the
7 * complete EMAN2 and SPARX software packages have some GPL dependencies,
8 * so you are responsible for compliance with the licenses of these packages
9 * if you opt to use BSD licensing. The warranty disclaimer below holds
10 * in either instance.
11 *
12 * This complete copyright notice must be included in any revised version of the
13 * source code. Additional authorship citations may be added, but existing
14 * author citations must be preserved.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 *
30 * */
31
32#ifndef eman__mrcio_h__
33#define eman__mrcio_h__ 1
34
35#include "imageio.h"
36
37namespace EMAN
38{
44 class MrcIO : public ImageIO
45 {
46 public:
47 explicit MrcIO(const string & fname, IOMode rw_mode = READ_ONLY);
48 ~MrcIO();
49
51
52 int read_ctf(Ctf & ctf, int image_index = 0);
53 void write_ctf(const Ctf & ctf, int image_index = 0);
54
55 static bool is_valid(const void *first_block, off_t file_size = 0);
56 static int get_mode_size(int mm);
57 static int to_em_datatype(int mrcmode);
58 static int to_mrcmode(int em_datatype, int is_complex);
59
60 int get_nimg();
61
62 private:
63 enum MrcMode {
70 MRC_UCHAR3 = 16, // unsigned char * 3, for rgb data, non-standard
71 MRC_CHAR = 17, // non-standard - signed char
72 MRC_UHEX = 101, // 2 4-bit values packed into each 8-bit byte
73 MRC_UNKNOWN = 18
74 };
75
76 enum {
81 };
82
83 /* updated to MRC Image2000 format which is compatible with CCP4 format */
84 struct MrcHeader
85 {
86 int nx; /* 0 - number of columns */
87 int ny; /* 1 - number of rows */
88 int nz; /* 2 - number of sections */
89
90 int mode; /* 3 - See modes above. */
91
92 int nxstart; /* 4 - No. of first column in map, default 0. */
93 int nystart; /* 5 - No. of first row in map, default 0. */
94 int nzstart; /* 6 - No. of first section in map,default 0. */
95
96 int mx; /* 7 - Number of intervals along X. */
97 int my; /* 8 - Number of intervals along Y. */
98 int mz; /* 9 - Number of intervals along Z. */
99
100 /* Cell: treat a whole 2D image as a cell */
101 float xlen; /* 10 - Cell dimensions (Angstroms). */
102 float ylen; /* 11 - Cell dimensions (Angstroms). */
103 float zlen; /* 12 - Cell dimensions (Angstroms). */
104
105 float alpha; /* 13 - Cell angles (Degrees). */
106 float beta; /* 14 - Cell angles (Degrees). */
107 float gamma; /* 15 - Cell angles (Degrees). */
108
109 /* axis X => 1, Y => 2, Z => 3 */
110 int mapc; /* 16 - Which axis corresponds to Columns. */
111 int mapr; /* 17 - Which axis corresponds to Rows. */
112 int maps; /* 18 - Which axis corresponds to Sections. */
113
114 float amin; /* 19 - Minimum density value. */
115 float amax; /* 20 - Maximum density value. */
116 float amean; /* 21 - Mean density value. */
117
118 int ispg; /* 22 - Space group number (0 for images). */
119
120 int nsymbt; /* 23 - Number of chars used for storing symmetry operators. */
121
122 int user1[15]; // 24 - 38
123 int imod_flags; /* 39 - bit flags used by IMOD - >= 16 for 8 bit packed */
124 int user2[9];
125
126 float xorigin; /* 40 - X origin. */
127 float yorigin; /* Y origin. */
128 float zorigin; /* Z origin. */
129
130 char map[4]; /* constant string "MAP " */
131 int machinestamp; /* machine stamp in CCP4 convention:
132 big endian=0x11110000 little endian=0x44440000 */
133 /* There is an ambiguity in the specification, using 0x11111111 & 4 instead */
134
135 float rms; /* rms deviation of map from mean density */
136
137 int nlabels; /* Number of labels being used. */
139 };
140
154 {
155 int nx; /* The number of pixels in the x direction of the image */
156 int ny; /* The number of pixels in the y direction of the image */
157 int nz; /* The number of pixels in the z direction of the image
158 Effectively in the tomography tilt series this means the
159 number of images in the tilt series. */
160
161 int mode; /* Defines the data type. Should always be 1 (2-byte integer)
162 in the case of tomography*/
163
164 int nxstart; /* set to 0: not used; lower bound of columns */
165 int nystart; /* set to 0: not used; lower bound of rows */
166 int nzstart; /* set to 0: not used; lower bound of sections */
167
168 int mx; /* set to nx: not used; grid size x */
169 int my; /* set to ny: not used; grid size y */
170 int mz; /* set to nz: not used; grid size z */
171
172 float xlen; /* set to mx: not used; cell size in x Angstroms (pixel spacing=3Dxlen/mx) */
173 float ylen; /* set to mx: not used; cell size in y Angstroms (pixel spacing=3Dxlen/my) */
174 float zlen; /* set to mx: not used; cell size in z Angstroms (pixel spacing=3Dxlen/mz) */
175
176 float alpha; /* set to 90: not used; cell angles in degrees */
177 float beta; /* set to 90: not used; cell angles in degrees */
178 float gamma; /* set to 90: not used; cell angles in degrees */
179
180 /* axis X => 1, Y => 2, Z => 3 */
181 int mapc; /* set to 1: not used; mapping columns, rows, sections on axis (x=3D1, y=3D2, z=3D3) */
182 int mapr; /* set to 2: not used; mapping columns, rows, sections on axis (x=3D1, y=3D2, z=3D3) */
183 int maps; /* set to 3: not used; mapping columns, rows, sections on axis (x=3D1, y=3D2, z=3D3) */
184
185 float amin; /* minimum pixel value of all images in file */
186 float amax; /* maximum pixel value of all images in file */
187 float amean; /* mean pixel value of all images in file */
188
189 short ispg; /* set to 0: not used; space group number (0 for images) */
190
191 short nsymbt; /* set to 0: not used; number of bytes used for storing symmetry operators */
192
193 int next; /* This value gives the offset (in bytes) from the end
194 of the file header to the first dataset (image).
195 Thus you will find the first image at 1024 + next bytes. */
196 short dvid; /* set to 0: not used; creator id */
197 char extra[30]; /* set to 0: not used, extra 30 bytes data */
198 short numintegers; /* set to 0: not used */
199 short numfloats; /* set to 32; we always expect a extended header of 32 floats */
200
201 short sub;
202 short zfac;
203 float min2;
204 float max2;
205 float min3;
206 float max3;
207 float min4;
208 float max4;
209 short idtype;
210 short lens;
211 short nd1;
212 short nd2;
213 short vd1;
214 short vd2;
215 float tiltangles[9]; /* set to 0; not used; used to rotate model to match rotated image */
216
217 float zorg; /* set to 0: not used; origin of image */
218 float xorg;
219 float yorg;
220
221 int nlabl; /* number of labels */
222 char labl[MRC_NUM_LABELS][MRC_LABEL_SIZE]; /* Arrays of characters that can be used for description.
223 Label0 is used for copyright information, always start with "Fei"*/
224 };
225
231 {
232 float a_tilt; /* Alpha tilt, in degrees */
233 float b_tilt; /* beta tilt, in degrees */
234
235 float x_stage; /* Stage x position. Normally in SI units (meters), but some older files may be
236 in micrometers. Check by looking at values for x,y,z. If one of these exceeds 1,
237 it will be micrometers. */
238 float y_stage; /* Stage y position. For testing of units see x_stage */
239 float z_stage; /* Stage z position. For testing of units see x_stage */
240
241 float x_shift; /* Image shift x. For testing of units see x_stage */
242 float y_shift; /* Image shift y. For testing of units see x_stage */
243
244 float defocus; /* Defocus as read from microscope. For testing of units see x_stage */
245 float exp_time; /* Exposure time in seconds */
246 float mean_int; /* Mean value of image */
247
248 float tilt_axis; /* The orientation of the tilt axis in the image in degrees.
249 Vertical to the top is 0=B0, the direction of positive rotation is anti-clockwise */
250 float pixel_size; /* The pixel size of the images in SI units (meters) */
251 float magnification; /*The magnification used in SI units (volts) */
252 float ht; /* Value of the high tension in SI units (volts) */
253 float binning; /* The binning of the CCD or STEM acquisition */
254 float appliedDefocus; /* The intended application defocus in SI units (meters),
255 as defined for example in the tomography parameters view. */
256
257 float remainder[16]; /* not used */
258 };
259
260 static const char *CTF_MAGIC;
261 static const char *SHORT_CTF_MAGIC;
262
263 private:
265
266 union {
269 };
270
271 /* the extended MRC format for tomography, used by FEI */
272 bool isFEI;
273
274 /* for MRCS (MRC stack) format */
277
278 /* for MRC 8 bit packed format (2 4-bit values in each byte) */
281
282 int is_ri;
289
291 static int generate_machine_stamp();
293
298 void update_stats(void* data, size_t size);
299
301 static void check_swap(const int * data, const char * filnam, bool show_errors,
302 bool & do_swap, bool & have_err);
303
304 int read_mrc_header(Dict & dict, int image_index = 0, const Region * area = 0, bool is_3d = false);
305 int read_fei_header(Dict & dict, int image_index = 0, const Region * area = 0, bool is_3d = false);
306
307 //utility funciton to tranpose x and y dimension in case the source mrc image is mapc=2,mapr=1
308 int transpose(float *data, int nx, int ny, int nz) const;
309 };
310}
311
312#endif //eman__mrcio_h__
Ctf is the base class for all CTF model.
Definition: ctf.h:60
Dict is a dictionary to store <string, EMObject> pair.
Definition: emobject.h:385
ImageIO classes are designed for reading/writing various electron micrography image formats,...
Definition: imageio.h:127
IOMode rw_mode
Definition: imageio.h:353
MRC file = header + data (nx x ny x nz).
Definition: mrcio.h:45
MrcIO(const string &fname, IOMode rw_mode=READ_ONLY)
Definition: mrcio.cpp:49
static const char * CTF_MAGIC
Definition: mrcio.h:260
int get_nimg()
Return the number of images in this image file.
Definition: mrcio.cpp:1740
static void check_swap(const int *data, const char *filnam, bool show_errors, bool &do_swap, bool &have_err)
This is a utility routine to tell whether to byte swap MRC header.
Definition: mrcio.cpp:230
int read_fei_header(Dict &dict, int image_index=0, const Region *area=0, bool is_3d=false)
Definition: mrcio.cpp:574
static const char * SHORT_CTF_MAGIC
Definition: mrcio.h:261
int transpose(float *data, int nx, int ny, int nz) const
Definition: mrcio.cpp:1747
bool is_big_endian
Definition: mrcio.h:283
int mode_size
Definition: mrcio.h:264
float rendermin
Definition: mrcio.h:286
float rendermax
Definition: mrcio.h:287
static bool is_valid(const void *first_block, off_t file_size=0)
Definition: mrcio.cpp:342
int renderbits
Definition: mrcio.h:288
static int generate_machine_stamp()
generate the machine stamp used in MRC image format.
Definition: mrcio.cpp:1713
bool use_given_dimensions
Definition: mrcio.h:280
void write_ctf(const Ctf &ctf, int image_index=0)
Write CTF data to this image.
Definition: mrcio.cpp:1569
int read_mrc_header(Dict &dict, int image_index=0, const Region *area=0, bool is_3d=false)
Definition: mrcio.cpp:428
static int to_em_datatype(int mrcmode)
Definition: mrcio.cpp:1621
int is_ri
Definition: mrcio.h:282
@ MRC_LABEL_SIZE
Definition: mrcio.h:78
@ NUM_4BYTES_AFTER_MAP
Definition: mrcio.h:80
@ NUM_4BYTES_PRE_MAP
Definition: mrcio.h:79
@ MRC_NUM_LABELS
Definition: mrcio.h:77
bool is_stack
Definition: mrcio.h:275
DEFINE_IMAGEIO_FUNC
Definition: mrcio.h:50
bool is_transpose
Definition: mrcio.h:285
MrcHeader mrch
Definition: mrcio.h:267
void swap_header(MrcHeader &mrch)
Definition: mrcio.cpp:1734
bool isFEI
Definition: mrcio.h:272
static int get_mode_size(int mm)
Definition: mrcio.cpp:1595
bool is_8_bit_packed
Definition: mrcio.h:279
FeiMrcHeader feimrch
Definition: mrcio.h:268
void update_stats(void *data, size_t size)
This is a utility function used to calculate new min/max/mean/sigma when write MRC file as 16 bit or ...
Definition: mrcio.cpp:1406
@ MRC_FLOAT_COMPLEX
Definition: mrcio.h:68
@ MRC_SHORT_COMPLEX
Definition: mrcio.h:67
@ MRC_CHAR
Definition: mrcio.h:71
@ MRC_UCHAR3
Definition: mrcio.h:70
@ MRC_SHORT
Definition: mrcio.h:65
@ MRC_FLOAT
Definition: mrcio.h:66
@ MRC_UNKNOWN
Definition: mrcio.h:73
@ MRC_UCHAR
Definition: mrcio.h:64
@ MRC_UHEX
Definition: mrcio.h:72
@ MRC_USHORT
Definition: mrcio.h:69
bool is_new_file
Definition: mrcio.h:284
static int to_mrcmode(int em_datatype, int is_complex)
Definition: mrcio.cpp:1654
int stack_size
Definition: mrcio.h:276
int read_ctf(Ctf &ctf, int image_index=0)
Read CTF data from this image.
Definition: mrcio.cpp:1551
Region defines a 2D or 3D rectangular region specified by its origin coordinates and all edges' sizes...
Definition: geometry.h:497
bool is_complex() const
Is this a complex image?
E2Exception class.
Definition: aligner.h:40
The extended header used by Fei MRC image.
Definition: mrcio.h:231
Extended MRC format for tomography As used by Fei; original definition of extended header by Dave Aga...
Definition: mrcio.h:154
char labl[MRC_NUM_LABELS][MRC_LABEL_SIZE]
Definition: mrcio.h:222
char labels[MRC_NUM_LABELS][MRC_LABEL_SIZE]
Definition: mrcio.h:138