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 #include <boost/gil.hpp>
9 #include <boost/gil/extension/io/pnm.hpp>
10
11 #include <boost/mp11.hpp>
12 #include <boost/core/lightweight_test.hpp>
13
14 #include <fstream>
15 #include <sstream>
16 #include <string>
17
18 #include "mandel_view.hpp"
19 #include "paths.hpp"
20 #include "subimage_test.hpp"
21
22 namespace gil = boost::gil;
23 namespace mp11 = boost::mp11;
24
25 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
test_read_image_info_using_string()26 void test_read_image_info_using_string()
27 {
28 {
29 using backend_t = gil::get_reader_backend<std::string const, gil::pnm_tag>::type;
30 backend_t backend = gil::read_image_info(pnm_filename, gil::pnm_tag());
31
32 BOOST_TEST_EQ(backend._info._width, 256u);
33 BOOST_TEST_EQ(backend._info._height, 256u);
34 }
35 {
36 std::ifstream in(pnm_filename.c_str(), std::ios::binary);
37
38 using backend_t = gil::get_reader_backend<std::ifstream, gil::pnm_tag>::type;
39 backend_t backend = gil::read_image_info(in, gil::pnm_tag());
40
41 BOOST_TEST_EQ(backend._info._width, 256u);
42 BOOST_TEST_EQ(backend._info._height, 256u);
43 }
44 {
45 FILE* file = fopen(pnm_filename.c_str(), "rb");
46
47 using backend_t = gil::get_reader_backend<FILE*, gil::pnm_tag>::type;
48 backend_t backend = gil::read_image_info(file, gil::pnm_tag());
49
50 BOOST_TEST_EQ(backend._info._width, 256u);
51 BOOST_TEST_EQ(backend._info._height, 256u);
52 }
53 }
54
test_read_image()55 void test_read_image()
56 {
57 {
58 gil::rgb8_image_t img;
59 gil::read_image(pnm_filename, img, gil::pnm_tag());
60
61 BOOST_TEST_EQ(img.width(), 256u);
62 BOOST_TEST_EQ(img.height(), 256u);
63 }
64 {
65 std::ifstream in(pnm_filename.c_str(), std::ios::binary);
66
67 gil::rgb8_image_t img;
68 gil::read_image(in, img, gil::pnm_tag());
69
70 BOOST_TEST_EQ(img.width(), 256u);
71 BOOST_TEST_EQ(img.height(), 256u);
72 }
73 {
74 FILE* file = fopen(pnm_filename.c_str(), "rb");
75
76 gil::rgb8_image_t img;
77 gil::read_image(file, img, gil::pnm_tag());
78
79 BOOST_TEST_EQ(img.width(), 256u);
80 BOOST_TEST_EQ(img.height(), 256u);
81 }
82 }
83
test_read_and_convert_image()84 void test_read_and_convert_image()
85 {
86 {
87 gil::rgb8_image_t img;
88 gil::read_and_convert_image(pnm_filename, img, gil::pnm_tag());
89
90 BOOST_TEST_EQ(img.width(), 256u);
91 BOOST_TEST_EQ(img.height(), 256u);
92 }
93 {
94 std::ifstream in(pnm_filename.c_str(), std::ios::binary);
95
96 gil::rgb8_image_t img;
97 gil::read_and_convert_image(in, img, gil::pnm_tag());
98
99 BOOST_TEST_EQ(img.width(), 256u);
100 BOOST_TEST_EQ(img.height(), 256u);
101 }
102 {
103 FILE* file = fopen(pnm_filename.c_str(), "rb");
104
105 gil::rgb8_image_t img;
106 gil::read_and_convert_image(file, img, gil::pnm_tag());
107
108 BOOST_TEST_EQ(img.width(), 256u);
109 BOOST_TEST_EQ(img.height(), 256u);
110 }
111 }
112
test_read_view()113 void test_read_view()
114 {
115 {
116 gil::rgb8_image_t img(256, 256);
117 gil::read_view(pnm_filename, gil::view(img), gil::pnm_tag());
118 }
119 {
120 std::ifstream in(pnm_filename.c_str(), std::ios::binary);
121
122 gil::rgb8_image_t img(256, 256);
123 gil::read_view(in, gil::view(img), gil::pnm_tag());
124 }
125 {
126 FILE* file = fopen(pnm_filename.c_str(), "rb");
127
128 gil::rgb8_image_t img(256, 256);
129 gil::read_view(file, gil::view(img), gil::pnm_tag());
130 }
131 }
132
test_read_and_convert_view()133 void test_read_and_convert_view()
134 {
135 {
136 gil::rgb8_image_t img(256, 256);
137 gil::read_and_convert_view(pnm_filename, gil::view(img), gil::pnm_tag());
138 }
139 {
140 std::ifstream in(pnm_filename.c_str(), std::ios::binary);
141
142 gil::rgb8_image_t img(256, 256);
143 gil::read_and_convert_view(in, gil::view(img), gil::pnm_tag());
144 }
145 {
146 FILE* file = fopen(pnm_filename.c_str(), "rb");
147
148 gil::rgb8_image_t img(256, 256);
149
150 gil::read_and_convert_view(file, gil::view(img), gil::pnm_tag());
151 }
152 }
153
test_stream()154 void test_stream()
155 {
156 // 1. Read an image.
157 std::ifstream in(pnm_filename.c_str(), std::ios::binary);
158
159 gil::rgb8_image_t img;
160 gil::read_image(in, img, gil::pnm_tag());
161
162 // 2. Write image to in-memory buffer.
163 std::stringstream out_buffer(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
164 gil::write_view(out_buffer, gil::view(img), gil::pnm_tag());
165
166 // 3. Copy in-memory buffer to another.
167 std::stringstream in_buffer(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
168 in_buffer << out_buffer.rdbuf();
169
170 // 4. Read in-memory buffer to gil image
171 gil::rgb8_image_t dst;
172 gil::read_image(in_buffer, dst, gil::pnm_tag());
173
174 // 5. Write out image.
175 std::string filename(pnm_out + "stream_test.pnm");
176 std::ofstream out(filename.c_str(), std::ios_base::binary);
177 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
178 gil::write_view(out, gil::view(dst), gil::pnm_tag());
179 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
180 }
181
test_stream_2()182 void test_stream_2()
183 {
184 std::filebuf in_buf;
185 if (!in_buf.open(pnm_filename.c_str(), std::ios::in | std::ios::binary))
186 {
187 BOOST_TEST(false);
188 }
189
190 std::istream in(&in_buf);
191
192 gil::rgb8_image_t img;
193 gil::read_image(in, img, gil::pnm_tag());
194 }
195
test_subimage()196 void test_subimage()
197 {
198 run_subimage_test<gil::rgb8_image_t, gil::pnm_tag>(
199 pnm_filename, gil::point_t(0, 0), gil::point_t(50, 50));
200
201 run_subimage_test<gil::rgb8_image_t, gil::pnm_tag>(
202 pnm_filename, gil::point_t(103, 103), gil::point_t(50, 50));
203 }
204
test_dynamic_image_test()205 void test_dynamic_image_test()
206 {
207 using my_img_types = mp11::mp_list
208 <
209 gil::gray8_image_t,
210 gil::gray16_image_t,
211 gil::rgb8_image_t,
212 gil::gray1_image_t
213 >;
214
215 gil::any_image<my_img_types> image;
216
217 gil::read_image(pnm_filename.c_str(), image, gil::pnm_tag());
218
219 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
220 gil::write_view(pnm_out + "dynamic_image_test.pnm", gil::view(image), gil::pnm_tag());
221 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
222 }
223
main()224 int main()
225 {
226 test_read_image_info_using_string();
227 test_read_image();
228 test_read_and_convert_image();
229 test_read_view();
230 test_read_and_convert_view();
231 test_stream();
232 test_stream_2();
233 test_subimage();
234 test_dynamic_image_test();
235
236 return boost::report_errors();
237 }
238 #else
main()239 int main() {}
240 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
241