read_image.hpp 9.9 KB

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