read_image.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. Copyright 2007-2012 Christian Henning, Andreas Pokorny, Lubomir Bourdev
  3. Use, modification and distribution are subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt).
  6. */
  7. /*************************************************************************************************/
  8. #ifndef BOOST_GIL_IO_READ_IMAGE_HPP
  9. #define BOOST_GIL_IO_READ_IMAGE_HPP
  10. ////////////////////////////////////////////////////////////////////////////////////////
  11. /// \file
  12. /// \brief
  13. /// \author Christian Henning, Andreas Pokorny, Lubomir Bourdev \n
  14. ///
  15. /// \date 2007-2012 \n
  16. ///
  17. ////////////////////////////////////////////////////////////////////////////////////////
  18. #include <boost/type_traits/is_base_and_derived.hpp>
  19. #include <boost/algorithm/string.hpp>
  20. #include <boost/mpl/and.hpp>
  21. #include <boost/utility/enable_if.hpp>
  22. #include <boost/gil/extension/toolbox/dynamic_images.hpp>
  23. #include <boost/gil/io/base.hpp>
  24. #include <boost/gil/io/device.hpp>
  25. #include <boost/gil/io/path_spec.hpp>
  26. #include <boost/gil/io/conversion_policies.hpp>
  27. namespace boost{ namespace gil {
  28. /// \ingroup IO
  29. /// \brief Reads an image without conversion. Image memory is allocated.
  30. /// \param reader An image reader.
  31. /// \param img The image in which the data is read into. Must satisfy is_read_supported metafunction.
  32. /// \throw std::ios_base::failure
  33. template < typename Reader
  34. , typename Image
  35. >
  36. inline
  37. void read_image( Reader reader
  38. , Image& img
  39. , typename enable_if< mpl::and_< detail::is_reader< Reader >
  40. , is_format_tag< typename Reader::format_tag_t >
  41. , is_read_supported< typename get_pixel_type< typename Image::view_t >::type
  42. , typename Reader::format_tag_t
  43. >
  44. >
  45. >::type* /* ptr */ = 0
  46. )
  47. {
  48. reader.init_image( img
  49. , reader._settings
  50. );
  51. reader.apply( view( img ));
  52. }
  53. /// \brief Reads an image without conversion. Image memory is allocated.
  54. /// \param file It's a device. Must satisfy is_input_device metafunction.
  55. /// \param img The image in which the data is read into. Must satisfy is_read_supported metafunction.
  56. /// \param settings Specifies read settings depending on the image format.
  57. /// \throw std::ios_base::failure
  58. template < typename Device
  59. , typename Image
  60. , typename FormatTag
  61. >
  62. inline
  63. void read_image( Device& file
  64. , Image& img
  65. , const image_read_settings< FormatTag >& settings
  66. , typename enable_if< mpl::and_< detail::is_read_device< FormatTag
  67. , Device
  68. >
  69. , is_format_tag< FormatTag >
  70. , is_read_supported< typename get_pixel_type< typename Image::view_t >::type
  71. , FormatTag
  72. >
  73. >
  74. >::type* /* ptr */ = 0
  75. )
  76. {
  77. typedef typename get_reader< Device
  78. , FormatTag
  79. , detail::read_and_no_convert
  80. >::type reader_t;
  81. reader_t reader = make_reader( file
  82. , settings
  83. , detail::read_and_no_convert()
  84. );
  85. read_image( reader
  86. , img
  87. );
  88. }
  89. /// \brief Reads an image without conversion. Image memory is allocated.
  90. /// \param file It's a device. Must satisfy is_input_device metafunction.
  91. /// \param img The image in which the data is read into. Must satisfy is_read_supported metafunction.
  92. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  93. /// \throw std::ios_base::failure
  94. template < typename Device
  95. , typename Image
  96. , typename FormatTag
  97. >
  98. inline
  99. void read_image( Device& file
  100. , Image& img
  101. , const FormatTag& tag
  102. , typename enable_if< mpl::and_< detail::is_read_device< FormatTag
  103. , Device
  104. >
  105. , is_format_tag< FormatTag >
  106. , is_read_supported< typename get_pixel_type< typename Image::view_t >::type
  107. , FormatTag
  108. >
  109. >
  110. >::type* /* ptr */ = 0
  111. )
  112. {
  113. typedef typename get_reader< Device
  114. , FormatTag
  115. , detail::read_and_no_convert
  116. >::type reader_t;
  117. reader_t reader = make_reader( file
  118. , tag
  119. , detail::read_and_no_convert()
  120. );
  121. read_image( reader
  122. , img
  123. );
  124. }
  125. /// \brief Reads an image without conversion. Image memory is allocated.
  126. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  127. /// \param img The image in which the data is read into. Must satisfy is_read_supported metafunction.
  128. /// \param settings Specifies read settings depending on the image format.
  129. /// \throw std::ios_base::failure
  130. template < typename String
  131. , typename Image
  132. , typename FormatTag
  133. >
  134. inline
  135. void read_image( const String& file_name
  136. , Image& img
  137. , const image_read_settings< FormatTag >& settings
  138. , typename enable_if< mpl::and_< detail::is_supported_path_spec< String >
  139. , is_format_tag< FormatTag >
  140. , is_read_supported< typename get_pixel_type< typename Image::view_t >::type
  141. , FormatTag
  142. >
  143. >
  144. >::type* /* ptr */ = 0
  145. )
  146. {
  147. typedef typename get_reader< String
  148. , FormatTag
  149. , detail::read_and_no_convert
  150. >::type reader_t;
  151. reader_t reader = make_reader( file_name
  152. , settings
  153. , detail::read_and_no_convert()
  154. );
  155. read_image( reader
  156. , img
  157. );
  158. }
  159. /// \brief Reads an image without conversion. Image memory is allocated.
  160. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  161. /// \param img The image in which the data is read into. Must satisfy is_read_supported metafunction.
  162. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  163. /// \throw std::ios_base::failure
  164. template < typename String
  165. , typename Image
  166. , typename FormatTag
  167. >
  168. inline
  169. void read_image( const String& file_name
  170. , Image& img
  171. , const FormatTag& tag
  172. , typename enable_if< mpl::and_< detail::is_supported_path_spec< String >
  173. , is_format_tag< FormatTag >
  174. , is_read_supported< typename get_pixel_type< typename Image::view_t >::type
  175. , FormatTag
  176. >
  177. >
  178. >::type* /* ptr */ = 0
  179. )
  180. {
  181. typedef typename get_reader< String
  182. , FormatTag
  183. , detail::read_and_no_convert
  184. >::type reader_t;
  185. reader_t reader = make_reader( file_name
  186. , tag
  187. , detail::read_and_no_convert()
  188. );
  189. read_image( reader
  190. , img
  191. );
  192. }
  193. ///
  194. template < typename Reader
  195. , typename Images
  196. >
  197. inline
  198. void read_image( Reader& reader
  199. , any_image< Images >& images
  200. , typename enable_if< mpl::and_< detail::is_dynamic_image_reader< Reader >
  201. , is_format_tag< typename Reader::format_tag_t >
  202. >
  203. >::type* /* ptr */ = 0
  204. )
  205. {
  206. reader.apply( images );
  207. }
  208. /// \brief Reads an image without conversion. Image memory is allocated.
  209. /// \param file It's a device. Must satisfy is_adaptable_input_device metafunction.
  210. /// \param images Dynamic image ( mpl::vector ). See boost::gil::dynamic_image extension.
  211. /// \param settings Specifies read settings depending on the image format.
  212. /// \throw std::ios_base::failure
  213. template < typename Device
  214. , typename Images
  215. , typename FormatTag
  216. >
  217. inline
  218. void read_image( Device& file
  219. , any_image< Images >& images
  220. , const image_read_settings< FormatTag >& settings
  221. , typename enable_if< mpl::and_< detail::is_read_device< FormatTag
  222. , Device
  223. >
  224. , is_format_tag< FormatTag >
  225. >
  226. >::type* /* ptr */ = 0
  227. )
  228. {
  229. typedef typename get_dynamic_image_reader< Device
  230. , FormatTag
  231. >::type reader_t;
  232. reader_t reader = make_dynamic_image_reader( file
  233. , settings
  234. );
  235. read_image( reader
  236. , images
  237. );
  238. }
  239. /// \brief Reads an image without conversion. Image memory is allocated.
  240. /// \param file It's a device. Must satisfy is_adaptable_input_device metafunction.
  241. /// \param images Dynamic image ( mpl::vector ). See boost::gil::dynamic_image extension.
  242. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  243. /// \throw std::ios_base::failure
  244. template < typename Device
  245. , typename Images
  246. , typename FormatTag
  247. >
  248. inline
  249. void read_image( Device& file
  250. , any_image< Images >& images
  251. , const FormatTag& tag
  252. , typename enable_if< mpl::and_< detail::is_read_device< FormatTag
  253. , Device
  254. >
  255. , is_format_tag< FormatTag >
  256. >
  257. >::type* /* ptr */ = 0
  258. )
  259. {
  260. typedef typename get_dynamic_image_reader< Device
  261. , FormatTag
  262. >::type reader_t;
  263. reader_t reader = make_dynamic_image_reader( file
  264. , tag
  265. );
  266. read_image( reader
  267. , images
  268. );
  269. }
  270. /// \brief Reads an image without conversion. Image memory is allocated.
  271. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  272. /// \param images Dynamic image ( mpl::vector ). See boost::gil::dynamic_image extension.
  273. /// \param settings Specifies read settings depending on the image format.
  274. /// \throw std::ios_base::failure
  275. template < typename String
  276. , typename Images
  277. , typename FormatTag
  278. >
  279. inline
  280. void read_image( const String& file_name
  281. , any_image< Images >& images
  282. , const image_read_settings< FormatTag >& settings
  283. , typename enable_if< mpl::and_< detail::is_supported_path_spec< String >
  284. , is_format_tag< FormatTag >
  285. >
  286. >::type* /* ptr */ = 0
  287. )
  288. {
  289. typedef typename get_dynamic_image_reader< String
  290. , FormatTag
  291. >::type reader_t;
  292. reader_t reader = make_dynamic_image_reader( file_name
  293. , settings
  294. );
  295. read_image( reader
  296. , images
  297. );
  298. }
  299. /// \brief Reads an image without conversion. Image memory is allocated.
  300. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  301. /// \param images Dynamic image ( mpl::vector ). See boost::gil::dynamic_image extension.
  302. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  303. /// \throw std::ios_base::failure
  304. template < typename String
  305. , typename Images
  306. , typename FormatTag
  307. >
  308. inline
  309. void read_image( const String& file_name
  310. , any_image< Images >& images
  311. , const FormatTag& tag
  312. , typename enable_if< mpl::and_< detail::is_supported_path_spec< String >
  313. , is_format_tag< FormatTag >
  314. >
  315. >::type* /* ptr */ = 0
  316. )
  317. {
  318. typedef typename get_dynamic_image_reader< String
  319. , FormatTag
  320. >::type reader_t;
  321. reader_t reader = make_dynamic_image_reader( file_name, tag );
  322. read_image( reader
  323. , images
  324. );
  325. }
  326. } // namespace gil
  327. } // namespace boost
  328. #endif