make_reader.hpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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
  37. , typename ConversionPolicy
  38. >
  39. inline
  40. typename get_reader< std::wstring
  41. , FormatTag
  42. , ConversionPolicy
  43. >::type
  44. make_reader( const std::wstring& file_name
  45. , const image_read_settings< FormatTag >& settings
  46. , const ConversionPolicy&
  47. )
  48. {
  49. const char* str = detail::convert_to_native_string( file_name );
  50. typename get_read_device< std::wstring
  51. , FormatTag
  52. >::type device( str
  53. , typename detail::file_stream_device< FormatTag >::read_tag()
  54. );
  55. delete[] str; // TODO: RAII
  56. return typename get_reader< std::wstring
  57. , FormatTag
  58. , ConversionPolicy
  59. >::type( device
  60. , settings
  61. );
  62. }
  63. #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
  64. template< typename FormatTag
  65. , typename ConversionPolicy
  66. >
  67. inline
  68. typename get_reader< std::wstring
  69. , FormatTag
  70. , ConversionPolicy
  71. >::type
  72. make_reader( const filesystem::path& path
  73. , const image_read_settings< FormatTag >& settings
  74. , const ConversionPolicy& cc
  75. )
  76. {
  77. return make_reader( path.wstring()
  78. , settings
  79. , cc
  80. );
  81. }
  82. #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
  83. template <typename Device, typename FormatTag, typename ConversionPolicy>
  84. inline
  85. auto make_reader(
  86. Device& file,
  87. image_read_settings<FormatTag> const& settings,
  88. ConversionPolicy const&,
  89. typename std::enable_if
  90. <
  91. mp11::mp_and
  92. <
  93. detail::is_adaptable_input_device<FormatTag, Device>,
  94. is_format_tag<FormatTag>
  95. >::value
  96. >::type* /*dummy*/ = nullptr)
  97. -> typename get_reader<Device, FormatTag, ConversionPolicy>::type
  98. {
  99. typename get_read_device<Device, FormatTag>::type device(file);
  100. return
  101. typename get_reader<Device, FormatTag, ConversionPolicy>::type(device, settings);
  102. }
  103. // no image_read_settings
  104. template <typename String, typename FormatTag, typename ConversionPolicy>
  105. inline
  106. auto make_reader(
  107. String const&file_name,
  108. FormatTag const&,
  109. ConversionPolicy const& cc,
  110. typename std::enable_if
  111. <
  112. mp11::mp_and
  113. <
  114. detail::is_supported_path_spec<String>,
  115. is_format_tag<FormatTag>
  116. >::value
  117. >::type* /*dummy*/ = nullptr)
  118. -> typename get_reader<String, FormatTag, ConversionPolicy>::type
  119. {
  120. return make_reader(file_name, image_read_settings<FormatTag>(), cc);
  121. }
  122. template< typename FormatTag
  123. , typename ConversionPolicy
  124. >
  125. inline
  126. typename get_reader< std::wstring
  127. , FormatTag
  128. , ConversionPolicy
  129. >::type
  130. make_reader( const std::wstring& file_name
  131. , const FormatTag&
  132. , const ConversionPolicy& cc
  133. )
  134. {
  135. return make_reader( file_name
  136. , image_read_settings< FormatTag >()
  137. , cc
  138. );
  139. }
  140. #ifdef BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
  141. template< typename FormatTag
  142. , typename ConversionPolicy
  143. >
  144. inline
  145. typename get_reader< std::wstring
  146. , FormatTag
  147. , ConversionPolicy
  148. >::type
  149. make_reader( const filesystem::path& path
  150. , const FormatTag&
  151. , const ConversionPolicy& cc
  152. )
  153. {
  154. return make_reader( path.wstring()
  155. , image_read_settings< FormatTag >()
  156. , cc
  157. );
  158. }
  159. #endif // BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
  160. template <typename Device, typename FormatTag, typename ConversionPolicy>
  161. inline
  162. auto make_reader(
  163. Device& file,
  164. FormatTag const&,
  165. ConversionPolicy const& cc,
  166. typename std::enable_if
  167. <
  168. mp11::mp_and
  169. <
  170. detail::is_adaptable_input_device<FormatTag, Device>,
  171. is_format_tag<FormatTag>
  172. >::value
  173. >::type* /*dummy*/ = nullptr)
  174. -> typename get_reader<Device, FormatTag, ConversionPolicy>::type
  175. {
  176. return make_reader(file, image_read_settings<FormatTag>(), cc);
  177. }
  178. }} // namespace boost::gil
  179. #endif