read_image.hpp 9.8 KB

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