• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright 2013 Christian Henning
3 // Copyright 2020 Mateusz Loskot <mateusz@loskot.net>
4 //
5 // Distributed under the Boost Software License, Version 1.0
6 // See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt
8 //
9 #include <boost/gil.hpp>
10 #include <boost/gil/extension/toolbox/color_spaces/hsv.hpp>
11 
12 #include <boost/core/lightweight_test.hpp>
13 
14 #include <iostream>
15 
16 #include "test_utility_output_stream.hpp"
17 
18 namespace gil = boost::gil;
19 
test_rgb_to_hsv()20 void test_rgb_to_hsv()
21 {
22     gil::rgb8_pixel_t p{128, 0, 128};
23     gil::hsv32f_pixel_t h;
24     gil::color_convert(p, h);
25 
26     BOOST_TEST_GT(gil::get_color(h, gil::hsv_color_space::hue_t()), 0.80);       // 0.83333331
27     BOOST_TEST_LT(gil::get_color(h, gil::hsv_color_space::hue_t()), 0.85);
28     BOOST_TEST_GE(gil::get_color(h, gil::hsv_color_space::saturation_t()), 1.0); // 1.00000000
29     BOOST_TEST_LT(gil::get_color(h, gil::hsv_color_space::saturation_t()), 1.1);
30     BOOST_TEST_GE(gil::get_color(h, gil::hsv_color_space::value_t()), 0.50);     // 0.50196081
31     BOOST_TEST_LT(gil::get_color(h, gil::hsv_color_space::value_t()), 0.51);
32 }
33 
test_hsv_to_rgb()34 void test_hsv_to_rgb()
35 {
36     gil::rgb8_pixel_t p(128, 0, 128);
37     gil::hsv32f_pixel_t h;
38     gil::color_convert(p, h);
39 
40     gil::rgb8_pixel_t b;
41     gil::color_convert(h, b);
42     BOOST_TEST_EQ(b, gil::rgb8_pixel_t(128, 0, 128));
43 }
44 
45 
test_image_assign_hsv()46 void test_image_assign_hsv()
47 {
48     std::ptrdiff_t const w = 320;
49     std::ptrdiff_t const h = 240;
50     gil::hsv32f_image_t hsv_img(w, h);
51 
52     for (std::ptrdiff_t y = 0; y < h; y++)
53     {
54         gil::hsv32f_view_t::x_iterator hsv_x_it = view(hsv_img).row_begin(y);
55         float v = static_cast<float>(h - y) / h;
56         for (std::ptrdiff_t x = 0; x < w; x++)
57         {
58             float const hue = (x + 1.f) / w;
59             gil::hsv32f_pixel_t const p(hue, 1.0, v);
60             hsv_x_it[x] = p;
61             BOOST_TEST_EQ(gil::view(hsv_img)(x, y), p);
62         }
63     }
64 }
65 
test_copy_pixels_rgb_to_hsv()66 void test_copy_pixels_rgb_to_hsv()
67 {
68     gil::rgb8_image_t rgb_img(320, 240);
69     gil::rgb8_pixel_t rgb_pix(64, 32, 64);
70     gil::fill_pixels(view(rgb_img), rgb_pix);
71     gil::hsv32f_image_t hsv_img(view(rgb_img).dimensions());
72     gil::copy_pixels(gil::color_converted_view<gil::hsv32f_pixel_t>(view(rgb_img)), view(hsv_img));
73 
74     auto view = gil::view(hsv_img);
75     for (auto it = view.begin(), end = view.end(); it != end; ++it)
76     {
77         auto h = *it;
78         BOOST_TEST_GT(gil::get_color(h, gil::hsv_color_space::hue_t()), 0.80);       // 0.8333333
79         BOOST_TEST_LT(gil::get_color(h, gil::hsv_color_space::hue_t()), 0.85);
80         BOOST_TEST_GE(gil::get_color(h, gil::hsv_color_space::saturation_t()), 0.5); // 0.5000000
81         BOOST_TEST_LT(gil::get_color(h, gil::hsv_color_space::saturation_t()),  0.51);
82         BOOST_TEST_GT(gil::get_color(h, gil::hsv_color_space::value_t()), 0.25);     // 0.25
83         BOOST_TEST_LT(gil::get_color(h, gil::hsv_color_space::value_t()), 0.26);
84     }
85 }
86 
main()87 int main()
88 {
89     test_rgb_to_hsv();
90     test_hsv_to_rgb();
91     test_image_assign_hsv();
92     test_copy_pixels_rgb_to_hsv();
93 
94     return ::boost::report_errors();
95 }
96