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