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 #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
64 template< typename FormatTag >
65 inline
66 typename get_writer< std::wstring
67  , FormatTag
68  >::type
69 make_writer( const filesystem::path& path
70  , const image_write_info< FormatTag >& info
71  )
72 {
73  return make_writer( path.wstring()
74  , info
75  );
76 }
77 #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
78 
79 template <typename Device, typename FormatTag>
80 inline
81 auto make_writer(Device& file, image_write_info<FormatTag> const& info,
82  typename std::enable_if
83  <
84  mp11::mp_and
85  <
86  typename detail::is_adaptable_output_device<FormatTag, Device>::type,
87  is_format_tag<FormatTag>
88  >::value
89  >::type* /*dummy*/ = nullptr)
90  -> typename get_writer<Device, FormatTag>::type
91 {
92  typename get_write_device<Device, FormatTag>::type device(file);
93  return typename get_writer<Device, FormatTag>::type(device, info);
94 }
95 
96 // no image_write_info
97 
98 template <typename String, typename FormatTag>
99 inline
100 auto make_writer(String const& file_name, FormatTag const&,
101  typename std::enable_if
102  <
103  mp11::mp_and
104  <
105  detail::is_supported_path_spec<String>,
106  is_format_tag<FormatTag>
107  >::value
108  >::type* /*dummy*/ = nullptr)
109  -> typename get_writer<String, FormatTag>::type
110 {
111  return make_writer(file_name, image_write_info<FormatTag>());
112 }
113 
114 template< typename FormatTag >
115 inline
116 typename get_writer< std::wstring
117  , FormatTag
118  >::type
119 make_writer( const std::wstring& file_name
120  , const FormatTag&
121  )
122 {
123  return make_writer( file_name
124  , image_write_info< FormatTag >()
125  );
126 }
127 
128 #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
129 template< typename FormatTag >
130 inline
131 typename get_writer< std::wstring
132  , FormatTag
133  >::type
134 make_writer( const filesystem::path& path
135  , const FormatTag& tag
136  )
137 {
138  return make_writer( path.wstring()
139  , tag
140  );
141 }
142 #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
143 
144 template <typename Device, typename FormatTag>
145 inline
146 auto make_writer(Device& file, FormatTag const&,
147  typename std::enable_if
148  <
149  mp11::mp_and
150  <
151  typename detail::is_adaptable_output_device<FormatTag, Device>::type,
152  is_format_tag<FormatTag>
153  >::value
154  >::type* /*dummy*/ = nullptr)
155  -> typename get_writer<Device, FormatTag>::type
156 {
157  return make_writer(file, image_write_info<FormatTag>());
158 }
159 
160 }} // namespace boost::gil
161 
162 #endif