From 0332bed0045c8b0fe3fe06e6bcb8137973c3789b Mon Sep 17 00:00:00 2001 From: ExMix Date: Wed, 13 Mar 2013 11:45:53 +0300 Subject: [PATCH] Support png writer for gil::view. Work only for Texture --- coding/lodepng.cpp | 17 ++++++++--------- coding/lodepng.hpp | 3 ++- coding/lodepng_io.hpp | 8 ++++---- coding/lodepng_io_private.hpp | 8 ++++---- coding/writer.hpp | 28 ++++++++++++++++++++++++++++ 5 files changed, 46 insertions(+), 18 deletions(-) diff --git a/coding/lodepng.cpp b/coding/lodepng.cpp index dd07351236..a1db3ec190 100644 --- a/coding/lodepng.cpp +++ b/coding/lodepng.cpp @@ -4265,15 +4265,14 @@ namespace LodePNG } /*write given buffer to the file, overwriting the file, it doesn't append to it.*/ -// void saveFile(const std::vector& buffer, const std::string& filename) -// { -// size_t const sz = buffer.size(); -// if (sz > 0) -// { -// FileWriter writer(filename); -// writer.Write(&buffer[0], sz); -// } -// } + void saveFile(const std::vector& buffer, WriterPtr &writer) + { + size_t const sz = buffer.size(); + if (sz > 0) + { + writer.Write(&buffer[0], sz); + } + } //#endif /*LODEPNG_COMPILE_DISK*/ diff --git a/coding/lodepng.hpp b/coding/lodepng.hpp index d6c906e912..a9a4f8f532 100644 --- a/coding/lodepng.hpp +++ b/coding/lodepng.hpp @@ -31,6 +31,7 @@ freely, subject to the following restrictions: #include #include "reader.hpp" +#include "writer.hpp" /* ////////////////////////////////////////////////////////////////////////// */ /* Code Sections */ @@ -491,7 +492,7 @@ namespace LodePNG //#ifdef LODEPNG_COMPILE_DISK //free functions allowing to load and save a file from/to harddisk void loadFile(std::vector& buffer, ReaderPtr & reader); -// void saveFile(const std::vector& buffer, const std::string& filename); + void saveFile(const std::vector& buffer, WriterPtr & writer); //#endif //LODEPNG_COMPILE_DISK } //namespace LodePNG diff --git a/coding/lodepng_io.hpp b/coding/lodepng_io.hpp index f6254e4c62..23b7787e70 100644 --- a/coding/lodepng_io.hpp +++ b/coding/lodepng_io.hpp @@ -193,18 +193,18 @@ struct lodepng_write_support { /// Triggers a compile assert if the view color space and channel depth are not supported by the PNG library or by the I/O extension. /// Throws std::ios_base::failure if it fails to create the file. template -inline void lodepng_write_view(const char* filename,const View& view) { +inline void lodepng_write_view(WriterPtr & writer,const View& view) { BOOST_STATIC_ASSERT(lodepng_write_support::is_supported); - detail::lodepng_writer m(filename); + detail::lodepng_writer m(writer); m.apply(view); } /// \ingroup LODEPNG_IO /// \brief Saves the view to a png file specified by the given png image file name. -template +/*template inline void lodepng_write_view(const std::string& filename,const View& view) { lodepng_write_view(filename.c_str(),view); -} +}*/ } } // namespace boost::gil diff --git a/coding/lodepng_io_private.hpp b/coding/lodepng_io_private.hpp index 88e9ab741f..72e2732aaf 100644 --- a/coding/lodepng_io_private.hpp +++ b/coding/lodepng_io_private.hpp @@ -369,16 +369,16 @@ public: }; -class lodepng_writer : public file_mgr { +class lodepng_writer /*: public file_mgr */{ protected: LodePNG::Encoder m_encoder; - std::string m_fileName; + WriterPtr & m_writer; void init() {} public: - lodepng_writer(const char* filename) : file_mgr(filename, "wb"), m_fileName(filename) { init(); } + lodepng_writer(WriterPtr & writer) : /*file_mgr(filename, "wb"), m_fileName(filename)*/ m_writer(writer) { init(); } template void apply(const View& view) @@ -418,7 +418,7 @@ public: view.height() ); -// LodePNG::saveFile(buffer, m_fileName); + LodePNG::saveFile(buffer, m_writer); } }; diff --git a/coding/writer.hpp b/coding/writer.hpp index 2f300b2ffe..528d0f28da 100644 --- a/coding/writer.hpp +++ b/coding/writer.hpp @@ -3,6 +3,7 @@ #include "../base/base.hpp" #include "../base/exception.hpp" #include "../std/algorithm.hpp" +#include "../std/shared_ptr.hpp" #include "../std/memcpy.hpp" #include "../std/string.hpp" #include "../std/vector.hpp" @@ -115,6 +116,33 @@ private: #endif }; +template +class WriterPtr +{ +public: + WriterPtr(WriterT * p = 0) : m_p(p) {} + + void Seek(int64_t pos) + { + m_p->Seek(pos); + } + + int64_t Pos() const + { + return m_p->Pos(); + } + + void Write(void const * p, size_t size) + { + m_p->Write(p, size); + } + + WriterT * GetPtr() const { return m_p.get(); } + +protected: + shared_ptr m_p; +}; + template class WriterSink {