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