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