Imported Upstream version 1.71.0
[platform/upstream/boost.git] / libs / gil / io / test / targa_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 targa_read_test_module
9
10 #include <boost/gil.hpp>
11 #include <boost/gil/extension/io/targa.hpp>
12
13 #include <boost/test/unit_test.hpp>
14 #include <boost/type_traits/is_same.hpp>
15
16 #include "paths.hpp"
17 #include "scanline_read_test.hpp"
18
19 using namespace std;
20 using namespace boost::gil;
21
22 using tag_t = targa_tag;
23
24 BOOST_AUTO_TEST_SUITE( gil_io_targa_tests )
25
26 #ifdef BOOST_GIL_IO_USE_TARGA_FILEFORMAT_TEST_SUITE_IMAGES
27
28 template< typename Image >
29 void test_targa_scanline_reader( string filename )
30 {
31     test_scanline_reader<Image, targa_tag>( string( targa_in + filename ).c_str() );
32 }
33
34 template< typename Image >
35 void write( Image&        img
36           , const string& file_name
37           )
38 {
39 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
40     write_view( targa_out + file_name
41               , view( img )
42               , tag_t()
43               );
44 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
45 }
46
47 BOOST_AUTO_TEST_CASE( read_header_test )
48 {
49     {
50         using backend_t = get_reader_backend<std::string const, tag_t>::type;
51
52         backend_t backend = read_image_info( targa_filename
53                                            , tag_t()
54                                            );
55
56         BOOST_CHECK_EQUAL( backend._info._header_size     , 18  );
57         BOOST_CHECK_EQUAL( backend._info._offset          , 18  );
58         BOOST_CHECK_EQUAL( backend._info._color_map_type  , 0   );
59         BOOST_CHECK_EQUAL( backend._info._image_type      , 10  );
60         BOOST_CHECK_EQUAL( backend._info._color_map_start , 0   );
61         BOOST_CHECK_EQUAL( backend._info._color_map_length, 0   );
62         BOOST_CHECK_EQUAL( backend._info._color_map_depth , 0   );
63         BOOST_CHECK_EQUAL( backend._info._x_origin        , 0   );
64         BOOST_CHECK_EQUAL( backend._info._y_origin        , 0   );
65         BOOST_CHECK_EQUAL( backend._info._width           , 124 );
66         BOOST_CHECK_EQUAL( backend._info._height          , 124 );
67         BOOST_CHECK_EQUAL( backend._info._bits_per_pixel  , 24  );
68         BOOST_CHECK_EQUAL( backend._info._descriptor      , 0   );
69     }
70 }
71
72 BOOST_AUTO_TEST_CASE( read_reference_images_test )
73 {
74     // 24BPP_compressed.tga
75     {
76         rgb8_image_t img;
77         read_image( targa_in + "24BPP_compressed.tga", img, tag_t() );
78
79         typename rgb8_image_t::x_coord_t width  = view( img ).width();
80         typename rgb8_image_t::y_coord_t height = view( img ).height();
81
82         BOOST_CHECK_EQUAL( width , 124 );
83         BOOST_CHECK_EQUAL( height, 124 );
84         BOOST_CHECK( view( img )(0, 0) == rgb8_pixel_t(248, 0, 248) );
85         BOOST_CHECK( view( img )(width-1, 0) == rgb8_pixel_t(0, 0, 248) );
86         BOOST_CHECK( view( img )(0, height-1) == rgb8_pixel_t(248, 0, 0) );
87         BOOST_CHECK( view( img )(width-1, height-1) == rgb8_pixel_t(248, 0, 248) );
88
89         write( img, "24BPP_compressed_out.tga" );
90     }
91
92     // 24BPP_uncompressed.tga
93     {
94         rgb8_image_t img;
95         read_image( targa_in + "24BPP_uncompressed.tga", img, tag_t() );
96
97         typename rgb8_image_t::x_coord_t width  = view( img ).width();
98         typename rgb8_image_t::y_coord_t height = view( img ).height();
99
100         BOOST_CHECK_EQUAL( width , 124 );
101         BOOST_CHECK_EQUAL( height, 124 );
102         BOOST_CHECK( view( img )(0, 0) == rgb8_pixel_t(248, 0, 248) );
103         BOOST_CHECK( view( img )(width-1, 0) == rgb8_pixel_t(0, 0, 248) );
104         BOOST_CHECK( view( img )(0, height-1) == rgb8_pixel_t(248, 0, 0) );
105         BOOST_CHECK( view( img )(width-1, height-1) == rgb8_pixel_t(248, 0, 248) );
106
107         write( img, "24BPP_uncompressed_out.tga" );
108
109         test_targa_scanline_reader< bgr8_image_t >( "24BPP_uncompressed.tga" );
110     }
111
112     // 32BPP_compressed.tga
113     {
114         rgba8_image_t img;
115         read_image( targa_in + "32BPP_compressed.tga", img, tag_t() );
116
117         typename rgba8_image_t::x_coord_t width  = view( img ).width();
118         typename rgba8_image_t::y_coord_t height = view( img ).height();
119
120         BOOST_CHECK_EQUAL( width , 124 );
121         BOOST_CHECK_EQUAL( height, 124 );
122         BOOST_CHECK( view( img )(0, 0) == rgba8_pixel_t(248, 0, 248, 255) );
123         BOOST_CHECK( view( img )(width-1, 0) == rgba8_pixel_t(0, 0, 248, 255) );
124         BOOST_CHECK( view( img )(0, height-1) == rgba8_pixel_t(0, 0, 0, 0) );
125         BOOST_CHECK( view( img )(width-1, height-1) == rgba8_pixel_t(248, 0, 248, 255) );
126
127         write( img, "32BPP_compressed_out.tga" );
128     }
129
130     // 32BPP_uncompressed.tga
131     {
132         rgba8_image_t img;
133         read_image( targa_in + "32BPP_uncompressed.tga", img, tag_t() );
134
135         typename rgba8_image_t::x_coord_t width  = view( img ).width();
136         typename rgba8_image_t::y_coord_t height = view( img ).height();
137
138         BOOST_CHECK_EQUAL( width , 124 );
139         BOOST_CHECK_EQUAL( height, 124 );
140         BOOST_CHECK( view( img )(0, 0) == rgba8_pixel_t(248, 0, 248, 255) );
141         BOOST_CHECK( view( img )(width-1, 0) == rgba8_pixel_t(0, 0, 248, 255) );
142         BOOST_CHECK( view( img )(0, height-1) == rgba8_pixel_t(0, 0, 0, 0) );
143         BOOST_CHECK( view( img )(width-1, height-1) == rgba8_pixel_t(248, 0, 248, 255) );
144
145         write( img, "32BPP_uncompressed_out.tga" );
146
147         test_targa_scanline_reader< bgra8_image_t >( "32BPP_uncompressed.tga" );
148     }
149
150     // 24BPP_compressed_ul_origin.tga
151     {
152         rgb8_image_t img;
153         read_image( targa_in + "24BPP_compressed_ul_origin.tga", img, tag_t() );
154
155         typename rgb8_image_t::x_coord_t width  = view( img ).width();
156         typename rgb8_image_t::y_coord_t height = view( img ).height();
157
158         BOOST_CHECK_EQUAL( width , 124 );
159         BOOST_CHECK_EQUAL( height, 124 );
160         BOOST_CHECK( view( img )(0, 0) == rgb8_pixel_t(248, 0, 248) );
161         BOOST_CHECK( view( img )(width-1, 0) == rgb8_pixel_t(0, 0, 248) );
162         BOOST_CHECK( view( img )(0, height-1) == rgb8_pixel_t(248, 0, 0) );
163         BOOST_CHECK( view( img )(width-1, height-1) == rgb8_pixel_t(248, 0, 248) );
164
165         write( img, "24BPP_compressed_ul_origin_out.tga" );
166     }
167
168     // 24BPP_uncompressed_ul_origin.tga
169     {
170         rgb8_image_t img;
171         read_image( targa_in + "24BPP_uncompressed_ul_origin.tga", img, tag_t() );
172
173         typename rgb8_image_t::x_coord_t width  = view( img ).width();
174         typename rgb8_image_t::y_coord_t height = view( img ).height();
175
176         BOOST_CHECK_EQUAL( width , 124 );
177         BOOST_CHECK_EQUAL( height, 124 );
178         BOOST_CHECK( view( img )(0, 0) == rgb8_pixel_t(248, 0, 248) );
179         BOOST_CHECK( view( img )(width-1, 0) == rgb8_pixel_t(0, 0, 248) );
180         BOOST_CHECK( view( img )(0, height-1) == rgb8_pixel_t(248, 0, 0) );
181         BOOST_CHECK( view( img )(width-1, height-1) == rgb8_pixel_t(248, 0, 248) );
182
183         write( img, "24BPP_uncompressed_ul_origin_out.tga" );
184     }
185
186     // 32BPP_compressed_ul_origin.tga
187     {
188         rgba8_image_t img;
189         read_image( targa_in + "32BPP_compressed_ul_origin.tga", img, tag_t() );
190
191         typename rgba8_image_t::x_coord_t width  = view( img ).width();
192         typename rgba8_image_t::y_coord_t height = view( img ).height();
193
194         BOOST_CHECK_EQUAL( width , 124 );
195         BOOST_CHECK_EQUAL( height, 124 );
196         BOOST_CHECK( view( img )(0, 0) == rgba8_pixel_t(248, 0, 248, 255) );
197         BOOST_CHECK( view( img )(width-1, 0) == rgba8_pixel_t(0, 0, 248, 255) );
198         BOOST_CHECK( view( img )(0, height-1) == rgba8_pixel_t(0, 0, 0, 0) );
199         BOOST_CHECK( view( img )(width-1, height-1) == rgba8_pixel_t(248, 0, 248, 255) );
200
201         write( img, "32BPP_compressed_ul_origin_out.tga" );
202     }
203
204     // 32BPP_uncompressed_ul_origin.tga
205     {
206         rgba8_image_t img;
207         read_image( targa_in + "32BPP_uncompressed_ul_origin.tga", img, tag_t() );
208
209         typename rgba8_image_t::x_coord_t width  = view( img ).width();
210         typename rgba8_image_t::y_coord_t height = view( img ).height();
211
212         BOOST_CHECK_EQUAL( width , 124 );
213         BOOST_CHECK_EQUAL( height, 124 );
214         BOOST_CHECK( view( img )(0, 0) == rgba8_pixel_t(248, 0, 248, 255) );
215         BOOST_CHECK( view( img )(width-1, 0) == rgba8_pixel_t(0, 0, 248, 255) );
216         BOOST_CHECK( view( img )(0, height-1) == rgba8_pixel_t(0, 0, 0, 0) );
217         BOOST_CHECK( view( img )(width-1, height-1) == rgba8_pixel_t(248, 0, 248, 255) );
218
219         write( img, "32BPP_uncompressed_ul_origin_out.tga" );
220     }
221 }
222
223 BOOST_AUTO_TEST_CASE( partial_image_test )
224 {
225     const std::string filename( targa_in + "24BPP_compressed.tga" );
226
227     {
228         rgb8_image_t img;
229         read_image( filename
230                   , img
231                   , image_read_settings< targa_tag >( point_t( 0, 0 ), point_t( 50, 50 ) )
232                   );
233
234 #ifdef BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
235         write_view( targa_out + "targa_partial.tga"
236                   , view( img )
237                   , tag_t()
238                   );
239 #endif // BOOST_GIL_IO_TEST_ALLOW_WRITING_IMAGES
240     }
241 }
242
243 #endif // BOOST_GIL_IO_USE_TARGA_FILEFORMAT_TEST_SUITE_IMAGES
244
245 BOOST_AUTO_TEST_SUITE_END()