FastLED 3.9.15
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"
14
15namespace fl {
16namespace gfx {
17
19enum class LineCap { FLAT, ROUND, SQUARE };
20
21// Forward declarations
22template<typename RGB_T> struct Canvas;
23template<typename RGB_T> struct CanvasMapped;
24
25// Free function forward declarations
26template<int hRadius, int vRadius, typename RGB_T>
27void blurGaussian(Canvas<RGB_T>& canvas, fl::alpha8 dimFactor) FL_NOEXCEPT;
28
29template<int hRadius, int vRadius, typename RGB_T>
30void blurGaussian(Canvas<RGB_T>& canvas, fl::alpha16 dimFactor) FL_NOEXCEPT;
31
32template<int hRadius, int vRadius, typename RGB_T>
34
35// CanvasMapped blurGaussian forward declarations
36template<int hRadius, int vRadius, typename RGB_T>
37void blurGaussian(CanvasMapped<RGB_T>& canvas, fl::alpha8 dimFactor) FL_NOEXCEPT;
38
39template<int hRadius, int vRadius, typename RGB_T>
40void blurGaussian(CanvasMapped<RGB_T>& canvas, fl::alpha16 dimFactor) FL_NOEXCEPT;
41
42template<int hRadius, int vRadius, typename RGB_T>
46
47template<typename PixelT, typename Coord>
48void drawLine(Canvas<PixelT>& canvas, const PixelT& color, Coord x0, Coord y0, Coord x1, Coord y1,
50
51template<typename PixelT, typename Coord>
52void drawDisc(Canvas<PixelT>& canvas, const PixelT& color, Coord cx, Coord cy, Coord r,
54
55template<typename PixelT, typename Coord>
56void drawRing(Canvas<PixelT>& canvas, const PixelT& color, Coord cx, Coord cy, Coord r, Coord thickness,
58
59template<typename PixelT, typename Coord>
60void drawStrokeLine(Canvas<PixelT>& canvas, const PixelT& color, Coord x0, Coord y0, Coord x1, Coord y1,
62
65template<typename RGB_T>
66struct Canvas {
67 fl::variant<
71
72 RGB_T* pixels;
73 int width;
74 int height;
75
77 : ownership(buf), pixels(buf.data()), width(w), height(h) {}
79 : ownership(ptr), pixels(ptr.get()), width(w), height(h) {}
80
81 int size() const FL_NOEXCEPT { return width * height; }
82 RGB_T& at(int x, int y) FL_NOEXCEPT { return pixels[y * width + x]; }
83 const RGB_T& at(int x, int y) const FL_NOEXCEPT { return pixels[y * width + x]; }
84 bool has(int x, int y) const FL_NOEXCEPT { return x >= 0 && x < width && y >= 0 && y < height; }
85
86 template<int hRadius, int vRadius>
87 inline void blurGaussian(fl::alpha8 dimFactor) FL_NOEXCEPT {
89 }
90
91 template<int hRadius, int vRadius>
92 inline void blurGaussian(fl::alpha16 dimFactor) FL_NOEXCEPT {
94 }
95
96 template<int hRadius, int vRadius>
100
101 template<typename Coord>
102 inline void drawLine(const RGB_T& color, Coord x0, Coord y0, Coord x1, Coord y1,
104 gfx::drawLine(*this, color, x0, y0, x1, y1, mode);
105 }
106
107 template<typename Coord>
108 inline void drawDisc(const RGB_T& color, Coord cx, Coord cy, Coord r,
110 gfx::drawDisc(*this, color, cx, cy, r, mode);
111 }
112
113 template<typename Coord>
114 inline void drawRing(const RGB_T& color, Coord cx, Coord cy, Coord r, Coord thickness,
116 gfx::drawRing(*this, color, cx, cy, r, thickness, mode);
117 }
118
119 template<typename Coord>
120 inline void drawStrokeLine(const RGB_T& color, Coord x0, Coord y0, Coord x1, Coord y1, Coord thickness,
122 gfx::drawStrokeLine(*this, color, x0, y0, x1, y1, thickness, cap, mode);
123 }
124};
125
129template<typename RGB_T>
132 const XYMap* xymap;
133 int width;
135
137 : pixels(buf), xymap(&map),
138 width(map.getWidth()), height(map.getHeight()) {}
139
140 int size() const FL_NOEXCEPT { return width * height; }
141 RGB_T& at(int x, int y) FL_NOEXCEPT { return pixels[xymap->mapToIndex(x, y)]; }
142 const RGB_T& at(int x, int y) const FL_NOEXCEPT { return pixels[xymap->mapToIndex(x, y)]; }
143 bool has(int x, int y) const FL_NOEXCEPT { return x >= 0 && x < width && y >= 0 && y < height; }
144
145 template<int hRadius, int vRadius>
146 inline void blurGaussian(fl::alpha8 dimFactor) FL_NOEXCEPT {
147 gfx::blurGaussian<hRadius, vRadius>(*this, dimFactor);
148 }
149
150 template<int hRadius, int vRadius>
151 inline void blurGaussian(fl::alpha16 dimFactor) FL_NOEXCEPT {
152 gfx::blurGaussian<hRadius, vRadius>(*this, dimFactor);
153 }
154
155 template<int hRadius, int vRadius>
159};
160
161} // namespace gfx
162} // namespace fl
Unsigned alpha types with UNORM semantics (GPU industry standard).
Generic canvas for any pixel type (e.g.
Definition gfx.h:51
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_NOEXCEPT
============================================================================
Definition primitives.h:643
void blurGaussian(Canvas< RGB_T > &canvas, alpha8 dimFactor)
Compile-time Gaussian blur with independent H/V radii.
LineCap
Line cap styles for stroke operations.
Definition canvas.h:19
void drawDisc(Canvas< PixelT > &canvas, const PixelT &color, Coord cx, Coord cy, Coord r, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NOEXCEPT
Definition primitives.h:719
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_NOEXCEPT
Definition primitives.h:807
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_NOEXCEPT
Definition primitives.h:896
MapRedBlackTree< Key, T, Compare, fl::allocator_slab< char > > map
Definition map.h:283
gfx::LineCap LineCap
Line cap style.
Definition gfx.h:46
DrawMode
Definition draw_mode.h:5
@ DRAW_MODE_BLEND
Definition draw_mode.h:5
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
Unsigned 16-bit alpha / brightness — UNORM16.
Definition alpha.h:87
Unsigned 8-bit alpha / brightness — UNORM8.
Definition alpha.h:42
RGB_T & at(int x, int y) FL_NOEXCEPT
Definition canvas.h:82
void blurGaussian() FL_NOEXCEPT
Definition canvas.h:97
int size() const FL_NOEXCEPT
Definition canvas.h:81
fl::variant< fl::span< RGB_T >, fl::shared_ptr< RGB_T > > ownership
Definition canvas.h:70
RGB_T * pixels
Definition canvas.h:72
void drawRing(const RGB_T &color, Coord cx, Coord cy, Coord r, Coord thickness, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NOEXCEPT
Definition canvas.h:114
void drawLine(const RGB_T &color, Coord x0, Coord y0, Coord x1, Coord y1, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NOEXCEPT
Definition canvas.h:102
Canvas(fl::span< RGB_T > buf, int w, int h) FL_NOEXCEPT
Definition canvas.h:76
bool has(int x, int y) const FL_NOEXCEPT
Definition canvas.h:84
void blurGaussian(fl::alpha16 dimFactor) FL_NOEXCEPT
Definition canvas.h:92
Canvas(fl::shared_ptr< RGB_T > ptr, int w, int h) FL_NOEXCEPT
Definition canvas.h:78
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_NOEXCEPT
Definition canvas.h:120
void drawDisc(const RGB_T &color, Coord cx, Coord cy, Coord r, fl::DrawMode mode=fl::DrawMode::DRAW_MODE_BLEND) FL_NOEXCEPT
Definition canvas.h:108
const RGB_T & at(int x, int y) const FL_NOEXCEPT
Definition canvas.h:83
void blurGaussian(fl::alpha8 dimFactor) FL_NOEXCEPT
Definition canvas.h:87
void blurGaussian(fl::alpha16 dimFactor) FL_NOEXCEPT
Definition canvas.h:151
void blurGaussian(fl::alpha8 dimFactor) FL_NOEXCEPT
Definition canvas.h:146
const XYMap * xymap
Definition canvas.h:132
const RGB_T & at(int x, int y) const FL_NOEXCEPT
Definition canvas.h:142
bool has(int x, int y) const FL_NOEXCEPT
Definition canvas.h:143
int size() const FL_NOEXCEPT
Definition canvas.h:140
void blurGaussian() FL_NOEXCEPT
Definition canvas.h:156
fl::span< RGB_T > pixels
Definition canvas.h:131
CanvasMapped(fl::span< RGB_T > buf, const XYMap &map) FL_NOEXCEPT
Definition canvas.h:136
RGB_T & at(int x, int y) FL_NOEXCEPT
Definition canvas.h:141
XYMap-backed canvas for non-rectangular or remapped layouts.
Definition canvas.h:130