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