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