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/test/unit_test.hpp>
18 #include "mandel_view.hpp"
20 #include "subimage_test.hpp"
23 using namespace boost;
25 namespace fs = boost::filesystem;
27 using tag_t = bmp_tag;
29 BOOST_AUTO_TEST_SUITE( gil_io_bmp_tests )
31 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
33 BOOST_AUTO_TEST_CASE( read_image_info_using_string )
36 using backend_t = get_reader_backend<std::string const, tag_t>::type;
38 backend_t backend = read_image_info( bmp_filename
42 BOOST_CHECK_EQUAL( backend._info._width , 1000 );
43 BOOST_CHECK_EQUAL( backend._info._height, 600 );
47 ifstream in( bmp_filename.c_str(), ios::binary );
49 using backend_t = get_reader_backend<std::ifstream, tag_t>::type;
51 backend_t backend = read_image_info( in
55 BOOST_CHECK_EQUAL( backend._info._width , 1000 );
56 BOOST_CHECK_EQUAL( backend._info._height, 600 );
60 FILE* file = fopen( bmp_filename.c_str(), "rb" );
62 using backend_t = get_reader_backend<FILE*, tag_t>::type;
64 backend_t backend = read_image_info( file
68 BOOST_CHECK_EQUAL( backend._info._width , 1000 );
69 BOOST_CHECK_EQUAL( backend._info._height, 600 );
73 fs::path my_path( bmp_filename );
75 using backend_t = get_reader_backend<fs::path, tag_t>::type;
77 backend_t backend = read_image_info( my_path
81 BOOST_CHECK_EQUAL( backend._info._width , 1000 );
82 BOOST_CHECK_EQUAL( backend._info._height, 600 );
86 BOOST_AUTO_TEST_CASE( read_image_test )
90 read_image( bmp_filename, img, tag_t() );
92 BOOST_CHECK_EQUAL( img.width() , 1000 );
93 BOOST_CHECK_EQUAL( img.height(), 600 );
97 ifstream in( bmp_filename.c_str(), ios::binary );
100 read_image( in, img, tag_t() );
102 BOOST_CHECK_EQUAL( img.width() , 1000 );
103 BOOST_CHECK_EQUAL( img.height(), 600 );
107 FILE* file = fopen( bmp_filename.c_str(), "rb" );
110 read_image( file, img, tag_t() );
112 BOOST_CHECK_EQUAL( img.width() , 1000 );
113 BOOST_CHECK_EQUAL( img.height(), 600 );
117 fs::path my_path( bmp_filename );
120 read_image( my_path, img, tag_t() );
122 BOOST_CHECK_EQUAL( img.width() , 1000 );
123 BOOST_CHECK_EQUAL( img.height(), 600 );
127 BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
131 read_and_convert_image( bmp_filename, img, tag_t() );
133 BOOST_CHECK_EQUAL( img.width() , 1000 );
134 BOOST_CHECK_EQUAL( img.height(), 600 );
138 ifstream in( bmp_filename.c_str(), ios::binary );
141 read_and_convert_image( in, img, tag_t() );
143 BOOST_CHECK_EQUAL( img.width() , 1000 );
144 BOOST_CHECK_EQUAL( img.height(), 600 );
148 FILE* file = fopen( bmp_filename.c_str(), "rb" );
151 read_and_convert_image( file, img, tag_t() );
153 BOOST_CHECK_EQUAL( img.width() , 1000 );
154 BOOST_CHECK_EQUAL( img.height(), 600 );
158 BOOST_AUTO_TEST_CASE( read_view_test )
161 rgb8_image_t img( 1000, 600 );
162 read_view( bmp_filename, view( img ), tag_t() );
166 ifstream in( bmp_filename.c_str(), ios::binary );
168 rgb8_image_t img( 1000, 600 );
169 read_view( in, view( img ), tag_t() );
173 FILE* file = fopen( bmp_filename.c_str(), "rb" );
175 rgb8_image_t img( 1000, 600 );
176 read_view( file, view( img ), tag_t() );
180 BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
183 rgb8_image_t img( 1000, 600 );
184 read_and_convert_view( bmp_filename, view( img ), tag_t() );
188 ifstream in( bmp_filename.c_str(), ios::binary );
190 rgb8_image_t img( 1000, 600 );
191 read_and_convert_view( in, view( img ), tag_t() );
195 FILE* file = fopen( bmp_filename.c_str(), "rb" );
197 rgb8_image_t img( 1000, 600 );
198 read_and_convert_view( file
205 BOOST_AUTO_TEST_CASE( write_view_test )
207 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
209 string filename( bmp_out + "write_test_string.bmp" );
212 , create_mandel_view( 1000, 600
213 , rgb8_pixel_t( 0, 0, 255 )
214 , rgb8_pixel_t( 0, 255, 0 )
221 string filename( bmp_out + "write_test_ofstream.bmp" );
223 ofstream out( filename.c_str(), ios::binary );
226 , create_mandel_view( 1000, 600
227 , rgb8_pixel_t( 0, 0, 255 )
228 , rgb8_pixel_t( 0, 255, 0 )
235 string filename( bmp_out + "write_test_file.bmp" );
237 FILE* file = fopen( filename.c_str(), "wb" );
240 , create_mandel_view( 1000, 600
241 , rgb8_pixel_t( 0, 0, 255 )
242 , rgb8_pixel_t( 0, 255, 0 )
249 string filename( bmp_out + "write_test_info.bmp" );
251 image_write_info< tag_t > info;
253 FILE* file = fopen( filename.c_str(), "wb" );
256 , create_mandel_view( 1000, 600
257 , rgb8_pixel_t( 0, 0, 255 )
258 , rgb8_pixel_t( 0, 255, 0 )
264 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
267 BOOST_AUTO_TEST_CASE( stream_test )
270 ifstream in( bmp_filename.c_str(), ios::binary );
273 read_image( in, img, tag_t() );
275 // 2. Write image to in-memory buffer.
276 stringstream out_buffer( ios_base::in | ios_base::out | ios_base::binary );
277 write_view( out_buffer, view( img ), tag_t() );
279 // 3. Copy in-memory buffer to another.
280 stringstream in_buffer( ios_base::in | ios_base::out | ios_base::binary );
281 in_buffer << out_buffer.rdbuf();
283 // 4. Read in-memory buffer to gil image
285 read_image( in_buffer, dst, tag_t() );
287 // 5. Write out image.
288 string filename( bmp_out + "stream_test.bmp" );
289 ofstream out( filename.c_str(), ios_base::binary );
291 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
292 write_view( out, view( dst ), tag_t() );
293 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
296 BOOST_AUTO_TEST_CASE( stream_test_2 )
299 if( !in_buf.open( bmp_filename.c_str(), ios::in | ios::binary ) )
301 BOOST_CHECK( false );
304 istream in( &in_buf );
307 read_image( in, img, tag_t() );
310 BOOST_AUTO_TEST_CASE( subimage_test )
312 run_subimage_test< rgb8_image_t, tag_t >( bmp_filename
317 run_subimage_test< rgb8_image_t, tag_t >( bmp_filename
323 BOOST_AUTO_TEST_CASE( dynamic_image_test )
325 using my_img_types = mpl::vector
333 any_image< my_img_types > runtime_image;
335 read_image( bmp_filename.c_str()
340 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
341 write_view( bmp_out + "dynamic_image_test.bmp"
342 , view( runtime_image )
345 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
348 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
350 BOOST_AUTO_TEST_SUITE_END()