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