make_reader.hpp 4.8 KB

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