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