Boost GIL


make_writer.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_WRITER_HPP
9 #define BOOST_GIL_IO_MAKE_WRITER_HPP
10 
11 #include <boost/gil/detail/mp11.hpp>
12 #include <boost/gil/io/get_writer.hpp>
13 
14 #include <type_traits>
15 
16 namespace boost { namespace gil {
17 
18 template <typename String, typename FormatTag>
19 inline
20 auto make_writer(String const& file_name, image_write_info<FormatTag> const& info,
21  typename std::enable_if
22  <
23  mp11::mp_and
24  <
25  detail::is_supported_path_spec<String>,
26  is_format_tag<FormatTag>
27  >::value>::type* /*dummy*/ = nullptr)
28  -> typename get_writer<String, FormatTag>::type
29 {
30  typename get_write_device<String, FormatTag>::type device(
31  detail::convert_to_native_string(file_name),
32  typename detail::file_stream_device<FormatTag>::write_tag());
33 
34  return typename get_writer<String, FormatTag>::type(device, info);
35 }
36 
37 template< typename FormatTag >
38 inline
39 typename get_writer< std::wstring
40  , FormatTag
41  >::type
42 make_writer( const std::wstring& file_name
43  , const image_write_info< FormatTag >& info
44  )
45 {
46  const char* str = detail::convert_to_native_string( file_name );
47 
48  typename get_write_device< std::wstring
49  , FormatTag
50  >::type device( str
51  , typename detail::file_stream_device< FormatTag >::write_tag()
52  );
53 
54  delete[] str;
55 
56  return typename get_writer< std::wstring
57  , FormatTag
58  >::type( device
59  , info
60  );
61 }
62 
63 template< typename FormatTag >
64 inline
65 typename get_writer< std::wstring
66  , FormatTag
67  >::type
68 make_writer( detail::filesystem::path const& path
69  , const image_write_info< FormatTag >& info
70  )
71 {
72  return make_writer( path.wstring()
73  , info
74  );
75 }
76 
77 template <typename Device, typename FormatTag>
78 inline
79 auto make_writer(Device& file, image_write_info<FormatTag> const& info,
80  typename std::enable_if
81  <
82  mp11::mp_and
83  <
84  typename detail::is_adaptable_output_device<FormatTag, Device>::type,
85  is_format_tag<FormatTag>
86  >::value
87  >::type* /*dummy*/ = nullptr)
88  -> typename get_writer<Device, FormatTag>::type
89 {
90  typename get_write_device<Device, FormatTag>::type device(file);
91  return typename get_writer<Device, FormatTag>::type(device, info);
92 }
93 
94 // no image_write_info
95 
96 template <typename String, typename FormatTag>
97 inline
98 auto make_writer(String const& file_name, FormatTag const&,
99  typename std::enable_if
100  <
101  mp11::mp_and
102  <
103  detail::is_supported_path_spec<String>,
104  is_format_tag<FormatTag>
105  >::value
106  >::type* /*dummy*/ = nullptr)
107  -> typename get_writer<String, FormatTag>::type
108 {
109  return make_writer(file_name, image_write_info<FormatTag>());
110 }
111 
112 template< typename FormatTag >
113 inline
114 typename get_writer< std::wstring
115  , FormatTag
116  >::type
117 make_writer( const std::wstring& file_name
118  , const FormatTag&
119  )
120 {
121  return make_writer( file_name
122  , image_write_info< FormatTag >()
123  );
124 }
125 
126 template< typename FormatTag >
127 inline
128 typename get_writer< std::wstring
129  , FormatTag
130  >::type
131 make_writer( detail::filesystem::path const& path
132  , const FormatTag& tag
133  )
134 {
135  return make_writer( path.wstring()
136  , tag
137  );
138 }
139 
140 template <typename Device, typename FormatTag>
141 inline
142 auto make_writer(Device& file, FormatTag const&,
143  typename std::enable_if
144  <
145  mp11::mp_and
146  <
147  typename detail::is_adaptable_output_device<FormatTag, Device>::type,
148  is_format_tag<FormatTag>
149  >::value
150  >::type* /*dummy*/ = nullptr)
151  -> typename get_writer<Device, FormatTag>::type
152 {
153  return make_writer(file, image_write_info<FormatTag>());
154 }
155 
156 }} // namespace boost::gil
157 
158 #endif