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_TEST_MODULE pnm_test
9
10 #include <boost/gil.hpp>
11 #include <boost/gil/extension/io/pnm.hpp>
12
13 #include <boost/mp11.hpp>
14 #include <boost/test/unit_test.hpp>
15
16 #include <fstream>
17
18 #include "mandel_view.hpp"
19 #include "paths.hpp"
20 #include "subimage_test.hpp"
21
22 using namespace std;
23 using namespace boost;
24 using namespace gil;
25
26 using tag_t = pnm_tag;
27
28 BOOST_AUTO_TEST_SUITE( gil_io_pnm_tests )
29
30 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
31
BOOST_AUTO_TEST_CASE(read_image_info_using_string)32 BOOST_AUTO_TEST_CASE( read_image_info_using_string )
33 {
34 {
35 using backend_t = get_reader_backend<std::string const, tag_t>::type;
36
37 backend_t backend = read_image_info( pnm_filename
38 , tag_t()
39 );
40
41 BOOST_CHECK_EQUAL( backend._info._width , 256u );
42 BOOST_CHECK_EQUAL( backend._info._height, 256u );
43 }
44
45 {
46 ifstream in( pnm_filename.c_str(), ios::binary );
47
48 using backend_t = get_reader_backend<ifstream, tag_t>::type;
49
50 backend_t backend = read_image_info( in
51 , tag_t()
52 );
53
54 BOOST_CHECK_EQUAL( backend._info._width , 256u );
55 BOOST_CHECK_EQUAL( backend._info._height, 256u );
56 }
57
58 {
59 FILE* file = fopen( pnm_filename.c_str(), "rb" );
60
61 using backend_t = get_reader_backend<FILE*, tag_t>::type;
62
63 backend_t backend = read_image_info( file
64 , tag_t()
65 );
66
67 BOOST_CHECK_EQUAL( backend._info._width , 256u );
68 BOOST_CHECK_EQUAL( backend._info._height, 256u );
69 }
70 }
71
BOOST_AUTO_TEST_CASE(read_image_test)72 BOOST_AUTO_TEST_CASE( read_image_test )
73 {
74 {
75 rgb8_image_t img;
76 read_image( pnm_filename, img, tag_t() );
77
78 BOOST_CHECK_EQUAL( img.width() , 256u );
79 BOOST_CHECK_EQUAL( img.height(), 256u );
80 }
81
82 {
83 ifstream in( pnm_filename.c_str(), ios::binary );
84
85 rgb8_image_t img;
86 read_image( in, img, tag_t() );
87
88 BOOST_CHECK_EQUAL( img.width() , 256u );
89 BOOST_CHECK_EQUAL( img.height(), 256u );
90 }
91
92 {
93 FILE* file = fopen( pnm_filename.c_str(), "rb" );
94
95 rgb8_image_t img;
96 read_image( file, img, tag_t() );
97
98 BOOST_CHECK_EQUAL( img.width() , 256u );
99 BOOST_CHECK_EQUAL( img.height(), 256u );
100 }
101 }
102
BOOST_AUTO_TEST_CASE(read_and_convert_image_test)103 BOOST_AUTO_TEST_CASE( read_and_convert_image_test )
104 {
105 {
106 rgb8_image_t img;
107 read_and_convert_image( pnm_filename, img, tag_t() );
108
109 BOOST_CHECK_EQUAL( img.width() , 256u );
110 BOOST_CHECK_EQUAL( img.height(), 256u );
111 }
112
113 {
114 ifstream in( pnm_filename.c_str(), ios::binary );
115
116 rgb8_image_t img;
117 read_and_convert_image( in, img, tag_t() );
118
119 BOOST_CHECK_EQUAL( img.width() , 256u );
120 BOOST_CHECK_EQUAL( img.height(), 256u );
121 }
122
123 {
124 FILE* file = fopen( pnm_filename.c_str(), "rb" );
125
126 rgb8_image_t img;
127 read_and_convert_image( file, img, tag_t() );
128
129 BOOST_CHECK_EQUAL( img.width() , 256u );
130 BOOST_CHECK_EQUAL( img.height(), 256u );
131 }
132 }
133
BOOST_AUTO_TEST_CASE(read_view_test)134 BOOST_AUTO_TEST_CASE( read_view_test )
135 {
136 {
137 rgb8_image_t img( 256, 256 );
138 read_view( pnm_filename, view( img ), tag_t() );
139 }
140
141 {
142 ifstream in( pnm_filename.c_str(), ios::binary );
143
144 rgb8_image_t img( 256, 256 );
145 read_view( in, view( img ), tag_t() );
146 }
147
148 {
149 FILE* file = fopen( pnm_filename.c_str(), "rb" );
150
151 rgb8_image_t img( 256, 256 );
152 read_view( file, view( img ), tag_t() );
153 }
154 }
155
BOOST_AUTO_TEST_CASE(read_and_convert_view_test)156 BOOST_AUTO_TEST_CASE( read_and_convert_view_test )
157 {
158 {
159 rgb8_image_t img( 256, 256 );
160 read_and_convert_view( pnm_filename, view( img ), tag_t() );
161 }
162
163 {
164 ifstream in( pnm_filename.c_str(), ios::binary );
165
166 rgb8_image_t img( 256, 256 );
167 read_and_convert_view( in, view( img ), tag_t() );
168 }
169
170 {
171 FILE* file = fopen( pnm_filename.c_str(), "rb" );
172
173 rgb8_image_t img( 256, 256 );
174
175 read_and_convert_view( file
176 , view( img )
177 , tag_t()
178 );
179 }
180 }
181
182 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
183
184 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
185
BOOST_AUTO_TEST_CASE(write_view_test)186 BOOST_AUTO_TEST_CASE( write_view_test )
187 {
188 {
189 string filename( pnm_out + "write_test_string.pnm" );
190
191 write_view( filename
192 , create_mandel_view( 320, 240
193 , rgb8_pixel_t( 0, 0, 255 )
194 , rgb8_pixel_t( 0, 255, 0 )
195 )
196 , tag_t()
197 );
198 }
199
200 {
201 string filename( pnm_out + "write_test_ofstream.pnm" );
202
203 ofstream out( filename.c_str(), ios::out | ios::binary );
204
205 write_view( out
206 , create_mandel_view( 320, 240
207 , rgb8_pixel_t( 0, 0, 255 )
208 , rgb8_pixel_t( 0, 255, 0 )
209 )
210 , tag_t()
211 );
212 }
213
214 {
215 string filename( pnm_out + "write_test_file.pnm" );
216
217 FILE* file = fopen( filename.c_str(), "wb" );
218
219 write_view( file
220 , create_mandel_view( 320, 240
221 , rgb8_pixel_t( 0, 0, 255 )
222 , rgb8_pixel_t( 0, 255, 0 )
223 )
224 , tag_t()
225 );
226 }
227
228 {
229 string filename( pnm_out + "write_test_info.pnm" );
230 FILE* file = fopen( filename.c_str(), "wb" );
231
232 image_write_info< tag_t > info;
233
234 write_view( file
235 , create_mandel_view( 320, 240
236 , rgb8_pixel_t( 0, 0, 255 )
237 , rgb8_pixel_t( 0, 255, 0 )
238 )
239 , info
240 );
241 }
242 }
243 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
244
245 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
246
BOOST_AUTO_TEST_CASE(stream_test)247 BOOST_AUTO_TEST_CASE( stream_test )
248 {
249 // 1. Read an image.
250 ifstream in( pnm_filename.c_str(), ios::binary );
251
252 rgb8_image_t img;
253 read_image( in, img, tag_t() );
254
255 // 2. Write image to in-memory buffer.
256 stringstream out_buffer( ios_base::in | ios_base::out | ios_base::binary );
257 write_view( out_buffer, view( img ), tag_t() );
258
259 // 3. Copy in-memory buffer to another.
260 stringstream in_buffer( ios_base::in | ios_base::out | ios_base::binary );
261 in_buffer << out_buffer.rdbuf();
262
263 // 4. Read in-memory buffer to gil image
264 rgb8_image_t dst;
265 read_image( in_buffer, dst, tag_t() );
266
267 // 5. Write out image.
268 string filename( pnm_out + "stream_test.pnm" );
269 ofstream out( filename.c_str(), ios_base::binary );
270 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
271 write_view( out, view( dst ), tag_t() );
272 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
273 }
274
BOOST_AUTO_TEST_CASE(stream_test_2)275 BOOST_AUTO_TEST_CASE( stream_test_2 )
276 {
277 filebuf in_buf;
278 if( !in_buf.open( pnm_filename.c_str(), ios::in | ios::binary ) )
279 {
280 BOOST_CHECK( false );
281 }
282
283 istream in( &in_buf );
284
285 rgb8_image_t img;
286 read_image( in, img, tag_t() );
287 }
288
BOOST_AUTO_TEST_CASE(subimage_test)289 BOOST_AUTO_TEST_CASE( subimage_test )
290 {
291 run_subimage_test< rgb8_image_t, tag_t >( pnm_filename
292 , point_t( 0, 0 )
293 , point_t( 50, 50 )
294 );
295
296 run_subimage_test< rgb8_image_t, tag_t >( pnm_filename
297 , point_t( 103, 103 )
298 , point_t( 50, 50 )
299 );
300 }
301
BOOST_AUTO_TEST_CASE(dynamic_image_test)302 BOOST_AUTO_TEST_CASE( dynamic_image_test )
303 {
304 using my_img_types = mp11::mp_list
305 <
306 gray8_image_t,
307 gray16_image_t,
308 rgb8_image_t,
309 gray1_image_t
310 >;
311
312 any_image< my_img_types > runtime_image;
313
314 read_image( pnm_filename.c_str()
315 , runtime_image
316 , tag_t()
317 );
318
319 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
320 write_view( pnm_out + "dynamic_image_test.pnm"
321 , view( runtime_image )
322 , tag_t()
323 );
324 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
325 }
326
327 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
328
329 BOOST_AUTO_TEST_SUITE_END()
330