Boost GIL


make_dynamic_image_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_DYNAMIC_IMAGE_WRITER_HPP
9 #define BOOST_GIL_IO_MAKE_DYNAMIC_IMAGE_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_dynamic_image_writer(
21  String const& file_name, image_write_info<FormatTag> const& info,
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_writer<String, FormatTag>::type
31 {
32  using deveice_t = typename get_write_device<String, FormatTag>::type;
33  deveice_t device(
34  detail::convert_to_native_string(file_name),
35  typename detail::file_stream_device<FormatTag>::write_tag());
36 
37  return typename get_dynamic_image_writer<String, FormatTag>::type(device, info);
38 }
39 
40 template< typename FormatTag >
41 inline
42 typename get_dynamic_image_writer< std::wstring
43  , FormatTag
44  >::type
45 make_dynamic_image_writer( const std::wstring& file_name
46  , const image_write_info< FormatTag >& info
47  )
48 {
49  const char* str = detail::convert_to_native_string( file_name );
50 
51  typename get_write_device< std::wstring
52  , FormatTag
53  >::type device( str
54  , typename detail::file_stream_device< FormatTag >::write_tag()
55  );
56 
57  delete[] str; // TODO: RAII
58 
59  return typename get_dynamic_image_writer< std::wstring
60  , FormatTag
61  >::type( device
62  , info
63  );
64 }
65 
66 #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
67 template< typename FormatTag >
68 inline
69 typename get_dynamic_image_writer< std::wstring
70  , FormatTag
71  >::type
72 make_dynamic_image_writer( const filesystem::path& path
73  , const image_write_info< FormatTag >& info
74  )
75 {
76  return make_dynamic_image_writer( path.wstring()
77  , info
78  );
79 }
80 #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
81 
82 template <typename Device, typename FormatTag>
83 inline
84 auto make_dynamic_image_writer(Device& file, image_write_info<FormatTag> const& info,
85  typename std::enable_if
86  <
87  mp11::mp_and
88  <
89  typename detail::is_adaptable_output_device<FormatTag, Device>::type,
90  is_format_tag<FormatTag>
91  >::value
92  >::type* /*dummy*/ = nullptr)
93  -> typename get_dynamic_image_writer<Device, FormatTag>::type
94 {
95  typename get_write_device<Device, FormatTag>::type device(file);
96  return typename get_dynamic_image_writer<Device, FormatTag>::type(device, info);
97 }
98 
99 // no image_write_info
100 
101 template <typename String, typename FormatTag>
102 inline
103 auto make_dynamic_image_writer(String const& file_name, FormatTag const&,
104  typename std::enable_if
105  <
106  mp11::mp_and
107  <
108  detail::is_supported_path_spec<String>,
109  is_format_tag<FormatTag>
110  >::value
111  >::type* /*dummy*/ = nullptr)
112  -> typename get_dynamic_image_writer<String, FormatTag>::type
113 {
114  return make_dynamic_image_writer(file_name, image_write_info<FormatTag>());
115 }
116 
117 template< typename FormatTag >
118 inline
119 typename get_dynamic_image_writer< std::wstring
120  , FormatTag
121  >::type
122 make_dynamic_image_writer( const std::wstring& file_name
123  , const FormatTag&
124  )
125 {
126  return make_dynamic_image_writer( file_name
127  , image_write_info< FormatTag >()
128  );
129 }
130 
131 #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
132 template< typename FormatTag >
133 inline
134 typename get_dynamic_image_writer< std::wstring
135  , FormatTag
136  >::type
137 make_dynamic_image_writer( const filesystem::path& path
138  , const FormatTag&
139  )
140 {
141  return make_dynamic_image_writer( path.wstring()
142  , image_write_info< FormatTag >()
143  );
144 }
145 #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
146 
147 
148 template <typename Device, typename FormatTag>
149 inline
150 auto make_dynamic_image_writer(Device& file, FormatTag const&,
151  typename std::enable_if
152  <
153  mp11::mp_and
154  <
155  typename detail::is_adaptable_output_device<FormatTag, Device>::type,
156  is_format_tag<FormatTag>
157  >::value
158  >::type* /*dummy*/ = nullptr)
159  -> typename get_dynamic_image_writer<Device, FormatTag>::type
160 {
161  return make_dynamic_image_writer(file, image_write_info<FormatTag>());
162 }
163 
164 }} // namespace boost::gil
165 
166 #endif