read_and_convert_image.hpp 9.8 KB

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