2 // Copyright 2013 Christian Henning
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
8 #define BOOST_TEST_MODULE bmp_test
9 #define BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
10 #define BOOST_FILESYSTEM_VERSION 3
11 #include <boost/gil.hpp>
12 #include <boost/gil/extension/io/bmp.hpp>
14 #include <boost/mp11.hpp>
15 #include <boost/test/unit_test.hpp>
19 #include "mandel_view.hpp"
21 #include "subimage_test.hpp"
24 using namespace boost;
26 namespace fs = boost::filesystem;
28 using tag_t = bmp_tag;
30 BOOST_AUTO_TEST_SUITE( gil_io_bmp_tests )
32 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
34 BOOST_AUTO_TEST_CASE( read_image_info_using_string )
37 using backend_t = get_reader_backend<std::string const, tag_t>::type;
39 backend_t backend = read_image_info( bmp_filename
43 BOOST_CHECK_EQUAL( backend._info._width , 1000 );
44 BOOST_CHECK_EQUAL( backend._info._height, 600 );
48 ifstream in( bmp_filename.c_str(), ios::binary );
50 using backend_t = get_reader_backend<std::ifstream, tag_t>::type;
52 backend_t backend = read_image_info( in
56 BOOST_CHECK_EQUAL( backend._info._width , 1000 );
57 BOOST_CHECK_EQUAL( backend._info._height, 600 );
61 FILE* file = fopen( bmp_filename.c_str(), "rb" );
63 using backend_t = get_reader_backend<FILE*, tag_t>::type;
65 backend_t backend = read_image_info( file
69 BOOST_CHECK_EQUAL( backend._info._width , 1000 );
70 BOOST_CHECK_EQUAL( backend._info._height, 600 );
74 fs::path my_path( bmp_filename );
76 using backend_t = get_reader_backend<fs::path, tag_t>::type;
78 backend_t backend = read_image_info( my_path
82 BOOST_CHECK_EQUAL( backend._info._width , 1000 );
83 BOOST_CHECK_EQUAL( backend._info._height, 600 );
87 BOOST_AUTO_TEST_CASE( read_image_test )
91 read_image( bmp_filename, img, tag_t() );
93 BOOST_CHECK_EQUAL( img.width() , 1000 );
94 BOOST_CHECK_EQUAL( img.height(), 600 );
98 ifstream in( bmp_filename.c_str(), ios::binary );
101 read_image( in, img, tag_t() );
103 BOOST_CHECK_EQUAL( img.width() , 1000 );
104 BOOST_CHECK_EQUAL( img.height(), 600 );
108 FILE* file = fopen( bmp_filename.c_str(), "rb" );
111 read_image( file, img, tag_t() );
113 BOOST_CHECK_EQUAL( img.width() , 1000 );
114 BOOST_CHECK_EQUAL( img.height(), 600 );
118 fs::path my_path( bmp_filename );
121 read_image( my_path, img, tag_t() );
123 BOOST_CHECK_EQUAL( img.width() , 1000 );
124 BOOST_CHECK_EQUAL( img.height(), 600 );
128 BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
132 read_and_convert_image( bmp_filename, img, tag_t() );
134 BOOST_CHECK_EQUAL( img.width() , 1000 );
135 BOOST_CHECK_EQUAL( img.height(), 600 );
139 ifstream in( bmp_filename.c_str(), ios::binary );
142 read_and_convert_image( in, img, tag_t() );
144 BOOST_CHECK_EQUAL( img.width() , 1000 );
145 BOOST_CHECK_EQUAL( img.height(), 600 );
149 FILE* file = fopen( bmp_filename.c_str(), "rb" );
152 read_and_convert_image( file, img, tag_t() );
154 BOOST_CHECK_EQUAL( img.width() , 1000 );
155 BOOST_CHECK_EQUAL( img.height(), 600 );
159 BOOST_AUTO_TEST_CASE( read_view_test )
162 rgb8_image_t img( 1000, 600 );
163 read_view( bmp_filename, view( img ), tag_t() );
167 ifstream in( bmp_filename.c_str(), ios::binary );
169 rgb8_image_t img( 1000, 600 );
170 read_view( in, view( img ), tag_t() );
174 FILE* file = fopen( bmp_filename.c_str(), "rb" );
176 rgb8_image_t img( 1000, 600 );
177 read_view( file, view( img ), tag_t() );
181 BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
184 rgb8_image_t img( 1000, 600 );
185 read_and_convert_view( bmp_filename, view( img ), tag_t() );
189 ifstream in( bmp_filename.c_str(), ios::binary );
191 rgb8_image_t img( 1000, 600 );
192 read_and_convert_view( in, view( img ), tag_t() );
196 FILE* file = fopen( bmp_filename.c_str(), "rb" );
198 rgb8_image_t img( 1000, 600 );
199 read_and_convert_view( file
206 BOOST_AUTO_TEST_CASE( write_view_test )
208 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
210 string filename( bmp_out + "write_test_string.bmp" );
213 , create_mandel_view( 1000, 600
214 , rgb8_pixel_t( 0, 0, 255 )
215 , rgb8_pixel_t( 0, 255, 0 )
222 string filename( bmp_out + "write_test_ofstream.bmp" );
224 ofstream out( filename.c_str(), ios::binary );
227 , create_mandel_view( 1000, 600
228 , rgb8_pixel_t( 0, 0, 255 )
229 , rgb8_pixel_t( 0, 255, 0 )
236 string filename( bmp_out + "write_test_file.bmp" );
238 FILE* file = fopen( filename.c_str(), "wb" );
241 , create_mandel_view( 1000, 600
242 , rgb8_pixel_t( 0, 0, 255 )
243 , rgb8_pixel_t( 0, 255, 0 )
250 string filename( bmp_out + "write_test_info.bmp" );
252 image_write_info< tag_t > info;
254 FILE* file = fopen( filename.c_str(), "wb" );
257 , create_mandel_view( 1000, 600
258 , rgb8_pixel_t( 0, 0, 255 )
259 , rgb8_pixel_t( 0, 255, 0 )
265 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
268 BOOST_AUTO_TEST_CASE( stream_test )
271 ifstream in( bmp_filename.c_str(), ios::binary );
274 read_image( in, img, tag_t() );
276 // 2. Write image to in-memory buffer.
277 stringstream out_buffer( ios_base::in | ios_base::out | ios_base::binary );
278 write_view( out_buffer, view( img ), tag_t() );
280 // 3. Copy in-memory buffer to another.
281 stringstream in_buffer( ios_base::in | ios_base::out | ios_base::binary );
282 in_buffer << out_buffer.rdbuf();
284 // 4. Read in-memory buffer to gil image
286 read_image( in_buffer, dst, tag_t() );
288 // 5. Write out image.
289 string filename( bmp_out + "stream_test.bmp" );
290 ofstream out( filename.c_str(), ios_base::binary );
292 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
293 write_view( out, view( dst ), tag_t() );
294 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
297 BOOST_AUTO_TEST_CASE( stream_test_2 )
300 if( !in_buf.open( bmp_filename.c_str(), ios::in | ios::binary ) )
302 BOOST_CHECK( false );
305 istream in( &in_buf );
308 read_image( in, img, tag_t() );
311 BOOST_AUTO_TEST_CASE( subimage_test )
313 run_subimage_test< rgb8_image_t, tag_t >( bmp_filename
318 run_subimage_test< rgb8_image_t, tag_t >( bmp_filename
324 BOOST_AUTO_TEST_CASE( dynamic_image_test )
326 using my_img_types = mp11::mp_list
334 any_image< my_img_types > runtime_image;
336 read_image( bmp_filename.c_str()
341 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
342 write_view( bmp_out + "dynamic_image_test.bmp"
343 , view( runtime_image )
346 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
349 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
351 BOOST_AUTO_TEST_SUITE_END()