jpeg_test.cpp 9.0 KB

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