1 //
2 // Copyright 2007-2012 Christian Henning, Andreas Pokorny, Lubomir Bourdev
3 //
4 // Distributed under the Boost Software License, Version 1.0
5 // See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt
7 //
8 #ifndef BOOST_GIL_IO_READ_AND_CONVERT_IMAGE_HPP
9 #define BOOST_GIL_IO_READ_AND_CONVERT_IMAGE_HPP
10 
11 #include <boost/gil/io/base.hpp>
12 #include <boost/gil/io/conversion_policies.hpp>
13 #include <boost/gil/io/device.hpp>
14 #include <boost/gil/io/get_reader.hpp>
15 #include <boost/gil/io/path_spec.hpp>
16 #include <boost/gil/detail/mp11.hpp>
17 
18 #include <type_traits>
19 
20 namespace boost{ namespace gil {
21 
22 /// \ingroup IO
23 
24 /// \brief Reads and color-converts an image. Image memory is allocated.
25 /// \param reader    An image reader.
26 /// \param img       The image in which the data is read into.
27 /// \param settings  Specifies read settings depending on the image format.
28 /// \param cc        Color converter function object.
29 /// \throw std::ios_base::failure
30 template <typename Reader, typename Image>
31 inline
read_and_convert_image(Reader & reader,Image & img,typename std::enable_if<mp11::mp_and<detail::is_reader<Reader>,is_format_tag<typename Reader::format_tag_t>>::value>::type * =nullptr)32 void read_and_convert_image(Reader& reader, Image& img,
33     typename std::enable_if
34     <
35         mp11::mp_and
36         <
37             detail::is_reader<Reader>,
38             is_format_tag<typename Reader::format_tag_t>
39         >::value
40     >::type* /*dummy*/ = nullptr)
41 {
42     reader.init_image(img, reader._settings);
43     reader.apply(view(img));
44 }
45 
46 /// \brief Reads and color-converts an image. Image memory is allocated.
47 /// \param device    Must satisfy is_input_device metafunction.
48 /// \param img       The image in which the data is read into.
49 /// \param settings  Specifies read settings depending on the image format.
50 /// \param cc        Color converter function object.
51 /// \throw std::ios_base::failure
52 template <typename Device, typename Image, typename ColorConverter, typename FormatTag>
53 inline
read_and_convert_image(Device & device,Image & img,image_read_settings<FormatTag> const & settings,ColorConverter const & cc,typename std::enable_if<mp11::mp_and<detail::is_read_device<FormatTag,Device>,is_format_tag<FormatTag>>::value>::type * =nullptr)54 void read_and_convert_image(
55     Device& device,
56     Image& img,
57     image_read_settings<FormatTag> const& settings,
58     ColorConverter const& cc,
59     typename std::enable_if
60     <
61         mp11::mp_and
62         <
63             detail::is_read_device<FormatTag, Device>,
64             is_format_tag<FormatTag>
65         >::value
66     >::type* /*dummy*/ = nullptr)
67 {
68     using read_and_convert_t = detail::read_and_convert<ColorConverter>;
69     using reader_t = typename get_reader<Device, FormatTag, read_and_convert_t>::type;
70 
71     reader_t reader = make_reader(device, settings, read_and_convert_t{cc});
72     read_and_convert_image(reader, img);
73 }
74 
75 /// \brief Reads and color-converts an image. Image memory is allocated.
76 /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
77 /// \param img       The image in which the data is read into.
78 /// \param settings  Specifies read settings depending on the image format.
79 /// \param cc        Color converter function object.
80 /// \throw std::ios_base::failure
81 template <typename String, typename Image, typename ColorConverter, typename FormatTag>
82 inline
read_and_convert_image(String const & file_name,Image & img,image_read_settings<FormatTag> const & settings,ColorConverter const & cc,typename std::enable_if<mp11::mp_and<is_format_tag<FormatTag>,detail::is_supported_path_spec<String>>::value>::type * =nullptr)83 void read_and_convert_image(
84     String const& file_name,
85     Image& img,
86     image_read_settings<FormatTag> const& settings,
87     ColorConverter const& cc,
88     typename std::enable_if
89     <
90         mp11::mp_and
91         <
92             is_format_tag<FormatTag>,
93             detail::is_supported_path_spec<String>
94         >::value
95     >::type* /*dummy*/ = nullptr)
96 {
97     using read_and_convert_t = detail::read_and_convert<ColorConverter>;
98     using reader_t = typename get_reader<String, FormatTag, read_and_convert_t>::type;
99 
100     reader_t reader = make_reader(file_name, settings, read_and_convert_t{cc});
101     read_and_convert_image(reader, img);
102 }
103 
104 /// \brief Reads and color-converts an image. Image memory is allocated.
105 /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
106 /// \param img       The image in which the data is read into.
107 /// \param cc        Color converter function object.
108 /// \param tag       Defines the image format. Must satisfy is_format_tag metafunction.
109 /// \throw std::ios_base::failure
110 template <typename String, typename Image, typename ColorConverter, typename FormatTag>
111 inline
read_and_convert_image(String const & file_name,Image & img,ColorConverter const & cc,FormatTag const & tag,typename std::enable_if<mp11::mp_and<is_format_tag<FormatTag>,detail::is_supported_path_spec<String>>::value>::type * =nullptr)112 void read_and_convert_image(
113     String const& file_name,
114     Image& img,
115     ColorConverter const& cc,
116     FormatTag const& tag,
117     typename std::enable_if
118     <
119         mp11::mp_and
120         <
121             is_format_tag<FormatTag>,
122             detail::is_supported_path_spec<String>
123         >::value
124     >::type* /*dummy*/ = nullptr)
125 {
126     using read_and_convert_t = detail::read_and_convert<ColorConverter>;
127     using reader_t = typename get_reader<String, FormatTag, read_and_convert_t>::type;
128 
129     reader_t reader = make_reader(file_name, tag, read_and_convert_t{cc});
130     read_and_convert_image(reader, img);
131 }
132 
133 /// \brief Reads and color-converts an image. Image memory is allocated.
134 /// \param device Must satisfy is_input_device metafunction or is_adaptable_input_device.
135 /// \param img    The image in which the data is read into.
136 /// \param cc     Color converter function object.
137 /// \param tag    Defines the image format. Must satisfy is_format_tag metafunction.
138 /// \throw std::ios_base::failure
139 template <typename Device, typename Image, typename ColorConverter, typename FormatTag>
140 inline
read_and_convert_image(Device & device,Image & img,ColorConverter const & cc,FormatTag const & tag,typename std::enable_if<mp11::mp_and<detail::is_read_device<FormatTag,Device>,is_format_tag<FormatTag>>::value>::type * =nullptr)141 void read_and_convert_image(
142     Device& device,
143     Image& img,
144     ColorConverter const& cc,
145     FormatTag const& tag,
146     typename std::enable_if
147     <
148         mp11::mp_and
149         <
150             detail::is_read_device<FormatTag, Device>,
151             is_format_tag<FormatTag>
152         >::value
153     >::type* /*dummy*/ = nullptr)
154 {
155     using read_and_convert_t = detail::read_and_convert<ColorConverter>;
156     using reader_t = typename get_reader<Device, FormatTag, read_and_convert_t>::type;
157 
158     reader_t reader = make_reader(device, tag, read_and_convert_t{cc});
159     read_and_convert_image(reader, img);
160 }
161 
162 /// \brief Reads and color-converts an image. Image memory is allocated. Default color converter is used.
163 /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
164 /// \param img       The image in which the data is read into.
165 /// \param settings  Specifies read settings depending on the image format.
166 /// \throw std::ios_base::failure
167 template <typename String, typename Image, typename FormatTag>
read_and_convert_image(String const & file_name,Image & img,image_read_settings<FormatTag> const & settings,typename std::enable_if<mp11::mp_and<is_format_tag<FormatTag>,detail::is_supported_path_spec<String>>::value>::type * =nullptr)168 inline void read_and_convert_image(
169     String const& file_name,
170     Image& img,
171     image_read_settings<FormatTag> const& settings,
172     typename std::enable_if
173     <
174         mp11::mp_and
175         <
176             is_format_tag<FormatTag>,
177             detail::is_supported_path_spec<String>
178         >::value
179     >::type* /*dummy*/ = nullptr)
180 {
181     using read_and_convert_t = detail::read_and_convert<default_color_converter>;
182     using reader_t = typename get_reader<String, FormatTag, read_and_convert_t>::type;
183 
184     reader_t reader = make_reader(file_name, settings, read_and_convert_t{});
185     read_and_convert_image(reader, img);
186 }
187 
188 /// \brief Reads and color-converts an image. Image memory is allocated. Default color converter is used.
189 /// \param device    It's a device. Must satisfy is_input_device metafunction or is_adaptable_input_device.
190 /// \param img       The image in which the data is read into.
191 /// \param settings  Specifies read settings depending on the image format.
192 /// \throw std::ios_base::failure
193 template <typename Device, typename Image, typename FormatTag>
read_and_convert_image(Device & device,Image & img,image_read_settings<FormatTag> const & settings,typename std::enable_if<mp11::mp_and<detail::is_read_device<FormatTag,Device>,is_format_tag<FormatTag>>::value>::type * =nullptr)194 inline void read_and_convert_image(
195     Device& device,
196     Image& img,
197     image_read_settings<FormatTag> const& settings,
198     typename std::enable_if
199     <
200         mp11::mp_and
201         <
202             detail::is_read_device<FormatTag, Device>,
203             is_format_tag<FormatTag>
204         >::value
205     >::type* /*dummy*/ = nullptr)
206 {
207     using read_and_convert_t = detail::read_and_convert<default_color_converter>;
208     using reader_t = typename get_reader<Device, FormatTag, read_and_convert_t>::type;
209 
210     reader_t reader = make_reader(device, settings, read_and_convert_t{});
211     read_and_convert_image(reader, img);
212 }
213 
214 /// \brief Reads and color-converts an image. Image memory is allocated. Default color converter is used.
215 /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
216 /// \param img       The image in which the data is read into.
217 /// \param tag       Defines the image format. Must satisfy is_format_tag metafunction.
218 /// \throw std::ios_base::failure
219 template <typename String, typename Image, typename FormatTag>
220 inline
read_and_convert_image(String const & file_name,Image & img,FormatTag const & tag,typename std::enable_if<mp11::mp_and<is_format_tag<FormatTag>,detail::is_supported_path_spec<String>>::value>::type * =nullptr)221 void read_and_convert_image(
222     String const& file_name,
223     Image& img,
224     FormatTag const& tag,
225     typename std::enable_if
226     <
227         mp11::mp_and
228         <
229             is_format_tag<FormatTag>,
230             detail::is_supported_path_spec<String>
231         >::value
232     >::type* /*dummy*/ = nullptr)
233 {
234     using read_and_convert_t = detail::read_and_convert<default_color_converter>;
235     using reader_t = typename get_reader<String, FormatTag, read_and_convert_t>::type;
236 
237     reader_t reader = make_reader(file_name, tag, read_and_convert_t{});
238     read_and_convert_image(reader, img);
239 }
240 
241 /// \brief Reads and color-converts an image. Image memory is allocated. Default color converter is used.
242 /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
243 /// \param img       The image in which the data is read into.
244 /// \param tag       Defines the image format. Must satisfy is_format_tag metafunction.
245 /// \throw std::ios_base::failure
246 template <typename Device, typename Image, typename FormatTag>
read_and_convert_image(Device & device,Image & img,FormatTag const & tag,typename std::enable_if<mp11::mp_and<detail::is_read_device<FormatTag,Device>,is_format_tag<FormatTag>>::value>::type * =nullptr)247 inline void read_and_convert_image(
248     Device& device,
249     Image& img,
250     FormatTag const& tag,
251     typename std::enable_if
252     <
253         mp11::mp_and
254         <
255             detail::is_read_device<FormatTag, Device>,
256             is_format_tag<FormatTag>
257         >::value
258     >::type* /*dummy*/ = nullptr)
259 {
260     using read_and_convert_t = detail::read_and_convert<default_color_converter>;
261     using reader_t = typename get_reader<Device, FormatTag, read_and_convert_t>::type;
262 
263     reader_t reader = make_reader(device, tag, read_and_convert_t{});
264     read_and_convert_image(reader, img);
265 }
266 
267 }} // namespace boost::gil
268 
269 #endif
270