Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / gil / test / extension / io / targa_test.cpp
1 //
2 // Copyright 2013 Christian Henning
3 //
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
7 //
8 #define BOOST_TEST_MODULE targa_test
9 #define BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
10
11 #include <boost/gil.hpp>
12 #include <boost/gil/extension/io/targa.hpp>
13
14 #include <boost/mp11.hpp>
15 #include <boost/test/unit_test.hpp>
16
17 #include <fstream>
18
19 #include "mandel_view.hpp"
20 #include "paths.hpp"
21 #include "subimage_test.hpp"
22
23 using namespace std;
24 using namespace boost;
25 using namespace gil;
26 namespace fs = boost::filesystem;
27
28 using tag_t = targa_tag;
29
30 BOOST_AUTO_TEST_SUITE( gil_io_targa_tests )
31
32 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
33
34 BOOST_AUTO_TEST_CASE( read_image_info_using_string )
35 {
36
37     {
38         using backend_t = get_reader_backend<std::string const, tag_t>::type;
39
40         backend_t backend = read_image_info( targa_filename
41                                            , tag_t()
42                                            );
43
44         BOOST_CHECK_EQUAL( backend._info._width , 124 );
45         BOOST_CHECK_EQUAL( backend._info._height, 124 );
46     }
47
48     {
49         ifstream in( targa_filename.c_str(), ios::binary );
50
51         using backend_t = get_reader_backend<ifstream, tag_t>::type;
52
53         backend_t backend = read_image_info( in
54                                            , tag_t()
55                                            );
56
57         BOOST_CHECK_EQUAL( backend._info._width , 124 );
58         BOOST_CHECK_EQUAL( backend._info._height, 124 );
59     }
60
61     {
62         FILE* file = fopen( targa_filename.c_str(), "rb" );
63
64         using backend_t = get_reader_backend<FILE*, tag_t>::type;
65
66         backend_t backend = read_image_info( file
67                                            , tag_t()
68                                            );
69
70         BOOST_CHECK_EQUAL( backend._info._width , 124 );
71         BOOST_CHECK_EQUAL( backend._info._height, 124 );
72     }
73
74     {
75         fs::path my_path( targa_filename );
76
77         using backend_t = get_reader_backend<fs::path, tag_t>::type;
78
79         backend_t backend = read_image_info( my_path
80                                            , tag_t()
81                                            );
82
83         BOOST_CHECK_EQUAL( backend._info._width , 124 );
84         BOOST_CHECK_EQUAL( backend._info._height, 124  );
85     }
86 }
87
88 BOOST_AUTO_TEST_CASE( read_image_test )
89 {
90     {
91         rgb8_image_t img;
92         read_image( targa_filename, img, tag_t() );
93
94         BOOST_CHECK_EQUAL( img.width() , 124 );
95         BOOST_CHECK_EQUAL( img.height(), 124 );
96     }
97
98     {
99         ifstream in( targa_filename.c_str(), ios::binary );
100
101         rgb8_image_t img;
102         read_image( in, img, tag_t() );
103
104         BOOST_CHECK_EQUAL( img.width() , 124 );
105         BOOST_CHECK_EQUAL( img.height(), 124 );
106     }
107
108     {
109         FILE* file = fopen( targa_filename.c_str(), "rb" );
110
111         rgb8_image_t img;
112         read_image( file, img, tag_t() );
113
114         BOOST_CHECK_EQUAL( img.width() , 124 );
115         BOOST_CHECK_EQUAL( img.height(), 124 );
116     }
117 }
118
119 BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
120 {
121     {
122         rgb8_image_t img;
123         read_and_convert_image( targa_filename, img, tag_t() );
124
125         BOOST_CHECK_EQUAL( img.width() , 124 );
126         BOOST_CHECK_EQUAL( img.height(), 124 );
127     }
128
129     {
130         ifstream in( targa_filename.c_str(), ios::binary );
131
132         rgb8_image_t img;
133         read_and_convert_image( in, img, tag_t() );
134
135         BOOST_CHECK_EQUAL( img.width() , 124 );
136         BOOST_CHECK_EQUAL( img.height(), 124 );
137     }
138
139     {
140         FILE* file = fopen( targa_filename.c_str(), "rb" );
141
142         rgb8_image_t img;
143         read_and_convert_image( file, img, tag_t() );
144
145         BOOST_CHECK_EQUAL( img.width() , 124 );
146         BOOST_CHECK_EQUAL( img.height(), 124 );
147     }
148 }
149
150 BOOST_AUTO_TEST_CASE( read_view_test )
151 {
152     {
153         rgb8_image_t img( 124, 124 );
154         read_view( targa_filename, view( img ), tag_t() );
155     }
156
157     {
158         ifstream in( targa_filename.c_str(), ios::binary );
159
160         rgb8_image_t img( 124, 124 );
161         read_view( in, view( img ), tag_t() );
162     }
163
164     {
165         FILE* file = fopen( targa_filename.c_str(), "rb" );
166
167         rgb8_image_t img( 124, 124 );
168         read_view( file, view( img ), tag_t() );
169     }
170 }
171
172 BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
173 {
174     {
175         rgb8_image_t img( 124, 124 );
176         read_and_convert_view( targa_filename, view( img ), tag_t() );
177     }
178
179     {
180         ifstream in( targa_filename.c_str(), ios::binary );
181
182         rgb8_image_t img( 124, 124 );
183         read_and_convert_view( in, view( img ), tag_t() );
184     }
185
186     {
187         FILE* file = fopen( targa_filename.c_str(), "rb" );
188
189         rgb8_image_t img( 124, 124 );
190         read_and_convert_view( file
191                              , view( img )
192                              , tag_t()
193                              );
194     }
195 }
196
197 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
198
199 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
200 BOOST_AUTO_TEST_CASE( write_view_test )
201 {
202     {
203         string filename( targa_out + "write_test_ofstream.tga" );
204
205         ofstream out( filename.c_str(), ios::binary );
206
207         write_view( out
208                   , create_mandel_view( 124, 124
209                                       , rgb8_pixel_t( 0,   0, 255 )
210                                       , rgb8_pixel_t( 0, 255,   0 )
211                                       )
212                   , tag_t()
213                   );
214     }
215
216     {
217         string filename( targa_out + "write_test_file.tga" );
218
219         FILE* file = fopen( filename.c_str(), "wb" );
220
221         write_view( file
222                   , create_mandel_view( 124, 124
223                                       , rgb8_pixel_t( 0,   0, 255 )
224                                       , rgb8_pixel_t( 0, 255,   0 )
225                                       )
226                   , tag_t()
227                   );
228     }
229
230     {
231         string filename( targa_out + "write_test_info.tga" );
232
233         image_write_info< tag_t > info;
234
235         FILE* file = fopen( filename.c_str(), "wb" );
236
237         write_view( file
238                   , create_mandel_view( 124, 124
239                                       , rgb8_pixel_t( 0,   0, 255 )
240                                       , rgb8_pixel_t( 0, 255,   0 )
241                                       )
242                   , info
243                   );
244     }
245 }
246 #endif //BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
247
248 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
249 BOOST_AUTO_TEST_CASE( stream_test )
250 {
251     // 1. Read an image.
252     ifstream in( targa_filename.c_str(), ios::binary );
253
254     rgb8_image_t img;
255     read_image( in, img, tag_t() );
256
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() );
260
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();
264
265     // 4. Read in-memory buffer to gil image
266     rgb8_image_t dst;
267     read_image( in_buffer, dst, tag_t() );
268
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
275 }
276
277 BOOST_AUTO_TEST_CASE( stream_test_2 )
278 {
279     filebuf in_buf;
280     if( !in_buf.open( targa_filename.c_str(), ios::in | ios::binary ) )
281     {
282         BOOST_CHECK( false );
283     }
284
285     istream in( &in_buf );
286
287     rgb8_image_t img;
288     read_image( in, img, tag_t() );
289 }
290
291 BOOST_AUTO_TEST_CASE( subimage_test )
292 {
293     run_subimage_test< rgb8_image_t, tag_t >( targa_filename
294                                             , point_t(  0,  0 )
295                                             , point_t( 50, 50 )
296                                             );
297
298     // not working
299     //run_subimage_test< rgb8_image_t, tag_t >( targa_filename
300     //                                        , point_t( 39,  7 )
301     //                                        , point_t( 50, 50 )
302     //                                        );
303 }
304
305 BOOST_AUTO_TEST_CASE( dynamic_image_test )
306 {
307     using my_img_types = mp11::mp_list
308         <
309             gray8_image_t,
310             gray16_image_t,
311             rgb8_image_t,
312             rgba8_image_t
313         >;
314
315     any_image< my_img_types > runtime_image;
316
317     read_image( targa_filename.c_str()
318               , runtime_image
319               , tag_t()
320               );
321
322 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
323     write_view( targa_out + "dynamic_image_test.tga"
324               , view( runtime_image )
325               , tag_t()
326               );
327 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
328 }
329
330 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
331
332 BOOST_AUTO_TEST_SUITE_END()