IMP  2.4.0
The Integrative Modeling Platform
SpiderImageReaderWriter.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/SpiderImageReaderWriter.h
3  * \brief Management of Images in Spider format
4  * Copyright 2007-2015 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_SPIDER_IMAGE_READER_WRITER_H
8 #define IMPEM2D_SPIDER_IMAGE_READER_WRITER_H
9 
10 #include "IMP/em2d/em2d_config.h"
13 #include "IMP/em/ImageHeader.h"
15 #include <IMP/algebra/utility.h>
16 #include <IMP/algebra/endian.h>
17 #include <IMP/base/exception.h>
18 #include <IMP/base/log.h>
19 #include <typeinfo>
20 #include <complex>
21 #include <string>
22 #include <cstdio>
23 #include <iostream>
24 #include <fstream>
25 #include <cstring>
26 
27 IMPEM2D_BEGIN_NAMESPACE
28 
29 //! Class to read and write EM images in Spider format. They are stored in
30 //! the header and data passed as arguments
32  public:
33  String filename_;
34  bool skip_type_check_;
35  bool force_reversed_;
36  bool skip_extra_checkings_;
37 
38  //! Empty constructor. It does not force reversed header and does not
39  //! skip any of the tests
40  /*!
41  * \note reversed is only used in case that the type_check is skipped
42  */
44  skip_type_check_ = false;
45  force_reversed_ = false;
46  skip_extra_checkings_ = false;
47  }
48 
49  //! Full constructor.
50  /*!
51  * \param[in] filename file to read
52  * \param[in] skip_type_check if true, the check for type of image is skipped
53  * \param[in] force_reversed if true, the reverse mode is enforced
54  * for reading and writing
55  * \param[in] skip_extra_checkings if true, the most stringent
56  * tests for consistency of images are skipped when reading
57  */
58  SpiderImageReaderWriter(const String &filename, bool skip_type_check,
59  bool force_reversed, bool skip_extra_checkings) {
60  filename_ = filename;
61  skip_type_check_ = skip_type_check;
62  force_reversed_ = force_reversed;
63  skip_extra_checkings_ = skip_extra_checkings;
64  }
65 
66  void read(const String &filename, em::ImageHeader &header,
67  cv::Mat &data) const {
68  this->read_from_floats(filename, header, data);
69  }
70 
71  void write(const String &filename, em::ImageHeader &header,
72  const cv::Mat &data) const {
73  this->write_to_floats(filename, header, data);
74  }
75 
76  void read_from_ints(const String &, em::ImageHeader &, cv::Mat &) const {}
77 
78  void write_to_ints(const String &, em::ImageHeader &, const cv::Mat &) const {
79  }
80 
81  //! Reads an image file in Spider format and stores the content
82  //! int the header and data parameters
83  /*!
84  \param[in] filename file to read
85  \param[in] header header to store the info
86  \param[in] data a matrix to store the grid of data of the image
87  */
88  void read_from_floats(const String &filename, em::ImageHeader &header,
89  cv::Mat &data) const {
90  IMP_LOG_VERBOSE("reading with SpiderImageReaderWriter" << std::endl);
91  std::ifstream in;
92  in.open(filename.c_str(), std::ios::in | std::ios::binary);
93  if (in.fail() || in.bad()) {
94  IMP_THROW("Error reading from Spider Image " << filename, IOException);
95  }
96  //! The header format is already in Spider format, just read it
97  bool success = header.read(in, skip_type_check_, force_reversed_,
98  skip_extra_checkings_);
99  if (!success) {
100  IMP_THROW("Error reading header from Spider Image " << filename,
101  IOException);
102  }
103  IMP_LOG_VERBOSE("Header of image " << filename << std::endl << header
104  << std::endl);
105  // Adjust size of the matrix according to the header
106  unsigned int rows = (int)header.get_number_of_rows();
107  unsigned int cols = (int)header.get_number_of_columns();
108  data.create(rows, cols, CV_64FC1);
109  // Read with casting
110  float aux;
111  for (cvDoubleMatIterator it = data.begin<double>();
112  it != data.end<double>(); ++it) {
113  if (!(force_reversed_ ^ algebra::get_is_big_endian())) {
114  in.read(reinterpret_cast<char *>(&aux), sizeof(float));
115  } else {
116  algebra::reversed_read(reinterpret_cast<char *>(&aux), sizeof(float), 1,
117  in, true);
118  }
119  (*it) = static_cast<double>(aux);
120  }
121  in.close();
122  }
123 
124  //! Writes an EM image in Spider format
125  /*!
126  * \param[in] filename file to write
127  * \param[in] header header with the image info
128  * \param[in] data a matrix with the grid of data of the image
129  */
130  void write_to_floats(const String &filename, em::ImageHeader &header,
131  const cv::Mat &data) const {
132  std::ofstream out;
133  out.open(filename.c_str(), std::ios::out | std::ios::binary);
134  //! The image header is already in Spider format, just write it
135  header.write(out, force_reversed_ ^ algebra::get_is_big_endian());
136 
137  float aux;
138  for (cvDoubleConstMatIterator it = data.begin<double>();
139  it != data.end<double>(); ++it) {
140  aux = (float)(*it);
141  if (!(force_reversed_ ^ algebra::get_is_big_endian())) {
142  out.write(reinterpret_cast<char *>(&aux), sizeof(float));
143  } else {
144  algebra::reversed_write(reinterpret_cast<char *>(&aux), sizeof(float),
145  1, out, true);
146  }
147  }
148  out.close();
149  }
150 
152 };
153 
154 IMPEM2D_END_NAMESPACE
155 
156 #endif /* IMPEM2D_SPIDER_IMAGE_READER_WRITER_H */
bool get_is_big_endian()
Returns 1 if machine is big endian else 0.
Functions to convert between ImageHeader and DensityHeader Copyright 2007-2015 IMP Inventors...
Class to deal with the header of Electron Microscopy images in IMP.
Definition: ImageHeader.h:28
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
Virtual class for reader/writers of images.
SpiderImageReaderWriter(const String &filename, bool skip_type_check, bool force_reversed, bool skip_extra_checkings)
Full constructor.
Virtual class for reader/writers of images Copyright 2007-2015 IMP Inventors. All rights reserved...
Interface with OpenCV Copyright 2007-2015 IMP Inventors. All rights reserved.
#define IMP_LOG_VERBOSE(expr)
Definition: log_macros.h:94
Exception definitions and assertions.
void read_from_floats(const String &filename, em::ImageHeader &header, cv::Mat &data) const
void write(const String &filename, bool force_reversed=false)
Writes the header of a EM image.
functions to deal with endian of EM images
Functions to deal with very common math operations.
void write_to_floats(const String &filename, em::ImageHeader &header, const cv::Mat &data) const
Writes an EM image in Spider format.
void reversed_read(void *dest, size_t size, size_t nitems, std::ifstream &f, bool reverse)
Reads from file in normal or reverse order.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Definition: check_macros.h:50
int read(const String filename, bool skip_type_check=false, bool force_reversed=false, bool skip_extra_checkings=false)
Reads the header of a EM image.
void reversed_write(const void *src, size_t size, size_t nitems, std::ofstream &f, bool reverse=false)
Writes to a file in normal or reversed order.
Header for EM images. Compatible with Spider and Xmipp formats Copyright 2007-2015 IMP Inventors...
Logging and error reporting support.
std::string String
Basic string value.
Definition: types.h:44