png_dynamic_io.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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_DYNAMIC_IO_H
  10. #define GIL_PNG_DYNAMIC_IO_H
  11. /// \file
  12. /// \brief Support for reading and writing PNG files
  13. /// Requires libpng and zlib!
  14. ///
  15. /// \author Hailin Jin and Lubomir Bourdev \n
  16. /// Adobe Systems Incorporated
  17. /// \date 2005-2007 \n Last updated June 10, 2006
  18. //
  19. // We are currently providing the following functions:
  20. // template <typename Images> void png_read_image(const char*,any_image<Images>&)
  21. // template <typename Images> void png_read_image(FILE*,any_image<Images>&,std::size_t)
  22. // template <typename Views> void png_write_view(const char*,const any_image_view<View>&)
  23. // template <typename Views> void png_write_view(FILE*,const any_image_view<View>&)
  24. #include <string>
  25. #include <stdio.h>
  26. #include <boost/mpl/bool.hpp>
  27. #include <boost/shared_ptr.hpp>
  28. #include "../dynamic_image/dynamic_image_all.hpp"
  29. #include "io_error.hpp"
  30. #include "png_io.hpp"
  31. #include "png_io_private.hpp"
  32. #include "dynamic_io.hpp"
  33. namespace boost { namespace gil {
  34. namespace detail {
  35. struct png_write_is_supported {
  36. template<typename View> struct apply
  37. : public mpl::bool_<png_write_support<View>::is_supported> {};
  38. };
  39. class png_writer_dynamic : public png_writer {
  40. public:
  41. png_writer_dynamic(FILE* file ) : png_writer(file) {}
  42. png_writer_dynamic(const char* filename) : png_writer(filename){}
  43. template <typename Views>
  44. void write_view(const any_image_view<Views>& runtime_view) {
  45. dynamic_io_fnobj<png_write_is_supported, png_writer> op(this);
  46. apply_operation(runtime_view,op);
  47. }
  48. };
  49. class png_type_format_checker {
  50. int _bit_depth;
  51. int _color_type;
  52. public:
  53. png_type_format_checker(int bit_depth_in,int color_type_in) :
  54. _bit_depth(bit_depth_in),_color_type(color_type_in) {}
  55. template <typename Image>
  56. bool apply() {
  57. return png_read_support<typename Image::view_t>::bit_depth==_bit_depth &&
  58. png_read_support<typename Image::view_t>::color_type==_color_type;
  59. }
  60. };
  61. struct png_read_is_supported {
  62. template<typename View> struct apply
  63. : public mpl::bool_<png_read_support<View>::is_supported> {};
  64. };
  65. class png_reader_dynamic : public png_reader {
  66. public:
  67. png_reader_dynamic(FILE* file) : png_reader(file) {}
  68. png_reader_dynamic(const char* filename) : png_reader(filename){}
  69. template <typename Images>
  70. void read_image(any_image<Images>& im) {
  71. png_uint_32 width, height;
  72. int bit_depth, color_type, interlace_type;
  73. png_get_IHDR(_png_ptr, _info_ptr,
  74. &width, &height,&bit_depth,&color_type,&interlace_type,
  75. NULL, NULL);
  76. if (!construct_matched(im,png_type_format_checker(bit_depth,color_type))) {
  77. io_error("png_reader_dynamic::read_image(): no matching image type between those of the given any_image and that of the file");
  78. } else {
  79. im.recreate(width,height);
  80. dynamic_io_fnobj<png_read_is_supported, png_reader> op(this);
  81. apply_operation(view(im),op);
  82. }
  83. }
  84. };
  85. } // namespace detail
  86. /// \ingroup PNG_IO
  87. /// \brief reads a PNG image into a run-time instantiated image
  88. /// 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
  89. /// and creates a new image of that type with the dimensions specified by the image file.
  90. /// Throws std::ios_base::failure if none of the types in Images are compatible with the type on disk.
  91. template <typename Images>
  92. inline void png_read_image(const char* filename,any_image<Images>& im) {
  93. detail::png_reader_dynamic m(filename);
  94. m.read_image(im);
  95. }
  96. /// \ingroup PNG_IO
  97. /// \brief reads a PNG image into a run-time instantiated image
  98. template <typename Images>
  99. inline void png_read_image(const std::string& filename,any_image<Images>& im) {
  100. png_read_image(filename.c_str(),im);
  101. }
  102. /// \ingroup PNG_IO
  103. /// \brief Saves the currently instantiated view to a png file specified by the given png image file name.
  104. /// Throws std::ios_base::failure if the currently instantiated view type is not supported for writing by the I/O extension
  105. /// or if it fails to create the file.
  106. template <typename Views>
  107. inline void png_write_view(const char* filename,const any_image_view<Views>& runtime_view) {
  108. detail::png_writer_dynamic m(filename);
  109. m.write_view(runtime_view);
  110. }
  111. /// \ingroup PNG_IO
  112. /// \brief Saves the currently instantiated view to a png file specified by the given png image file name.
  113. template <typename Views>
  114. inline void png_write_view(const std::string& filename,const any_image_view<Views>& runtime_view) {
  115. png_write_view(filename.c_str(),runtime_view);
  116. }
  117. } } // namespace boost::gil
  118. #endif