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