• 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/hsl.hpp>
11 
12 #include <boost/core/lightweight_test.hpp>
13 
14 #include <cstdint>
15 
16 #include "test_utility_output_stream.hpp"
17 
18 namespace gil = boost::gil;
19 
test_rgb_to_hsl()20 void test_rgb_to_hsl()
21 {
22     gil::rgb8_pixel_t p{128, 0, 128};
23     gil::hsl32f_pixel_t h;
24     gil::color_convert(p, h);
25 
26     BOOST_TEST_GT(gil::get_color(h, gil::hsl_color_space::hue_t()), 0.8);        // 0.83333331
27     BOOST_TEST_EQ(gil::get_color(h, gil::hsl_color_space::saturation_t()), 1.0); // 1.00000000
28     BOOST_TEST_GT(gil::get_color(h, gil::hsl_color_space::lightness_t()), 0.25); // 0.25098040
29 }
30 
test_hsl_to_rgb()31 void test_hsl_to_rgb()
32 {
33     gil::rgb8_pixel_t p(64, 0, 64);
34     gil::hsl32f_pixel_t h;
35     gil::color_convert(p, h);
36 
37     gil::rgb8_pixel_t b;
38     gil::color_convert(h, b);
39     BOOST_TEST_EQ(b, p);
40 }
41 
test_image_assign_hsl()42 void test_image_assign_hsl()
43 {
44     std::ptrdiff_t const w = 320;
45     std::ptrdiff_t const h = 240;
46     gil::hsl32f_image_t hsl_img(w, h);
47 
48     for (std::ptrdiff_t y = 0; y < h; y++)
49     {
50         gil::hsl32f_view_t::x_iterator hsl_x_it = view(hsl_img).row_begin(y);
51         float v = static_cast<float>(h - y) / h;
52         for (std::ptrdiff_t x = 0; x < w; x++)
53         {
54             float const hue = (x + 1.f) / w;
55             gil::hsl32f_pixel_t const p(hue, 1.0, v);
56             hsl_x_it[x] = p;
57             BOOST_TEST_EQ(gil::view(hsl_img)(x, y), p);
58         }
59     }
60 }
61 
test_copy_pixels_rgb_to_hsl()62 void test_copy_pixels_rgb_to_hsl()
63 {
64     gil::rgb8_image_t rgb_img(320, 240);
65     gil::rgb8_pixel_t rgb_pix(64, 32, 64);
66     gil::fill_pixels(view(rgb_img), rgb_pix);
67     gil::hsl32f_image_t hsl_img(view(rgb_img).dimensions());
68     gil::copy_pixels(gil::color_converted_view<gil::hsl32f_pixel_t>(view(rgb_img)), view(hsl_img));
69 
70     auto view = gil::view(hsl_img);
71     for (auto it = view.begin(), end = view.end(); it != end; ++it)
72     {
73         gil::rgb8_pixel_t p;
74         gil::color_convert(*it, p);
75         BOOST_TEST_EQ(p, rgb_pix);
76     }
77 }
78 
main()79 int main()
80 {
81     test_rgb_to_hsl();
82     test_hsl_to_rgb();
83     test_image_assign_hsl();
84     test_copy_pixels_rgb_to_hsl();
85 
86     return ::boost::report_errors();
87 }
88