make_backend.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. //
  3. // Copyright 2012 Christian Henning
  4. //
  5. // Distributed under the Boost Software License, Version 1.0
  6. // See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt
  8. //
  9. #ifndef BOOST_GIL_IO_MAKE_BACKEND_HPP
  10. #define BOOST_GIL_IO_MAKE_BACKEND_HPP
  11. #include <boost/gil/detail/mp11.hpp>
  12. #include <boost/gil/io/get_reader.hpp>
  13. #include <type_traits>
  14. namespace boost { namespace gil {
  15. template <typename String, typename FormatTag>
  16. inline
  17. auto make_reader_backend(
  18. String const& file_name, image_read_settings<FormatTag> const& settings,
  19. typename std::enable_if
  20. <
  21. mp11::mp_and
  22. <
  23. detail::is_supported_path_spec<String>,
  24. is_format_tag<FormatTag>
  25. >::value
  26. >::type* /*dummy*/ = nullptr)
  27. -> typename get_reader_backend<String, FormatTag>::type
  28. {
  29. using device_t = typename get_read_device<String, FormatTag>::type;
  30. device_t device(
  31. detail::convert_to_native_string(file_name),
  32. typename detail::file_stream_device<FormatTag>::read_tag());
  33. return reader_backend<device_t, FormatTag>(device, settings);
  34. }
  35. template <typename FormatTag>
  36. inline
  37. auto make_reader_backend(
  38. std::wstring const& file_name, image_read_settings<FormatTag> const& settings)
  39. -> typename get_reader_backend<std::wstring, FormatTag>::type
  40. {
  41. char const* str = detail::convert_to_native_string(file_name);
  42. using device_t = typename get_read_device<std::wstring, FormatTag>::type;
  43. device_t device(str, typename detail::file_stream_device<FormatTag>::read_tag());
  44. delete[] str; // TODO: RAII
  45. return reader_backend<device_t, FormatTag>(device, settings);
  46. }
  47. #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
  48. template <typename FormatTag>
  49. inline
  50. auto make_reader_backend(
  51. filesystem::path const& path,
  52. image_read_settings<FormatTag> const& settings)
  53. -> typename get_reader_backend<std::wstring, FormatTag>::type
  54. {
  55. return make_reader_backend(path.wstring(), settings);
  56. }
  57. #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
  58. template <typename Device, typename FormatTag>
  59. inline
  60. auto make_reader_backend(Device& io_dev, image_read_settings<FormatTag> const& settings,
  61. typename std::enable_if
  62. <
  63. mp11::mp_and
  64. <
  65. detail::is_adaptable_input_device<FormatTag, Device>,
  66. is_format_tag<FormatTag>
  67. >::value
  68. >::type* /*dummy*/ = nullptr)
  69. -> typename get_reader_backend<Device, FormatTag>::type
  70. {
  71. using device_t = typename get_read_device<Device, FormatTag>::type;
  72. device_t device(io_dev);
  73. return reader_backend<device_t, FormatTag>(device, settings);
  74. }
  75. template <typename String, typename FormatTag>
  76. inline
  77. auto make_reader_backend(String const& file_name, FormatTag const&,
  78. typename std::enable_if
  79. <
  80. mp11::mp_and
  81. <
  82. detail::is_supported_path_spec<String>,
  83. is_format_tag<FormatTag>
  84. >::value
  85. >::type* /*dummy*/ = nullptr)
  86. -> typename get_reader_backend<String, FormatTag>::type
  87. {
  88. return make_reader_backend(file_name, image_read_settings<FormatTag>());
  89. }
  90. template <typename Device, typename FormatTag>
  91. inline
  92. auto make_reader_backend(Device& io_dev, FormatTag const&,
  93. typename std::enable_if
  94. <
  95. mp11::mp_and
  96. <
  97. detail::is_adaptable_input_device<FormatTag, Device>,
  98. is_format_tag<FormatTag>
  99. >::value
  100. >::type* /*dummy*/ = nullptr)
  101. -> typename get_reader_backend<Device, FormatTag>::type
  102. {
  103. return make_reader_backend(io_dev, image_read_settings<FormatTag>());
  104. }
  105. }} // namespace boost::gil
  106. #endif