Boost GIL


make_reader.hpp
1 //
2 // Copyright 2012 Christian Henning
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_MAKE_READER_HPP
9 #define BOOST_GIL_IO_MAKE_READER_HPP
10 
11 #include <boost/gil/detail/mp11.hpp>
12 #include <boost/gil/io/get_reader.hpp>
13 
14 #include <type_traits>
15 
16 namespace boost { namespace gil {
17 
18 template <typename String, typename FormatTag, typename ConversionPolicy>
19 inline
20 auto make_reader(
21  String const&file_name,
22  image_read_settings<FormatTag> const& settings,
23  ConversionPolicy const&,
24  typename std::enable_if
25  <
26  mp11::mp_and
27  <
28  detail::is_supported_path_spec<String>,
29  is_format_tag<FormatTag>
30  >::value
31  >::type* /*dummy*/ = nullptr)
32  -> typename get_reader<String, FormatTag, ConversionPolicy>::type
33 {
34  typename get_read_device<String, FormatTag>::type device(
35  detail::convert_to_native_string(file_name),
36  typename detail::file_stream_device<FormatTag>::read_tag());
37 
38  return
39  typename get_reader<String, FormatTag, ConversionPolicy>::type(device, settings);
40 }
41 
42 template< typename FormatTag
43  , typename ConversionPolicy
44  >
45 inline
46 typename get_reader< std::wstring
47  , FormatTag
48  , ConversionPolicy
49  >::type
50 make_reader( const std::wstring& file_name
51  , const image_read_settings< FormatTag >& settings
52  , const ConversionPolicy&
53  )
54 {
55  const char* str = detail::convert_to_native_string( file_name );
56 
57  typename get_read_device< std::wstring
58  , FormatTag
59  >::type device( str
60  , typename detail::file_stream_device< FormatTag >::read_tag()
61  );
62 
63  delete[] str; // TODO: RAII
64 
65  return typename get_reader< std::wstring
66  , FormatTag
67  , ConversionPolicy
68  >::type( device
69  , settings
70  );
71 }
72 
73 #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
74 template< typename FormatTag
75  , typename ConversionPolicy
76  >
77 inline
78 typename get_reader< std::wstring
79  , FormatTag
80  , ConversionPolicy
81  >::type
82 make_reader( const filesystem::path& path
83  , const image_read_settings< FormatTag >& settings
84  , const ConversionPolicy& cc
85  )
86 {
87  return make_reader( path.wstring()
88  , settings
89  , cc
90  );
91 }
92 #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
93 
94 template <typename Device, typename FormatTag, typename ConversionPolicy>
95 inline
96 auto make_reader(
97  Device& file,
98  image_read_settings<FormatTag> const& settings,
99  ConversionPolicy const&,
100  typename std::enable_if
101  <
102  mp11::mp_and
103  <
104  detail::is_adaptable_input_device<FormatTag, Device>,
105  is_format_tag<FormatTag>
106  >::value
107  >::type* /*dummy*/ = nullptr)
108  -> typename get_reader<Device, FormatTag, ConversionPolicy>::type
109 {
110  typename get_read_device<Device, FormatTag>::type device(file);
111 
112  return
113  typename get_reader<Device, FormatTag, ConversionPolicy>::type(device, settings);
114 }
115 
116 // no image_read_settings
117 
118 template <typename String, typename FormatTag, typename ConversionPolicy>
119 inline
120 auto make_reader(
121  String const&file_name,
122  FormatTag const&,
123  ConversionPolicy const& cc,
124  typename std::enable_if
125  <
126  mp11::mp_and
127  <
128  detail::is_supported_path_spec<String>,
129  is_format_tag<FormatTag>
130  >::value
131  >::type* /*dummy*/ = nullptr)
132  -> typename get_reader<String, FormatTag, ConversionPolicy>::type
133 {
134  return make_reader(file_name, image_read_settings<FormatTag>(), cc);
135 }
136 
137 template< typename FormatTag
138  , typename ConversionPolicy
139  >
140 inline
141 typename get_reader< std::wstring
142  , FormatTag
143  , ConversionPolicy
144  >::type
145 make_reader( const std::wstring& file_name
146  , const FormatTag&
147  , const ConversionPolicy& cc
148  )
149 {
150  return make_reader( file_name
151  , image_read_settings< FormatTag >()
152  , cc
153  );
154 }
155 
156 #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
157 template< typename FormatTag
158  , typename ConversionPolicy
159  >
160 inline
161 typename get_reader< std::wstring
162  , FormatTag
163  , ConversionPolicy
164  >::type
165 make_reader( const filesystem::path& path
166  , const FormatTag&
167  , const ConversionPolicy& cc
168  )
169 {
170  return make_reader( path.wstring()
171  , image_read_settings< FormatTag >()
172  , cc
173  );
174 }
175 #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
176 
177 template <typename Device, typename FormatTag, typename ConversionPolicy>
178 inline
179 auto make_reader(
180  Device& file,
181  FormatTag const&,
182  ConversionPolicy const& cc,
183  typename std::enable_if
184  <
185  mp11::mp_and
186  <
187  detail::is_adaptable_input_device<FormatTag, Device>,
188  is_format_tag<FormatTag>
189  >::value
190  >::type* /*dummy*/ = nullptr)
191  -> typename get_reader<Device, FormatTag, ConversionPolicy>::type
192 {
193  return make_reader(file, image_read_settings<FormatTag>(), cc);
194 }
195 
196 }} // namespace boost::gil
197 
198 #endif