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