Chris@16: /* Chris@16: Copyright 2005-2007 Adobe Systems Incorporated Chris@16: Chris@16: Use, modification and distribution are subject to the Boost Software License, Chris@16: Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at Chris@16: http://www.boost.org/LICENSE_1_0.txt). Chris@16: Chris@16: See http://opensource.adobe.com/gil for most recent version including documentation. Chris@16: */ Chris@16: Chris@16: /*************************************************************************************************/ Chris@16: Chris@16: #ifndef GIL_PNG_IO_H Chris@16: #define GIL_PNG_IO_H Chris@16: Chris@16: /// \file Chris@16: /// \brief Support for reading and writing PNG files Chris@16: /// Requires libpng and zlib! Chris@16: // Chris@16: // We are currently providing the following functions: Chris@16: // point2 png_read_dimensions(const char*) Chris@16: // template void png_read_view(const char*,const View&) Chris@16: // template void png_read_image(const char*,image&) Chris@16: // template void png_write_view(const char*,const View&) Chris@16: // template struct png_read_support; Chris@16: // template struct png_write_support; Chris@16: // Chris@16: /// \author Hailin Jin and Lubomir Bourdev \n Chris@16: /// Adobe Systems Incorporated Chris@16: /// \date 2005-2007 \n Last updated September 24, 2006 Chris@16: Chris@16: #include Chris@16: #include Chris@16: extern "C" { Chris@16: #include "png.h" Chris@16: } Chris@16: #include Chris@16: #include "../../gil_config.hpp" Chris@16: #include "../../utilities.hpp" Chris@16: #include "io_error.hpp" Chris@16: #include "png_io_private.hpp" Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Returns the width and height of the PNG file at the specified location. Chris@16: /// Throws std::ios_base::failure if the location does not correspond to a valid PNG file Chris@16: inline point2 png_read_dimensions(const char *filename) { Chris@16: detail::png_reader m(filename); Chris@16: return m.get_dimensions(); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Returns the width and height of the PNG file at the specified location. Chris@16: /// Throws std::ios_base::failure if the location does not correspond to a valid PNG file Chris@16: inline point2 png_read_dimensions(const std::string& filename) { Chris@16: return png_read_dimensions(filename.c_str()); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Determines whether the given view type is supported for reading Chris@16: template Chris@16: struct png_read_support { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported= Chris@16: (detail::png_read_support_private::type, Chris@16: typename color_space_type::type>::is_supported)); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth= Chris@16: (detail::png_read_support_private::type, Chris@16: typename color_space_type::type>::bit_depth)); Chris@16: BOOST_STATIC_CONSTANT(int,color_type= Chris@16: (detail::png_read_support_private::type, Chris@16: typename color_space_type::type>::color_type)); Chris@16: BOOST_STATIC_CONSTANT(bool, value=is_supported); Chris@16: }; Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Loads the image specified by the given png image file name into the given view. Chris@16: /// 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. Chris@16: /// Throws std::ios_base::failure if the file is not a valid PNG file, or if its color space or channel depth are not Chris@16: /// compatible with the ones specified by View, or if its dimensions don't match the ones of the view. Chris@16: template Chris@16: inline void png_read_view(const char* filename,const View& view) { Chris@16: BOOST_STATIC_ASSERT(png_read_support::is_supported); Chris@16: detail::png_reader m(filename); Chris@16: m.apply(view); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Loads the image specified by the given png image file name into the given view. Chris@16: template Chris@16: inline void png_read_view(const std::string& filename,const View& view) { Chris@16: png_read_view(filename.c_str(),view); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given png image file, and loads the pixels into it. Chris@16: /// Triggers a compile assert if the image color space or channel depth are not supported by the PNG library or by the I/O extension. Chris@16: /// Throws std::ios_base::failure if the file is not a valid PNG file, or if its color space or channel depth are not Chris@16: /// compatible with the ones specified by Image Chris@16: template Chris@16: inline void png_read_image(const char* filename,Image& im) { Chris@16: BOOST_STATIC_ASSERT(png_read_support::is_supported); Chris@16: detail::png_reader m(filename); Chris@16: m.read_image(im); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given png image file, and loads the pixels into it. Chris@16: template Chris@16: inline void png_read_image(const std::string& filename,Image& im) { Chris@16: png_read_image(filename.c_str(),im); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Loads the image specified by the given png image file name and color-converts it into the given view. Chris@16: /// Throws std::ios_base::failure if the file is not a valid PNG file, or if its dimensions don't match the ones of the view. Chris@16: template Chris@16: inline void png_read_and_convert_view(const char* filename,const View& view,CC cc) { Chris@16: detail::png_reader_color_convert m(filename,cc); Chris@16: m.apply(view); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Loads the image specified by the given png image file name and color-converts it into the given view. Chris@16: /// Throws std::ios_base::failure if the file is not a valid PNG file, or if its dimensions don't match the ones of the view. Chris@16: template Chris@16: inline void png_read_and_convert_view(const char* filename,const View& view) { Chris@16: detail::png_reader_color_convert m(filename,default_color_converter()); Chris@16: m.apply(view); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Loads the image specified by the given png image file name and color-converts it into the given view. Chris@16: template Chris@16: inline void png_read_and_convert_view(const std::string& filename,const View& view,CC cc) { Chris@16: png_read_and_convert_view(filename.c_str(),view,cc); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Loads the image specified by the given png image file name and color-converts it into the given view. Chris@16: template Chris@16: inline void png_read_and_convert_view(const std::string& filename,const View& view) { Chris@16: png_read_and_convert_view(filename.c_str(),view); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it. Chris@16: /// Throws std::ios_base::failure if the file is not a valid PNG file Chris@16: template Chris@16: inline void png_read_and_convert_image(const char* filename,Image& im,CC cc) { Chris@16: detail::png_reader_color_convert m(filename,cc); Chris@16: m.read_image(im); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it. Chris@16: /// Throws std::ios_base::failure if the file is not a valid PNG file Chris@16: template Chris@16: inline void png_read_and_convert_image(const char* filename,Image& im) { Chris@16: detail::png_reader_color_convert m(filename,default_color_converter()); Chris@16: m.read_image(im); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it. Chris@16: template Chris@16: inline void png_read_and_convert_image(const std::string& filename,Image& im,CC cc) { Chris@16: png_read_and_convert_image(filename.c_str(),im,cc); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it. Chris@16: template Chris@16: inline void png_read_and_convert_image(const std::string& filename,Image& im) { Chris@16: png_read_and_convert_image(filename.c_str(),im); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Determines whether the given view type is supported for writing Chris@16: template Chris@16: struct png_write_support { Chris@16: BOOST_STATIC_CONSTANT(bool,is_supported= Chris@16: (detail::png_write_support_private::type, Chris@16: typename color_space_type::type>::is_supported)); Chris@16: BOOST_STATIC_CONSTANT(int,bit_depth= Chris@16: (detail::png_write_support_private::type, Chris@16: typename color_space_type::type>::bit_depth)); Chris@16: BOOST_STATIC_CONSTANT(int,color_type= Chris@16: (detail::png_write_support_private::type, Chris@16: typename color_space_type::type>::color_type)); Chris@16: BOOST_STATIC_CONSTANT(bool, value=is_supported); Chris@16: }; Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Saves the view to a png file specified by the given png image file name. Chris@16: /// 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. Chris@16: /// Throws std::ios_base::failure if it fails to create the file. Chris@16: template Chris@16: inline void png_write_view(const char* filename,const View& view) { Chris@16: BOOST_STATIC_ASSERT(png_write_support::is_supported); Chris@16: detail::png_writer m(filename); Chris@16: m.apply(view); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Saves the view to a png file specified by the given png image file name. Chris@16: template Chris@16: inline void png_write_view(const std::string& filename,const View& view) { Chris@16: png_write_view(filename.c_str(),view); Chris@16: } Chris@16: Chris@16: } } // namespace boost::gil Chris@16: Chris@16: #endif