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