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/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 = targa_tag;
29 BOOST_AUTO_TEST_SUITE( gil_io_targa_tests )
31 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
33 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( targa_filename
43 BOOST_CHECK_EQUAL( backend._info._width , 124 );
44 BOOST_CHECK_EQUAL( backend._info._height, 124 );
48 ifstream in( targa_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 , 124 );
57 BOOST_CHECK_EQUAL( backend._info._height, 124 );
61 FILE* file = fopen( targa_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 , 124 );
70 BOOST_CHECK_EQUAL( backend._info._height, 124 );
74 fs::path my_path( targa_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 , 124 );
83 BOOST_CHECK_EQUAL( backend._info._height, 124 );
87 BOOST_AUTO_TEST_CASE( read_image_test )
91 read_image( targa_filename, img, tag_t() );
93 BOOST_CHECK_EQUAL( img.width() , 124 );
94 BOOST_CHECK_EQUAL( img.height(), 124 );
98 ifstream in( targa_filename.c_str(), ios::binary );
101 read_image( in, img, tag_t() );
103 BOOST_CHECK_EQUAL( img.width() , 124 );
104 BOOST_CHECK_EQUAL( img.height(), 124 );
108 FILE* file = fopen( targa_filename.c_str(), "rb" );
111 read_image( file, img, tag_t() );
113 BOOST_CHECK_EQUAL( img.width() , 124 );
114 BOOST_CHECK_EQUAL( img.height(), 124 );
118 BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
122 read_and_convert_image( targa_filename, img, tag_t() );
124 BOOST_CHECK_EQUAL( img.width() , 124 );
125 BOOST_CHECK_EQUAL( img.height(), 124 );
129 ifstream in( targa_filename.c_str(), ios::binary );
132 read_and_convert_image( in, img, tag_t() );
134 BOOST_CHECK_EQUAL( img.width() , 124 );
135 BOOST_CHECK_EQUAL( img.height(), 124 );
139 FILE* file = fopen( targa_filename.c_str(), "rb" );
142 read_and_convert_image( file, img, tag_t() );
144 BOOST_CHECK_EQUAL( img.width() , 124 );
145 BOOST_CHECK_EQUAL( img.height(), 124 );
149 BOOST_AUTO_TEST_CASE( read_view_test )
152 rgb8_image_t img( 124, 124 );
153 read_view( targa_filename, view( img ), tag_t() );
157 ifstream in( targa_filename.c_str(), ios::binary );
159 rgb8_image_t img( 124, 124 );
160 read_view( in, view( img ), tag_t() );
164 FILE* file = fopen( targa_filename.c_str(), "rb" );
166 rgb8_image_t img( 124, 124 );
167 read_view( file, view( img ), tag_t() );
171 BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
174 rgb8_image_t img( 124, 124 );
175 read_and_convert_view( targa_filename, view( img ), tag_t() );
179 ifstream in( targa_filename.c_str(), ios::binary );
181 rgb8_image_t img( 124, 124 );
182 read_and_convert_view( in, view( img ), tag_t() );
186 FILE* file = fopen( targa_filename.c_str(), "rb" );
188 rgb8_image_t img( 124, 124 );
189 read_and_convert_view( file
196 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
198 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
199 BOOST_AUTO_TEST_CASE( write_view_test )
202 string filename( targa_out + "write_test_ofstream.tga" );
204 ofstream out( filename.c_str(), ios::binary );
207 , create_mandel_view( 124, 124
208 , rgb8_pixel_t( 0, 0, 255 )
209 , rgb8_pixel_t( 0, 255, 0 )
216 string filename( targa_out + "write_test_file.tga" );
218 FILE* file = fopen( filename.c_str(), "wb" );
221 , create_mandel_view( 124, 124
222 , rgb8_pixel_t( 0, 0, 255 )
223 , rgb8_pixel_t( 0, 255, 0 )
230 string filename( targa_out + "write_test_info.tga" );
232 image_write_info< tag_t > info;
234 FILE* file = fopen( filename.c_str(), "wb" );
237 , create_mandel_view( 124, 124
238 , rgb8_pixel_t( 0, 0, 255 )
239 , rgb8_pixel_t( 0, 255, 0 )
245 #endif //BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
247 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
248 BOOST_AUTO_TEST_CASE( stream_test )
251 ifstream in( targa_filename.c_str(), ios::binary );
254 read_image( in, img, tag_t() );
256 // 2. Write image to in-memory buffer.
257 stringstream out_buffer( ios_base::in | ios_base::out | ios_base::binary );
258 write_view( out_buffer, view( img ), tag_t() );
260 // 3. Copy in-memory buffer to another.
261 stringstream in_buffer( ios_base::in | ios_base::out | ios_base::binary );
262 in_buffer << out_buffer.rdbuf();
264 // 4. Read in-memory buffer to gil image
266 read_image( in_buffer, dst, tag_t() );
268 // 5. Write out image.
269 string filename( targa_out + "stream_test.tga" );
270 ofstream out( filename.c_str(), ios_base::binary );
271 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
272 write_view( out, view( dst ), tag_t() );
273 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
276 BOOST_AUTO_TEST_CASE( stream_test_2 )
279 if( !in_buf.open( targa_filename.c_str(), ios::in | ios::binary ) )
281 BOOST_CHECK( false );
284 istream in( &in_buf );
287 read_image( in, img, tag_t() );
290 BOOST_AUTO_TEST_CASE( subimage_test )
292 run_subimage_test< rgb8_image_t, tag_t >( targa_filename
298 //run_subimage_test< rgb8_image_t, tag_t >( targa_filename
299 // , point_t( 39, 7 )
300 // , point_t( 50, 50 )
304 BOOST_AUTO_TEST_CASE( dynamic_image_test )
306 using my_img_types = mpl::vector
314 any_image< my_img_types > runtime_image;
316 read_image( targa_filename.c_str()
321 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
322 write_view( targa_out + "dynamic_image_test.tga"
323 , view( runtime_image )
326 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
329 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
331 BOOST_AUTO_TEST_SUITE_END()