IMP  2.4.0
The Integrative Modeling Platform
ImageHeader.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em/ImageHeader.h
3  * \brief Header for EM images. Compatible with Spider and Xmipp formats
4  * Copyright 2007-2015 IMP Inventors. All rights reserved.
5 **/
6 
7 #ifndef IMPEM_IMAGE_HEADER_H
8 #define IMPEM_IMAGE_HEADER_H
9 
10 #include "IMP/em/em_config.h"
11 #include "IMP/em/SpiderHeader.h"
12 #include <IMP/algebra/Vector3D.h>
13 #include <IMP/algebra/Vector2D.h>
14 #include <IMP/algebra/utility.h>
15 #include <IMP/algebra/endian.h>
17 #include <cstdio>
18 #include <iostream>
19 #include <fstream>
20 #include <cstring>
21 
22 IMPEM_BEGIN_NAMESPACE
23 
24 //! Class to deal with the header of Electron Microscopy images in IMP.
25 /*!
26  \note Compatible with SPIDER format
27 */
28 class IMPEMEXPORT ImageHeader {
29  public:
30  //! Types of initialization of ImageHeader
31  typedef enum {
32  IMG_BYTE = 0,
33  IMG_IMPEM = 1,
34  IMG_INT = 9, /*IMG_SHORT = , IMG_UCHAR = ,*/
35  VOL_BYTE = 2,
36  VOL_IMPEM = 3,
37  VOL_INT = 10, /*VOL_SHORT = , VOL_UCHAR =, */
38  IMG_FOURIER = -1,
39  VOL_FOURIER = -3
40  } img_type;
41 
42  ImageHeader() {
43  clear();
44  set_image_type((float)IMG_IMPEM);
45  }
46 
47  ImageHeader(img_type im) {
48  clear();
49  set_image_type((float)im);
50  }
51 
52  ImageHeader(float im) {
53  clear();
54  set_image_type(im);
55  }
56 
57  float get_image_type() const { return spider_header_.fIform; }
58 
59  //! Set the type of image
60  /**
61  \param[in] im type of image. Available values:
62  IMG_BYTE = 0, IMG_IMPEM = 1,IMG_INT = 9, VOL_BYTE = 2, VOL_IMPEM = 3,
63  VOL_INT = 10 , IMG_FOURIER = -1, VOL_FOURIER = -3
64  \note No check is done for correctness of the value
65  */
66  void set_image_type(img_type im) { set_image_type((float)im); }
67 
68  void set_image_type(float im) { spider_header_.fIform = im; }
69 
70  void do_show(std::ostream& out) const {
71  out << "Image type : ";
72  switch ((int)spider_header_.fIform) {
73  case IMG_BYTE:
74  out << "2D Byte image";
75  break;
76  case IMG_IMPEM:
77  out << "2D IMP EM image";
78  break;
79  case IMG_INT:
80  out << "2D INT image";
81  break;
82  case VOL_BYTE:
83  out << "3D Byte volume";
84  break;
85  case VOL_IMPEM:
86  out << "3D IMP EM volume";
87  break;
88  case VOL_INT:
89  out << "3D INT volume";
90  break;
91  case IMG_FOURIER:
92  out << "2D Fourier image";
93  break;
94  case VOL_FOURIER:
95  out << "3D Fourier volume";
96  break;
97  }
98  out << std::endl;
99  out << "Reversed : ";
100  if (reversed_) {
101  out << "TRUE" << std::endl;
102  } else {
103  out << "FALSE" << std::endl;
104  }
105  out << "dimensions (slices x rows x columns) : " << get_number_of_slices()
106  << " " << get_number_of_rows() << " " << get_number_of_columns()
107  << std::endl;
108  out << "Origin (shift) : " << get_origin() << std::endl;
109  out << "Euler angles (Phi, Theta, Psi) (ZYZ convention): "
110  << get_euler_angles() << std::endl;
111  if (spider_header_.fFlag == 1.0f || spider_header_.fFlag == 2.0f) {
112  out << "Euler angles (Phi1, Theta1, Psi1) (ZYZ convention): "
113  << get_euler_angles1() << std::endl;
114  }
115  if (spider_header_.fFlag == 2.0f) {
116  out << "Euler angles (Phi2, Theta2, Psi2) (ZYZ convention): "
117  << get_euler_angles1() << std::endl;
118  }
119 
120  out << "Date : " << get_date() << std::endl;
121  out << "Time : " << get_time() << std::endl;
122  out << "Title : " << get_title() << std::endl;
123  out << "Header size : " << get_spider_header_size() << std::endl;
124  out << "Weight : " << get_Weight() << std::endl;
125  }
126 
127  IMP_SHOWABLE_INLINE(ImageHeader, do_show(out));
128  //! Prints a reduced set of information (debugging purposes)
129  void print_hard(std::ostream& out) const;
130 
131  //! Shows only the projection parameters
132  inline void show_projection_params(std::ostream& out) const {
133  out << "(Phi,Theta,Psi) = ( " << spider_header_.fPhi << " , "
134  << spider_header_.fTheta << " , " << spider_header_.fPsi << " ) "
135  << " (x,y) = ( " << spider_header_.fXoff << " , "
136  << spider_header_.fYoff << " ) " << std::endl;
137  }
138 
139  //! Reads the header of a EM image
140  // \note reversed is only used in case that the type_check is skipped
141  int read(const String filename, bool skip_type_check = false,
142  bool force_reversed = false, bool skip_extra_checkings = false);
143 
144  //! Reads the header of a EM image from an input file stream
145  bool read(std::ifstream& f, bool skip_type_check = false,
146  bool force_reversed = false, bool skip_extra_checkings = false);
147 
148  //! Writes the header of a EM image
149  void write(const String& filename, bool force_reversed = false);
150 
151  //! Writes the header of a EM image to an output file stream
152  void write(std::ofstream& f, bool force_reversed = false);
153 
154  //! Clear header data and sets a consistent header
155  void clear();
156 
157  //! Sets a consistent header
158  void set_header();
159 
160  //! Interaction with data
161  bool get_reversed() const { return reversed_; }
162  void set_reversed(bool value) { reversed_ = value; }
163 
164  unsigned int get_number_of_slices() const {
165  return (unsigned int)spider_header_.fNslice;
166  }
167  void set_number_of_slices(unsigned int n) {
168  spider_header_.fNslice = (float)n;
169  }
170 
171  unsigned int get_number_of_rows() const {
172  return (unsigned int)spider_header_.fNrow;
173  }
174  void set_number_of_rows(unsigned int n) { spider_header_.fNrow = (float)n; }
175 
176  unsigned int get_number_of_columns() const {
177  return (unsigned int)spider_header_.fNcol;
178  }
179  void set_number_of_columns(unsigned int n) {
180  spider_header_.fNcol = (float)n;
181  }
182 
183  //! get rotation angle. (Xmipp compatibility)
184  float get_old_rot() const { return spider_header_.fAngle1; }
185 
186  //! set rotation angle. (Xmipp compatibility)
187  void set_old_rot(float value) { spider_header_.fAngle1 = value; }
188 
189  //! get rotation angle. (Xmipp compatibility)
190  float get_fAngle1() const { return spider_header_.fAngle1; }
191 
192  //! set rotation angle. (Xmipp compatibility)
193  void set_fAngle1(float value) { spider_header_.fAngle1 = value; }
194 
195  float get_Scale() const { return spider_header_.fScale; }
196 
197  void set_Scale(float value) { spider_header_.fScale = value; }
198 
199  /* For Maximum-Likelihood refinement (Xmipp compatibility:
200  not currently used)
201  */
202  float get_Flip() const { return spider_header_.Flip; }
203 
204  /* For Maximum-Likelihood refinement (Xmipp compatibility:
205  not currently used)
206  */
207  void set_Flip(float value) { spider_header_.Flip = value; }
208 
209  float get_Weight() const { return spider_header_.Weight; }
210  void set_Weight(float value) { spider_header_.Weight = value; }
211 
212  float get_fNrec() const { return spider_header_.fNrec; }
213  void set_fNrec(float value) { spider_header_.fNrec = value; }
214 
215  float get_fNlabel() const { return spider_header_.fNlabel; }
216  void set_fNlabel(float value) { spider_header_.fNlabel = value; }
217 
218  float get_fIform() const { return spider_header_.fIform; }
219  void set_fIform(float value) { spider_header_.fIform = value; }
220 
221  float get_fImami() const { return spider_header_.fImami; }
222  void set_fImami(float value) { spider_header_.fImami = value; }
223 
224  float get_fFmax() const { return spider_header_.fFmax; }
225  void set_fFmax(float value) { spider_header_.fFmax = value; }
226 
227  float get_fFmin() const { return spider_header_.fFmin; }
228  void set_fFmin(float value) { spider_header_.fFmin = value; }
229 
230  float get_fAv() const { return spider_header_.fAv; }
231  void set_fAv(float value) { spider_header_.fAv = value; }
232 
233  float get_fSig() const { return spider_header_.fSig; }
234  void set_fSig(float value) { spider_header_.fSig = value; }
235 
236  float get_fIhist() const { return spider_header_.fIhist; }
237  void set_fIhist(float value) { spider_header_.fIhist = value; }
238 
239  float get_fLabrec() const { return spider_header_.fLabrec; }
240  void set_fLabrec(float value) { spider_header_.fLabrec = value; }
241 
242  float get_fIangle() const { return spider_header_.fIangle; }
243  void set_fIangle(float value) { spider_header_.fIangle = value; }
244 
245  algebra::Vector3D get_origin() const {
246  return algebra::Vector3D(spider_header_.fXoff, spider_header_.fYoff,
247  spider_header_.fZoff);
248  };
249 
250  void set_origin(const algebra::Vector3D& v) {
251  spider_header_.fXoff = v[0];
252  spider_header_.fYoff = v[1];
253  spider_header_.fZoff = v[2];
254  }
255 
256  void set_origin(const algebra::Vector2D& v) {
257  spider_header_.fXoff = (float)v[0];
258  spider_header_.fYoff = (float)v[1];
259  spider_header_.fZoff = (float)0.0;
260  }
261 
262  float get_object_pixel_size() const { return spider_header_.fScale; }
263  void set_object_pixel_size(float value) { spider_header_.fScale = value; }
264 
265  float get_fLabbyt() const { return spider_header_.fLabbyt; }
266  void set_fLabbyt(float value) { spider_header_.fLabbyt = value; }
267 
268  float get_fLenbyt() const { return spider_header_.fLenbyt; }
269  void set_fLenbyt(float value) { spider_header_.fLenbyt = value; }
270 
271  double get_fGeo_matrix(unsigned int i, unsigned int j) const;
272 
273  //! Gets the fFlag.
274  /** fFlag contains the number of triads of Euler angles stored
275  in the header (up to three).
276  set_euler_angles2 makes fFlag=2, set_euler_angles1 makes
277  fFlag=max(fFlag, 1), set_euler_angles does not change fFlag
278  */
279  float get_fFlag() const { return spider_header_.fFlag; }
280  void set_fFlag(float value) { spider_header_.fFlag = value; }
281 
282  algebra::Vector3D get_euler_angles() const {
283  return algebra::Vector3D(spider_header_.fPhi, spider_header_.fTheta,
284  spider_header_.fPsi);
285  }
286 
287  algebra::Vector3D get_euler_angles1() const {
288  return algebra::Vector3D(spider_header_.fPhi1, spider_header_.fTheta1,
289  spider_header_.fPsi1);
290  }
291 
292  algebra::Vector3D get_euler_angles2() const {
293  return algebra::Vector3D(spider_header_.fPhi2, spider_header_.fTheta2,
294  spider_header_.fPsi2);
295  }
296 
297  void set_euler_angles(const algebra::Vector3D& euler) {
298  spider_header_.fIangle = 1;
299  spider_header_.fPhi = euler[0]; // z
300  spider_header_.fTheta = euler[1]; // y
301  spider_header_.fPsi = euler[2]; // z
302  }
303 
304  void set_euler_angles1(const algebra::Vector3D& euler) {
305  if (spider_header_.fFlag != 2.f) {
306  spider_header_.fFlag = 1.f;
307  }
308  spider_header_.fPhi1 = euler[0]; // z
309  spider_header_.fTheta1 = euler[1]; // y
310  spider_header_.fPsi1 = euler[2]; // z
311  }
312 
313  void set_euler_angles2(const algebra::Vector3D& euler) {
314  spider_header_.fFlag = 2;
315  spider_header_.fPhi2 = euler[0]; // z
316  spider_header_.fTheta2 = euler[1]; // y
317  spider_header_.fPsi2 = euler[2]; // z
318  }
319 
320  bool is_normalized() const {
321  if (std::abs(get_fAv()) < 1e-6 && std::abs(get_fSig() - 1.0) < 1e-6) {
322  return true;
323  }
324  return false;
325  }
326 
327 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
328  // Date and Time
329  char* get_date() const;
330  char* get_time() const;
331 #endif
332  void set_date();
333  void set_time();
334 
335 #if !defined(IMP_DOXYGEN) && !defined(SWIG)
336  // Title
337  char* get_title() const;
338 #endif
339 
340  //! Set title of image in the header
341  void set_title(String newName);
342 
343  private:
344  //! get header size
345  int get_spider_header_size() const {
346  return (int)spider_header_.fNcol * (int)spider_header_.fLabrec *
347  sizeof(float);
348  }
349 
350  // The header is directly in format Spider with a couple of additions
351  em::SpiderHeader spider_header_;
352  bool reversed_;
353 }; // ImageHeader
354 
355 IMPEM_END_NAMESPACE
356 
357 #endif /* IMPEM_IMAGE_HEADER_H */
void set_image_type(img_type im)
Set the type of image.
Definition: ImageHeader.h:66
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
float get_fFlag() const
Gets the fFlag.
Definition: ImageHeader.h:279
VectorD< 2 > Vector2D
Definition: VectorD.h:391
Class to deal with the header of Electron Microscopy images in IMP.
Definition: ImageHeader.h:28
float get_old_rot() const
get rotation angle. (Xmipp compatibility)
Definition: ImageHeader.h:184
Simple 2D vector class.
functions to deal with endian of EM images
Functions to deal with very common math operations.
void show_projection_params(std::ostream &out) const
Shows only the projection parameters.
Definition: ImageHeader.h:132
bool get_reversed() const
Interaction with data.
Definition: ImageHeader.h:161
Header for Spider and Xmipp formats.
VectorD< 3 > Vector3D
Definition: VectorD.h:395
Simple 3D vector class.
img_type
Types of initialization of ImageHeader.
Definition: ImageHeader.h:31
void set_old_rot(float value)
set rotation angle. (Xmipp compatibility)
Definition: ImageHeader.h:187
void set_fAngle1(float value)
set rotation angle. (Xmipp compatibility)
Definition: ImageHeader.h:193
std::string String
Basic string value.
Definition: types.h:44
Various general useful macros for IMP.
float get_fAngle1() const
get rotation angle. (Xmipp compatibility)
Definition: ImageHeader.h:190