bmp_test.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //
  2. // Copyright 2013 Christian Henning
  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. #define BOOST_TEST_MODULE bmp_test
  9. #define BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
  10. #define BOOST_FILESYSTEM_VERSION 3
  11. #include <boost/gil.hpp>
  12. #include <boost/gil/extension/io/bmp.hpp>
  13. #include <boost/mp11.hpp>
  14. #include <boost/test/unit_test.hpp>
  15. #include <fstream>
  16. #include "mandel_view.hpp"
  17. #include "paths.hpp"
  18. #include "subimage_test.hpp"
  19. using namespace std;
  20. using namespace boost;
  21. using namespace gil;
  22. namespace fs = boost::filesystem;
  23. using tag_t = bmp_tag;
  24. BOOST_AUTO_TEST_SUITE( gil_io_bmp_tests )
  25. #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
  26. BOOST_AUTO_TEST_CASE( read_image_info_using_string )
  27. {
  28. {
  29. using backend_t = get_reader_backend<std::string const, tag_t>::type;
  30. backend_t backend = read_image_info( bmp_filename
  31. , tag_t()
  32. );
  33. BOOST_CHECK_EQUAL( backend._info._width , 1000 );
  34. BOOST_CHECK_EQUAL( backend._info._height, 600 );
  35. }
  36. {
  37. ifstream in( bmp_filename.c_str(), ios::binary );
  38. using backend_t = get_reader_backend<std::ifstream, tag_t>::type;
  39. backend_t backend = read_image_info( in
  40. , tag_t()
  41. );
  42. BOOST_CHECK_EQUAL( backend._info._width , 1000 );
  43. BOOST_CHECK_EQUAL( backend._info._height, 600 );
  44. }
  45. {
  46. FILE* file = fopen( bmp_filename.c_str(), "rb" );
  47. using backend_t = get_reader_backend<FILE*, tag_t>::type;
  48. backend_t backend = read_image_info( file
  49. , tag_t()
  50. );
  51. BOOST_CHECK_EQUAL( backend._info._width , 1000 );
  52. BOOST_CHECK_EQUAL( backend._info._height, 600 );
  53. }
  54. {
  55. fs::path my_path( bmp_filename );
  56. using backend_t = get_reader_backend<fs::path, tag_t>::type;
  57. backend_t backend = read_image_info( my_path
  58. , tag_t()
  59. );
  60. BOOST_CHECK_EQUAL( backend._info._width , 1000 );
  61. BOOST_CHECK_EQUAL( backend._info._height, 600 );
  62. }
  63. }
  64. BOOST_AUTO_TEST_CASE( read_image_test )
  65. {
  66. {
  67. rgb8_image_t img;
  68. read_image( bmp_filename, img, tag_t() );
  69. BOOST_CHECK_EQUAL( img.width() , 1000 );
  70. BOOST_CHECK_EQUAL( img.height(), 600 );
  71. }
  72. {
  73. ifstream in( bmp_filename.c_str(), ios::binary );
  74. rgb8_image_t img;
  75. read_image( in, img, tag_t() );
  76. BOOST_CHECK_EQUAL( img.width() , 1000 );
  77. BOOST_CHECK_EQUAL( img.height(), 600 );
  78. }
  79. {
  80. FILE* file = fopen( bmp_filename.c_str(), "rb" );
  81. rgb8_image_t img;
  82. read_image( file, img, tag_t() );
  83. BOOST_CHECK_EQUAL( img.width() , 1000 );
  84. BOOST_CHECK_EQUAL( img.height(), 600 );
  85. }
  86. {
  87. fs::path my_path( bmp_filename );
  88. rgb8_image_t img;
  89. read_image( my_path, img, tag_t() );
  90. BOOST_CHECK_EQUAL( img.width() , 1000 );
  91. BOOST_CHECK_EQUAL( img.height(), 600 );
  92. }
  93. }
  94. BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
  95. {
  96. {
  97. rgb8_image_t img;
  98. read_and_convert_image( bmp_filename, img, tag_t() );
  99. BOOST_CHECK_EQUAL( img.width() , 1000 );
  100. BOOST_CHECK_EQUAL( img.height(), 600 );
  101. }
  102. {
  103. ifstream in( bmp_filename.c_str(), ios::binary );
  104. rgb8_image_t img;
  105. read_and_convert_image( in, img, tag_t() );
  106. BOOST_CHECK_EQUAL( img.width() , 1000 );
  107. BOOST_CHECK_EQUAL( img.height(), 600 );
  108. }
  109. {
  110. FILE* file = fopen( bmp_filename.c_str(), "rb" );
  111. rgb8_image_t img;
  112. read_and_convert_image( file, img, tag_t() );
  113. BOOST_CHECK_EQUAL( img.width() , 1000 );
  114. BOOST_CHECK_EQUAL( img.height(), 600 );
  115. }
  116. }
  117. BOOST_AUTO_TEST_CASE( read_view_test )
  118. {
  119. {
  120. rgb8_image_t img( 1000, 600 );
  121. read_view( bmp_filename, view( img ), tag_t() );
  122. }
  123. {
  124. ifstream in( bmp_filename.c_str(), ios::binary );
  125. rgb8_image_t img( 1000, 600 );
  126. read_view( in, view( img ), tag_t() );
  127. }
  128. {
  129. FILE* file = fopen( bmp_filename.c_str(), "rb" );
  130. rgb8_image_t img( 1000, 600 );
  131. read_view( file, view( img ), tag_t() );
  132. }
  133. }
  134. BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
  135. {
  136. {
  137. rgb8_image_t img( 1000, 600 );
  138. read_and_convert_view( bmp_filename, view( img ), tag_t() );
  139. }
  140. {
  141. ifstream in( bmp_filename.c_str(), ios::binary );
  142. rgb8_image_t img( 1000, 600 );
  143. read_and_convert_view( in, view( img ), tag_t() );
  144. }
  145. {
  146. FILE* file = fopen( bmp_filename.c_str(), "rb" );
  147. rgb8_image_t img( 1000, 600 );
  148. read_and_convert_view( file
  149. , view( img )
  150. , tag_t()
  151. );
  152. }
  153. }
  154. BOOST_AUTO_TEST_CASE( write_view_test )
  155. {
  156. #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  157. {
  158. string filename( bmp_out + "write_test_string.bmp" );
  159. write_view( filename
  160. , create_mandel_view( 1000, 600
  161. , rgb8_pixel_t( 0, 0, 255 )
  162. , rgb8_pixel_t( 0, 255, 0 )
  163. )
  164. , tag_t()
  165. );
  166. }
  167. {
  168. string filename( bmp_out + "write_test_ofstream.bmp" );
  169. ofstream out( filename.c_str(), ios::binary );
  170. write_view( out
  171. , create_mandel_view( 1000, 600
  172. , rgb8_pixel_t( 0, 0, 255 )
  173. , rgb8_pixel_t( 0, 255, 0 )
  174. )
  175. , tag_t()
  176. );
  177. }
  178. {
  179. string filename( bmp_out + "write_test_file.bmp" );
  180. FILE* file = fopen( filename.c_str(), "wb" );
  181. write_view( file
  182. , create_mandel_view( 1000, 600
  183. , rgb8_pixel_t( 0, 0, 255 )
  184. , rgb8_pixel_t( 0, 255, 0 )
  185. )
  186. , tag_t()
  187. );
  188. }
  189. {
  190. string filename( bmp_out + "write_test_info.bmp" );
  191. image_write_info< tag_t > info;
  192. FILE* file = fopen( filename.c_str(), "wb" );
  193. write_view( file
  194. , create_mandel_view( 1000, 600
  195. , rgb8_pixel_t( 0, 0, 255 )
  196. , rgb8_pixel_t( 0, 255, 0 )
  197. )
  198. , info
  199. );
  200. }
  201. #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  202. }
  203. BOOST_AUTO_TEST_CASE( stream_test )
  204. {
  205. // 1. Read an image.
  206. ifstream in( bmp_filename.c_str(), ios::binary );
  207. rgb8_image_t img;
  208. read_image( in, img, tag_t() );
  209. // 2. Write image to in-memory buffer.
  210. stringstream out_buffer( ios_base::in | ios_base::out | ios_base::binary );
  211. write_view( out_buffer, view( img ), tag_t() );
  212. // 3. Copy in-memory buffer to another.
  213. stringstream in_buffer( ios_base::in | ios_base::out | ios_base::binary );
  214. in_buffer << out_buffer.rdbuf();
  215. // 4. Read in-memory buffer to gil image
  216. rgb8_image_t dst;
  217. read_image( in_buffer, dst, tag_t() );
  218. // 5. Write out image.
  219. string filename( bmp_out + "stream_test.bmp" );
  220. ofstream out( filename.c_str(), ios_base::binary );
  221. #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  222. write_view( out, view( dst ), tag_t() );
  223. #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  224. }
  225. BOOST_AUTO_TEST_CASE( stream_test_2 )
  226. {
  227. filebuf in_buf;
  228. if( !in_buf.open( bmp_filename.c_str(), ios::in | ios::binary ) )
  229. {
  230. BOOST_CHECK( false );
  231. }
  232. istream in( &in_buf );
  233. rgb8_image_t img;
  234. read_image( in, img, tag_t() );
  235. }
  236. BOOST_AUTO_TEST_CASE( subimage_test )
  237. {
  238. run_subimage_test< rgb8_image_t, tag_t >( bmp_filename
  239. , point_t( 0, 0 )
  240. , point_t( 1000, 1 )
  241. );
  242. run_subimage_test< rgb8_image_t, tag_t >( bmp_filename
  243. , point_t( 39, 7 )
  244. , point_t( 50, 50 )
  245. );
  246. }
  247. BOOST_AUTO_TEST_CASE( dynamic_image_test )
  248. {
  249. using my_img_types = mp11::mp_list
  250. <
  251. gray8_image_t,
  252. gray16_image_t,
  253. rgb8_image_t,
  254. rgba8_image_t
  255. >;
  256. any_image< my_img_types > runtime_image;
  257. read_image( bmp_filename.c_str()
  258. , runtime_image
  259. , tag_t()
  260. );
  261. #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  262. write_view( bmp_out + "dynamic_image_test.bmp"
  263. , view( runtime_image )
  264. , tag_t()
  265. );
  266. #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  267. }
  268. #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
  269. BOOST_AUTO_TEST_SUITE_END()