2 // Copyright 2013 Christian Henning
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
8 #include <boost/gil.hpp>
9 #include <boost/gil/extension/toolbox/image_types/indexed_image.hpp>
11 #include <boost/assert.hpp>
12 #include <boost/test/unit_test.hpp>
16 namespace bg = boost::gil;
18 BOOST_AUTO_TEST_SUITE(toolbox_tests)
20 BOOST_AUTO_TEST_CASE(index_image_test)
22 auto const pixel_generator = []() -> bg::rgb8_pixel_t {
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);
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));
33 bg::rgb8_pixel_t const p = *bg::view(img).xy_at(10, 10);
34 BOOST_TEST(p[0] == 255);
37 using image_t = bg::indexed_image<bg::gray8_pixel_t, bg::rgb8_pixel_t>;
38 image_t img(640, 480, 256);
40 generate_pixels(img.get_indices_view(), []() -> bg::gray8_pixel_t
43 i = (i > 255) ? 0 : (i + 1);
44 auto const i8 = static_cast<std::uint8_t>(i);
45 return bg::gray8_pixel_t(i8);
47 generate_pixels(img.get_palette_view(), pixel_generator);
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);
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]);
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]);
69 using image_t = bg::indexed_image<bg::gray8_pixel_t, bg::rgb8_pixel_t>;
70 image_t img(640, 480, 256);
72 generate_pixels(img.get_indices_view(), []() -> uint8_t
75 i = (i > 255) ? 0 : (i + 1);
76 return static_cast<std::uint8_t>(i);
78 generate_pixels(img.get_palette_view(), pixel_generator);
80 std::uint8_t index = *img.get_indices_view().xy_at(128, 0);
81 BOOST_TEST(static_cast<int>(index) == (128 + 1));
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]);
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]);
92 using image_t = bg::indexed_image<std::uint8_t, bg::rgb8_pixel_t>;
93 image_t img(640, 480, 256);
95 for (image_t::y_coord_t y = 0; y < bg::view(img).height(); ++y)
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)
100 bg::rgb8_pixel_t p = *it;
101 boost::ignore_unused(p);
106 // TODO: No checks? ~mloskot
110 BOOST_AUTO_TEST_CASE(index_image_view_test)
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;
119 std::vector<std::uint8_t> indices(width * height, index);
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);
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
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
136 auto ii_view = bg::view(indices_view, palette_view);
138 auto p = ii_view(bg::point_t(0, 0));
139 auto q = *ii_view.at(bg::point_t(0, 0));
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);
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);
150 BOOST_AUTO_TEST_SUITE_END()