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