diff --git a/modules/io/src/img/CMakeLists.txt b/modules/io/src/img/CMakeLists.txt index 4ed3e1999b5be767123770d113d215c8bbe70b39..8fa9a30ffd45564867fef83e5b17ef7bee0250b0 100644 --- a/modules/io/src/img/CMakeLists.txt +++ b/modules/io/src/img/CMakeLists.txt @@ -10,6 +10,7 @@ map_io_dat_handler.cc map_io_jpk_handler.cc map_io_nanoscope_handler.cc map_io_png_handler.cc +map_io_df3_handler.cc convert_vax_data.c image_format.cc tiff_util.cc @@ -18,6 +19,7 @@ PARENT_SCOPE set(OST_IO_IMG_HEADERS load_map.hh +map_io_df3_handler.hh image_format.hh image_format_conversion.cc map_io_dx_handler.hh diff --git a/modules/io/src/img/map_io_df3_handler.cc b/modules/io/src/img/map_io_df3_handler.cc new file mode 100644 index 0000000000000000000000000000000000000000..e33e64f7a9339ca453e10ed78bf5f48aa2c3f726 --- /dev/null +++ b/modules/io/src/img/map_io_df3_handler.cc @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------------ +// This file is part of the OpenStructure project <www.openstructure.org> +// +// Copyright (C) 2008-2010 by the OpenStructure authors +// Copyright (C) 2003-2010 by the IPLT authors +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation; either version 3.0 of the License, or (at your option) +// any later version. +// This library is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +// details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this library; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +//------------------------------------------------------------------------------ +/* + Author: Marco Biasini +*/ + +#include <cstdio> +#include <cstring> +#include <iomanip> +#include <limits> +#include <sstream> +#include <fstream> +#include <ost/log.hh> +#include <boost/filesystem/fstream.hpp> +#include <boost/algorithm/string.hpp> +#include <ost/io/swap_util.hh> +#include <ost/img/alg/normalizer_factory.hh> +#include <ost/io/io_exception.hh> +#include <ost/io/converting_streams.hh> +#include <ost/img/progress.hh> + +#include "map_io_df3_handler.hh" + +namespace bf = boost::filesystem; + +namespace ost { namespace io { + +String DF3::FORMAT_STRING="defined_df3"; + +DF3::DF3(bool normalize_on_save): + ImageFormatBase(FORMAT_STRING) +{ + normalize_on_save_ = normalize_on_save; +} + +bool DF3::GetNormalizeOnSave() const +{ + return normalize_on_save_; +} + +void DF3::SetNormalizeOnSave(bool normalize_on_save) +{ + normalize_on_save_ = normalize_on_save; +} + +void DF3MapIOHandler::Import(img::MapHandle& mh, const bf::path& loc, + const ImageFormatBase& formatstruct) +{ +} + +void DF3MapIOHandler::Import(img::MapHandle& mh, std::istream& infile, + const ImageFormatBase& formatstruct) +{ +} + + +void DF3MapIOHandler::Export(const img::MapHandle& mh2, + const bf::path& loc, + const ImageFormatBase& format) const +{ + boost::filesystem::ofstream outfile(loc, std::ios::binary); + if(!outfile) + { + throw IOException("could not open "+loc.string()); + } + BinaryOStream<OST_BIG_ENDIAN> big_endian_stream(outfile); + this->Export(mh2, big_endian_stream, format); + outfile.close(); + +} + +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) { + fmt=format.As<DF3>(); + } else { + assert(format.GetFormatString()==UndefinedImageFormat::FORMAT_STRING); + } + if (mh2.GetType()==img::COMPLEX) { + throw IOException("DF3 doesn't support complex-valued maps"); + } + img::alg::Normalizer norm=img::alg::CreateNoOpNormalizer(); + if (fmt.GetNormalizeOnSave() == true) { + + norm=img::alg::CreateLinearRangeNormalizer(mh2, format.GetMinimum(), + format.GetMaximum()); + } + img::Size size=mh2.GetSize(); + for (size_t i=0; i<3; ++i) { + unsigned short v=size[i]; + Convert<OST_BIG_ENDIAN,unsigned short>::FromIP(&v); + 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; + 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)); + } +} + +bool DF3MapIOHandler::MatchContent(unsigned char* header) +{ + return false; +} + +bool DF3MapIOHandler::MatchType(const ImageFormatBase& type) +{ + if(type.GetFormatString()==DF3::FORMAT_STRING) { + return true; + } + return false; +} + +bool DF3MapIOHandler::MatchSuffix(const String& loc) +{ + return detail::FilenameEndsWith(loc,".df3"); +} + + + +}} //namespaces diff --git a/modules/io/src/img/map_io_df3_handler.hh b/modules/io/src/img/map_io_df3_handler.hh new file mode 100644 index 0000000000000000000000000000000000000000..c6f3afd2f7f07bcbb4ad53d65d9be0afca3b95dc --- /dev/null +++ b/modules/io/src/img/map_io_df3_handler.hh @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// This file is part of the OpenStructure project <www.openstructure.org> +// +// Copyright (C) 2008-2010 by the OpenStructure authors +// Copyright (C) 2003-2010 by the IPLT authors +// +// This library is free software; you can redistribute it and/or modify it under +// the terms of the GNU Lesser General Public License as published by the Free +// Software Foundation; either version 3.0 of the License, or (at your option) +// any later version. +// This library is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more +// details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with this library; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +//------------------------------------------------------------------------------ +#ifndef OST_IO_DF3_HANDLER_HH +#define OST_IO_DF3_HANDLER_HH + +/* + Author: Marco Biasini + */ +#include "map_io_handler.hh" + +namespace ost { namespace io { + +class DLLEXPORT_OST_IO DF3: public ImageFormatBase +{ + public: + + DF3(bool normalize_on_save = false); + + bool GetNormalizeOnSave() const; + void SetNormalizeOnSave(bool normalize_on_save); + static String FORMAT_STRING; + static String FORMAT_NAME_STRING; + static String FORMAT_ALIGNMENT_STRING; + + private: + + bool normalize_on_save_; +}; + +class DLLEXPORT_OST_IO DF3MapIOHandler: public MapIOHandler +{ + public: + virtual void Import(img::MapHandle& sh, const boost::filesystem::path& loc, + const ImageFormatBase& formatstruct); + virtual void Import(img::MapHandle& sh, std::istream& loc, + const ImageFormatBase& formatstruct); + virtual void Export(const img::MapHandle& sh, const boost::filesystem::path& loc, + const ImageFormatBase& formatstruct) const; + virtual void Export(const img::MapHandle& sh, std::ostream& loc, + const ImageFormatBase& formatstruct) const; + static bool MatchContent(unsigned char* header); + static bool MatchType(const ImageFormatBase& type); + static bool MatchSuffix(const String& loc); + static String GetFormatName() { return "DF3"; }; + static String GetFormatDescription() { return "PovRay Density file format"; }; +}; + +typedef MapIOHandlerFactory<DF3MapIOHandler> MapIODF3HandlerFactory; + +}} // ns + +#endif diff --git a/modules/io/src/io_manager.cc b/modules/io/src/io_manager.cc index 3e61ce59b61e54d72fa11436dcba3614c007a9c7..c97938f907004b7a046d0bf0ae100d43adfbd525 100644 --- a/modules/io/src/io_manager.cc +++ b/modules/io/src/io_manager.cc @@ -37,6 +37,7 @@ # include <ost/io/img/map_io_dat_handler.hh> # include <ost/io/img/map_io_jpk_handler.hh> # include <ost/io/img/map_io_nanoscope_handler.hh> +# include <ost/io/img/map_io_df3_handler.hh> #endif namespace ost { namespace io { @@ -62,6 +63,7 @@ IOManager::IOManager() RegisterFactory(MapIOHandlerFactoryBasePtr(new MapIOJpkHandlerFactory)); RegisterFactory(MapIOHandlerFactoryBasePtr(new MapIODatHandlerFactory)); RegisterFactory(MapIOHandlerFactoryBasePtr(new MapIONanoscopeHandlerFactory)); + RegisterFactory(MapIOHandlerFactoryBasePtr(new MapIODF3HandlerFactory)); #endif }