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