targa_test.cpp 8.7 KB

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