read_and_convert_view.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. //
  2. // Copyright 2007-2012 Christian Henning, Andreas Pokorny, Lubomir Bourdev
  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_READ_AND_CONVERT_VIEW_HPP
  9. #define BOOST_GIL_IO_READ_AND_CONVERT_VIEW_HPP
  10. #include <boost/gil/io/base.hpp>
  11. #include <boost/gil/io/conversion_policies.hpp>
  12. #include <boost/gil/io/device.hpp>
  13. #include <boost/gil/io/get_reader.hpp>
  14. #include <boost/gil/io/path_spec.hpp>
  15. #include <boost/mpl/and.hpp>
  16. #include <boost/type_traits/is_base_and_derived.hpp>
  17. #include <type_traits>
  18. namespace boost{ namespace gil {
  19. /// \ingroup IO
  20. /// \brief Reads and color-converts an image view. No memory is allocated.
  21. /// \param reader An image reader.
  22. /// \param img The image in which the data is read into.
  23. /// \param settings Specifies read settings depending on the image format.
  24. /// \param cc Color converter function object.
  25. /// \throw std::ios_base::failure
  26. template <typename Reader, typename View>
  27. inline
  28. void read_and_convert_view(Reader& reader, View const& view,
  29. typename std::enable_if
  30. <
  31. mpl::and_
  32. <
  33. detail::is_reader<Reader>,
  34. is_format_tag<typename Reader::format_tag_t>
  35. >::value
  36. >::type* /*dummy*/ = nullptr)
  37. {
  38. reader.check_image_size(view.dimensions());
  39. reader.init_view(view, reader._settings);
  40. reader.apply(view);
  41. }
  42. /// \brief Reads and color-converts an image view. No memory is allocated.
  43. /// \param file It's a device. Must satisfy is_input_device metafunction.
  44. /// \param view The image view in which the data is read into.
  45. /// \param settings Specifies read settings depending on the image format.
  46. /// \param cc Color converter function object.
  47. /// \throw std::ios_base::failure
  48. template <typename Device, typename View, typename ColorConverter, typename FormatTag>
  49. inline
  50. void read_and_convert_view(
  51. Device& device,
  52. View const& view,
  53. image_read_settings<FormatTag> const& settings,
  54. ColorConverter const& cc,
  55. typename std::enable_if
  56. <
  57. mpl::and_
  58. <
  59. detail::is_read_device<FormatTag, Device>,
  60. is_format_tag<FormatTag>
  61. >::value
  62. >::type* /*dummy*/ = nullptr)
  63. {
  64. using read_and_convert_t = detail::read_and_convert<ColorConverter>;
  65. using reader_t = typename get_reader<Device, FormatTag, read_and_convert_t>::type;
  66. reader_t reader = make_reader(device, settings, read_and_convert_t{cc});
  67. read_and_convert_view(reader, view);
  68. }
  69. /// \brief Reads and color-converts an image view. No memory is allocated.
  70. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  71. /// \param view The image view in which the data is read into.
  72. /// \param settings Specifies read settings depending on the image format.
  73. /// \param cc Color converter function object.
  74. /// \throw std::ios_base::failure
  75. template <typename String, typename View, typename ColorConverter, typename FormatTag>
  76. inline
  77. void read_and_convert_view(
  78. String const& file_name,
  79. View const& view,
  80. image_read_settings<FormatTag> const& settings,
  81. ColorConverter const& cc,
  82. typename std::enable_if
  83. <
  84. mpl::and_
  85. <
  86. is_format_tag<FormatTag>,
  87. detail::is_supported_path_spec<String>
  88. >::value
  89. >::type* /*dummy*/ = nullptr)
  90. {
  91. using read_and_convert_t = detail::read_and_convert<ColorConverter>;
  92. using reader_t = typename get_reader<String, FormatTag, read_and_convert_t>::type;
  93. reader_t reader = make_reader(file_name, settings, read_and_convert_t{cc});
  94. read_and_convert_view(reader, view);
  95. }
  96. /// \brief Reads and color-converts an image view. No memory is allocated.
  97. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  98. /// \param view The image view in which the data is read into.
  99. /// \param cc Color converter function object.
  100. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  101. /// \throw std::ios_base::failure
  102. template <typename String, typename View, typename ColorConverter, typename FormatTag>
  103. inline
  104. void read_and_convert_view(
  105. String const& file_name,
  106. View const& view,
  107. ColorConverter const& cc,
  108. FormatTag const& tag,
  109. typename std::enable_if
  110. <
  111. mpl::and_
  112. <
  113. is_format_tag<FormatTag>,
  114. detail::is_supported_path_spec<String>
  115. >::value
  116. >::type* /*dummy*/ = nullptr)
  117. {
  118. using read_and_convert_t = detail::read_and_convert<ColorConverter>;
  119. using reader_t = typename get_reader<String, FormatTag, read_and_convert_t>::type;
  120. reader_t reader = make_reader(file_name, tag, read_and_convert_t{cc});
  121. read_and_convert_view(reader, view);
  122. }
  123. /// \brief Reads and color-converts an image view. No memory is allocated.
  124. /// \param file It's a device. Must satisfy is_input_device metafunction or is_adaptable_input_device.
  125. /// \param view The image view in which the data is read into.
  126. /// \param cc Color converter function object.
  127. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  128. /// \throw std::ios_base::failure
  129. template <typename Device, typename View, typename ColorConverter, typename FormatTag>
  130. inline
  131. void read_and_convert_view(
  132. Device& device,
  133. View const& view,
  134. ColorConverter const& cc,
  135. FormatTag const& tag,
  136. typename std::enable_if
  137. <
  138. mpl::and_
  139. <
  140. detail::is_read_device<FormatTag, Device>,
  141. is_format_tag<FormatTag>
  142. >::value
  143. >::type* /*dummy*/ = nullptr)
  144. {
  145. using read_and_convert_t = detail::read_and_convert<ColorConverter>;
  146. using reader_t = typename get_reader<Device, FormatTag, read_and_convert_t>::type;
  147. reader_t reader = make_reader(device, tag, read_and_convert_t{cc});
  148. read_and_convert_view(reader, view);
  149. }
  150. /// \brief Reads and color-converts an image view. No memory is allocated.
  151. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  152. /// \param view The image view in which the data is read into.
  153. /// \param settings Specifies read settings depending on the image format.
  154. /// \throw std::ios_base::failure
  155. template <typename String, typename View, typename FormatTag>
  156. inline
  157. void read_and_convert_view(
  158. String const& file_name,
  159. View const& view,
  160. image_read_settings<FormatTag> const& settings,
  161. typename std::enable_if
  162. <
  163. mpl::and_
  164. <
  165. is_format_tag<FormatTag>,
  166. detail::is_supported_path_spec<String>
  167. >::value
  168. >::type* /*dummy*/ = nullptr)
  169. {
  170. using read_and_convert_t = detail::read_and_convert<default_color_converter>;
  171. using reader_t = typename get_reader<String, FormatTag, read_and_convert_t>::type;
  172. reader_t reader = make_reader(file_name, settings, read_and_convert_t{});
  173. read_and_convert_view(reader, view);
  174. }
  175. /// \brief Reads and color-converts an image view. No memory is allocated.
  176. /// \param file It's a device. Must satisfy is_input_device metafunction or is_adaptable_input_device.
  177. /// \param view The image view in which the data is read into.
  178. /// \param settings Specifies read settings depending on the image format.
  179. /// \throw std::ios_base::failure
  180. template <typename Device, typename View, typename FormatTag>
  181. inline
  182. void read_and_convert_view(
  183. Device& device,
  184. View const& view,
  185. image_read_settings<FormatTag> const& settings,
  186. typename std::enable_if
  187. <
  188. mpl::and_
  189. <
  190. detail::is_read_device<FormatTag, Device>,
  191. is_format_tag<FormatTag>
  192. >::value
  193. >::type* /*dummy*/ = nullptr)
  194. {
  195. using read_and_convert_t = detail::read_and_convert<default_color_converter>;
  196. using reader_t = typename get_reader<Device, FormatTag, read_and_convert_t>::type;
  197. reader_t reader = make_reader(device, settings, read_and_convert_t{});
  198. read_and_convert_view(reader, view);
  199. }
  200. /// \brief Reads and color-converts an image view. No memory is allocated.
  201. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  202. /// \param view The image view in which the data is read into.
  203. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  204. /// \throw std::ios_base::failure
  205. template <typename String, typename View, typename FormatTag>
  206. inline
  207. void read_and_convert_view(
  208. String const& file_name,
  209. View const& view,
  210. FormatTag const& tag,
  211. typename std::enable_if
  212. <
  213. mpl::and_
  214. <
  215. is_format_tag<FormatTag>,
  216. detail::is_supported_path_spec<String>
  217. >::value
  218. >::type* /*dummy*/ = nullptr)
  219. {
  220. using read_and_convert_t = detail::read_and_convert<default_color_converter>;
  221. using reader_t = typename get_reader<String, FormatTag, read_and_convert_t>::type;
  222. reader_t reader = make_reader(file_name, tag, read_and_convert_t{});
  223. read_and_convert_view(reader, view);
  224. }
  225. /// \brief Reads and color-converts an image view. No memory is allocated.
  226. /// \param file It's a device. Must satisfy is_input_device metafunction or is_adaptable_input_device.
  227. /// \param view The image view in which the data is read into.
  228. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  229. /// \throw std::ios_base::failure
  230. template <typename Device, typename View, typename FormatTag>
  231. inline
  232. void read_and_convert_view(
  233. Device& device,
  234. View const& view,
  235. FormatTag const& tag,
  236. typename std::enable_if
  237. <
  238. mpl::and_
  239. <
  240. detail::is_read_device<FormatTag, Device>,
  241. is_format_tag<FormatTag>
  242. >::value
  243. >::type* /*dummy*/ = nullptr)
  244. {
  245. using read_and_convert_t = detail::read_and_convert<default_color_converter>;
  246. using reader_t = typename get_reader<Device, FormatTag, read_and_convert_t>::type;
  247. reader_t reader = make_reader(device, tag, read_and_convert_t{});
  248. read_and_convert_view(reader, view);
  249. }
  250. }} // namespace boost::gill
  251. #endif