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