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_DYNAMIC_IO_H Chris@16: #define GIL_PNG_DYNAMIC_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: /// \author Hailin Jin and Lubomir Bourdev \n Chris@16: /// Adobe Systems Incorporated Chris@16: /// \date 2005-2007 \n Last updated June 10, 2006 Chris@16: // Chris@16: // We are currently providing the following functions: Chris@16: // template void png_read_image(const char*,any_image&) Chris@16: // template void png_read_image(FILE*,any_image&,std::size_t) Chris@16: // template void png_write_view(const char*,const any_image_view&) Chris@16: // template void png_write_view(FILE*,const any_image_view&) Chris@16: Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include "../dynamic_image/dynamic_image_all.hpp" Chris@16: #include "io_error.hpp" Chris@16: #include "png_io.hpp" Chris@16: #include "png_io_private.hpp" Chris@16: #include "dynamic_io.hpp" Chris@16: Chris@16: namespace boost { namespace gil { Chris@16: Chris@16: namespace detail { Chris@16: Chris@16: struct png_write_is_supported { Chris@16: template struct apply Chris@16: : public mpl::bool_::is_supported> {}; Chris@16: }; Chris@16: Chris@16: class png_writer_dynamic : public png_writer { Chris@16: public: Chris@16: png_writer_dynamic(FILE* file ) : png_writer(file) {} Chris@16: png_writer_dynamic(const char* filename) : png_writer(filename){} Chris@16: Chris@16: template Chris@16: void write_view(const any_image_view& runtime_view) { Chris@16: dynamic_io_fnobj op(this); Chris@16: apply_operation(runtime_view,op); Chris@16: } Chris@16: }; Chris@16: Chris@16: class png_type_format_checker { Chris@16: int _bit_depth; Chris@16: int _color_type; Chris@16: public: Chris@16: png_type_format_checker(int bit_depth_in,int color_type_in) : Chris@16: _bit_depth(bit_depth_in),_color_type(color_type_in) {} Chris@16: template Chris@16: bool apply() { Chris@16: return png_read_support::bit_depth==_bit_depth && Chris@16: png_read_support::color_type==_color_type; Chris@16: } Chris@16: }; Chris@16: Chris@16: struct png_read_is_supported { Chris@16: template struct apply Chris@16: : public mpl::bool_::is_supported> {}; Chris@16: }; Chris@16: Chris@16: class png_reader_dynamic : public png_reader { Chris@16: public: Chris@16: png_reader_dynamic(FILE* file) : png_reader(file) {} Chris@16: png_reader_dynamic(const char* filename) : png_reader(filename){} Chris@16: Chris@16: template Chris@16: void read_image(any_image& im) { Chris@16: png_uint_32 width, height; Chris@16: int bit_depth, color_type, interlace_type; Chris@16: png_get_IHDR(_png_ptr, _info_ptr, Chris@16: &width, &height,&bit_depth,&color_type,&interlace_type, Chris@16: NULL, NULL); Chris@16: if (!construct_matched(im,png_type_format_checker(bit_depth,color_type))) { Chris@16: io_error("png_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file"); Chris@16: } else { Chris@16: im.recreate(width,height); Chris@16: dynamic_io_fnobj op(this); Chris@16: apply_operation(view(im),op); Chris@16: } Chris@16: } Chris@16: }; Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief reads a PNG image into a run-time instantiated image Chris@16: /// Opens the given png file name, selects the first type in Images whose color space and channel are compatible to those of the image file Chris@16: /// and creates a new image of that type with the dimensions specified by the image file. Chris@16: /// Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk. Chris@16: template Chris@16: inline void png_read_image(const char* filename,any_image& im) { Chris@16: detail::png_reader_dynamic m(filename); Chris@16: m.read_image(im); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief reads a PNG image into a run-time instantiated image Chris@16: template Chris@16: inline void png_read_image(const std::string& filename,any_image& im) { Chris@16: png_read_image(filename.c_str(),im); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Saves the currently instantiated view to a png file specified by the given png image file name. Chris@16: /// Throws std::ios_base::failure if the currently instantiated view type is not supported for writing by the I/O extension Chris@16: /// or if it fails to create the file. Chris@16: template Chris@16: inline void png_write_view(const char* filename,const any_image_view& runtime_view) { Chris@16: detail::png_writer_dynamic m(filename); Chris@16: m.write_view(runtime_view); Chris@16: } Chris@16: Chris@16: /// \ingroup PNG_IO Chris@16: /// \brief Saves the currently instantiated 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 any_image_view& runtime_view) { Chris@16: png_write_view(filename.c_str(),runtime_view); Chris@16: } Chris@16: Chris@16: } } // namespace boost::gil Chris@16: Chris@16: #endif