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