IMP  2.0.0
The Integrative Modeling Platform
TIFFImageReaderWriter.h
Go to the documentation of this file.
1 /**
2  * \file TIFFImageReaderWriter.h
3  * \brief Management of reading/writing TIFF images
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPEM2D_TIFF_IMAGE_READER_WRITER_H
10 #define IMPEM2D_TIFF_IMAGE_READER_WRITER_H
11 
13 #include <boost/filesystem.hpp>
14 
15 IMPEM2D_BEGIN_NAMESPACE
16 
17 class TIFFImageReaderWriter: public ImageReaderWriter {
18 
19 public:
20 
21  TIFFImageReaderWriter() {}
22 
23  void read(const String &filename,
24  em::ImageHeader& header,
25  cv::Mat &data) const {
26  this->read_from_ints(filename, header,data);
27  }
28 
29  void write(const String &filename, em::ImageHeader& header,
30  const cv::Mat &data) const {
31  this->write_to_ints(filename, header,data);
32  }
33 
34  void write_to_floats(const String &, em::ImageHeader&, const cv::Mat &) const
35  {
36  }
37 
38  void read_from_floats(const String &, em::ImageHeader&, cv::Mat &) const {}
39 
40  //! Reads an image file in TIFF format
41  /*!
42  \param[in] filename file to read
43  \param[in] header header to store the info
44  \param[in] data a matrix to store the grid of data of the image
45  \warning: The header passed is filled with standard values
46  If you need full header information you must work with other
47  format. Eg, Spider.
48  */
49  void read_from_ints(const String &filename,
50  em::ImageHeader &header,cv::Mat &data) const {
51  IMP_LOG_VERBOSE("reading with TIFFImageReaderWriter" << std::endl);
52  // read
53  cv::Mat temp= cv::imread(filename,0);
54  if(temp.empty()) {
55  IMP_THROW("Error reading from TIFF Image " << filename,IOException);
56  }
57  temp.assignTo(data,CV_64FC1);
58  // fill the header with default values
59  header.clear();
60  }
61 
62  //! Writes an EM image in TIFF format
63  /*!
64  \param[in] filename file to write
65  \param[in] header header with the image info
66  \param[in] data a matrix with the grid of data of the image
67  \warning: Careful: This function writes a 16-bit image.
68  You might be discarding float information.
69  */
70  void write_to_ints(const String &filename,
71  em::ImageHeader& header,
72  const cv::Mat &data) const {
73  IMP_UNUSED(header);
74  // discard header
75  IMP_LOG(IMP::WARNING,"Writing with TIFFImageReaderWriter "
76  "discards image header " << std::endl);
77  // check extension
78  String ext=boost::filesystem::extension(filename);
79  IMP_LOG_VERBOSE("TIFFImageReaderWriter writting to "
80  << filename <<std::endl);
81  if(ext!=".tiff" && ext!=".tif") {
82  IMP_THROW("TIFFImageReaderWriter: The filename extension is not .tiff "
83  "or .tif",IOException);
84  }
85 
86  // Convert the data value to 16-bit so it can be written as TIFF
87  cv::Mat TIFF_data;
88  double max,min;
89  cv::minMaxLoc(data,&min,&max);
90  double TIFF_max= 255;
91  double TIFF_min=0;
92  double alpha = (TIFF_max-TIFF_min)/(max-min);
93  double beta = TIFF_min-alpha*min;
94  data.convertTo(TIFF_data,CV_8UC1,alpha,beta);
95  // write image
96  cv::minMaxLoc(TIFF_data,&min,&max);
97 
98  cv::imwrite(filename,TIFF_data);
99  }
100 
101  IMP_OBJECT_INLINE(TIFFImageReaderWriter,
102  { out << "TIFFImageReaderWriter"; }, {});
103 };
104 
105 
106 IMPEM2D_END_NAMESPACE
107 
108 #endif /* IMPEM2D_TIFF_IMAGE_READER_WRITER_H */