diff --git a/modules/io/src/img/image_format.hh b/modules/io/src/img/image_format.hh index a9d0ff9e078e7d3aa330d4841625945f8be8a57e..95a6113677385c708adb1241ea47804169bffa59 100644 --- a/modules/io/src/img/image_format.hh +++ b/modules/io/src/img/image_format.hh @@ -39,23 +39,17 @@ class DLLEXPORT_OST_IO ImageFormatBase protected: ImageFormatBase(): - format_string_(""), - min_(0.0), - max_(1.0) + format_string_("") {}; ImageFormatBase(const String& formatstring): - format_string_(formatstring), - min_(0.0), - max_(1.0) + format_string_(formatstring) {}; public: - Real GetMaximum() const { return max_;} - void SetMaximum(Real max) { max_ = max; } - Real GetMinimum() const { return min_;} - void SetMinimum(Real min) { min_ = min; } + Real GetMaximum() const { return 1.0;} + Real GetMinimum() const { return 0.0;} template <typename T> @@ -68,7 +62,6 @@ class DLLEXPORT_OST_IO ImageFormatBase private: String format_string_; - Real min_,max_; }; diff --git a/modules/io/src/img/map_io_dat_handler.cc b/modules/io/src/img/map_io_dat_handler.cc index 09d5e55fc65fa115bb7e43c2c0e862803dee9a5a..4fdd6e285c2dc8fd0e2bbc2fd39f596b6edf2fa9 100644 --- a/modules/io/src/img/map_io_dat_handler.cc +++ b/modules/io/src/img/map_io_dat_handler.cc @@ -84,6 +84,66 @@ void DAT::SetNormalizeOnSave(bool normalize_on_save) normalize_on_save_ = normalize_on_save; } +Real DAT::GetMaximum() const +{ + if(GetSigned()){ + switch(GetBitDepth()){ + case OST_BIT8_FORMAT: + return 127.0; + break; + case OST_BIT16_FORMAT: + case OST_DEFAULT_FORMAT: + return 32767.0; + break; + case OST_BIT32_FORMAT: + return 2147483647.0; + break; + default: + return 1.0; + break; + } + }else{ + switch(GetBitDepth()){ + case OST_BIT8_FORMAT: + return 255.0; + break; + case OST_BIT16_FORMAT: + case OST_DEFAULT_FORMAT: + return 65535.0; + break; + case OST_BIT32_FORMAT: + return 4294967295.0; + break; + default: + return 1.0; + break; + } + } + return 1.0; +} + +Real DAT::GetMinimum() const +{ + if(GetSigned()){ + switch(GetBitDepth()){ + case OST_BIT8_FORMAT: + return -128.0; + break; + case OST_BIT16_FORMAT: + case OST_DEFAULT_FORMAT: + return -32768.0; + break; + case OST_BIT32_FORMAT: + return -2147483648.0; + break; + default: + return 0.0; + break; + } + } + return 0.0; +} + bool MapIODatHandler::MatchContent(unsigned char* header) { return false; @@ -442,39 +502,39 @@ void MapIODatHandler::Export(const img::MapHandle& sh, std::ostream& file,const case OST_DEFAULT_FORMAT: switch(formatdat.GetEndianess()) { case OST_BIG_ENDIAN: - real_dumper<OST_BIG_ENDIAN,int8_t>(sh,file,formatdat); + real_dumper<OST_BIG_ENDIAN,uint8_t>(sh,file,formatdat); break; case OST_LITTLE_ENDIAN: - real_dumper<OST_LITTLE_ENDIAN,int8_t>(sh,file,formatdat); + real_dumper<OST_LITTLE_ENDIAN,uint8_t>(sh,file,formatdat); break; case OST_VAX_DATA: - real_dumper<OST_VAX_DATA,int8_t>(sh,file,formatdat); + real_dumper<OST_VAX_DATA,uint8_t>(sh,file,formatdat); break; } break; case OST_BIT16_FORMAT: switch(formatdat.GetEndianess()) { case OST_BIG_ENDIAN: - real_dumper<OST_BIG_ENDIAN,int16_t>(sh,file,formatdat); + real_dumper<OST_BIG_ENDIAN,uint16_t>(sh,file,formatdat); break; case OST_LITTLE_ENDIAN: - real_dumper<OST_LITTLE_ENDIAN,int16_t>(sh,file,formatdat); + real_dumper<OST_LITTLE_ENDIAN,uint16_t>(sh,file,formatdat); break; case OST_VAX_DATA: - real_dumper<OST_VAX_DATA,int16_t>(sh,file,formatdat); + real_dumper<OST_VAX_DATA,uint16_t>(sh,file,formatdat); break; } break; case OST_BIT32_FORMAT: switch(formatdat.GetEndianess()) { case OST_BIG_ENDIAN: - real_dumper<OST_BIG_ENDIAN,int32_t>(sh,file,formatdat); + real_dumper<OST_BIG_ENDIAN,uint32_t>(sh,file,formatdat); break; case OST_LITTLE_ENDIAN: - real_dumper<OST_LITTLE_ENDIAN,int32_t>(sh,file,formatdat); + real_dumper<OST_LITTLE_ENDIAN,uint32_t>(sh,file,formatdat); break; case OST_VAX_DATA: - real_dumper<OST_VAX_DATA,int32_t>(sh,file,formatdat); + real_dumper<OST_VAX_DATA,uint32_t>(sh,file,formatdat); break; } break; diff --git a/modules/io/src/img/map_io_dat_handler.hh b/modules/io/src/img/map_io_dat_handler.hh index e1ba422e223109b464889f96cc1929d8129b79ea..c4f130e44d470c2031efde906f923392b0088ce3 100644 --- a/modules/io/src/img/map_io_dat_handler.hh +++ b/modules/io/src/img/map_io_dat_handler.hh @@ -46,6 +46,8 @@ class DLLEXPORT_OST_IO DAT: public ImageFormatBase void SetEndianess(Endianess end); bool GetNormalizeOnSave() const; void SetNormalizeOnSave(bool noralize_on_save); + Real GetMaximum() const; + Real GetMinimum() const; static String FORMAT_STRING; private: diff --git a/modules/io/src/img/map_io_df3_handler.cc b/modules/io/src/img/map_io_df3_handler.cc index ac5d30c4f95e7039314e2300c9ffec2740c83137..dba2d957f54cc015debc8f493971d5f9fe245cfd 100644 --- a/modules/io/src/img/map_io_df3_handler.cc +++ b/modules/io/src/img/map_io_df3_handler.cc @@ -60,6 +60,15 @@ void DF3::SetNormalizeOnSave(bool normalize_on_save) normalize_on_save_ = normalize_on_save; } +Real DF3::GetMaximum() const +{ + return 65535.0; +} +Real DF3::GetMinimum() const +{ + return 0.0; +} + void DF3MapIOHandler::Import(img::MapHandle& mh, const bf::path& loc, const ImageFormatBase& formatstruct) { @@ -90,7 +99,6 @@ void DF3MapIOHandler::Export(const img::MapHandle& mh2, std::ostream& outfile, const ImageFormatBase& format) const { - static unsigned short max_val=std::numeric_limits<unsigned short>::max(); DF3 default_df3; DF3& fmt=default_df3; if (format.GetFormatString()==DF3::FORMAT_STRING) { @@ -104,8 +112,8 @@ void DF3MapIOHandler::Export(const img::MapHandle& mh2, img::alg::Normalizer norm=img::alg::CreateNoOpNormalizer(); if (fmt.GetNormalizeOnSave() == true) { - norm=img::alg::CreateLinearRangeNormalizer(mh2, format.GetMinimum(), - format.GetMaximum()); + norm=img::alg::CreateLinearRangeNormalizer(mh2, fmt.GetMinimum(), + fmt.GetMaximum()); } img::Size size=mh2.GetSize(); for (size_t i=0; i<3; ++i) { @@ -114,7 +122,7 @@ void DF3MapIOHandler::Export(const img::MapHandle& mh2, outfile.write(reinterpret_cast<const char*>(&v), sizeof(unsigned short)); } for (img::ExtentIterator i(mh2.GetExtent()); !i.AtEnd(); ++i) { - Real norm_value=norm.Convert(mh2.GetReal(i))*max_val; + Real norm_value=norm.Convert(mh2.GetReal(i)); unsigned short v=static_cast<unsigned short>(norm_value); Convert<OST_BIG_ENDIAN,unsigned short>::FromIP(&v); outfile.write(reinterpret_cast<const char*>(&v), sizeof(unsigned short)); diff --git a/modules/io/src/img/map_io_df3_handler.hh b/modules/io/src/img/map_io_df3_handler.hh index b8bb21851aee110caebaea8a8799e08fc0b58684..7682d0131a2ac14f1d9142faf85fc1b5cd9432d1 100644 --- a/modules/io/src/img/map_io_df3_handler.hh +++ b/modules/io/src/img/map_io_df3_handler.hh @@ -35,6 +35,8 @@ class DLLEXPORT_OST_IO DF3: public ImageFormatBase bool GetNormalizeOnSave() const; void SetNormalizeOnSave(bool normalize_on_save); + Real GetMaximum() const; + Real GetMinimum() const; static String FORMAT_STRING; static String FORMAT_NAME_STRING; static String FORMAT_ALIGNMENT_STRING; diff --git a/modules/io/src/img/map_io_png_handler.cc b/modules/io/src/img/map_io_png_handler.cc index 58cf49ee9e78de22a5d9ebeea5c04ccc4aeaa55d..571cecfa907547716b6997e9397dccb04313b4bd 100644 --- a/modules/io/src/img/map_io_png_handler.cc +++ b/modules/io/src/img/map_io_png_handler.cc @@ -49,8 +49,6 @@ PNG::PNG(bool normalize_on_save): ImageFormatBase(FORMAT_STRING), normalize_on_save_(normalize_on_save) { - this->SetMinimum(0.0); - this->SetMaximum(255.0); } bool PNG::GetNormalizeOnSave() const @@ -63,6 +61,15 @@ void PNG::SetNormalizeOnSave(bool normalize_on_save) normalize_on_save_ = normalize_on_save; } +Real PNG::GetMaximum() const +{ + return 255.0; +} +Real PNG::GetMinimum() const +{ + return 0.0; +} + namespace detail { void user_read_data(png_structp pngPtr, png_bytep data, png_size_t length) { @@ -286,7 +293,7 @@ void MapIOPngHandler::Export(const img::MapHandle& image, std::ostream& f,const png_write_end(png_ptr, info_ptr); - png_destroy_write_struct(&png_ptr, &info_ptr); + png_destroy_write_struct(&png_ptr, (png_infopp)NULL); } diff --git a/modules/io/src/img/map_io_png_handler.hh b/modules/io/src/img/map_io_png_handler.hh index e7eb7957d9dbabaaeff0628c42d272745ae90e75..cc6a84d4afbcd76ff2455875e41fb02bbc8b3202 100644 --- a/modules/io/src/img/map_io_png_handler.hh +++ b/modules/io/src/img/map_io_png_handler.hh @@ -32,6 +32,9 @@ class DLLEXPORT_OST_IO PNG: public ImageFormatBase bool GetNormalizeOnSave() const; void SetNormalizeOnSave(bool normalize_on_save); + Real GetMaximum() const; + Real GetMinimum() const; + static String FORMAT_STRING; private: diff --git a/modules/io/src/img/map_io_tiff_handler.cc b/modules/io/src/img/map_io_tiff_handler.cc index 319a9ecaf992b243321d1de7ab948b353e1d4c18..2ca26e838f363f9de88e4b435a895003aafb8a93 100644 --- a/modules/io/src/img/map_io_tiff_handler.cc +++ b/modules/io/src/img/map_io_tiff_handler.cc @@ -51,8 +51,7 @@ String TIF::FORMAT_STRING="defined_tiff"; normalize_on_save_(normalize_on_save), bit_depth_(bit_depth), signed_(sign), - phasecolor_(phasecolor), - subimage_(-1) + phasecolor_(phasecolor) { } @@ -61,8 +60,7 @@ TIF::TIF(String format_string, boost::logic::tribool normalize_on_save, Format normalize_on_save_(normalize_on_save), bit_depth_(bit_depth), signed_(sign), - phasecolor_(phasecolor), - subimage_(-1) + phasecolor_(phasecolor) { } @@ -126,6 +124,66 @@ void TIF::SetSubimage (int subimage) subimage_ = subimage; } +Real TIF::GetMaximum() const +{ + if(GetSigned()){ + switch(GetBitDepth()){ + case OST_BIT8_FORMAT: + return 127.0; + break; + case OST_BIT16_FORMAT: + case OST_DEFAULT_FORMAT: + return 32767.0; + break; + case OST_BIT32_FORMAT: + return 2147483647.0; + break; + default: + return 1.0; + break; + } + }else{ + switch(GetBitDepth()){ + case OST_BIT8_FORMAT: + return 255.0; + break; + case OST_BIT16_FORMAT: + case OST_DEFAULT_FORMAT: + return 65535.0; + break; + case OST_BIT32_FORMAT: + return 4294967295.0; + break; + default: + return 1.0; + break; + } + } + return 1.0; +} + +Real TIF::GetMinimum() const +{ + if(GetSigned()){ + switch(GetBitDepth()){ + case OST_BIT8_FORMAT: + return -128.0; + break; + case OST_BIT16_FORMAT: + case OST_DEFAULT_FORMAT: + return -32768.0; + break; + case OST_BIT32_FORMAT: + return -2147483648.0; + break; + default: + return 0.0; + break; + } + } + return 0.0; +} + void MapIOTiffHandler::Import(img::MapHandle& image, const boost::filesystem::path& location,const ImageFormatBase& formatstruct) { TIF form; diff --git a/modules/io/src/img/map_io_tiff_handler.hh b/modules/io/src/img/map_io_tiff_handler.hh index 3ea310e858ae14b01e70d3b8b3d72decdbf7a51b..5c6985a3d60f66eab368142020ae94d53e5c5b84 100644 --- a/modules/io/src/img/map_io_tiff_handler.hh +++ b/modules/io/src/img/map_io_tiff_handler.hh @@ -59,6 +59,10 @@ class DLLEXPORT_OST_IO TIF: public ImageFormatBase int GetSubimage() const; void SetSubimage(int subimage); + + Real GetMaximum() const; + Real GetMinimum() const; + static String FORMAT_STRING; diff --git a/modules/io/tests/test_io_img.cc b/modules/io/tests/test_io_img.cc index 229b6293be1d8ae97679a7f5b05cb5792c59bdf3..5c17b353b1d811eeb8dd34c99b24e5f818d6742b 100644 --- a/modules/io/tests/test_io_img.cc +++ b/modules/io/tests/test_io_img.cc @@ -45,7 +45,7 @@ BOOST_AUTO_TEST_SUITE( io ) BOOST_AUTO_TEST_CASE(test_io_img) { //float tests - boost::test_tools::close_at_tolerance<Real> close_test(::boost::test_tools::percent_tolerance(0.0001)); + boost::test_tools::close_at_tolerance<Real> close_test(::boost::test_tools::percent_tolerance(0.001)); ost::img::ImageHandle testimage=ost::img::CreateImage(ost::img::Extent(ost::img::Point(0,0),ost::img::Point(3,3))); testimage.ApplyIP(ost::img::alg::Randomize()); testimage+=0.01; //if all values are > 0.0 we can use close_at_tolerance @@ -82,6 +82,7 @@ BOOST_AUTO_TEST_CASE(test_io_img) int_formats["DAT (16 bit)"]=new DAT(true,OST_BIT16_FORMAT); int_formats["TIF (16 bit)"]=new TIF; int_formats["JPK (16 bit)"]=new JPK; + int_formats["DF3"]=new DF3; for(std::map<String,ImageFormatBase*>::iterator it=int_formats.begin();it!=int_formats.end();++it){ ost::io::SaveImage(testimage,fname,*(it->second)); ost::img::ImageHandle loadedimage=ost::io::LoadImage(fname,*(it->second)); @@ -105,9 +106,8 @@ BOOST_AUTO_TEST_CASE(test_io_img) std::map<String,ImageFormatBase*> byte_formats; byte_formats["DAT (byte)"]=new DAT(true,OST_BIT8_FORMAT); byte_formats["PNG"]=new PNG; - byte_formats["DF3"]=new DF3; - byte_formats["JPK (byte)"]= new JPK(false,OST_BIT8_FORMAT); - byte_formats["TIF (byte)"]= new TIF(false,OST_BIT8_FORMAT); + byte_formats["JPK (byte)"]= new JPK(true,OST_BIT8_FORMAT); + byte_formats["TIF (byte)"]= new TIF(true,OST_BIT8_FORMAT); for(std::map<String,ImageFormatBase*>::iterator it=byte_formats.begin();it!=byte_formats.end();++it){ ost::io::SaveImage(testimage,fname,*(it->second)); ost::img::ImageHandle loadedimage=ost::io::LoadImage(fname,*(it->second));