Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / gil / io / test / png_read_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_read_test_module
9 #define BOOST_GIL_IO_ADD_FS_PATH_SUPPORT
10 #define BOOST_GIL_IO_ENABLE_GRAY_ALPHA
11 #define BOOST_FILESYSTEM_VERSION 3
12
13 #include <boost/gil/extension/io/png.hpp>
14
15 #include <boost/test/unit_test.hpp>
16
17 #include <cstdint>
18 #include <iostream>
19
20 #include "paths.hpp"
21 #include "scanline_read_test.hpp"
22
23 using namespace std;
24 using namespace boost;
25 using namespace gil;
26 using namespace boost::gil::detail;
27 namespace fs = boost::filesystem;
28
29 using tag_t = png_tag;
30
31 BOOST_AUTO_TEST_SUITE( gil_io_png_tests )
32
33 using gray_alpha8_pixel_t = pixel<uint8_t, gray_alpha_layout_t>;
34 using gray_alpha8_image_t= image<gray_alpha8_pixel_t, false>;
35
36 using gray_alpha16_pixel_t = pixel<uint16_t, gray_alpha_layout_t>;
37 using gray_alpha16_image_t = image<gray_alpha16_pixel_t, false>;
38
39 template< typename Image >
40 void test_file( string filename )
41 {
42     Image src, dst;
43
44     image_read_settings< png_tag > settings;
45     settings._read_file_gamma        = true;
46     settings._read_transparency_data = true;
47
48
49     using backend_t = get_reader_backend<std::string const, tag_t>::type;
50
51     backend_t backend = read_image_info( png_in + filename
52                                         , settings
53                                         );
54
55     read_image( png_in + filename
56               , src
57               , settings
58               );
59
60
61 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
62     image_write_info< png_tag > write_info;
63     write_info._file_gamma = backend._info._file_gamma;
64
65     write_view( png_out + filename
66               , view( src )
67               , write_info
68               );
69
70     read_image( png_out + filename
71               , dst
72               , settings
73               );
74
75
76     BOOST_CHECK( equal_pixels( const_view( src )
77                              , const_view( dst )
78                              )
79                );
80 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
81 }
82
83 template< typename Image >
84 void test_png_scanline_reader( string filename )
85 {
86     test_scanline_reader<Image, png_tag>( string( png_in + filename ).c_str() );
87 }
88
89 BOOST_AUTO_TEST_CASE( read_header_test )
90 {
91     using backend_t = get_reader_backend<std::string const, tag_t>::type;
92
93     backend_t backend = read_image_info( png_filename
94                                        , tag_t()
95                                        );
96
97     BOOST_CHECK_EQUAL( backend._info._width , 1000u );
98     BOOST_CHECK_EQUAL( backend._info._height,  600u );
99
100     BOOST_CHECK_EQUAL( backend._info._num_channels, 4                   );
101     BOOST_CHECK_EQUAL( backend._info._bit_depth   , 8                   );
102     BOOST_CHECK_EQUAL( backend._info._color_type  , PNG_COLOR_TYPE_RGBA );
103
104     BOOST_CHECK_EQUAL( backend._info._interlace_method  , PNG_INTERLACE_NONE        );
105     BOOST_CHECK_EQUAL( backend._info._compression_method, PNG_COMPRESSION_TYPE_BASE );
106     BOOST_CHECK_EQUAL( backend._info._filter_method     , PNG_FILTER_TYPE_BASE      );
107
108
109     BOOST_CHECK_EQUAL( backend._info._file_gamma, 1 );
110 }
111
112 #ifdef BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
113
114 BOOST_AUTO_TEST_CASE( read_pixel_per_meter )
115 {
116     image_read_settings< png_tag > settings;
117     settings.set_read_members_true();
118
119     using backend_t = get_reader_backend<std::string const, tag_t>::type;
120
121     backend_t backend = read_image_info( png_base_in + "EddDawson/36dpi.png"
122                                       , settings
123                                       );
124
125     BOOST_CHECK_EQUAL( backend._info._pixels_per_meter, png_uint_32( 1417 ));
126 }
127
128 #endif // BOOST_GIL_IO_TEST_ALLOW_READING_IMAGES
129
130 #ifdef BOOST_GIL_IO_USE_PNG_TEST_SUITE_IMAGES
131
132 BOOST_AUTO_TEST_CASE( BASIc_format_test )
133 {
134     // Basic format test files (non-interlaced)
135
136     // BASN0g01    -   black & white
137     test_file< gray1_image_t >( "BASN0G01.PNG" );
138     test_png_scanline_reader< gray1_image_t >( "BASN0G01.PNG" );
139
140     // BASN0g02    -   2 bit (4 level) grayscale
141     test_file< gray2_image_t >( "BASN0G02.PNG" );
142     test_png_scanline_reader< gray2_image_t >( "BASN0G02.PNG" );
143
144     // BASN0g04    -   4 bit (16 level) grayscale
145     test_file< gray4_image_t >( "BASN0G04.PNG" );
146     test_png_scanline_reader< gray4_image_t >( "BASN0G04.PNG" );
147
148     // BASN0g08    -   8 bit (256 level) grayscale
149     test_file< gray8_image_t >( "BASN0G08.PNG" );
150     test_png_scanline_reader< gray8_image_t >( "BASN0G08.PNG" );
151
152     // BASN0g16    -   16 bit (64k level) grayscale
153     test_file< gray16_image_t >( "BASN0G16.PNG" );
154     test_png_scanline_reader< gray16_image_t >( "BASN0G16.PNG" );
155
156     // BASN2c08    -   3x8 bits rgb color
157     test_file< rgb8_image_t >( "BASN2C08.PNG" );
158     test_png_scanline_reader< rgb8_image_t >( "BASN2C08.PNG" );
159
160     // BASN2c16    -   3x16 bits rgb color
161     test_file< rgb16_image_t >( "BASN2C16.PNG" );
162     test_png_scanline_reader< rgb16_image_t >( "BASN2C16.PNG" );
163
164     // BASN3p01    -   1 bit (2 color) paletted
165     test_file< rgb8_image_t >( "BASN3P01.PNG" );
166     test_png_scanline_reader< rgb8_image_t >( "BASN3P01.PNG" );
167
168     // BASN3p02    -   2 bit (4 color) paletted
169     test_file< rgb8_image_t >( "BASN3P02.PNG" );
170     test_png_scanline_reader< rgb8_image_t >( "BASN3P02.PNG" );
171
172     // BASN3p04    -   4 bit (16 color) paletted
173     test_file< rgb8_image_t >( "BASN3P04.PNG" );
174     test_png_scanline_reader< rgb8_image_t >( "BASN3P04.PNG" );
175
176     // BASN3p08    -   8 bit (256 color) paletted
177     test_file< rgb8_image_t >( "BASN3P08.PNG" );
178     test_png_scanline_reader< rgb8_image_t >( "BASN3P08.PNG" );
179
180     // BASN4a08    -   8 bit grayscale + 8 bit alpha-channel
181     test_file< gray_alpha8_image_t >( "BASN4A08.PNG" );
182     test_png_scanline_reader< gray_alpha8_image_t >( "BASN4A08.PNG" );
183
184     // BASN4a16    -   16 bit grayscale + 16 bit alpha-channel
185     test_file< gray_alpha16_image_t >( "BASN4A16.PNG" );
186     test_png_scanline_reader< gray_alpha16_image_t >( "BASN4A16.PNG" );
187
188     // BASN6a08    -   3x8 bits rgb color + 8 bit alpha-channel
189     test_file< rgba8_image_t >( "BASN6A08.PNG" );
190     test_png_scanline_reader< rgba8_image_t >( "BASN6A08.PNG" );
191
192     // BASN6a16    -   3x16 bits rgb color + 16 bit alpha-channel
193     test_file< rgba16_image_t >( "BASN6A16.PNG" );
194     test_png_scanline_reader< rgba16_image_t >( "BASN6A16.PNG" );
195 }
196
197 BOOST_AUTO_TEST_CASE( BASIc_format_interlaced_test )
198 {
199     // Basic format test files (Adam-7 interlaced)
200
201     // BASI0g01    -   black & white
202     test_file< gray1_image_t >( "BASI0G01.PNG" );
203
204     // BASI0g02    -   2 bit (4 level) grayscale
205     test_file< gray2_image_t >( "BASI0G02.PNG" );
206
207     // BASI0g04    -   4 bit (16 level) grayscale
208     test_file< gray4_image_t >( "BASI0G04.PNG" );
209
210     // BASI0g08    -   8 bit (256 level) grayscale
211     test_file< gray8_image_t >( "BASI0G08.PNG" );
212
213     // BASI0g16    -   16 bit (64k level) grayscale
214     test_file< gray16_image_t >( "BASI0G16.PNG" );
215
216     // BASI2c08    -   3x8 bits rgb color
217     test_file< rgb8_image_t >( "BASI2C08.PNG" );
218
219     // BASI2c16    -   3x16 bits rgb color
220     test_file< rgb16_image_t >( "BASI2C16.PNG" );
221
222     // BASI3p01    -   1 bit (2 color) paletted
223     test_file< rgb8_image_t >( "BASI3P01.PNG" );
224
225     // BASI3p02    -   2 bit (4 color) paletted
226     test_file< rgb8_image_t >( "BASI3P02.PNG" );
227
228     // BASI3p04    -   4 bit (16 color) paletted
229     test_file< rgb8_image_t >( "BASI3P04.PNG" );
230
231     // BASI3p08    -   8 bit (256 color) paletted
232     test_file< rgb8_image_t >( "BASI3P08.PNG" );
233
234     // BASI4a08    -   8 bit grayscale + 8 bit alpha-channel
235     test_file< gray_alpha8_image_t >( "BASI4A08.PNG" );
236
237     // BASI4a16    -   16 bit grayscale + 16 bit alpha-channel
238     test_file< gray_alpha16_image_t >( "BASI4A16.PNG" );
239
240     // BASI6a08    -   3x8 bits rgb color + 8 bit alpha-channel
241     test_file< rgba8_image_t >( "BASI6A08.PNG" );
242
243     // BASI6a16    -   3x16 bits rgb color + 16 bit alpha-channel
244     test_file< rgba16_image_t >( "BASI6A16.PNG" );
245 }
246
247 BOOST_AUTO_TEST_CASE( odd_sizes_test )
248 {
249     // S01I3P01 - 1x1 paletted file, interlaced
250     test_file< rgb8_image_t >( "S01I3P01.PNG" );
251
252     // S01N3P01 - 1x1 paletted file, no interlacing
253     test_file< rgb8_image_t >( "S01N3P01.PNG" );
254     test_png_scanline_reader< rgb8_image_t >( "S01N3P01.PNG" );
255
256     // S02I3P01 - 2x2 paletted file, interlaced
257     test_file< rgb8_image_t >( "S02I3P01.PNG" );
258
259     // S02N3P01 - 2x2 paletted file, no interlacing
260     test_file< rgb8_image_t >( "S02N3P01.PNG" );
261     test_png_scanline_reader< rgb8_image_t >( "S02N3P01.PNG" );
262
263     // S03I3P01 - 3x3 paletted file, interlaced
264     test_file< rgb8_image_t >( "S03I3P01.PNG" );
265
266     // S03N3P01 - 3x3 paletted file, no interlacing
267     test_file< rgb8_image_t >( "S03N3P01.PNG" );
268     test_png_scanline_reader< rgb8_image_t >( "S03N3P01.PNG" );
269
270     // S04I3P01 - 4x4 paletted file, interlaced
271     test_file< rgb8_image_t >( "S04I3P01.PNG" );
272
273     // S04N3P01 - 4x4 paletted file, no interlacing
274     test_file< rgb8_image_t >( "S04N3P01.PNG" );
275     test_png_scanline_reader< rgb8_image_t >( "S04N3P01.PNG" );
276
277     // S05I3P02 - 5x5 paletted file, interlaced
278     test_file< rgb8_image_t >( "S05I3P02.PNG" );
279
280     // S05N3P02 - 5x5 paletted file, no interlacing
281     test_file< rgb8_image_t >( "S05N3P02.PNG" );
282     test_png_scanline_reader< rgb8_image_t >( "S05N3P02.PNG" );
283
284     // S06I3P02 - 6x6 paletted file, interlaced
285     test_file< rgb8_image_t >( "S06I3P02.PNG" );
286
287     // S06N3P02 - 6x6 paletted file, no interlacing
288     test_file< rgb8_image_t >( "S06N3P02.PNG" );
289     test_png_scanline_reader< rgb8_image_t >( "S06N3P02.PNG" );
290
291     // S07I3P02 - 7x7 paletted file, interlaced
292     test_file< rgb8_image_t >( "S07I3P02.PNG" );
293
294     // S07N3P02 - 7x7 paletted file, no interlacing
295     test_file< rgb8_image_t >( "S07N3P02.PNG" );
296     test_png_scanline_reader< rgb8_image_t >( "S07N3P02.PNG" );
297
298     // S08I3P02 - 8x8 paletted file, interlaced
299     test_file< rgb8_image_t >( "S08I3P02.PNG" );
300
301     // S08N3P02 - 8x8 paletted file, no interlacing
302     test_file< rgb8_image_t >( "S08N3P02.PNG" );
303     test_png_scanline_reader< rgb8_image_t >( "S08N3P02.PNG" );
304
305     // S09I3P02 - 9x9 paletted file, interlaced
306     test_file< rgb8_image_t >( "S09I3P02.PNG" );
307
308     // S09N3P02 - 9x9 paletted file, no interlacing
309     test_file< rgb8_image_t >( "S09N3P02.PNG" );
310     test_png_scanline_reader< rgb8_image_t >( "S09N3P02.PNG" );
311
312     // S32I3P04 - 32x32 paletted file, interlaced
313     test_file< rgb8_image_t >( "S32I3P04.PNG" );
314
315     // S32N3P04 - 32x32 paletted file, no interlacing
316     test_file< rgb8_image_t >( "S32N3P04.PNG" );
317     test_png_scanline_reader< rgb8_image_t >( "S32N3P04.PNG" );
318
319     // S33I3P04 - 33x33 paletted file, interlaced
320     test_file< rgb8_image_t >( "S33I3P04.PNG" );
321
322     // S33N3P04 - 33x33 paletted file, no interlacing
323     test_file< rgb8_image_t >( "S33N3P04.PNG" );
324     test_png_scanline_reader< rgb8_image_t >( "S33N3P04.PNG" );
325
326     // S34I3P04 - 34x34 paletted file, interlaced
327     test_file< rgb8_image_t >( "S34I3P04.PNG" );
328
329     // S34N3P04 - 34x34 paletted file, no interlacing
330     test_file< rgb8_image_t >( "S34N3P04.PNG" );
331     test_png_scanline_reader< rgb8_image_t >( "S34N3P04.PNG" );
332
333     // S35I3P04 - 35x35 paletted file, interlaced
334     test_file< rgb8_image_t >( "S35I3P04.PNG" );
335
336     // S35N3P04 - 35x35 paletted file, no interlacing
337     test_file< rgb8_image_t >( "S35N3P04.PNG" );
338     test_png_scanline_reader< rgb8_image_t >( "S35N3P04.PNG" );
339
340     // S36I3P04 - 36x36 paletted file, interlaced
341     test_file< rgb8_image_t >( "S36I3P04.PNG" );
342
343     // S36N3P04 - 36x36 paletted file, no interlacing
344     test_file< rgb8_image_t >( "S36N3P04.PNG" );
345     test_png_scanline_reader< rgb8_image_t >( "S36N3P04.PNG" );
346
347     // S37I3P04 - 37x37 paletted file, interlaced
348     test_file< rgb8_image_t >( "S37I3P04.PNG" );
349
350     // S37N3P04 - 37x37 paletted file, no interlacing
351     test_file< rgb8_image_t >( "S37N3P04.PNG" );
352     test_png_scanline_reader< rgb8_image_t >( "S37N3P04.PNG" );
353
354     // S38I3P04 - 38x38 paletted file, interlaced
355     test_file< rgb8_image_t >( "S38I3P04.PNG" );
356
357     // S38N3P04 - 38x38 paletted file, no interlacing
358     test_file< rgb8_image_t >( "S38N3P04.PNG" );
359     test_png_scanline_reader< rgb8_image_t >( "S38N3P04.PNG" );
360
361     // S39I3P04 - 39x39 paletted file, interlaced
362     test_file< rgb8_image_t >( "S39I3P04.PNG" );
363
364     // S39N3P04 - 39x39 paletted file, no interlacing
365     test_file< rgb8_image_t >( "S39N3P04.PNG" );
366     test_png_scanline_reader< rgb8_image_t >( "S39N3P04.PNG" );
367
368     // S40I3P04 - 40x40 paletted file, interlaced
369     test_file< rgb8_image_t >( "S40I3P04.PNG" );
370
371     // S40N3P04 - 40x40 paletted file, no interlacing
372     test_file< rgb8_image_t >( "S40N3P04.PNG" );
373     test_png_scanline_reader< rgb8_image_t >( "S40N3P04.PNG" );
374 }
375
376 BOOST_AUTO_TEST_CASE( background_test )
377 {
378     // BGAI4A08 - 8 bit grayscale, alpha, no background chunk, interlaced
379     test_file< gray_alpha8_image_t >( "BGAI4A08.PNG" );
380
381     // BGAI4A16 - 16 bit grayscale, alpha, no background chunk, interlaced
382     test_file< gray_alpha16_image_t >( "BGAI4A16.PNG" );
383
384     // BGAN6A08 - 3x8 bits rgb color, alpha, no background chunk
385     test_file< rgba8_image_t >( "BGAN6A08.PNG" );
386
387     // BGAN6A16 - 3x16 bits rgb color, alpha, no background chunk
388     test_file< rgba16_image_t >( "BGAN6A16.PNG" );
389
390     // BGBN4A08 - 8 bit grayscale, alpha, black background chunk
391     test_file< gray_alpha8_image_t >( "BGBN4A08.PNG" );
392
393     // BGGN4A16 - 16 bit grayscale, alpha, gray background chunk
394     test_file< gray_alpha16_image_t >( "BGGN4A16.PNG" );
395
396     // BGWN6A08 - 3x8 bits rgb color, alpha, white background chunk
397     test_file< rgba8_image_t >( "BGWN6A08.PNG" );
398
399     // BGYN6A16 - 3x16 bits rgb color, alpha, yellow background chunk
400     test_file< rgba16_image_t >( "BGYN6A16.PNG" );
401 }
402
403 BOOST_AUTO_TEST_CASE( transparency_test )
404 {
405     // TBBN1G04 - transparent, black background chunk
406     // file missing
407     //test_file< gray_alpha8_image_t >( "TBBN1G04.PNG" );
408
409     // TBBN2C16 - transparent, blue background chunk
410     test_file< rgba16_image_t >( "TBBN2C16.PNG" );
411
412     // TBBN3P08 - transparent, black background chunk
413     test_file< rgba8_image_t >( "TBBN3P08.PNG" );
414
415     // TBGN2C16 - transparent, green background chunk
416     test_file< rgba16_image_t >( "TBGN2C16.PNG" );
417
418     // TBGN3P08 - transparent, light-gray background chunk
419     test_file< rgba8_image_t >( "TBGN3P08.PNG" );
420
421     // TBRN2C08 - transparent, red background chunk
422     test_file< rgba8_image_t >( "TBRN2C08.PNG" );
423
424     // TBWN1G16 - transparent, white background chunk
425     test_file< gray_alpha16_image_t >( "TBWN0G16.PNG" );
426
427     // TBWN3P08 - transparent, white background chunk
428     test_file< rgba8_image_t >( "TBWN3P08.PNG" );
429
430     // TBYN3P08 - transparent, yellow background chunk
431     test_file< rgba8_image_t >( "TBYN3P08.PNG" );
432
433     // TP0N1G08 - not transparent for reference (logo on gray)
434     test_file< gray8_image_t >( "TP0N0G08.PNG" );
435
436     // TP0N2C08 - not transparent for reference (logo on gray)
437     test_file< rgb8_image_t >( "TP0N2C08.PNG" );
438
439     // TP0N3P08 - not transparent for reference (logo on gray)
440     test_file< rgb8_image_t >( "TP0N3P08.PNG" );
441
442     // TP1N3P08 - transparent, but no background chunk
443     test_file< rgba8_image_t >( "TP1N3P08.PNG" );
444 }
445
446 BOOST_AUTO_TEST_CASE( gamma_test )
447 {
448     // G03N0G16 - grayscale, file-gamma = 0.35
449     test_file< gray16_image_t >( "G03N0G16.PNG" );
450
451     // G03N2C08 - color, file-gamma = 0.35
452     test_file< rgb8_image_t >( "G03N2C08.PNG" );
453
454     // G03N3P04 - paletted, file-gamma = 0.35
455     test_file< rgb8_image_t >( "G03N3P04.PNG" );
456
457     // G04N0G16 - grayscale, file-gamma = 0.45
458     test_file< gray16_image_t >( "G04N0G16.PNG" );
459
460     // G04N2C08 - color, file-gamma = 0.45
461     test_file< rgb8_image_t >( "G04N2C08.PNG" );
462
463     // G04N3P04 - paletted, file-gamma = 0.45
464     test_file< rgb8_image_t >( "G04N3P04.PNG" );
465
466     // G05N0G16 - grayscale, file-gamma = 0.55
467     test_file< gray16_image_t >( "G05N0G16.PNG" );
468
469     // G05N2C08 - color, file-gamma = 0.55
470     test_file< rgb8_image_t >( "G05N2C08.PNG" );
471
472     // G05N3P04 - paletted, file-gamma = 0.55
473     test_file< rgb8_image_t >( "G05N3P04.PNG" );
474
475     // G07N0G16 - grayscale, file-gamma = 0.70
476     test_file< gray16_image_t >( "G07N0G16.PNG" );
477
478     // G07N2C08 - color, file-gamma = 0.70
479     test_file< rgb8_image_t >( "G07N2C08.PNG" );
480
481     // G07N3P04 - paletted, file-gamma = 0.70
482     test_file< rgb8_image_t >( "G07N3P04.PNG" );
483
484     // G10N0G16 - grayscale, file-gamma = 1.00
485     test_file< gray16_image_t >( "G10N0G16.PNG" );
486
487     // G10N2C08 - color, file-gamma = 1.00
488     test_file< rgb8_image_t >( "G10N2C08.PNG" );
489
490     // G10N3P04 - paletted, file-gamma = 1.00
491     test_file< rgb8_image_t >( "G10N3P04.PNG" );
492
493     // G25N0G16 - grayscale, file-gamma = 2.50
494     test_file< gray16_image_t >( "G25N0G16.PNG" );
495
496     // G25N2C08 - color, file-gamma = 2.50
497     test_file< rgb8_image_t >( "G25N2C08.PNG" );
498
499     // G25N3P04 - paletted, file-gamma = 2.50
500     test_file< rgb8_image_t >( "G25N3P04.PNG" );
501 }
502
503 #endif // BOOST_GIL_IO_USE_PNG_TEST_SUITE_IMAGES
504
505 BOOST_AUTO_TEST_SUITE_END()