make_dynamic_image_writer.hpp 5.2 KB

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