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 targa_test
9 #define BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
11 #include <boost/gil.hpp>
12 #include <boost/gil/extension/io/targa.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 = targa_tag;
30 BOOST_AUTO_TEST_SUITE( gil_io_targa_tests )
32 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
34 BOOST_AUTO_TEST_CASE( read_image_info_using_string )
38 using backend_t = get_reader_backend<std::string const, tag_t>::type;
40 backend_t backend = read_image_info( targa_filename
44 BOOST_CHECK_EQUAL( backend._info._width , 124 );
45 BOOST_CHECK_EQUAL( backend._info._height, 124 );
49 ifstream in( targa_filename.c_str(), ios::binary );
51 using backend_t = get_reader_backend<ifstream, tag_t>::type;
53 backend_t backend = read_image_info( in
57 BOOST_CHECK_EQUAL( backend._info._width , 124 );
58 BOOST_CHECK_EQUAL( backend._info._height, 124 );
62 FILE* file = fopen( targa_filename.c_str(), "rb" );
64 using backend_t = get_reader_backend<FILE*, tag_t>::type;
66 backend_t backend = read_image_info( file
70 BOOST_CHECK_EQUAL( backend._info._width , 124 );
71 BOOST_CHECK_EQUAL( backend._info._height, 124 );
75 fs::path my_path( targa_filename );
77 using backend_t = get_reader_backend<fs::path, tag_t>::type;
79 backend_t backend = read_image_info( my_path
83 BOOST_CHECK_EQUAL( backend._info._width , 124 );
84 BOOST_CHECK_EQUAL( backend._info._height, 124 );
88 BOOST_AUTO_TEST_CASE( read_image_test )
92 read_image( targa_filename, img, tag_t() );
94 BOOST_CHECK_EQUAL( img.width() , 124 );
95 BOOST_CHECK_EQUAL( img.height(), 124 );
99 ifstream in( targa_filename.c_str(), ios::binary );
102 read_image( in, img, tag_t() );
104 BOOST_CHECK_EQUAL( img.width() , 124 );
105 BOOST_CHECK_EQUAL( img.height(), 124 );
109 FILE* file = fopen( targa_filename.c_str(), "rb" );
112 read_image( file, img, tag_t() );
114 BOOST_CHECK_EQUAL( img.width() , 124 );
115 BOOST_CHECK_EQUAL( img.height(), 124 );
119 BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
123 read_and_convert_image( targa_filename, img, tag_t() );
125 BOOST_CHECK_EQUAL( img.width() , 124 );
126 BOOST_CHECK_EQUAL( img.height(), 124 );
130 ifstream in( targa_filename.c_str(), ios::binary );
133 read_and_convert_image( in, img, tag_t() );
135 BOOST_CHECK_EQUAL( img.width() , 124 );
136 BOOST_CHECK_EQUAL( img.height(), 124 );
140 FILE* file = fopen( targa_filename.c_str(), "rb" );
143 read_and_convert_image( file, img, tag_t() );
145 BOOST_CHECK_EQUAL( img.width() , 124 );
146 BOOST_CHECK_EQUAL( img.height(), 124 );
150 BOOST_AUTO_TEST_CASE( read_view_test )
153 rgb8_image_t img( 124, 124 );
154 read_view( targa_filename, view( img ), tag_t() );
158 ifstream in( targa_filename.c_str(), ios::binary );
160 rgb8_image_t img( 124, 124 );
161 read_view( in, view( img ), tag_t() );
165 FILE* file = fopen( targa_filename.c_str(), "rb" );
167 rgb8_image_t img( 124, 124 );
168 read_view( file, view( img ), tag_t() );
172 BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
175 rgb8_image_t img( 124, 124 );
176 read_and_convert_view( targa_filename, view( img ), tag_t() );
180 ifstream in( targa_filename.c_str(), ios::binary );
182 rgb8_image_t img( 124, 124 );
183 read_and_convert_view( in, view( img ), tag_t() );
187 FILE* file = fopen( targa_filename.c_str(), "rb" );
189 rgb8_image_t img( 124, 124 );
190 read_and_convert_view( file
197 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
199 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
200 BOOST_AUTO_TEST_CASE( write_view_test )
203 string filename( targa_out + "write_test_ofstream.tga" );
205 ofstream out( filename.c_str(), ios::binary );
208 , create_mandel_view( 124, 124
209 , rgb8_pixel_t( 0, 0, 255 )
210 , rgb8_pixel_t( 0, 255, 0 )
217 string filename( targa_out + "write_test_file.tga" );
219 FILE* file = fopen( filename.c_str(), "wb" );
222 , create_mandel_view( 124, 124
223 , rgb8_pixel_t( 0, 0, 255 )
224 , rgb8_pixel_t( 0, 255, 0 )
231 string filename( targa_out + "write_test_info.tga" );
233 image_write_info< tag_t > info;
235 FILE* file = fopen( filename.c_str(), "wb" );
238 , create_mandel_view( 124, 124
239 , rgb8_pixel_t( 0, 0, 255 )
240 , rgb8_pixel_t( 0, 255, 0 )
246 #endif //BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
248 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
249 BOOST_AUTO_TEST_CASE( stream_test )
252 ifstream in( targa_filename.c_str(), ios::binary );
255 read_image( in, img, tag_t() );
257 // 2. Write image to in-memory buffer.
258 stringstream out_buffer( ios_base::in | ios_base::out | ios_base::binary );
259 write_view( out_buffer, view( img ), tag_t() );
261 // 3. Copy in-memory buffer to another.
262 stringstream in_buffer( ios_base::in | ios_base::out | ios_base::binary );
263 in_buffer << out_buffer.rdbuf();
265 // 4. Read in-memory buffer to gil image
267 read_image( in_buffer, dst, tag_t() );
269 // 5. Write out image.
270 string filename( targa_out + "stream_test.tga" );
271 ofstream out( filename.c_str(), ios_base::binary );
272 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
273 write_view( out, view( dst ), tag_t() );
274 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
277 BOOST_AUTO_TEST_CASE( stream_test_2 )
280 if( !in_buf.open( targa_filename.c_str(), ios::in | ios::binary ) )
282 BOOST_CHECK( false );
285 istream in( &in_buf );
288 read_image( in, img, tag_t() );
291 BOOST_AUTO_TEST_CASE( subimage_test )
293 run_subimage_test< rgb8_image_t, tag_t >( targa_filename
299 //run_subimage_test< rgb8_image_t, tag_t >( targa_filename
300 // , point_t( 39, 7 )
301 // , point_t( 50, 50 )
305 BOOST_AUTO_TEST_CASE( dynamic_image_test )
307 using my_img_types = mp11::mp_list
315 any_image< my_img_types > runtime_image;
317 read_image( targa_filename.c_str()
322 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
323 write_view( targa_out + "dynamic_image_test.tga"
324 , view( runtime_image )
327 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
330 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
332 BOOST_AUTO_TEST_SUITE_END()