HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
pixmap_span.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2023.
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 "../utility/utility.hpp"
12#include "../macros.hpp"
13#include <cstddef>
14#include <span>
15#include <memory>
16
17hi_export_module(hikogui.image.pixmap_span);
18
19hi_warning_push();
20// C26459: You called an STL function 'std::copy' with a raw pointer paramter... (stl.1)
21// Using iterators adds a lot of code without any extra safety.
22hi_warning_ignore_msvc(26459);
23
24hi_export namespace hi { inline namespace v1 {
25template<typename T, typename Allocator>
26class pixmap;
27
33template<typename T>
35public:
36 using value_type = T;
37 using reference = value_type&;
38 using const_reference = value_type const&;
39 using pointer = value_type *;
40 using const_pointer = value_type const *;
41 using row_type = std::span<value_type>;
42 using const_row_type = std::span<value_type const>;
43 using nc_value_type = std::remove_const_t<value_type>;
44 using size_type = size_t;
45
46 template<typename PixmapView>
47 struct row_iterator {
48 PixmapView *_ptr;
49 size_t _y;
50
51 // clang-format off
52 constexpr row_iterator(row_iterator const &) noexcept = default;
53 constexpr row_iterator(row_iterator &&) noexcept = default;
54 constexpr row_iterator &operator=(row_iterator const &) noexcept = default;
55 constexpr row_iterator &operator=(row_iterator &&) noexcept = default;
56 [[nodiscard]] constexpr row_iterator(PixmapView *ptr, size_t y) noexcept : _ptr(ptr), _y(y) {}
57 [[nodiscard]] constexpr friend bool operator==(row_iterator const &, row_iterator const &) noexcept = default;
58 constexpr row_iterator &operator++() noexcept { ++_y; return *this; }
59 constexpr row_iterator &operator++(int) noexcept { auto tmp = *this; ++_y; return tmp; }
60 constexpr row_iterator &operator--() noexcept { --_y; return *this; }
61 constexpr row_iterator &operator--(int) noexcept { auto tmp = *this; --_y; return tmp; }
62 [[nodiscard]] constexpr auto operator*() const noexcept { return (*_ptr)[_y]; }
63 // clang-format on
64 };
65
66 template<typename PixmapView>
67 struct row_range {
68 PixmapView *_ptr;
69
70 // clang-format off
71 constexpr row_range(row_range const &) noexcept = default;
72 constexpr row_range(row_range &&) noexcept = default;
73 constexpr row_range &operator=(row_range const &) noexcept = default;
74 constexpr row_range &operator=(row_range &&) noexcept = default;
75 [[nodiscard]] constexpr row_range(PixmapView *ptr) noexcept : _ptr(ptr) {}
76 [[nodiscard]] constexpr auto begin() const noexcept { return row_iterator{_ptr, 0_uz}; }
77 [[nodiscard]] constexpr auto end() const noexcept { return row_iterator{_ptr, _ptr->height()}; }
78 // clang-format on
79 };
80
81 ~pixmap_span() = default;
82 constexpr pixmap_span(pixmap_span const&) noexcept = default;
83 constexpr pixmap_span(pixmap_span&&) noexcept = default;
84 constexpr pixmap_span& operator=(pixmap_span const&) noexcept = default;
85 constexpr pixmap_span& operator=(pixmap_span&&) noexcept = default;
86 [[nodiscard]] constexpr pixmap_span() noexcept = default;
87
88 [[nodiscard]] constexpr pixmap_span(value_type *data, size_type width, size_type height, size_type stride) noexcept :
89 _data(data), _width(width), _height(height), _stride(stride)
90 {
91 }
92
93 [[nodiscard]] constexpr pixmap_span(value_type *data, size_type width, size_type height) noexcept :
94 pixmap_span(data, width, height, width)
95 {
96 }
97
98 template<std::same_as<std::remove_const_t<value_type>> O, typename Allocator>
99 [[nodiscard]] constexpr pixmap_span(pixmap<O, Allocator> const& other) noexcept :
100 pixmap_span(other.data(), other.width(), other.height())
101 {
102 }
103
104 template<std::same_as<std::remove_const_t<value_type>> O, typename Allocator>
105 [[nodiscard]] constexpr pixmap_span(pixmap<O, Allocator>& other) noexcept :
106 pixmap_span(other.data(), other.width(), other.height())
107 {
108 }
109
110 template<std::same_as<std::remove_const_t<value_type>> O, typename Allocator>
111 [[nodiscard]] constexpr pixmap_span(pixmap<O, Allocator>&& other) = delete;
112
113 [[nodiscard]] constexpr bool empty() const noexcept
114 {
115 return _width == 0 and _height == 0;
116 }
117
118 [[nodiscard]] constexpr size_type width() const noexcept
119 {
120 return _width;
121 }
122
123 [[nodiscard]] constexpr size_type height() const noexcept
124 {
125 return _height;
126 }
127
128 [[nodiscard]] constexpr size_type stride() const noexcept
129 {
130 return _stride;
131 }
132
133 [[nodiscard]] constexpr pointer data() noexcept
134 {
135 return _data;
136 }
137
138 [[nodiscard]] constexpr const_pointer data() const noexcept
139 {
140 return _data;
141 }
142
143 constexpr reference operator()(size_type x, size_type y) noexcept
144 {
145 hi_axiom(x < _width);
146 hi_axiom(y < _height);
147 return _data[y * _stride + x];
148 }
149
150 constexpr const_reference operator()(size_type x, size_type y) const noexcept
151 {
152 hi_axiom(x < _width);
153 hi_axiom(y < _height);
154 return _data[y * _stride + x];
155 }
156
157 [[nodiscard]] constexpr row_type operator[](size_type y) noexcept
158 {
159 hi_axiom(y < _height);
160 return {_data + y * _stride, _width};
161 }
162
163 [[nodiscard]] constexpr const_row_type operator[](size_type y) const noexcept
164 {
165 hi_axiom(y < _height);
166 return {_data + y * _stride, _width};
167 }
168
169 [[nodiscard]] constexpr auto rows() noexcept
170 {
171 return row_range{this};
172 }
173
174 [[nodiscard]] constexpr auto rows() const noexcept
175 {
176 return row_range{this};
177 }
178
179 [[nodiscard]] constexpr pixmap_span subimage(size_type x, size_type y, size_type new_width, size_type new_height) noexcept
180 {
181 return {_data + y * _stride + x, new_width, new_height, _stride};
182 }
183
184 [[nodiscard]] constexpr pixmap_span<value_type const>
185 subimage(size_type x, size_type y, size_type new_width, size_type new_height) const noexcept
186 {
187 return {_data + y * _stride + x, new_width, new_height, _stride};
188 }
189
190 constexpr friend void copy(pixmap_span src, pixmap_span<std::remove_const_t<value_type>> dst) noexcept
191 {
192 hi_axiom(src.width() == dst.width());
193 hi_axiom(src.height() == dst.height());
194
195 if (src.width() == src.stride() and dst.width() == dst.stride()) {
196 std::copy(src.data(), src.data() + src.width() * src.height(), dst.data());
197 } else {
198 for (auto y = 0_uz; y != src.height(); ++y) {
199 auto const src_line = src[y];
200 auto const dst_line = dst[y];
201 std::copy(src_line.begin(), src_line.end(), dst_line.begin());
202 }
203 }
204 }
205
206 constexpr friend void fill(pixmap_span dst, value_type value = value_type{}) noexcept
207 {
208 if (dst._width == dst._stride) {
209 std::fill_n(dst._data, dst._width, dst._height, value);
210 } else {
211 for (auto line: dst.rows()) {
212 std::fill(line.begin(), line.end(), value);
213 }
214 }
215 }
216
217private:
218 value_type *_data = nullptr;
219 size_type _width = 0;
220 size_type _height = 0;
221 size_type _stride = 0;
222};
223
224template<typename T, typename Allocator>
225pixmap_span(pixmap<T, Allocator> const& other) -> pixmap_span<T const>;
226
227template<typename T, typename Allocator>
228pixmap_span(pixmap<T, Allocator>& other) -> pixmap_span<T>;
229
230}} // namespace hi::v1
231
232hi_warning_pop();
@ other
The gui_event does not have associated data.
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A non-owning 2D pixel-based image.
Definition pixmap_span.hpp:34
A 2D pixel-based image.
Definition pixmap.hpp:38
Definition pixmap_span.hpp:47
Definition pixmap_span.hpp:67
T copy(T... args)
T fill(T... args)
T fill_n(T... args)