7 #ifndef IMPEM2D_JPG_IMAGE_READER_WRITER_H
8 #define IMPEM2D_JPG_IMAGE_READER_WRITER_H
10 #include "IMP/em2d/em2d_config.h"
13 #include <boost/filesystem/convenience.hpp>
15 IMPEM2D_BEGIN_NAMESPACE
23 void read(
const String &filename,
25 cv::Mat &data)
const {
26 this->read_from_ints(filename, header,data);
30 const cv::Mat &data)
const {
31 this->write_to_ints(filename, header,data);
43 void read_from_ints(
const String &filename,
47 cv::Mat temp= cv::imread(filename,0);
49 IMP_THROW(
"Error reading from JPG Image " << filename,IOException);
51 temp.assignTo(data,CV_64FC1);
71 const cv::Mat &data)
const {
74 IMP_LOG(
IMP::WARNING,
"Writing with JPGImageReaderWriter "
75 "discards image header " << std::endl);
77 String ext=boost::filesystem::extension(filename);
79 << filename <<std::endl);
80 if(ext!=
".jpg" && ext!=
".jpeg") {
81 IMP_THROW(
"JPGImageReaderWriter: The filename extension is not .jpg "
82 "or .jpeg",IOException);
88 cv::minMaxLoc(data,&min,&max);
91 double alpha = (jpg_max-jpg_min)/(max-min);
92 double beta = jpg_min-alpha*min;
93 data.convertTo(jpg_data,CV_8UC1,alpha,beta);
95 std::vector<int> flags;
96 flags.push_back(CV_IMWRITE_JPEG_QUALITY);
98 cv::imwrite(filename,jpg_data,flags);
102 { out <<
"JPGImageReaderWriter"; }, {});
105 IMPEM2D_END_NAMESPACE