tiff_test.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 tiff_test
  9. #define BOOST_FILESYSTEM_VERSION 3
  10. #define BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
  11. #include <boost/gil/extension/io/tiff.hpp>
  12. #include <boost/mp11.hpp>
  13. #include <boost/test/unit_test.hpp>
  14. #include <fstream>
  15. #include <sstream>
  16. #include "mandel_view.hpp"
  17. #include "paths.hpp"
  18. #include "subimage_test.hpp"
  19. // This test file will only test the library's interface.
  20. // It's more of a compile time test than a runtime test.
  21. using namespace std;
  22. using namespace boost;
  23. using namespace gil;
  24. namespace fs = boost::filesystem;
  25. using tag_t = tiff_tag;
  26. BOOST_AUTO_TEST_SUITE( gil_io_tiff_tests )
  27. #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
  28. BOOST_AUTO_TEST_CASE( read_image_info_test )
  29. {
  30. {
  31. using backend_t = get_reader_backend<std::string const, tag_t>::type;
  32. backend_t backend = read_image_info( tiff_filename
  33. , tag_t()
  34. );
  35. BOOST_CHECK_EQUAL( backend._info._width , 1000u );
  36. BOOST_CHECK_EQUAL( backend._info._height, 600u );
  37. }
  38. {
  39. ifstream in( tiff_filename.c_str(), ios::binary );
  40. using backend_t = get_reader_backend<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. TIFF* file = TIFFOpen( tiff_filename.c_str(), "r" );
  49. using backend_t = get_reader_backend<FILE*, tag_t>::type;
  50. backend_t backend = 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. fs::path my_path( tiff_filename );
  58. using backend_t = get_reader_backend<fs::path, tag_t>::type;
  59. backend_t backend = read_image_info( my_path
  60. , tag_t()
  61. );
  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. rgba8_image_t img;
  70. read_image( tiff_filename, img, tag_t() );
  71. BOOST_CHECK_EQUAL( img.width() , 1000u );
  72. BOOST_CHECK_EQUAL( img.height(), 600u );
  73. }
  74. {
  75. ifstream in( tiff_filename.c_str(), ios::binary );
  76. rgba8_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. TIFF* file = TIFFOpen( tiff_filename.c_str(), "r" );
  83. rgba8_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. BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
  90. {
  91. {
  92. rgb8_image_t img;
  93. read_and_convert_image( tiff_filename, img, tag_t() );
  94. BOOST_CHECK_EQUAL( img.width() , 1000u );
  95. BOOST_CHECK_EQUAL( img.height(), 600u );
  96. }
  97. {
  98. ifstream in( tiff_filename.c_str(), ios::binary );
  99. rgb8_image_t img;
  100. read_and_convert_image( in, img, tag_t() );
  101. BOOST_CHECK_EQUAL( img.width() , 1000u );
  102. BOOST_CHECK_EQUAL( img.height(), 600u );
  103. }
  104. {
  105. TIFF* file = TIFFOpen( tiff_filename.c_str(), "r" );
  106. rgb8_image_t img;
  107. read_and_convert_image( file, img, tag_t() );
  108. BOOST_CHECK_EQUAL( img.width() , 1000u );
  109. BOOST_CHECK_EQUAL( img.height(), 600u );
  110. }
  111. }
  112. BOOST_AUTO_TEST_CASE( read_and_convert_image_test_2 )
  113. {
  114. gray8_image_t img;
  115. read_and_convert_image( tiff_filename, img, tag_t() );
  116. rgba8_image_t img2;
  117. read_image( tiff_filename, img2, tag_t() );
  118. BOOST_CHECK( equal_pixels( const_view( img )
  119. , color_converted_view< gray8_pixel_t>( const_view( img2 ) )
  120. )
  121. );
  122. }
  123. BOOST_AUTO_TEST_CASE( read_view_test )
  124. {
  125. {
  126. rgba8_image_t img( 1000, 600 );
  127. read_view( tiff_filename, view( img ), tag_t() );
  128. }
  129. {
  130. ifstream in( tiff_filename.c_str(), ios::binary );
  131. rgba8_image_t img( 1000, 600 );
  132. read_view( in, view( img ), tag_t() );
  133. BOOST_CHECK_EQUAL( img.width() , 1000u );
  134. BOOST_CHECK_EQUAL( img.height(), 600u );
  135. }
  136. {
  137. TIFF* file = TIFFOpen( tiff_filename.c_str(), "r" );
  138. rgba8_image_t img( 1000, 600 );
  139. read_view( file, view( img ), tag_t() );
  140. }
  141. }
  142. BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
  143. {
  144. {
  145. rgb8_image_t img( 1000, 600 );
  146. read_and_convert_view( tiff_filename, view( img ), tag_t() );
  147. }
  148. {
  149. ifstream in( tiff_filename.c_str(), ios::binary );
  150. rgb8_image_t img( 1000, 600 );
  151. read_and_convert_view( in, view( img ), tag_t() );
  152. BOOST_CHECK_EQUAL( img.width() , 1000u );
  153. BOOST_CHECK_EQUAL( img.height(), 600u );
  154. }
  155. {
  156. TIFF* file = TIFFOpen( tiff_filename.c_str(), "r" );
  157. rgb8_image_t img( 1000, 600 );
  158. read_and_convert_view( file, view( img ), tag_t() );
  159. }
  160. }
  161. #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
  162. #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  163. BOOST_AUTO_TEST_CASE( write_view_test )
  164. {
  165. {
  166. string filename( tiff_out + "write_test_string.tif" );
  167. write_view( filename
  168. , create_mandel_view( 320, 240
  169. , rgb8_pixel_t( 0, 0, 255 )
  170. , rgb8_pixel_t( 0, 255, 0 )
  171. )
  172. , tag_t()
  173. );
  174. }
  175. {
  176. string filename( tiff_out + "write_test_ofstream.tif" );
  177. ofstream out( filename.c_str(), ios_base::binary );
  178. write_view( out
  179. , create_mandel_view( 320, 240
  180. , rgb8_pixel_t( 0, 0, 255 )
  181. , rgb8_pixel_t( 0, 255, 0 )
  182. )
  183. , tag_t()
  184. );
  185. }
  186. {
  187. string filename( tiff_out + "write_test_tiff.tif" );
  188. TIFF* file = TIFFOpen( filename.c_str(), "w" );
  189. write_view( file
  190. , create_mandel_view( 320, 240
  191. , rgb8_pixel_t( 0, 0, 255 )
  192. , rgb8_pixel_t( 0, 255, 0 )
  193. )
  194. , tag_t()
  195. );
  196. }
  197. {
  198. string filename( tiff_out + "write_test_info.tif" );
  199. image_write_info< tiff_tag > info;
  200. write_view( filename
  201. , create_mandel_view( 320, 240
  202. , rgb8_pixel_t( 0, 0, 255 )
  203. , rgb8_pixel_t( 0, 255, 0 )
  204. )
  205. , info
  206. );
  207. }
  208. }
  209. #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  210. #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
  211. BOOST_AUTO_TEST_CASE( stream_test )
  212. {
  213. // 1. Read an image.
  214. ifstream in( tiff_filename.c_str(), ios::binary );
  215. rgba8_image_t img;
  216. read_image( in, img, tag_t() );
  217. // 2. Write image to in-memory buffer.
  218. stringstream out_buffer( ios_base::in | ios_base::out | ios_base::binary );
  219. write_view( out_buffer, view( img ), tag_t() );
  220. // 3. Copy in-memory buffer to another.
  221. stringstream in_buffer( ios_base::in | ios_base::out | ios_base::binary );
  222. in_buffer << out_buffer.rdbuf();
  223. // 4. Read in-memory buffer to gil image
  224. rgba8_image_t dst;
  225. read_image( in_buffer, dst, tag_t() );
  226. // 5. Write out image.
  227. string filename( tiff_out + "stream_test.tif" );
  228. ofstream out( filename.c_str(), ios_base::binary );
  229. #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  230. write_view( out, view( dst ), tag_t() );
  231. #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  232. }
  233. BOOST_AUTO_TEST_CASE( stream_test_2 )
  234. {
  235. filebuf in_buf;
  236. if( !in_buf.open( tiff_filename.c_str(), ios::in | ios::binary ) )
  237. {
  238. BOOST_CHECK( false );
  239. }
  240. istream in( &in_buf );
  241. rgba8_image_t img;
  242. read_image( in, img, tag_t() );
  243. }
  244. BOOST_AUTO_TEST_CASE( subimage_test )
  245. {
  246. run_subimage_test< rgba8_image_t, tag_t >( tiff_filename
  247. , point_t( 0, 0 )
  248. , point_t( 50, 50 )
  249. );
  250. run_subimage_test< rgba8_image_t, tag_t >( tiff_filename
  251. , point_t( 50, 50 )
  252. , point_t( 50, 50 )
  253. );
  254. }
  255. BOOST_AUTO_TEST_CASE( dynamic_image_test )
  256. {
  257. // FIXME: This test has been disabled for now because of compilation issues with MSVC10.
  258. using my_img_types = mp11::mp_list
  259. <
  260. gray8_image_t,
  261. gray16_image_t,
  262. rgb8_image_t,
  263. gray1_image_t
  264. >;
  265. any_image< my_img_types > runtime_image;
  266. read_image( tiff_filename.c_str()
  267. , runtime_image
  268. , tag_t()
  269. );
  270. #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  271. write_view( tiff_out + "dynamic_image_test.tif"
  272. , view( runtime_image )
  273. , tag_t()
  274. );
  275. #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
  276. }
  277. #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
  278. BOOST_AUTO_TEST_SUITE_END()