HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
sfloat_rgba16.hpp
1// Copyright Take Vos 2020-2021.
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
5#pragma once
6
7#include "../color/color.hpp"
8#include "../float16.hpp"
9#include "../pixel_map.hpp"
10#include "../geometry/corner_radii.hpp"
11#include "../rapid/numeric_array.hpp"
12#include "../hash.hpp"
13#include <algorithm>
14#include <bit>
15#include <array>
16
17namespace hi::inline v1 {
18
20 // Red, Green, Blue, Alpha in binary16 (native endian).
22
23public:
24 constexpr sfloat_rgba16() noexcept : v() {}
25
26 constexpr sfloat_rgba16(sfloat_rgba16 const &rhs) noexcept = default;
27 constexpr sfloat_rgba16(sfloat_rgba16 &&rhs) noexcept = default;
28 constexpr sfloat_rgba16 &operator=(sfloat_rgba16 const &rhs) noexcept = default;
29 constexpr sfloat_rgba16 &operator=(sfloat_rgba16 &&rhs) noexcept = default;
30
31 constexpr sfloat_rgba16(f16x4 const &rhs) noexcept : v(std::bit_cast<decltype(v)>(rhs)) {}
32
33 constexpr sfloat_rgba16 &operator=(f16x4 const &rhs) noexcept
34 {
35 v = std::bit_cast<decltype(v)>(rhs);
36 return *this;
37 }
38
39 constexpr explicit operator f16x4() const noexcept
40 {
41 return std::bit_cast<f16x4>(v);
42 }
43
44 constexpr sfloat_rgba16(f32x4 const &rhs) noexcept : sfloat_rgba16(static_cast<f16x4>(rhs)) {}
45
46 constexpr sfloat_rgba16 &operator=(f32x4 const &rhs) noexcept
47 {
48 return *this = static_cast<f16x4>(rhs);
49 }
50
51 constexpr sfloat_rgba16(color const &rhs) noexcept : sfloat_rgba16(static_cast<f16x4>(rhs)) {}
52
53 constexpr sfloat_rgba16 &operator=(color const &rhs) noexcept
54 {
55 return *this = static_cast<f16x4>(rhs);
56 }
57
58 constexpr explicit operator color() const noexcept
59 {
60 return color{static_cast<f16x4>(*this)};
61 }
62
63 [[nodiscard]] constexpr sfloat_rgba16(corner_radii const &rhs) noexcept : sfloat_rgba16(static_cast<f32x4>(rhs)) {}
64
65 [[nodiscard]] std::size_t hash() const noexcept
66 {
67 return hash_mix(v[0], v[1], v[2], v[3]);
68 }
69
70 [[nodiscard]] constexpr friend bool operator==(sfloat_rgba16 const &lhs, sfloat_rgba16 const &rhs) noexcept = default;
71
72 [[nodiscard]] friend sfloat_rgba16 make_transparent(sfloat_rgba16 const &rhs) noexcept
73 {
75 r.v = rhs.v;
76 std::get<3>(r.v) = 0x0000; // 0.0f
77 return r;
78 }
79};
80
81inline void fill(pixel_map<sfloat_rgba16> &image, f32x4 color) noexcept
82{
83 for (std::size_t y = 0; y != image.height(); ++y) {
84 auto row = image[y];
85 for (std::size_t x = 0; x != image.width(); ++x) {
86 row[x] = color;
87 }
88 }
89}
90
91inline void composit(pixel_map<sfloat_rgba16> &under, pixel_map<sfloat_rgba16> const &over) noexcept
92{
93 hi_assert(over.height() >= under.height());
94 hi_assert(over.width() >= under.width());
95
96 for (std::size_t rowNr = 0; rowNr != under.height(); ++rowNr) {
97 hilet overRow = over.at(rowNr);
98 auto underRow = under.at(rowNr);
99 for (std::size_t columnNr = 0; columnNr != under.width(); ++columnNr) {
100 hilet &overPixel = overRow[columnNr];
101 auto &underPixel = underRow[columnNr];
102
103 underPixel = composit(static_cast<f16x4>(underPixel), static_cast<f16x4>(overPixel));
104 }
105 }
106}
107
108inline void composit(pixel_map<sfloat_rgba16> &under, color over, pixel_map<uint8_t> const &mask) noexcept
109{
110 hi_assert(mask.height() >= under.height());
111 hi_assert(mask.width() >= under.width());
112
113 auto maskPixel = color{1.0f, 1.0f, 1.0f, 1.0f};
114
115 for (std::size_t rowNr = 0; rowNr != under.height(); ++rowNr) {
116 hilet maskRow = mask.at(rowNr);
117 auto underRow = under.at(rowNr);
118 for (std::size_t columnNr = 0; columnNr != under.width(); ++columnNr) {
119 hilet maskValue = maskRow[columnNr] / 255.0f;
120 maskPixel.a() = maskValue;
121
122 auto &pixel = underRow[columnNr];
123 pixel = composit(static_cast<color>(pixel), over * maskPixel);
124 }
125 }
126}
127
128} // namespace hi::inline v1
129
130template<>
131struct std::hash<hi::sfloat_rgba16> {
132 std::size_t operator()(hi::sfloat_rgba16 const &rhs) const noexcept
133 {
134 return rhs.hash();
135 }
136};
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
This is a RGBA floating point color.
Definition color.hpp:39
Definition corner_radii.hpp:9
A 2D canvas of pixels.
Definition pixel_map.hpp:110
Definition sfloat_rgba16.hpp:19
T operator()(T... args)
Definition datum.hpp:2458