• 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_GIL_IO_ADD_FS_PATH_SUPPORT
9 #define BOOST_FILESYSTEM_VERSION 3
10 #include <boost/gil.hpp>
11 #include <boost/gil/extension/io/bmp.hpp>
12 
13 #include <boost/mp11.hpp>
14 #include <boost/core/lightweight_test.hpp>
15 
16 #include <fstream>
17 #include <sstream>
18 #include <string>
19 
20 #include "mandel_view.hpp"
21 #include "paths.hpp"
22 #include "subimage_test.hpp"
23 
24 namespace fs = boost::filesystem;
25 namespace gil = boost::gil;
26 namespace mp11 = boost::mp11;
27 
test_read_image_info_using_string()28 void test_read_image_info_using_string()
29 {
30     {
31         using backend_t = gil::get_reader_backend<std::string const, gil::bmp_tag>::type;
32         backend_t backend = read_image_info(bmp_filename, gil::bmp_tag());
33 
34         BOOST_TEST_EQ(backend._info._width, 1000);
35         BOOST_TEST_EQ(backend._info._height, 600);
36     }
37     {
38         std::ifstream in(bmp_filename.c_str(), std::ios::binary);
39         using backend_t = gil::get_reader_backend<std::ifstream, gil::bmp_tag>::type;
40         backend_t backend = read_image_info(in, gil::bmp_tag());
41 
42         BOOST_TEST_EQ(backend._info._width, 1000);
43         BOOST_TEST_EQ(backend._info._height, 600);
44     }
45     {
46         FILE *file = fopen(bmp_filename.c_str(), "rb");
47         using backend_t = gil::get_reader_backend<FILE *, gil::bmp_tag>::type;
48         backend_t backend = read_image_info(file, gil::bmp_tag());
49 
50         BOOST_TEST_EQ(backend._info._width, 1000);
51         BOOST_TEST_EQ(backend._info._height, 600);
52     }
53     {
54         fs::path my_path(bmp_filename);
55         using backend_t = gil::get_reader_backend<fs::path, gil::bmp_tag>::type;
56         backend_t backend = read_image_info(my_path, gil::bmp_tag());
57 
58         BOOST_TEST_EQ(backend._info._width, 1000);
59         BOOST_TEST_EQ(backend._info._height, 600);
60     }
61 }
62 
test_read_image()63 void test_read_image()
64 {
65     {
66         gil::rgb8_image_t img;
67         read_image(bmp_filename, img, gil::bmp_tag());
68 
69         BOOST_TEST_EQ(img.width(), 1000);
70         BOOST_TEST_EQ(img.height(), 600);
71     }
72     {
73         std::ifstream in(bmp_filename.c_str(), std::ios::binary);
74 
75         gil::rgb8_image_t img;
76         read_image(in, img, gil::bmp_tag());
77 
78         BOOST_TEST_EQ(img.width(), 1000);
79         BOOST_TEST_EQ(img.height(), 600);
80     }
81     {
82         FILE *file = fopen(bmp_filename.c_str(), "rb");
83 
84         gil::rgb8_image_t img;
85         read_image(file, img, gil::bmp_tag());
86 
87         BOOST_TEST_EQ(img.width(), 1000);
88         BOOST_TEST_EQ(img.height(), 600);
89     }
90     {
91         fs::path my_path(bmp_filename);
92 
93         gil::rgb8_image_t img;
94         read_image(my_path, img, gil::bmp_tag());
95 
96         BOOST_TEST_EQ(img.width(), 1000);
97         BOOST_TEST_EQ(img.height(), 600);
98     }
99 }
100 
test_read_and_convert_image()101 void test_read_and_convert_image()
102 {
103     {
104         gil::rgb8_image_t img;
105         read_and_convert_image(bmp_filename, img, gil::bmp_tag());
106 
107         BOOST_TEST_EQ(img.width(), 1000);
108         BOOST_TEST_EQ(img.height(), 600);
109     }
110     {
111         std::ifstream in(bmp_filename.c_str(), std::ios::binary);
112 
113         gil::rgb8_image_t img;
114         read_and_convert_image(in, img, gil::bmp_tag());
115 
116         BOOST_TEST_EQ(img.width(), 1000);
117         BOOST_TEST_EQ(img.height(), 600);
118     }
119     {
120         FILE *file = fopen(bmp_filename.c_str(), "rb");
121 
122         gil::rgb8_image_t img;
123         read_and_convert_image(file, img, gil::bmp_tag());
124 
125         BOOST_TEST_EQ(img.width(), 1000);
126         BOOST_TEST_EQ(img.height(), 600);
127     }
128 }
129 
test_read_view()130 void test_read_view()
131 {
132     {
133         gil::rgb8_image_t img(1000, 600);
134         read_view(bmp_filename, gil::view(img), gil::bmp_tag());
135     }
136     {
137         std::ifstream in(bmp_filename.c_str(), std::ios::binary);
138 
139         gil::rgb8_image_t img(1000, 600);
140         read_view(in, gil::view(img), gil::bmp_tag());
141     }
142     {
143         FILE *file = fopen(bmp_filename.c_str(), "rb");
144 
145         gil::rgb8_image_t img(1000, 600);
146         read_view(file, gil::view(img), gil::bmp_tag());
147     }
148 }
149 
test_read_and_convert_view()150 void test_read_and_convert_view()
151 {
152     {
153         gil::rgb8_image_t img(1000, 600);
154         read_and_convert_view(bmp_filename, gil::view(img), gil::bmp_tag());
155     }
156     {
157         std::ifstream in(bmp_filename.c_str(), std::ios::binary);
158 
159         gil::rgb8_image_t img(1000, 600);
160         read_and_convert_view(in, gil::view(img), gil::bmp_tag());
161     }
162     {
163         FILE *file = fopen(bmp_filename.c_str(), "rb");
164 
165         gil::rgb8_image_t img(1000, 600);
166         read_and_convert_view(file, gil::view(img), gil::bmp_tag());
167     }
168 }
169 
test_write_view()170 void test_write_view()
171 {
172     auto const b = gil::rgb8_pixel_t(0, 0, 255);
173     auto const g = gil::rgb8_pixel_t(0, 255, 0);
174     {
175         std::string filename(bmp_out + "write_test_string.bmp");
176         gil::write_view(filename, create_mandel_view(1000, 600, b, g), gil::bmp_tag());
177     }
178     {
179         std::string filename(bmp_out + "write_test_ofstream.bmp");
180         std::ofstream out(filename.c_str(), std::ios::binary);
181 
182         gil::write_view(out, create_mandel_view(1000, 600, b, g), gil::bmp_tag());
183     }
184     {
185         std::string filename(bmp_out + "write_test_file.bmp");
186 
187         FILE *file = fopen(filename.c_str(), "wb");
188         gil::write_view(file, create_mandel_view(1000, 600, b, g), gil::bmp_tag());
189     }
190     {
191         std::string filename(bmp_out + "write_test_info.bmp");
192 
193         gil::image_write_info<gil::bmp_tag> info;
194         FILE *file = fopen(filename.c_str(), "wb");
195         gil::write_view(file, create_mandel_view(1000, 600, b, g), info);
196     }
197 }
198 
test_stream()199 void test_stream()
200 {
201     // 1. Read an image.
202     std::ifstream in(bmp_filename.c_str(), std::ios::binary);
203 
204     gil::rgb8_image_t img;
205     gil::read_image(in, img, gil::bmp_tag());
206 
207     // 2. Write image to in-memory buffer.
208     std::stringstream out_buffer(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
209     gil::write_view(out_buffer, gil::view(img), gil::bmp_tag());
210 
211     // 3. Copy in-memory buffer to another.
212     std::stringstream in_buffer(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
213     in_buffer << out_buffer.rdbuf();
214 
215     // 4. Read in-memory buffer to gil image
216     gil::rgb8_image_t dst;
217     gil::read_image(in_buffer, dst, gil::bmp_tag());
218 
219     // 5. Write out image.
220     std::string filename(bmp_out + "stream_test.bmp");
221     std::ofstream out(filename.c_str(), std::ios_base::binary);
222 
223     gil::write_view( out, gil::view( dst ), gil::bmp_tag() );
224 }
225 
test_stream_2()226 void test_stream_2()
227 {
228     std::filebuf in_buf;
229     if (!in_buf.open(bmp_filename.c_str(), std::ios::in | std::ios::binary))
230     {
231         BOOST_TEST(false);
232     }
233     std::istream in(&in_buf);
234 
235     gil::rgb8_image_t img;
236     read_image(in, img, gil::bmp_tag());
237 }
238 
test_subimage()239 void test_subimage()
240 {
241     run_subimage_test<gil::rgb8_image_t, gil::bmp_tag>(
242         bmp_filename, gil::point_t(0, 0), gil::point_t(1000, 1));
243 
244     run_subimage_test<gil::rgb8_image_t, gil::bmp_tag>(
245         bmp_filename, gil::point_t(39, 7), gil::point_t(50, 50));
246 }
247 
test_dynamic_image()248 void test_dynamic_image()
249 {
250     using my_img_types = mp11::mp_list
251         <
252             gil::gray8_image_t,
253             gil::gray16_image_t,
254             gil::rgb8_image_t,
255             gil::rgba8_image_t
256         >;
257 
258     gil::any_image<my_img_types> image;
259     gil::read_image(bmp_filename.c_str(), image, gil::bmp_tag());
260 
261     gil::write_view(bmp_out + "dynamic_image_test.bmp", gil::view(image), gil::bmp_tag());
262 }
263 
main(int argc,char * argv[])264 int main(int argc, char *argv[])
265 {
266     try
267     {
268         test_read_image_info_using_string();
269         test_read_image();
270         test_read_and_convert_image();
271         test_read_view();
272         test_read_and_convert_view();
273         test_write_view();
274         test_stream();
275         test_stream_2();
276         test_subimage();
277         test_dynamic_image();
278     }
279     catch (std::exception const& e)
280     {
281         BOOST_ERROR(e.what());
282     }
283     return boost::report_errors();
284 }
285