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/tiff.hpp>
12 
13 #include <boost/core/lightweight_test.hpp>
14 #include <boost/mp11.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::tiff_tag>::type;
34         backend_t backend = gil::read_image_info(tiff_filename, gil::tiff_tag());
35 
36         BOOST_TEST_EQ(backend._info._width, 1000u);
37         BOOST_TEST_EQ(backend._info._height, 600u);
38     }
39     {
40         std::ifstream in(tiff_filename.c_str(), std::ios::binary);
41 
42         using backend_t   = gil::get_reader_backend<std::ifstream, gil::tiff_tag>::type;
43         backend_t backend = gil::read_image_info(in, gil::tiff_tag());
44 
45         BOOST_TEST_EQ(backend._info._width, 1000u);
46         BOOST_TEST_EQ(backend._info._height, 600u);
47     }
48     {
49         TIFF* file = TIFFOpen(tiff_filename.c_str(), "r");
50 
51         using backend_t   = gil::get_reader_backend<FILE*, gil::tiff_tag>::type;
52         backend_t backend = gil::read_image_info(file, gil::tiff_tag());
53 
54         BOOST_TEST_EQ(backend._info._width, 1000u);
55         BOOST_TEST_EQ(backend._info._height, 600u);
56     }
57     {
58         fs::path my_path(tiff_filename);
59 
60         using backend_t   = gil::get_reader_backend<fs::path, gil::tiff_tag>::type;
61         backend_t backend = gil::read_image_info(my_path, gil::tiff_tag());
62 
63         BOOST_TEST_EQ(backend._info._width, 1000u);
64         BOOST_TEST_EQ(backend._info._height, 600u);
65     }
66 }
67 
test_read_image()68 void test_read_image()
69 {
70     {
71         gil::rgba8_image_t img;
72         gil::read_image(tiff_filename, img, gil::tiff_tag());
73 
74         BOOST_TEST_EQ(img.width(), 1000u);
75         BOOST_TEST_EQ(img.height(), 600u);
76     }
77     {
78         std::ifstream in(tiff_filename.c_str(), std::ios::binary);
79 
80         gil::rgba8_image_t img;
81         gil::read_image(in, img, gil::tiff_tag());
82 
83         BOOST_TEST_EQ(img.width(), 1000u);
84         BOOST_TEST_EQ(img.height(), 600u);
85     }
86     {
87         TIFF* file = TIFFOpen(tiff_filename.c_str(), "r");
88 
89         gil::rgba8_image_t img;
90         gil::read_image(file, img, gil::tiff_tag());
91 
92         BOOST_TEST_EQ(img.width(), 1000u);
93         BOOST_TEST_EQ(img.height(), 600u);
94     }
95 }
96 
test_read_and_convert_image()97 void test_read_and_convert_image()
98 {
99     {
100         gil::rgb8_image_t img;
101         gil::read_and_convert_image(tiff_filename, img, gil::tiff_tag());
102 
103         BOOST_TEST_EQ(img.width(), 1000u);
104         BOOST_TEST_EQ(img.height(), 600u);
105     }
106     {
107         std::ifstream in(tiff_filename.c_str(), std::ios::binary);
108 
109         gil::rgb8_image_t img;
110         gil::read_and_convert_image(in, img, gil::tiff_tag());
111 
112         BOOST_TEST_EQ(img.width(), 1000u);
113         BOOST_TEST_EQ(img.height(), 600u);
114     }
115     {
116         TIFF* file = TIFFOpen(tiff_filename.c_str(), "r");
117 
118         gil::rgb8_image_t img;
119         gil::read_and_convert_image(file, img, gil::tiff_tag());
120 
121         BOOST_TEST_EQ(img.width(), 1000u);
122         BOOST_TEST_EQ(img.height(), 600u);
123     }
124 }
125 
test_read_and_convert_image_2()126 void test_read_and_convert_image_2()
127 {
128     gil::gray8_image_t img;
129     gil::read_and_convert_image(tiff_filename, img, gil::tiff_tag());
130 
131     gil::rgba8_image_t img2;
132     gil::read_image(tiff_filename, img2, gil::tiff_tag());
133 
134     BOOST_TEST(gil::equal_pixels(
135         gil::const_view(img),
136         gil::color_converted_view<gil::gray8_pixel_t>(gil::const_view(img2))));
137 }
138 
test_read_view()139 void test_read_view()
140 {
141     {
142         gil::rgba8_image_t img(1000, 600);
143         gil::read_view(tiff_filename, gil::view(img), gil::tiff_tag());
144     }
145     {
146         std::ifstream in(tiff_filename.c_str(), std::ios::binary);
147 
148         gil::rgba8_image_t img(1000, 600);
149         gil::read_view(in, gil::view(img), gil::tiff_tag());
150 
151         BOOST_TEST_EQ(img.width(), 1000u);
152         BOOST_TEST_EQ(img.height(), 600u);
153     }
154     {
155         TIFF* file = TIFFOpen(tiff_filename.c_str(), "r");
156 
157         gil::rgba8_image_t img(1000, 600);
158         gil::read_view(file, gil::view(img), gil::tiff_tag());
159     }
160 }
161 
test_read_and_convert_view()162 void test_read_and_convert_view()
163 {
164     {
165         gil::rgb8_image_t img(1000, 600);
166         gil::read_and_convert_view(tiff_filename, gil::view(img), gil::tiff_tag());
167     }
168     {
169         std::ifstream in(tiff_filename.c_str(), std::ios::binary);
170 
171         gil::rgb8_image_t img(1000, 600);
172         gil::read_and_convert_view(in, gil::view(img), gil::tiff_tag());
173 
174         BOOST_TEST_EQ(img.width(), 1000u);
175         BOOST_TEST_EQ(img.height(), 600u);
176     }
177     {
178         TIFF* file = TIFFOpen(tiff_filename.c_str(), "r");
179 
180         gil::rgb8_image_t img(1000, 600);
181         gil::read_and_convert_view(file, gil::view(img), gil::tiff_tag());
182     }
183 }
184 
test_write_view()185 void test_write_view()
186 {
187     auto const b = gil::rgb8_pixel_t(0, 0, 255);
188     auto const g = gil::rgb8_pixel_t(0, 255, 0);
189 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
190     {
191         std::string filename(tiff_out + "write_test_string.tif");
192 
193         gil::write_view(filename, create_mandel_view(320, 240, b, g), gil::tiff_tag());
194     }
195     {
196         std::string filename(tiff_out + "write_test_ofstream.tif");
197         std::ofstream out(filename.c_str(), std::ios_base::binary);
198 
199         gil::write_view(out, create_mandel_view(320, 240, b, g), gil::tiff_tag());
200     }
201     {
202         std::string filename(tiff_out + "write_test_tiff.tif");
203         TIFF* file = TIFFOpen(filename.c_str(), "w");
204 
205         gil::write_view(file, create_mandel_view(320, 240, b, g), gil::tiff_tag());
206     }
207     {
208         std::string filename(tiff_out + "write_test_info.tif");
209 
210         gil::image_write_info<gil::tiff_tag> info;
211         gil::write_view(filename, create_mandel_view(320, 240, b, g), info);
212     }
213 #endif  // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
214 }
215 
test_stream()216 void test_stream()
217 {
218     // 1. Read an image.
219     std::ifstream in(tiff_filename.c_str(), std::ios::binary);
220 
221     gil::rgba8_image_t img;
222     gil::read_image(in, img, gil::tiff_tag());
223 
224     // 2. Write image to in-memory buffer.
225     std::stringstream out_buffer(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
226     gil::write_view(out_buffer, gil::view(img), gil::tiff_tag());
227 
228     // 3. Copy in-memory buffer to another.
229     std::stringstream in_buffer(std::ios_base::in | std::ios_base::out | std::ios_base::binary);
230     in_buffer << out_buffer.rdbuf();
231 
232     // 4. Read in-memory buffer to gil image
233     gil::rgba8_image_t dst;
234     gil::read_image(in_buffer, dst, gil::tiff_tag());
235 
236     // 5. Write out image.
237     std::string filename(tiff_out + "stream_test.tif");
238     std::ofstream out(filename.c_str(), std::ios_base::binary);
239 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
240     gil::write_view(out, gil::view(dst), gil::tiff_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(tiff_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::tiff_tag());
255 }
256 
test_subimage()257 void test_subimage()
258 {
259     run_subimage_test<gil::rgba8_image_t, gil::tiff_tag>(
260         tiff_filename, gil::point_t(0, 0), gil::point_t(50, 50));
261 
262     run_subimage_test<gil::rgba8_image_t, gil::tiff_tag>(
263         tiff_filename, gil::point_t(50, 50), gil::point_t(50, 50));
264 }
265 
test_dynamic_image()266 void test_dynamic_image()
267 {
268     // FIXME: This test has been disabled for now because of compilation issues with MSVC10.
269 
270     using my_img_types = mp11::mp_list
271     <
272         gil::gray8_image_t,
273         gil::gray16_image_t,
274         gil::rgb8_image_t,
275         gil::rgba8_image_t,
276         gil::gray1_image_t
277     >;
278     gil::any_image<my_img_types> image;
279 
280     gil::read_image(tiff_filename.c_str(), image, gil::tiff_tag());
281 
282 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
283     gil::write_view(tiff_out + "dynamic_image_test.tif", gil::view(image), gil::tiff_tag());
284 #endif  // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
285 }
286 
main()287 int main()
288 {
289     test_read_image_info();
290     test_read_image();
291     test_read_and_convert_image();
292     test_read_and_convert_image_2();
293     test_read_view();
294     test_read_and_convert_view();
295     test_write_view();
296     test_stream();
297     test_stream_2();
298     test_subimage();
299     test_dynamic_image();
300 
301     return boost::report_errors();
302 }
303 
304 #else
main()305 int main() {}
306 #endif  // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
307