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 png_test
9 //#define BOOST_GIL_IO_PNG_FLOATING_POINT_SUPPORTED
10 //#define BOOST_GIL_IO_PNG_FIXED_POINT_SUPPORTED
12 #include <boost/gil.hpp>
13 #include <boost/gil/extension/io/png.hpp>
15 #include <boost/test/unit_test.hpp>
16 #include <boost/type_traits/is_same.hpp>
20 #include "mandel_view.hpp"
22 #include "subimage_test.hpp"
25 using namespace boost;
28 using tag_t = png_tag;
30 BOOST_AUTO_TEST_SUITE( gil_io_png_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( png_filename
43 BOOST_CHECK_EQUAL( backend._info._width , 1000u );
44 BOOST_CHECK_EQUAL( backend._info._height, 600u );
48 ifstream in( png_filename.c_str(), ios::binary );
50 using backend_t = get_reader_backend<ifstream, tag_t>::type;
52 backend_t backend = read_image_info( in
56 BOOST_CHECK_EQUAL( backend._info._width , 1000u );
57 BOOST_CHECK_EQUAL( backend._info._height, 600u );
61 FILE* file = fopen( png_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 , 1000u );
70 BOOST_CHECK_EQUAL( backend._info._height, 600u );
74 BOOST_AUTO_TEST_CASE( read_image_test )
78 read_image( png_filename, img, tag_t() );
80 BOOST_CHECK_EQUAL( img.width() , 1000u );
81 BOOST_CHECK_EQUAL( img.height(), 600u );
85 ifstream in( png_filename.c_str(), ios::binary );
88 read_image( in, img, tag_t() );
90 BOOST_CHECK_EQUAL( img.width() , 1000u );
91 BOOST_CHECK_EQUAL( img.height(), 600u );
95 FILE* file = fopen( png_filename.c_str(), "rb" );
98 read_image( file, img, tag_t() );
100 BOOST_CHECK_EQUAL( img.width() , 1000u );
101 BOOST_CHECK_EQUAL( img.height(), 600u );
105 BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
109 read_and_convert_image( png_filename, img, tag_t() );
111 BOOST_CHECK_EQUAL( img.width() , 1000u );
112 BOOST_CHECK_EQUAL( img.height(), 600u );
117 read_and_convert_image( png_filename, img, tag_t() );
119 BOOST_CHECK_EQUAL( img.width() , 1000u );
120 BOOST_CHECK_EQUAL( img.height(), 600u );
124 ifstream in( png_filename.c_str(), ios::binary );
127 read_and_convert_image( in, img, tag_t() );
129 BOOST_CHECK_EQUAL( img.width() , 1000u );
130 BOOST_CHECK_EQUAL( img.height(), 600u );
134 FILE* file = fopen( png_filename.c_str(), "rb" );
137 read_and_convert_image( file, img, tag_t() );
139 BOOST_CHECK_EQUAL( img.width() , 1000u );
140 BOOST_CHECK_EQUAL( img.height(), 600u );
144 BOOST_AUTO_TEST_CASE( read_view_test )
147 rgba8_image_t img( 1000, 600 );
148 read_view( png_filename, view( img ), tag_t() );
152 ifstream in( png_filename.c_str(), ios::binary );
154 rgba8_image_t img( 1000, 600 );
155 read_view( in, view( img ), tag_t() );
159 FILE* file = fopen( png_filename.c_str(), "rb" );
161 rgba8_image_t img( 1000, 600 );
162 read_view( file, view( img ), tag_t() );
166 BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
169 rgb8_image_t img( 1000, 600 );
170 read_and_convert_view( png_filename, view( img ), tag_t() );
174 ifstream in( png_filename.c_str(), ios::binary );
176 rgb8_image_t img( 1000, 600 );
177 read_and_convert_view( in, view( img ), tag_t() );
181 FILE* file = fopen( png_filename.c_str(), "rb" );
183 rgb8_image_t img( 1000, 600 );
185 read_and_convert_view( file
192 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
194 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
195 BOOST_AUTO_TEST_CASE( write_view_test )
198 string filename( png_out + "write_test_string.png" );
201 , create_mandel_view( 320, 240
202 , rgb8_pixel_t( 0, 0, 255 )
203 , rgb8_pixel_t( 0, 255, 0 )
210 string filename( png_out + "write_test_string_bgr.png" );
213 , create_mandel_view( 320, 240
214 , bgr8_pixel_t( 255, 0, 0 )
215 , bgr8_pixel_t( 0, 255, 0 )
222 string filename( png_out + "write_test_ofstream.png" );
224 ofstream out( filename.c_str(), ios::out | ios::binary );
227 , create_mandel_view( 320, 240
228 , rgb8_pixel_t( 0, 0, 255 )
229 , rgb8_pixel_t( 0, 255, 0 )
236 string filename( png_out + "write_test_file.png" );
238 FILE* file = fopen( filename.c_str(), "wb" );
241 , create_mandel_view( 320, 240
242 , rgb8_pixel_t( 0, 0, 255 )
243 , rgb8_pixel_t( 0, 255, 0 )
250 string filename( png_out + "write_test_info.png" );
251 FILE* file = fopen( filename.c_str(), "wb" );
253 image_write_info< png_tag > info;
256 , create_mandel_view( 320, 240
257 , rgb8_pixel_t( 0, 0, 255 )
258 , rgb8_pixel_t( 0, 255, 0 )
264 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
266 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
267 BOOST_AUTO_TEST_CASE( stream_test )
270 ifstream in( png_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( png_out + "stream_test.png" );
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( png_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< rgba8_image_t, tag_t >( png_filename
318 run_subimage_test< rgba8_image_t, tag_t >( png_filename
324 BOOST_AUTO_TEST_CASE( dynamic_image_test )
326 using my_img_types = mpl::vector
334 any_image< my_img_types > runtime_image;
336 read_image( png_filename.c_str()
341 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
342 write_view( png_out + "dynamic_image_test.png"
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()