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