make_writer.hpp 4.5 KB

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