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