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/toolbox/image_types/indexed_image.hpp>
10
11 #include <boost/core/lightweight_test.hpp>
12
13 #include <cstdint>
14
15 #include "test_utility_output_stream.hpp"
16
17 namespace gil = boost::gil;
18
test_index_image()19 void test_index_image()
20 {
21 auto const pixel_generator = []() -> gil::rgb8_pixel_t {
22 static int i = 0;
23 i = (i > 255) ? 0 : (i + 1);
24 auto const i8 = static_cast<std::uint8_t>(i);
25 return gil::rgb8_pixel_t(i8, i8, i8);
26 };
27
28 {
29 gil::indexed_image<std::uint8_t, gil::rgb8_pixel_t> img(640, 480);
30 gil::fill_pixels(gil::view(img), gil::rgb8_pixel_t(255, 0, 0));
31
32 gil::rgb8_pixel_t const p = *gil::view(img).xy_at(10, 10);
33 BOOST_TEST_EQ(p[0], 255);
34 }
35 {
36 using image_t = gil::indexed_image<gil::gray8_pixel_t, gil::rgb8_pixel_t>;
37 image_t img(640, 480, 256);
38
39 gil::generate_pixels(img.get_indices_view(), []() -> gil::gray8_pixel_t {
40 static int i = 0;
41 i = (i > 255) ? 0 : (i + 1);
42 auto const i8 = static_cast<std::uint8_t>(i);
43 return gil::gray8_pixel_t(i8);
44 });
45 gil::generate_pixels(img.get_palette_view(), pixel_generator);
46
47 gil::gray8_pixel_t index{0};
48 index = *img.get_indices_view().xy_at(0, 0); // verify values along first row
49 BOOST_TEST_EQ(static_cast<int>(index), (0 + 1));
50 index = *img.get_indices_view().xy_at(128, 0);
51 BOOST_TEST_EQ(static_cast<int>(index), (128 + 1));
52 // verify wrapping of value by the pixels generator above
53 index = *img.get_indices_view().xy_at(255, 0);
54 BOOST_TEST_EQ(static_cast<int>(index), 0);
55
56 // access via member function
57 gil::rgb8_pixel_t const pixel1 = *img.get_palette_view().xy_at(index, 0);
58 BOOST_TEST_EQ(pixel1[0], pixel1[1]);
59 BOOST_TEST_EQ(pixel1[1], pixel1[2]);
60
61 // access via free function
62 gil::rgb8_pixel_t const pixel2 = *gil::view(img).xy_at(10, 1);
63 BOOST_TEST_EQ(pixel2[0], pixel2[1]);
64 BOOST_TEST_EQ(pixel2[1], pixel2[2]);
65 }
66 {
67 using image_t = gil::indexed_image<gil::gray8_pixel_t, gil::rgb8_pixel_t>;
68 image_t img(640, 480, 256);
69
70 gil::generate_pixels(img.get_indices_view(), []() -> uint8_t
71 {
72 static int i = 0;
73 i = (i > 255) ? 0 : (i + 1);
74 return static_cast<std::uint8_t>(i);
75 });
76 gil::generate_pixels(img.get_palette_view(), pixel_generator);
77
78 std::uint8_t index = *img.get_indices_view().xy_at(128, 0);
79 BOOST_TEST_EQ(static_cast<int>(index), (128 + 1));
80
81 gil::rgb8_pixel_t const pixel1 = *img.get_palette_view().xy_at(index, 0);
82 BOOST_TEST_EQ(pixel1[0], pixel1[1]);
83 BOOST_TEST_EQ(pixel1[1], pixel1[2]);
84
85 gil::rgb8_pixel_t const pixel2 = *view(img).xy_at(10, 1);
86 BOOST_TEST_EQ(pixel2[0], pixel2[1]);
87 BOOST_TEST_EQ(pixel2[1], pixel2[2]);
88 }
89 {
90 using image_t = gil::indexed_image<std::uint8_t, gil::rgb8_pixel_t>;
91 image_t img(640, 480, 256);
92
93 for (image_t::y_coord_t y = 0; y < gil::view(img).height(); ++y)
94 {
95 image_t::view_t::x_iterator it = gil::view(img).row_begin(y);
96 for (image_t::x_coord_t x = 0; x < gil::view(img).width(); ++x)
97 {
98 gil::rgb8_pixel_t p = *it;
99 boost::ignore_unused(p);
100 it++;
101 }
102 }
103
104 // TODO: No checks? ~mloskot
105 }
106 }
107
test_index_image_view()108 void test_index_image_view()
109 {
110 // generate some data
111 std::size_t const width = 640;
112 std::size_t const height = 480;
113 std::size_t const num_colors = 3;
114 std::uint8_t const index = 2;
115
116 // indices
117 std::vector<std::uint8_t> indices(width * height, index);
118
119 // colors
120 std::vector<gil::rgb8_pixel_t> palette(num_colors);
121 palette[0] = gil::rgb8_pixel_t(10, 20, 30);
122 palette[1] = gil::rgb8_pixel_t(40, 50, 60);
123 palette[2] = gil::rgb8_pixel_t(70, 80, 90);
124
125 // create image views from raw memory
126 auto indices_view = gil::interleaved_view(width, height,
127 (gil::gray8_image_t::view_t::x_iterator) indices.data(),
128 width); // row size in bytes
129
130 auto palette_view = gil::interleaved_view(100, 1,
131 (gil::rgb8_image_t::view_t::x_iterator) palette.data(),
132 num_colors * 3); // row size in bytes
133
134 auto ii_view = gil::view(indices_view, palette_view);
135
136 auto p = ii_view(gil::point_t(0, 0));
137 auto q = *ii_view.at(gil::point_t(0, 0));
138
139 BOOST_TEST_EQ(gil::get_color(p, gil::red_t()), 70);
140 BOOST_TEST_EQ(gil::get_color(p, gil::green_t()), 80);
141 BOOST_TEST_EQ(gil::get_color(p, gil::blue_t()), 90);
142
143 BOOST_TEST_EQ(gil::get_color(q, gil::red_t()), 70);
144 BOOST_TEST_EQ(gil::get_color(q, gil::green_t()), 80);
145 BOOST_TEST_EQ(gil::get_color(q, gil::blue_t()), 90);
146 }
147
main()148 int main()
149 {
150 test_index_image();
151 test_index_image_view();
152
153 return ::boost::report_errors();
154 }
155