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