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