read_and_convert_image.hpp 9.8 KB

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