read_and_convert_image.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_AND_CONVERT_IMAGE_HPP
  9. #define BOOST_GIL_IO_READ_AND_CONVERT_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/mpl/and.hpp>
  20. #include <boost/utility/enable_if.hpp>
  21. #include <boost/gil/io/base.hpp>
  22. #include <boost/gil/io/device.hpp>
  23. #include <boost/gil/io/path_spec.hpp>
  24. #include <boost/gil/io/conversion_policies.hpp>
  25. namespace boost{ namespace gil {
  26. /// \ingroup IO
  27. /// \brief Reads and color-converts an image. Image memory is allocated.
  28. /// \param reader An image reader.
  29. /// \param img The image in which the data is read into.
  30. /// \param settings Specifies read settings depending on the image format.
  31. /// \param cc Color converter function object.
  32. /// \throw std::ios_base::failure
  33. template< typename Reader
  34. , typename Image
  35. >
  36. inline
  37. void read_and_convert_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. >
  42. >::type* /* ptr */ = 0
  43. )
  44. {
  45. reader.init_image( img
  46. , reader._settings
  47. );
  48. reader.apply( view( img ));
  49. }
  50. /// \brief Reads and color-converts an image. Image memory is allocated.
  51. /// \param device Must satisfy is_input_device metafunction.
  52. /// \param img The image in which the data is read into.
  53. /// \param settings Specifies read settings depending on the image format.
  54. /// \param cc Color converter function object.
  55. /// \throw std::ios_base::failure
  56. template< typename Device
  57. , typename Image
  58. , typename ColorConverter
  59. , typename FormatTag
  60. >
  61. inline
  62. void read_and_convert_image( Device& device
  63. , Image& img
  64. , const image_read_settings< FormatTag >& settings
  65. , const ColorConverter& cc
  66. , typename enable_if< mpl::and_< detail::is_read_device< FormatTag
  67. , Device
  68. >
  69. , is_format_tag< FormatTag >
  70. >
  71. >::type* /* ptr */ = 0
  72. )
  73. {
  74. typedef typename get_reader< Device
  75. , FormatTag
  76. , detail::read_and_convert< ColorConverter >
  77. >::type reader_t;
  78. reader_t reader = make_reader( device
  79. , settings
  80. , detail::read_and_convert< ColorConverter >( cc )
  81. );
  82. read_and_convert_image( reader
  83. , img
  84. );
  85. }
  86. /// \brief Reads and color-converts an image. Image memory is allocated.
  87. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  88. /// \param img The image in which the data is read into.
  89. /// \param settings Specifies read settings depending on the image format.
  90. /// \param cc Color converter function object.
  91. /// \throw std::ios_base::failure
  92. template < typename String
  93. , typename Image
  94. , typename ColorConverter
  95. , typename FormatTag
  96. >
  97. inline
  98. void read_and_convert_image( const String& file_name
  99. , Image& img
  100. , const image_read_settings< FormatTag >& settings
  101. , const ColorConverter& cc
  102. , typename enable_if< mpl::and_< is_format_tag< FormatTag >
  103. , detail::is_supported_path_spec< String >
  104. >
  105. >::type* /* ptr */ = 0
  106. )
  107. {
  108. typedef typename get_reader< String
  109. , FormatTag
  110. , detail::read_and_convert< ColorConverter >
  111. >::type reader_t;
  112. reader_t reader = make_reader( file_name
  113. , settings
  114. , detail::read_and_convert< ColorConverter >( cc )
  115. );
  116. read_and_convert_image( reader
  117. , img
  118. );
  119. }
  120. /// \brief Reads and color-converts an image. Image memory is allocated.
  121. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  122. /// \param img The image in which the data is read into.
  123. /// \param cc Color converter function object.
  124. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  125. /// \throw std::ios_base::failure
  126. template < typename String
  127. , typename Image
  128. , typename ColorConverter
  129. , typename FormatTag
  130. >
  131. inline
  132. void read_and_convert_image( const String& file_name
  133. , Image& img
  134. , const ColorConverter& cc
  135. , const FormatTag& tag
  136. , typename enable_if< mpl::and_< is_format_tag< FormatTag >
  137. , detail::is_supported_path_spec< String >
  138. >
  139. >::type* /* ptr */ = 0
  140. )
  141. {
  142. typedef typename get_reader< String
  143. , FormatTag
  144. , detail::read_and_convert< ColorConverter >
  145. >::type reader_t;
  146. reader_t reader = make_reader( file_name
  147. , tag
  148. , detail::read_and_convert< ColorConverter >( cc )
  149. );
  150. read_and_convert_image( reader
  151. , img
  152. );
  153. }
  154. /// \brief Reads and color-converts an image. Image memory is allocated.
  155. /// \param device Must satisfy is_input_device metafunction or is_adaptable_input_device.
  156. /// \param img The image in which the data is read into.
  157. /// \param cc Color converter function object.
  158. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  159. /// \throw std::ios_base::failure
  160. template < typename Device
  161. , typename Image
  162. , typename ColorConverter
  163. , typename FormatTag
  164. >
  165. inline
  166. void read_and_convert_image( Device& device
  167. , Image& img
  168. , const ColorConverter& cc
  169. , const FormatTag& tag
  170. , typename enable_if< mpl::and_< detail::is_read_device< FormatTag
  171. , Device
  172. >
  173. , is_format_tag< FormatTag >
  174. >
  175. >::type* /* ptr */ = 0
  176. )
  177. {
  178. typedef typename get_reader< Device
  179. , FormatTag
  180. , detail::read_and_convert< ColorConverter >
  181. >::type reader_t;
  182. reader_t reader = make_reader( device
  183. , tag
  184. , detail::read_and_convert< ColorConverter >( cc )
  185. );
  186. read_and_convert_image( reader
  187. , img
  188. );
  189. }
  190. /// \brief Reads and color-converts an image. Image memory is allocated. Default color converter is used.
  191. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  192. /// \param img The image in which the data is read into.
  193. /// \param settings Specifies read settings depending on the image format.
  194. /// \throw std::ios_base::failure
  195. template < typename String
  196. , typename Image
  197. , typename FormatTag
  198. >
  199. inline
  200. void read_and_convert_image( const String& file_name
  201. , Image& img
  202. , const image_read_settings< FormatTag >& settings
  203. , typename enable_if< mpl::and_< is_format_tag< FormatTag >
  204. , detail::is_supported_path_spec< String >
  205. >
  206. >::type* /* ptr */ = 0
  207. )
  208. {
  209. typedef typename get_reader< String
  210. , FormatTag
  211. , detail::read_and_convert< default_color_converter >
  212. >::type reader_t;
  213. reader_t reader = make_reader( file_name
  214. , settings
  215. , detail::read_and_convert< default_color_converter >()
  216. );
  217. read_and_convert_image( reader
  218. , img
  219. );
  220. }
  221. /// \brief Reads and color-converts an image. Image memory is allocated. Default color converter is used.
  222. /// \param device It's a device. Must satisfy is_input_device metafunction or is_adaptable_input_device.
  223. /// \param img The image in which the data is read into.
  224. /// \param settings Specifies read settings depending on the image format.
  225. /// \throw std::ios_base::failure
  226. template < typename Device
  227. , typename Image
  228. , typename FormatTag
  229. >
  230. inline
  231. void read_and_convert_image( Device& device
  232. , Image& img
  233. , const image_read_settings< FormatTag >& settings
  234. , typename enable_if< mpl::and_< detail::is_read_device< FormatTag
  235. , Device
  236. >
  237. , is_format_tag< FormatTag >
  238. >
  239. >::type* /* ptr */ = 0
  240. )
  241. {
  242. typedef typename get_reader< Device
  243. , FormatTag
  244. , detail::read_and_convert< default_color_converter >
  245. >::type reader_t;
  246. reader_t reader = make_reader( device
  247. , settings
  248. , detail::read_and_convert< default_color_converter >()
  249. );
  250. read_and_convert_image( reader
  251. , img
  252. );
  253. }
  254. /// \brief Reads and color-converts an image. Image memory is allocated. Default color converter is used.
  255. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  256. /// \param img The image in which the data is read into.
  257. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  258. /// \throw std::ios_base::failure
  259. template < typename String
  260. , typename Image
  261. , typename FormatTag
  262. >
  263. inline
  264. void read_and_convert_image( const String& file_name
  265. , Image& img
  266. , const FormatTag& tag
  267. , typename enable_if< mpl::and_< is_format_tag< FormatTag >
  268. , detail::is_supported_path_spec< String >
  269. >
  270. >::type* /* ptr */ = 0
  271. )
  272. {
  273. typedef typename get_reader< String
  274. , FormatTag
  275. , detail::read_and_convert< default_color_converter >
  276. >::type reader_t;
  277. reader_t reader = make_reader( file_name
  278. , tag
  279. , detail::read_and_convert< default_color_converter >()
  280. );
  281. read_and_convert_image( reader
  282. , img
  283. );
  284. }
  285. /// \brief Reads and color-converts an image. Image memory is allocated. Default color converter is used.
  286. /// \param file_name File name. Must satisfy is_supported_path_spec metafunction.
  287. /// \param img The image in which the data is read into.
  288. /// \param tag Defines the image format. Must satisfy is_format_tag metafunction.
  289. /// \throw std::ios_base::failure
  290. template < typename Device
  291. , typename Image
  292. , typename FormatTag
  293. >
  294. inline
  295. void read_and_convert_image( Device& device
  296. , Image& img
  297. , const FormatTag& tag
  298. , typename enable_if< mpl::and_< detail::is_read_device< FormatTag
  299. , Device
  300. >
  301. , is_format_tag< FormatTag >
  302. >
  303. >::type* /* ptr */ = 0
  304. )
  305. {
  306. typedef typename get_reader< Device
  307. , FormatTag
  308. , detail::read_and_convert< default_color_converter >
  309. >::type reader_t;
  310. reader_t reader = make_reader( device
  311. , tag
  312. , detail::read_and_convert< default_color_converter >()
  313. );
  314. read_and_convert_image( reader
  315. , img
  316. );
  317. }
  318. } // namespace gil
  319. } // namespace boost
  320. #endif