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 jpeg_test
9 #define BOOST_FILESYSTEM_VERSION 3
10 #define BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
12 #include <boost/gil.hpp>
14 #include <boost/gil/extension/io/jpeg.hpp>
16 #include <boost/test/unit_test.hpp>
20 #include "mandel_view.hpp"
22 #include "subimage_test.hpp"
24 using namespace boost;
27 using tag_t = jpeg_tag;
29 BOOST_AUTO_TEST_SUITE( gil_io_jpeg_tests )
31 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
33 BOOST_AUTO_TEST_CASE( read_image_info_test )
37 using backend_t = get_reader_backend
43 backend_t backend = read_image_info( jpeg_filename
47 BOOST_CHECK_EQUAL( backend._info._width , 1000u );
48 BOOST_CHECK_EQUAL( backend._info._height, 600u );
52 std::ifstream in( jpeg_filename.c_str(), ios::binary );
54 using backend_t = get_reader_backend<std::ifstream, tag_t>::type;
56 backend_t backend = read_image_info( in
60 BOOST_CHECK_EQUAL( backend._info._width , 1000u );
61 BOOST_CHECK_EQUAL( backend._info._height, 600u );
65 FILE* file = fopen( jpeg_filename.c_str(), "rb" );
67 using backend_t = get_reader_backend<FILE*, tag_t>::type;
69 backend_t backend = boost::gil::read_image_info( file
73 BOOST_CHECK_EQUAL( backend._info._width , 1000u );
74 BOOST_CHECK_EQUAL( backend._info._height, 600u );
78 using backend_t = get_reader_backend<boost::filesystem::path, tag_t>::type;
81 boost::gil::read_image_info(
82 boost::filesystem::path(jpeg_filename),
85 BOOST_CHECK_EQUAL( backend._info._width , 1000u );
86 BOOST_CHECK_EQUAL( backend._info._height, 600u );
90 BOOST_AUTO_TEST_CASE( read_image_test )
94 read_image( jpeg_filename, img, tag_t() );
96 BOOST_CHECK_EQUAL( img.width() , 1000u );
97 BOOST_CHECK_EQUAL( img.height(), 600u );
101 std::ifstream in( jpeg_filename.c_str(), ios::binary );
104 read_image( in, img, tag_t() );
106 BOOST_CHECK_EQUAL( img.width() , 1000u );
107 BOOST_CHECK_EQUAL( img.height(), 600u );
111 FILE* file = fopen( jpeg_filename.c_str(), "rb" );
114 read_image( file, img, tag_t() );
116 BOOST_CHECK_EQUAL( img.width() , 1000u );
117 BOOST_CHECK_EQUAL( img.height(), 600u );
123 image_read_settings< jpeg_tag > settings( point_t( 0, 0 )
125 , jpeg_dct_method::slow
128 read_image( jpeg_filename, img, settings );
130 BOOST_CHECK_EQUAL( img.width() , 10u );
131 BOOST_CHECK_EQUAL( img.height(), 10u );
136 BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
140 read_and_convert_image( jpeg_filename, img, tag_t() );
144 std::ifstream in( jpeg_filename.c_str(), ios::binary );
147 read_and_convert_image( in, img, tag_t() );
151 BOOST_AUTO_TEST_CASE( read_view_test )
154 rgb8_image_t img( 1000, 600 );
155 read_view( jpeg_filename, view( img ), tag_t() );
159 std::ifstream in( jpeg_filename.c_str(), ios::binary );
161 rgb8_image_t img( 1000, 600 );
162 read_view( in, view( img ), tag_t() );
166 FILE* file = fopen( jpeg_filename.c_str(), "rb" );
168 rgb8_image_t img( 1000, 600 );
169 read_view( file, view( img ), tag_t() );
172 BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
175 rgb8_image_t img( 1000, 600 );
176 read_and_convert_view( jpeg_filename, view( img ), tag_t() );
180 std::ifstream in( jpeg_filename.c_str(), ios::binary );
182 rgb8_image_t img( 1000, 600 );
183 read_and_convert_view( in, view( img ), tag_t() );
187 FILE* file = fopen( jpeg_filename.c_str(), "rb" );
189 rgb8_image_t img( 1000, 600 );
190 read_and_convert_view( file
197 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
198 BOOST_AUTO_TEST_CASE( write_view_test )
201 std::string filename( jpeg_out + "write_test_string.jpg" );
204 , create_mandel_view( 320, 240
205 , rgb8_pixel_t( 0, 0, 255 )
206 , rgb8_pixel_t( 0, 255, 0 )
213 std::string filename( jpeg_out + "write_test_ofstream.jpg" );
214 std::ofstream out( filename.c_str(), ios::binary );
217 , create_mandel_view( 320, 240
218 , rgb8_pixel_t( 0, 0, 255 )
219 , rgb8_pixel_t( 0, 255, 0 )
226 std::string filename( jpeg_out + "write_test_file.jpg" );
227 FILE* file = fopen( filename.c_str(), "wb" );
230 , create_mandel_view( 320, 240
231 , rgb8_pixel_t( 0, 0, 255 )
232 , rgb8_pixel_t( 0, 255, 0 )
239 std::string filename( jpeg_out + "write_test_info.jpg" );
240 FILE* file = fopen( filename.c_str(), "wb" );
242 image_write_info< jpeg_tag > info;
244 , create_mandel_view( 320, 240
245 , rgb8_pixel_t( 0, 0, 255 )
246 , rgb8_pixel_t( 0, 255, 0 )
252 #endif //BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
254 BOOST_AUTO_TEST_CASE( stream_test )
257 std::ifstream in( jpeg_filename.c_str(), ios::binary );
260 read_image( in, img, tag_t() );
262 // 2. Write image to in-memory buffer.
263 std::stringstream out_buffer( ios_base::in | ios_base::out | ios_base::binary );
264 write_view( out_buffer, view( img ), tag_t() );
266 // 3. Copy in-memory buffer to another.
267 std::stringstream in_buffer( ios_base::in | ios_base::out | ios_base::binary );
268 in_buffer << out_buffer.rdbuf();
270 // 4. Read in-memory buffer to gil image
272 read_image( in_buffer, dst, tag_t() );
274 // 5. Write out image.
275 std::string filename( jpeg_out + "stream_test.jpg" );
276 std::ofstream out( filename.c_str(), ios_base::binary );
277 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
278 write_view( out, view( dst ), tag_t() );
279 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
282 BOOST_AUTO_TEST_CASE( stream_test_2 )
285 if( !in_buf.open( jpeg_filename.c_str(), ios::in | ios::binary ) )
287 BOOST_CHECK( false );
290 std::istream in( &in_buf );
293 read_image( in, img, tag_t() );
296 BOOST_AUTO_TEST_CASE( subimage_test )
298 run_subimage_test< rgb8_image_t, tag_t >( jpeg_filename
303 run_subimage_test< rgb8_image_t, tag_t >( jpeg_filename
309 BOOST_AUTO_TEST_CASE( dynamic_image_test )
311 using my_img_types = mpl::vector
320 any_image< my_img_types > runtime_image;
322 read_image( jpeg_filename.c_str()
327 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
328 write_view( jpeg_out + "old_dynamic_image_test.jpg"
329 , view( runtime_image )
332 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
335 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
337 BOOST_AUTO_TEST_SUITE_END()