FastLED 3.10.10
Loading...
Searching...
No Matches
canvas.h
Go to the documentation of this file.
1#pragma once
2
5
6#include "fl/math/alpha.h"
7#include "fl/math/xymap.h"
8#include "fl/gfx/draw_mode.h"
9#include "fl/stl/int.h"
10#include "fl/stl/span.h"
11#include "fl/stl/variant.h"
12#include "fl/stl/shared_ptr.h"
13#include "fl/stl/noexcept.h"
15
16namespace fl {
17namespace gfx {
18
20enum class LineCap { FLAT, ROUND, SQUARE };
21
24template<typename Coord>
25struct PolyPoint {
26 Coord x;
27 Coord y;
28};
29
38enum class PolyFill { Exact, ABuffer };
39
58#ifndef FL_GFX_POLYGON_DEFAULT_MAXN
59#if FL_PLATFORM_HAS_SMALL_STACK
60#define FL_GFX_POLYGON_DEFAULT_MAXN 8
61#else
62#define FL_GFX_POLYGON_DEFAULT_MAXN 32
63#endif
64#endif
65
66#ifndef FL_GFX_POLYGON_DEFAULT_CHUNK
67#if FL_PLATFORM_HAS_SMALL_STACK
68#define FL_GFX_POLYGON_DEFAULT_CHUNK 16
69#else
70#define FL_GFX_POLYGON_DEFAULT_CHUNK 64
71#endif
72#endif
73
74// Forward declarations
75template<typename RGB_T> struct Canvas;
76template<typename RGB_T> struct CanvasMapped;
77
78// Free function forward declarations
79template<int hRadius, int vRadius, typename RGB_T>
80void blurGaussian(Canvas<RGB_T>& canvas, fl::alpha8 dimFactor) FL_NO_EXCEPT;
81
82template<int hRadius, int vRadius, typename RGB_T>
83void blurGaussian(Canvas<RGB_T>& canvas, fl::alpha16 dimFactor) FL_NO_EXCEPT;
84
85template<int hRadius, int vRadius, typename RGB_T>
86void blurGaussian(Canvas<RGB_T>& canvas) FL_NO_EXCEPT;
87
88// CanvasMapped blurGaussian forward declarations
89template<int hRadius, int vRadius, typename RGB_T>
90void blurGaussian(CanvasMapped<RGB_T>& canvas, fl::alpha8 dimFactor) FL_NO_EXCEPT;
91
92template<int hRadius, int vRadius, typename RGB_T>
93void blurGaussian(CanvasMapped<RGB_T>& canvas, fl::alpha16 dimFactor) FL_NO_EXCEPT;
94
95template<int hRadius, int vRadius, typename RGB_T>
99
100template<typename PixelT, typename Coord>
101void drawLine(Canvas<PixelT>& canvas, const PixelT& color, Coord x0, Coord y0, Coord x1, Coord y1,
103
104template<typename PixelT, typename Coord>
105void drawDisc(Canvas<PixelT>& canvas, const PixelT& color, Coord cx, Coord cy, Coord r,
107
108template<typename PixelT, typename Coord>
109void drawRing(Canvas<PixelT>& canvas, const PixelT& color, Coord cx, Coord cy, Coord r, Coord thickness,
111
112template<typename PixelT, typename Coord>
113void drawStrokeLine(Canvas<PixelT>& canvas, const PixelT& color, Coord x0, Coord y0, Coord x1, Coord y1,
115
119template<typename PixelT, typename Coord, int MAXN = FL_GFX_POLYGON_DEFAULT_MAXN,
122void drawPolygon(Canvas<PixelT>& canvas, const PixelT& color,
123 fl::span<const PolyPoint<Coord>> pts,
125
126// Array overload: lets Coord be deduced from a C array (span is a
127// converting constructor, so it cannot participate in deduction).
128template<typename PixelT, typename Coord, int N, int MAXN = FL_GFX_POLYGON_DEFAULT_MAXN,
131void drawPolygon(Canvas<PixelT>& canvas, const PixelT& color,
132 const PolyPoint<Coord> (&pts)[N],
134
137template<typename RGB_T>
138struct Canvas {
139 fl::variant<
143
144 RGB_T* pixels;
145 int width;
147
149 : ownership(buf), pixels(buf.data()), width(w), height(h) {}
151 : ownership(ptr), pixels(ptr.get()), width(w), height(h) {}
152
153 int size() const FL_NO_EXCEPT { return width * height; }
154 RGB_T& at(int x, int y) FL_NO_EXCEPT { return pixels[y * width + x]; }
155 const RGB_T& at(int x, int y) const FL_NO_EXCEPT { return pixels[y * width + x]; }
156 bool has(int x, int y) const FL_NO_EXCEPT { return x >= 0 && x < width && y >= 0 && y < height; }
157
158 template<int hRadius, int vRadius>
159 inline void blurGaussian(fl::alpha8 dimFactor) FL_NO_EXCEPT {
160 gfx::blurGaussian<hRadius, vRadius>(*this, dimFactor);
161 }
162
163 template<int hRadius, int vRadius>
164 inline void blurGaussian(fl::alpha16 dimFactor) FL_NO_EXCEPT {
165 gfx::blurGaussian<hRadius, vRadius>(*this, dimFactor);
166 }
167
168 template<int hRadius, int vRadius>
172
173 template<typename Coord>
174 inline void drawLine(const RGB_T& color, Coord x0, Coord y0, Coord x1, Coord y1,
176 gfx::drawLine(*this, color, x0, y0, x1, y1, mode);
177 }
178
179 template<typename Coord>
180 inline void drawDisc(const RGB_T& color, Coord cx, Coord cy, Coord r,
182 gfx::drawDisc(*this, color, cx, cy, r, mode);
183 }
184
185 template<typename Coord>
186 inline void drawRing(const RGB_T& color, Coord cx, Coord cy, Coord r, Coord thickness,
188 gfx::drawRing(*this, color, cx, cy, r, thickness, mode);
189 }
190
191 template<typename Coord>
192 inline void drawStrokeLine(const RGB_T& color, Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness,
194 gfx::drawStrokeLine(*this, color, x0, y0, x1, y1, thickness, cap, mode);
195 }
196
197 template<typename Coord, int MAXN = FL_GFX_POLYGON_DEFAULT_MAXN,
204
205 template<typename Coord, int N, int MAXN = FL_GFX_POLYGON_DEFAULT_MAXN,
212};
213
217template<typename RGB_T>
220 const XYMap* xymap;
221 int width;
223
225 : pixels(buf), xymap(&map),
226 width(map.getWidth()), height(map.getHeight()) {}
227
228 int size() const FL_NO_EXCEPT { return width * height; }
229 RGB_T& at(int x, int y) FL_NO_EXCEPT { return pixels[xymap->mapToIndex(x, y)]; }
230 const RGB_T& at(int x, int y) const FL_NO_EXCEPT { return pixels[xymap->mapToIndex(x, y)]; }
231 bool has(int x, int y) const FL_NO_EXCEPT { return x >= 0 && x < width && y >= 0 && y < height; }
232
233 template<int hRadius, int vRadius>
234 inline void blurGaussian(fl::alpha8 dimFactor) FL_NO_EXCEPT {
235 gfx::blurGaussian<hRadius, vRadius>(*this, dimFactor);
236 }
237
238 template<int hRadius, int vRadius>
239 inline void blurGaussian(fl::alpha16 dimFactor) FL_NO_EXCEPT {
240 gfx::blurGaussian<hRadius, vRadius>(*this, dimFactor);
241 }
242
243 template<int hRadius, int vRadius>
247};
248
249} // namespace gfx
250} // namespace fl
fl::CanvasRGB canvas(fl::span< CRGB >(leds, NUM_LEDS), WIDTH, HEIGHT)
Unsigned alpha types with UNORM semantics (GPU industry standard).
#define FL_GFX_POLYGON_DEFAULT_CHUNK
Definition canvas.h:70
#define FL_GFX_POLYGON_DEFAULT_MAXN
Default drawPolygon tier (MAXN vertex cap, CHUNK accumulator px) Both kernels keep their whole workin...
Definition canvas.h:62
Generic canvas for any pixel type (e.g.
Definition gfx.h:62
PolyFill
Fill backend for drawPolygon Both backends use the even-odd rule and the same row/vertex conventions;...
Definition canvas.h:38
void drawStrokeLine(Canvas< PixelT > &canvas, const PixelT &color, Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness, LineCap cap, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
LineCap
Line cap styles for stroke operations.
Definition canvas.h:20
void drawLine(Canvas< PixelT > &canvas, const PixelT &color, Coord x0, Coord y0, Coord x1, Coord y1, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
============================================================================
void blurGaussian(Canvas< RGB_T > &canvas, alpha8 dimFactor) FL_NO_EXCEPT
Compile-time Gaussian blur with independent H/V radii.
void drawRing(Canvas< PixelT > &canvas, const PixelT &color, Coord cx, Coord cy, Coord r, Coord thickness, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
void drawPolygon(Canvas< PixelT > &canvas, const PixelT &color, fl::span< const PolyPoint< Coord > > pts, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
Fill selects the polygon backend (PolyFill::Exact by default; PolyFill::ABuffer for the faster 8x4 A-...
void drawDisc(Canvas< PixelT > &canvas, const PixelT &color, Coord cx, Coord cy, Coord r, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
Polygon vertex (x, y coordinate pair) for drawPolygon C++11 aggregate: PolyPoint<float> p{3....
Definition canvas.h:25
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
MapRedBlackTree< Key, T, Compare, fl::allocator_slab< char > > map
Definition map.h:283
gfx::PolyPoint< Coord > PolyPoint
Polygon vertex (x, y coordinate pair) for drawPolygon.
Definition gfx.h:57
gfx::LineCap LineCap
Line cap style.
Definition gfx.h:53
DrawMode
Definition draw_mode.h:5
@ DRAW_MODE_BLEND
Definition draw_mode.h:5
Base definition for an LED controller.
#define FL_NO_EXCEPT
Unsigned 16-bit alpha / brightness — UNORM16.
Definition alpha.h:87
Unsigned 8-bit alpha / brightness — UNORM8.
Definition alpha.h:42
void drawStrokeLine(const RGB_T &color, Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness, LineCap cap=LineCap::FLAT, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
Definition canvas.h:192
Canvas(fl::span< RGB_T > buf, int w, int h) FL_NO_EXCEPT
Definition canvas.h:148
void blurGaussian(fl::alpha8 dimFactor) FL_NO_EXCEPT
Definition canvas.h:159
fl::variant< fl::span< RGB_T >, fl::shared_ptr< RGB_T > > ownership
Definition canvas.h:142
RGB_T * pixels
Definition canvas.h:144
const RGB_T & at(int x, int y) const FL_NO_EXCEPT
Definition canvas.h:155
RGB_T & at(int x, int y) FL_NO_EXCEPT
Definition canvas.h:154
void blurGaussian() FL_NO_EXCEPT
Definition canvas.h:169
void drawPolygon(const RGB_T &color, const PolyPoint< Coord >(&pts)[N], fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
Definition canvas.h:208
int size() const FL_NO_EXCEPT
Definition canvas.h:153
void drawRing(const RGB_T &color, Coord cx, Coord cy, Coord r, Coord thickness, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
Definition canvas.h:186
void drawLine(const RGB_T &color, Coord x0, Coord y0, Coord x1, Coord y1, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
Definition canvas.h:174
void drawPolygon(const RGB_T &color, fl::span< const PolyPoint< Coord > > pts, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
Definition canvas.h:200
Canvas(fl::shared_ptr< RGB_T > ptr, int w, int h) FL_NO_EXCEPT
Definition canvas.h:150
void drawDisc(const RGB_T &color, Coord cx, Coord cy, Coord r, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NO_EXCEPT
Definition canvas.h:180
void blurGaussian(fl::alpha16 dimFactor) FL_NO_EXCEPT
Definition canvas.h:164
bool has(int x, int y) const FL_NO_EXCEPT
Definition canvas.h:156
void blurGaussian(fl::alpha16 dimFactor) FL_NO_EXCEPT
Definition canvas.h:239
const RGB_T & at(int x, int y) const FL_NO_EXCEPT
Definition canvas.h:230
const XYMap * xymap
Definition canvas.h:220
void blurGaussian() FL_NO_EXCEPT
Definition canvas.h:244
int size() const FL_NO_EXCEPT
Definition canvas.h:228
void blurGaussian(fl::alpha8 dimFactor) FL_NO_EXCEPT
Definition canvas.h:234
RGB_T & at(int x, int y) FL_NO_EXCEPT
Definition canvas.h:229
fl::span< RGB_T > pixels
Definition canvas.h:219
CanvasMapped(fl::span< RGB_T > buf, const XYMap &map) FL_NO_EXCEPT
Definition canvas.h:224
bool has(int x, int y) const FL_NO_EXCEPT
Definition canvas.h:231
XYMap-backed canvas for non-rectangular or remapped layouts.
Definition canvas.h:218