2 Copyright 2005-2007 Adobe Systems Incorporated
4 Use, modification and distribution are subject to the Boost Software License,
5 Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 http://www.boost.org/LICENSE_1_0.txt).
8 See http://opensource.adobe.com/gil for most recent version including documentation.
11 /*************************************************************************************************/
17 /// \brief Support for reading and writing PNG files
18 /// Requires libpng and zlib!
20 // We are currently providing the following functions:
21 // point2<std::ptrdiff_t> png_read_dimensions(const char*)
22 // template <typename View> void png_read_view(const char*,const View&)
23 // template <typename View> void png_read_image(const char*,image<View>&)
24 // template <typename View> void png_write_view(const char*,const View&)
25 // template <typename View> struct png_read_support;
26 // template <typename View> struct png_write_support;
28 /// \author Hailin Jin and Lubomir Bourdev \n
29 /// Adobe Systems Incorporated
30 /// \date 2005-2007 \n Last updated September 24, 2006
37 #include <boost/static_assert.hpp>
38 #include "../../gil_config.hpp"
39 #include "../../utilities.hpp"
40 #include "io_error.hpp"
41 #include "png_io_private.hpp"
43 namespace boost { namespace gil {
46 /// \brief Returns the width and height of the PNG file at the specified location.
47 /// Throws std::ios_base::failure if the location does not correspond to a valid PNG file
48 inline point2<std::ptrdiff_t> png_read_dimensions(const char *filename) {
49 detail::png_reader m(filename);
50 return m.get_dimensions();
54 /// \brief Returns the width and height of the PNG file at the specified location.
55 /// Throws std::ios_base::failure if the location does not correspond to a valid PNG file
56 inline point2<std::ptrdiff_t> png_read_dimensions(const std::string& filename) {
57 return png_read_dimensions(filename.c_str());
61 /// \brief Determines whether the given view type is supported for reading
62 template <typename View>
63 struct png_read_support {
64 BOOST_STATIC_CONSTANT(bool,is_supported=
65 (detail::png_read_support_private<typename channel_type<View>::type,
66 typename color_space_type<View>::type>::is_supported));
67 BOOST_STATIC_CONSTANT(int,bit_depth=
68 (detail::png_read_support_private<typename channel_type<View>::type,
69 typename color_space_type<View>::type>::bit_depth));
70 BOOST_STATIC_CONSTANT(int,color_type=
71 (detail::png_read_support_private<typename channel_type<View>::type,
72 typename color_space_type<View>::type>::color_type));
73 BOOST_STATIC_CONSTANT(bool, value=is_supported);
77 /// \brief Loads the image specified by the given png image file name into the given view.
78 /// 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.
79 /// Throws std::ios_base::failure if the file is not a valid PNG file, or if its color space or channel depth are not
80 /// compatible with the ones specified by View, or if its dimensions don't match the ones of the view.
81 template <typename View>
82 inline void png_read_view(const char* filename,const View& view) {
83 BOOST_STATIC_ASSERT(png_read_support<View>::is_supported);
84 detail::png_reader m(filename);
89 /// \brief Loads the image specified by the given png image file name into the given view.
90 template <typename View>
91 inline void png_read_view(const std::string& filename,const View& view) {
92 png_read_view(filename.c_str(),view);
96 /// \brief Allocates a new image whose dimensions are determined by the given png image file, and loads the pixels into it.
97 /// 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.
98 /// Throws std::ios_base::failure if the file is not a valid PNG file, or if its color space or channel depth are not
99 /// compatible with the ones specified by Image
100 template <typename Image>
101 inline void png_read_image(const char* filename,Image& im) {
102 BOOST_STATIC_ASSERT(png_read_support<typename Image::view_t>::is_supported);
103 detail::png_reader m(filename);
108 /// \brief Allocates a new image whose dimensions are determined by the given png image file, and loads the pixels into it.
109 template <typename Image>
110 inline void png_read_image(const std::string& filename,Image& im) {
111 png_read_image(filename.c_str(),im);
115 /// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
116 /// 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.
117 template <typename View,typename CC>
118 inline void png_read_and_convert_view(const char* filename,const View& view,CC cc) {
119 detail::png_reader_color_convert<CC> m(filename,cc);
124 /// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
125 /// 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.
126 template <typename View>
127 inline void png_read_and_convert_view(const char* filename,const View& view) {
128 detail::png_reader_color_convert<default_color_converter> m(filename,default_color_converter());
133 /// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
134 template <typename View,typename CC>
135 inline void png_read_and_convert_view(const std::string& filename,const View& view,CC cc) {
136 png_read_and_convert_view(filename.c_str(),view,cc);
140 /// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
141 template <typename View>
142 inline void png_read_and_convert_view(const std::string& filename,const View& view) {
143 png_read_and_convert_view(filename.c_str(),view);
147 /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
148 /// Throws std::ios_base::failure if the file is not a valid PNG file
149 template <typename Image,typename CC>
150 inline void png_read_and_convert_image(const char* filename,Image& im,CC cc) {
151 detail::png_reader_color_convert<CC> m(filename,cc);
156 /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
157 /// Throws std::ios_base::failure if the file is not a valid PNG file
158 template <typename Image>
159 inline void png_read_and_convert_image(const char* filename,Image& im) {
160 detail::png_reader_color_convert<default_color_converter> m(filename,default_color_converter());
165 /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
166 template <typename Image,typename CC>
167 inline void png_read_and_convert_image(const std::string& filename,Image& im,CC cc) {
168 png_read_and_convert_image(filename.c_str(),im,cc);
172 /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
173 template <typename Image>
174 inline void png_read_and_convert_image(const std::string& filename,Image& im) {
175 png_read_and_convert_image(filename.c_str(),im);
179 /// \brief Determines whether the given view type is supported for writing
180 template <typename View>
181 struct png_write_support {
182 BOOST_STATIC_CONSTANT(bool,is_supported=
183 (detail::png_write_support_private<typename channel_type<View>::type,
184 typename color_space_type<View>::type>::is_supported));
185 BOOST_STATIC_CONSTANT(int,bit_depth=
186 (detail::png_write_support_private<typename channel_type<View>::type,
187 typename color_space_type<View>::type>::bit_depth));
188 BOOST_STATIC_CONSTANT(int,color_type=
189 (detail::png_write_support_private<typename channel_type<View>::type,
190 typename color_space_type<View>::type>::color_type));
191 BOOST_STATIC_CONSTANT(bool, value=is_supported);
195 /// \brief Saves the view to a png file specified by the given png image file name.
196 /// 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.
197 /// Throws std::ios_base::failure if it fails to create the file.
198 template <typename View>
199 inline void png_write_view(const char* filename,const View& view) {
200 BOOST_STATIC_ASSERT(png_write_support<View>::is_supported);
201 detail::png_writer m(filename);
206 /// \brief Saves the view to a png file specified by the given png image file name.
207 template <typename View>
208 inline void png_write_view(const std::string& filename,const View& view) {
209 png_write_view(filename.c_str(),view);
212 } } // namespace boost::gil