HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
sfloat_rgba16.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2020-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
9#pragma once
10
11#include "pixmap_span.hpp"
12#include "../color/module.hpp"
13#include "../geometry/module.hpp"
14#include "../SIMD/module.hpp"
15#include "../utility/module.hpp"
16#include <algorithm>
17#include <bit>
18#include <array>
19
20namespace hi::inline v1 {
21
27 // Red, Green, Blue, Alpha in binary16 (native endian).
29
30public:
31 constexpr sfloat_rgba16() noexcept : v() {}
32
33 constexpr sfloat_rgba16(sfloat_rgba16 const &rhs) noexcept = default;
34 constexpr sfloat_rgba16(sfloat_rgba16 &&rhs) noexcept = default;
35 constexpr sfloat_rgba16 &operator=(sfloat_rgba16 const &rhs) noexcept = default;
36 constexpr sfloat_rgba16 &operator=(sfloat_rgba16 &&rhs) noexcept = default;
37
38 constexpr sfloat_rgba16(f16x4 const &rhs) noexcept : v(std::bit_cast<decltype(v)>(rhs)) {}
39
40 constexpr sfloat_rgba16 &operator=(f16x4 const &rhs) noexcept
41 {
42 v = std::bit_cast<decltype(v)>(rhs);
43 return *this;
44 }
45
46 constexpr explicit operator f16x4() const noexcept
47 {
48 return std::bit_cast<f16x4>(v);
49 }
50
51 constexpr sfloat_rgba16(f32x4 const &rhs) noexcept : sfloat_rgba16(static_cast<f16x4>(rhs)) {}
52
53 constexpr sfloat_rgba16 &operator=(f32x4 const &rhs) noexcept
54 {
55 return *this = static_cast<f16x4>(rhs);
56 }
57
58 constexpr sfloat_rgba16(color const &rhs) noexcept : sfloat_rgba16(static_cast<f16x4>(rhs)) {}
59
60 constexpr sfloat_rgba16 &operator=(color const &rhs) noexcept
61 {
62 return *this = static_cast<f16x4>(rhs);
63 }
64
65 constexpr explicit operator color() const noexcept
66 {
67 return color{static_cast<f16x4>(*this)};
68 }
69
70 [[nodiscard]] constexpr sfloat_rgba16(corner_radii const &rhs) noexcept : sfloat_rgba16(static_cast<f32x4>(rhs)) {}
71
72 [[nodiscard]] std::size_t hash() const noexcept
73 {
74 return hash_mix(v[0], v[1], v[2], v[3]);
75 }
76
77 [[nodiscard]] constexpr friend bool operator==(sfloat_rgba16 const &lhs, sfloat_rgba16 const &rhs) noexcept = default;
78
79 [[nodiscard]] friend sfloat_rgba16 make_transparent(sfloat_rgba16 const &rhs) noexcept
80 {
82 r.v = rhs.v;
83 std::get<3>(r.v) = 0x0000; // 0.0f
84 return r;
85 }
86};
87
88constexpr void fill(pixmap_span<sfloat_rgba16> image, f32x4 color) noexcept
89{
90 for (std::size_t y = 0; y != image.height(); ++y) {
91 hilet row = image[y];
92 for (std::size_t x = 0; x != image.width(); ++x) {
93 row[x] = color;
94 }
95 }
96}
97
98inline void composit(pixmap_span<sfloat_rgba16> under, pixmap_span<sfloat_rgba16 const> over) noexcept
99{
100 hi_assert(over.height() >= under.height());
101 hi_assert(over.width() >= under.width());
102
103 for (auto y = 0_uz; y != under.height(); ++y) {
104 hilet over_line = over[y];
105 hilet under_line = under[y];
106 for (auto x = 0_uz; x != under.width(); ++x) {
107 hilet &overPixel = over_line[x];
108 auto &underPixel = under_line[x];
109
110 underPixel = composit(static_cast<f16x4>(underPixel), static_cast<f16x4>(overPixel));
111 }
112 }
113}
114
115inline void composit(pixmap_span<sfloat_rgba16> under, color over, pixmap_span<uint8_t const> mask) noexcept
116{
117 hi_assert(mask.height() >= under.height());
118 hi_assert(mask.width() >= under.width());
119
120 auto mask_pixel = color{1.0f, 1.0f, 1.0f, 1.0f};
121
122 for (auto y = 0_uz; y != under.height(); ++y) {
123 hilet mask_line = mask[y];
124 hilet under_line = under[y];
125 for (auto x = 0_uz; x != under.width(); ++x) {
126 hilet mask_value = mask_line[x] / 255.0f;
127 mask_pixel.a() = mask_value;
128
129 auto &pixel = under_line[x];
130 pixel = composit(static_cast<color>(pixel), over * mask_pixel);
131 }
132 }
133}
134
135} // namespace hi::inline v1
136
137template<>
138struct std::hash<hi::sfloat_rgba16> {
139 std::size_t operator()(hi::sfloat_rgba16 const &rhs) const noexcept
140 {
141 return rhs.hash();
142 }
143};
Defines the pixmap_span type.
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
void composit(pixmap_span< sfloat_rgba16 > dst, hi::color color, graphic_path const &mask) noexcept
Composit color onto the destination image where the mask is solid.
geometry/margins.hpp
Definition cache.hpp:11
Definition graphic_path.hpp:18
4 x float16 pixel format.
Definition sfloat_rgba16.hpp:26
T operator()(T... args)