Boost GIL


make_dynamic_image_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_DYNAMIC_IMAGE_READER_HPP
9 #define BOOST_GIL_IO_MAKE_DYNAMIC_IMAGE_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>
19 inline
20 auto make_dynamic_image_reader(
21  String const& file_name, image_read_settings<FormatTag> const& settings,
22  typename std::enable_if
23  <
24  mp11::mp_and
25  <
26  detail::is_supported_path_spec<String>,
27  is_format_tag<FormatTag>
28  >::value
29  >::type* /*dummy*/ = nullptr)
30  -> typename get_dynamic_image_reader<String, FormatTag>::type
31 {
32  using device_t = typename get_read_device<String, FormatTag>::type;
33  device_t device(
34  detail::convert_to_native_string(file_name),
35  typename detail::file_stream_device<FormatTag>::read_tag());
36 
37  return typename get_dynamic_image_reader<String, FormatTag>::type(device, settings);
38 }
39 
40 template <typename FormatTag>
41 inline
42 auto make_dynamic_image_reader(
43  std::wstring const& file_name, image_read_settings<FormatTag> const& settings)
44  -> typename get_dynamic_image_reader<std::wstring, FormatTag>::type
45 {
46  char const* str = detail::convert_to_native_string(file_name);
47 
48  using device_t = typename get_read_device<std::wstring, FormatTag>::type;
49  device_t device(str, typename detail::file_stream_device<FormatTag>::read_tag());
50 
51  delete[] str; // TODO: RAII
52 
53  return
54  typename get_dynamic_image_reader<std::wstring, FormatTag>::type(device, settings);
55 }
56 
57 #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
58 template <typename FormatTag>
59 inline
60 auto make_dynamic_image_reader(
61  filesystem::path const& path, image_read_settings<FormatTag> const& settings)
62  -> typename get_dynamic_image_reader<std::wstring, FormatTag>::type
63 {
64  return make_dynamic_image_reader(path.wstring(), settings);
65 }
66 #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
67 
68 template <typename Device, typename FormatTag>
69 inline
70 auto make_dynamic_image_reader(
71  Device& file, image_read_settings<FormatTag> const& settings,
72  typename std::enable_if
73  <
74  mp11::mp_and
75  <
76  detail::is_adaptable_input_device<FormatTag, Device>,
77  is_format_tag<FormatTag>
78  >::value
79  >::type* /*dummy*/ = nullptr)
80  -> typename get_dynamic_image_reader<Device, FormatTag>::type
81 {
82  typename get_read_device<Device, FormatTag>::type device(file);
83  return typename get_dynamic_image_reader<Device, FormatTag>::type(device, settings);
84 }
85 
86 // without image_read_settings
87 
88 template <typename String, typename FormatTag>
89 inline
90 auto make_dynamic_image_reader(String const& file_name, FormatTag const&,
91  typename std::enable_if
92  <
93  mp11::mp_and
94  <
95  detail::is_supported_path_spec<String>,
96  is_format_tag<FormatTag>
97  >::value
98  >::type* /*dummy*/ = nullptr)
99  -> typename get_dynamic_image_reader<String, FormatTag>::type
100 {
101  return make_dynamic_image_reader(file_name, image_read_settings<FormatTag>());
102 }
103 
104 template <typename FormatTag>
105 inline
106 auto make_dynamic_image_reader(std::wstring const& file_name, FormatTag const&)
107  -> typename get_dynamic_image_reader<std::wstring, FormatTag>::type
108 {
109  return make_dynamic_image_reader(file_name, image_read_settings<FormatTag>());
110 }
111 
112 #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
113 template <typename FormatTag>
114 inline
115 auto make_dynamic_image_reader(filesystem::path const& path, FormatTag const&)
116  -> typename get_dynamic_image_reader<std::wstring, FormatTag>::type
117 {
118  return make_dynamic_image_reader(path.wstring(), image_read_settings<FormatTag>());
119 }
120 #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
121 
122 template <typename Device, typename FormatTag>
123 inline
124 auto make_dynamic_image_reader(Device& file, FormatTag const&,
125  typename std::enable_if
126  <
127  mp11::mp_and
128  <
129  detail::is_adaptable_input_device<FormatTag, Device>,
130  is_format_tag<FormatTag>
131  >::value
132  >::type* /*dummy*/ = nullptr)
133  -> typename get_dynamic_image_reader<Device, FormatTag>::type
134 {
135  return make_dynamic_image_reader(file, image_read_settings<FormatTag>());
136 }
137 
138 }} // namespace boost::gil
139 
140 #endif