png_io.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright 2005-2007 Adobe Systems Incorporated
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. See http://opensource.adobe.com/gil for most recent version including documentation.
  7. */
  8. /*************************************************************************************************/
  9. #ifndef GIL_PNG_IO_H
  10. #define GIL_PNG_IO_H
  11. /// \file
  12. /// \brief Support for reading and writing PNG files
  13. /// Requires libpng and zlib!
  14. //
  15. // We are currently providing the following functions:
  16. // point2<std::ptrdiff_t> png_read_dimensions(const char*)
  17. // template <typename View> void png_read_view(const char*,const View&)
  18. // template <typename View> void png_read_image(const char*,image<View>&)
  19. // template <typename View> void png_write_view(const char*,const View&)
  20. // template <typename View> struct png_read_support;
  21. // template <typename View> struct png_write_support;
  22. //
  23. /// \author Hailin Jin and Lubomir Bourdev \n
  24. /// Adobe Systems Incorporated
  25. /// \date 2005-2007 \n Last updated September 24, 2006
  26. #include <stdio.h>
  27. #include <string>
  28. extern "C" {
  29. #include "png.h"
  30. }
  31. #include <boost/static_assert.hpp>
  32. #include "../../gil_config.hpp"
  33. #include "../../utilities.hpp"
  34. #include "io_error.hpp"
  35. #include "png_io_private.hpp"
  36. namespace boost { namespace gil {
  37. /// \ingroup PNG_IO
  38. /// \brief Returns the width and height of the PNG file at the specified location.
  39. /// Throws std::ios_base::failure if the location does not correspond to a valid PNG file
  40. inline point2<std::ptrdiff_t> png_read_dimensions(const char *filename) {
  41. detail::png_reader m(filename);
  42. return m.get_dimensions();
  43. }
  44. /// \ingroup PNG_IO
  45. /// \brief Returns the width and height of the PNG file at the specified location.
  46. /// Throws std::ios_base::failure if the location does not correspond to a valid PNG file
  47. inline point2<std::ptrdiff_t> png_read_dimensions(const std::string& filename) {
  48. return png_read_dimensions(filename.c_str());
  49. }
  50. /// \ingroup PNG_IO
  51. /// \brief Determines whether the given view type is supported for reading
  52. template <typename View>
  53. struct png_read_support {
  54. BOOST_STATIC_CONSTANT(bool,is_supported=
  55. (detail::png_read_support_private<typename channel_type<View>::type,
  56. typename color_space_type<View>::type>::is_supported));
  57. BOOST_STATIC_CONSTANT(int,bit_depth=
  58. (detail::png_read_support_private<typename channel_type<View>::type,
  59. typename color_space_type<View>::type>::bit_depth));
  60. BOOST_STATIC_CONSTANT(int,color_type=
  61. (detail::png_read_support_private<typename channel_type<View>::type,
  62. typename color_space_type<View>::type>::color_type));
  63. BOOST_STATIC_CONSTANT(bool, value=is_supported);
  64. };
  65. /// \ingroup PNG_IO
  66. /// \brief Loads the image specified by the given png image file name into the given view.
  67. /// 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.
  68. /// Throws std::ios_base::failure if the file is not a valid PNG file, or if its color space or channel depth are not
  69. /// compatible with the ones specified by View, or if its dimensions don't match the ones of the view.
  70. template <typename View>
  71. inline void png_read_view(const char* filename,const View& view) {
  72. BOOST_STATIC_ASSERT(png_read_support<View>::is_supported);
  73. detail::png_reader m(filename);
  74. m.apply(view);
  75. }
  76. /// \ingroup PNG_IO
  77. /// \brief Loads the image specified by the given png image file name into the given view.
  78. template <typename View>
  79. inline void png_read_view(const std::string& filename,const View& view) {
  80. png_read_view(filename.c_str(),view);
  81. }
  82. /// \ingroup PNG_IO
  83. /// \brief Allocates a new image whose dimensions are determined by the given png image file, and loads the pixels into it.
  84. /// 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.
  85. /// Throws std::ios_base::failure if the file is not a valid PNG file, or if its color space or channel depth are not
  86. /// compatible with the ones specified by Image
  87. template <typename Image>
  88. inline void png_read_image(const char* filename,Image& im) {
  89. BOOST_STATIC_ASSERT(png_read_support<typename Image::view_t>::is_supported);
  90. detail::png_reader m(filename);
  91. m.read_image(im);
  92. }
  93. /// \ingroup PNG_IO
  94. /// \brief Allocates a new image whose dimensions are determined by the given png image file, and loads the pixels into it.
  95. template <typename Image>
  96. inline void png_read_image(const std::string& filename,Image& im) {
  97. png_read_image(filename.c_str(),im);
  98. }
  99. /// \ingroup PNG_IO
  100. /// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
  101. /// 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.
  102. template <typename View,typename CC>
  103. inline void png_read_and_convert_view(const char* filename,const View& view,CC cc) {
  104. detail::png_reader_color_convert<CC> m(filename,cc);
  105. m.apply(view);
  106. }
  107. /// \ingroup PNG_IO
  108. /// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
  109. /// 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.
  110. template <typename View>
  111. inline void png_read_and_convert_view(const char* filename,const View& view) {
  112. detail::png_reader_color_convert<default_color_converter> m(filename,default_color_converter());
  113. m.apply(view);
  114. }
  115. /// \ingroup PNG_IO
  116. /// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
  117. template <typename View,typename CC>
  118. inline void png_read_and_convert_view(const std::string& filename,const View& view,CC cc) {
  119. png_read_and_convert_view(filename.c_str(),view,cc);
  120. }
  121. /// \ingroup PNG_IO
  122. /// \brief Loads the image specified by the given png image file name and color-converts it into the given view.
  123. template <typename View>
  124. inline void png_read_and_convert_view(const std::string& filename,const View& view) {
  125. png_read_and_convert_view(filename.c_str(),view);
  126. }
  127. /// \ingroup PNG_IO
  128. /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
  129. /// Throws std::ios_base::failure if the file is not a valid PNG file
  130. template <typename Image,typename CC>
  131. inline void png_read_and_convert_image(const char* filename,Image& im,CC cc) {
  132. detail::png_reader_color_convert<CC> m(filename,cc);
  133. m.read_image(im);
  134. }
  135. /// \ingroup PNG_IO
  136. /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
  137. /// Throws std::ios_base::failure if the file is not a valid PNG file
  138. template <typename Image>
  139. inline void png_read_and_convert_image(const char* filename,Image& im) {
  140. detail::png_reader_color_convert<default_color_converter> m(filename,default_color_converter());
  141. m.read_image(im);
  142. }
  143. /// \ingroup PNG_IO
  144. /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
  145. template <typename Image,typename CC>
  146. inline void png_read_and_convert_image(const std::string& filename,Image& im,CC cc) {
  147. png_read_and_convert_image(filename.c_str(),im,cc);
  148. }
  149. /// \ingroup PNG_IO
  150. /// \brief Allocates a new image whose dimensions are determined by the given png image file, loads and color-converts the pixels into it.
  151. template <typename Image>
  152. inline void png_read_and_convert_image(const std::string& filename,Image& im) {
  153. png_read_and_convert_image(filename.c_str(),im);
  154. }
  155. /// \ingroup PNG_IO
  156. /// \brief Determines whether the given view type is supported for writing
  157. template <typename View>
  158. struct png_write_support {
  159. BOOST_STATIC_CONSTANT(bool,is_supported=
  160. (detail::png_write_support_private<typename channel_type<View>::type,
  161. typename color_space_type<View>::type>::is_supported));
  162. BOOST_STATIC_CONSTANT(int,bit_depth=
  163. (detail::png_write_support_private<typename channel_type<View>::type,
  164. typename color_space_type<View>::type>::bit_depth));
  165. BOOST_STATIC_CONSTANT(int,color_type=
  166. (detail::png_write_support_private<typename channel_type<View>::type,
  167. typename color_space_type<View>::type>::color_type));
  168. BOOST_STATIC_CONSTANT(bool, value=is_supported);
  169. };
  170. /// \ingroup PNG_IO
  171. /// \brief Saves the view to a png file specified by the given png image file name.
  172. /// 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.
  173. /// Throws std::ios_base::failure if it fails to create the file.
  174. template <typename View>
  175. inline void png_write_view(const char* filename,const View& view) {
  176. BOOST_STATIC_ASSERT(png_write_support<View>::is_supported);
  177. detail::png_writer m(filename);
  178. m.apply(view);
  179. }
  180. /// \ingroup PNG_IO
  181. /// \brief Saves the view to a png file specified by the given png image file name.
  182. template <typename View>
  183. inline void png_write_view(const std::string& filename,const View& view) {
  184. png_write_view(filename.c_str(),view);
  185. }
  186. } } // namespace boost::gil
  187. #endif