make_writer.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. auto make_writer(std::wstring const& file_name, image_write_info<FormatTag> const& info)
  34. -> typename get_writer<std::wstring, FormatTag>::type
  35. {
  36. const char* str = detail::convert_to_native_string( file_name );
  37. typename get_write_device< std::wstring
  38. , FormatTag
  39. >::type device( str
  40. , typename detail::file_stream_device< FormatTag >::write_tag()
  41. );
  42. delete[] str;
  43. return typename get_writer< std::wstring
  44. , FormatTag
  45. >::type( device
  46. , info
  47. );
  48. }
  49. template <typename FormatTag>
  50. inline
  51. auto make_writer(detail::filesystem::path const& path, image_write_info<FormatTag> const& info)
  52. -> typename get_writer<std::wstring, FormatTag>::type
  53. {
  54. return make_writer(path.wstring(), info);
  55. }
  56. template <typename Device, typename FormatTag>
  57. inline
  58. auto make_writer(Device& file, image_write_info<FormatTag> const& info,
  59. typename std::enable_if
  60. <
  61. mp11::mp_and
  62. <
  63. typename detail::is_adaptable_output_device<FormatTag, Device>::type,
  64. is_format_tag<FormatTag>
  65. >::value
  66. >::type* /*dummy*/ = nullptr)
  67. -> typename get_writer<Device, FormatTag>::type
  68. {
  69. typename get_write_device<Device, FormatTag>::type device(file);
  70. return typename get_writer<Device, FormatTag>::type(device, info);
  71. }
  72. // no image_write_info
  73. template <typename String, typename FormatTag>
  74. inline
  75. auto make_writer(String const& file_name, FormatTag const&,
  76. typename std::enable_if
  77. <
  78. mp11::mp_and
  79. <
  80. detail::is_supported_path_spec<String>,
  81. is_format_tag<FormatTag>
  82. >::value
  83. >::type* /*dummy*/ = nullptr)
  84. -> typename get_writer<String, FormatTag>::type
  85. {
  86. return make_writer(file_name, image_write_info<FormatTag>());
  87. }
  88. template <typename FormatTag>
  89. inline
  90. auto make_writer(std::wstring const &file_name, FormatTag const&)
  91. -> typename get_writer<std::wstring, FormatTag>::type
  92. {
  93. return make_writer( file_name
  94. , image_write_info< FormatTag >()
  95. );
  96. }
  97. template <typename FormatTag>
  98. inline
  99. auto make_writer(detail::filesystem::path const& path, FormatTag const& tag)
  100. -> typename get_writer<std::wstring, FormatTag>::type
  101. {
  102. return make_writer(path.wstring(), tag);
  103. }
  104. template <typename Device, typename FormatTag>
  105. inline
  106. auto make_writer(Device& file, FormatTag const&,
  107. typename std::enable_if
  108. <
  109. mp11::mp_and
  110. <
  111. typename detail::is_adaptable_output_device<FormatTag, Device>::type,
  112. is_format_tag<FormatTag>
  113. >::value
  114. >::type* /*dummy*/ = nullptr)
  115. -> typename get_writer<Device, FormatTag>::type
  116. {
  117. return make_writer(file, image_write_info<FormatTag>());
  118. }
  119. }} // namespace boost::gil
  120. #endif