make_dynamic_image_writer.hpp 4.5 KB

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