/** * @date Wed May 11 12:39:37 MDT 2016 * @author Manuel Gunther * * @brief The file provides an easy C++ interface to read and write PNG images using LIBPNG * * Copyright (c) 2016, Regents of the University of Colorado on behalf of the University of Colorado Colorado Springs. * All rights reserved. * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef BOB_IO_IMAGE_PNG_H #define BOB_IO_IMAGE_PNG_H #ifdef HAVE_LIBPNG #include #include #include #include #include #include /** * @brief Array submodule API of the I/O module */ namespace bob { namespace io { namespace image { class PNGFile: public bob::io::base::File { public: //api PNGFile(const char* path, char mode); virtual ~PNGFile() { } virtual const char* filename() const { return m_filename.c_str(); } virtual const bob::io::base::array::typeinfo& type_all() const { return m_type; } virtual const bob::io::base::array::typeinfo& type() const { return m_type; } virtual size_t size() const { return m_length; } virtual const char* name() const { return s_codecname.c_str(); } virtual void read_all(bob::io::base::array::interface& buffer) { read(buffer, 0); ///we only have 1 image in an image file anyways } virtual void read(bob::io::base::array::interface& buffer, size_t index); virtual size_t append (const bob::io::base::array::interface& buffer); virtual void write (const bob::io::base::array::interface& buffer); using bob::io::base::File::write; using bob::io::base::File::read; private: //representation std::string m_filename; bool m_newfile; bob::io::base::array::typeinfo m_type; size_t m_length; static std::string s_codecname; }; inline bool is_color_png(const std::string& filename){ PNGFile png(filename.c_str(), 'r'); return png.type().nd == 3; } template blitz::Array read_png(const std::string& filename){ PNGFile png(filename.c_str(), 'r'); switch (png.type().dtype){ case bob::io::base::array::t_uint8:{ blitz::Array image(png.read(0)); return bob::core::array::convert(image); } case bob::io::base::array::t_uint16:{ blitz::Array image(png.read(0)); return bob::core::array::convert(image); } default: throw std::runtime_error("The png image has a weired data type"); } } template void write_png(const blitz::Array& image, const std::string& filename){ PNGFile png(filename.c_str(), 'w'); png.write(image); } }}} #endif // HAVE_LIBPNG #endif /* BOB_IO_IMAGE_PNG_H */