pnm_test.cpp 8.5 KB

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