9 #ifndef IMPEM2D_TIFF_IMAGE_READER_WRITER_H
10 #define IMPEM2D_TIFF_IMAGE_READER_WRITER_H
13 #include <boost/filesystem.hpp>
15 IMPEM2D_BEGIN_NAMESPACE
17 class TIFFImageReaderWriter:
public ImageReaderWriter {
21 TIFFImageReaderWriter() {}
23 void read(
const String &filename,
24 em::ImageHeader& header,
25 cv::Mat &data)
const {
26 this->read_from_ints(filename, header,data);
29 void write(
const String &filename, em::ImageHeader& header,
30 const cv::Mat &data)
const {
31 this->write_to_ints(filename, header,data);
34 void write_to_floats(
const String &, em::ImageHeader&,
const cv::Mat &)
const
38 void read_from_floats(
const String &, em::ImageHeader&, cv::Mat &)
const {}
49 void read_from_ints(
const String &filename,
50 em::ImageHeader &header,cv::Mat &data)
const {
53 cv::Mat temp= cv::imread(filename,0);
55 IMP_THROW(
"Error reading from TIFF Image " << filename,IOException);
57 temp.assignTo(data,CV_64FC1);
70 void write_to_ints(
const String &filename,
71 em::ImageHeader& header,
72 const cv::Mat &data)
const {
75 IMP_LOG(
IMP::WARNING,
"Writing with TIFFImageReaderWriter "
76 "discards image header " << std::endl);
78 String ext=boost::filesystem::extension(filename);
80 << filename <<std::endl);
81 if(ext!=
".tiff" && ext!=
".tif") {
82 IMP_THROW(
"TIFFImageReaderWriter: The filename extension is not .tiff "
83 "or .tif",IOException);
89 cv::minMaxLoc(data,&min,&max);
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);
96 cv::minMaxLoc(TIFF_data,&min,&max);
98 cv::imwrite(filename,TIFF_data);
102 { out <<
"TIFFImageReaderWriter"; }, {});
106 IMPEM2D_END_NAMESPACE