FastLED 3.9.15
Loading...
Searching...
No Matches
blend.cpp
Go to the documentation of this file.
1/*
2Fx2d class that allows to blend multiple Fx2d layers together.
3The bottom layer is always drawn at full capacity. Upper layers
4are blended by the the max luminance of the components.
5*/
6
7#include "blend.h"
8#include "colorutils.h"
9#include "pixelset.h"
10
11#include "fl/stdint.h"
12
13namespace fl {
14
16 // Warning, the xyMap will be the final transrformation applied to the
17 // frame. If the delegate Fx2d layers have their own transformation then
18 // both will be applied.
21}
22
24 fl::string out = "LayeredFx2d(";
25 for (size_t i = 0; i < mLayers.size(); ++i) {
26 out += mLayers[i].fx->fxName();
27 if (i != mLayers.size() - 1) {
28 out += ",";
29 }
30 }
31 out += ")";
32 return out;
33}
34
35void Blend2d::add(Fx2dPtr layer, const Params &p) {
36 if (!layer->getXYMap().isRectangularGrid()) {
37 if (!getXYMap().isRectangularGrid()) {
38 FASTLED_WARN("Blend2d has a xymap, but so does the Sub layer " << layer->fxName() << ", the sub layer will have it's map replaced with a rectangular map, to avoid double transformation.");
39 layer->setXYMap(XYMap::constructRectangularGrid(layer->getWidth(), layer->getHeight()));
40 }
41 }
42 uint8_t blurAmount = p.blur_amount;
43 uint8_t blurPasses = p.blur_passes;
44 Entry entry(layer, blurAmount, blurPasses);
45 mLayers.push_back(entry);
46}
47
48void Blend2d::add(Fx2d &layer, const Params &p) {
49 Fx2dPtr fx = fl::make_shared_no_tracking(layer);
50 this->add(fx, p);
51}
52
54 mFrame->clear();
55 mFrameTransform->clear();
56
57 // Draw each layer in reverse order and applying the blending.
58 bool first = true;
59 for (auto it = mLayers.begin(); it != mLayers.end(); ++it) {
60 DrawContext tmp_ctx = context;
61 tmp_ctx.leds = mFrame->rgb();
62 auto &fx = it->fx;
63 fx->draw(tmp_ctx);
66 first = false;
67 // Apply the blur effect per effect.
68 uint8_t blur_amount = it->blur_amount;
69 if (blur_amount > 0) {
70 const XYMap &xyMap = fx->getXYMap();
71 uint8_t blur_passes = MAX(1, it->blur_passes);
72 for (uint8_t i = 0; i < blur_passes; ++i) {
73 // Apply the blur effect
74 blur2d(mFrame->rgb(), mXyMap.getWidth(), mXyMap.getHeight(),
75 blur_amount, xyMap);
76 }
77 }
78 mFrame->draw(mFrameTransform->rgb(), mode);
79 }
80
81 if (mGlobalBlurAmount > 0) {
82 // Apply the blur effect
83 uint16_t width = mXyMap.getWidth();
84 uint16_t height = mXyMap.getHeight();
86 CRGB *rgb = mFrameTransform->rgb();
87 uint8_t blur_passes = MAX(1, mGlobalBlurPasses);
88 for (uint8_t i = 0; i < blur_passes; ++i) {
89 // Apply the blur effect
90 blur2d(rgb, width, height, mGlobalBlurAmount, rect);
91 }
92 }
93
94 // Copy the final result to the output
95 // memcpy(mFrameTransform->rgb(), context.leds, sizeof(CRGB) *
96 // mXyMap.getTotal());
97 mFrameTransform->drawXY(context.leds, mXyMap,
99}
100
101void Blend2d::clear() { mLayers.clear(); }
102
103bool Blend2d::setParams(Fx2dPtr fx, const Params &p) {
104 uint8_t blur_amount = p.blur_amount;
105 uint8_t blur_passes = p.blur_passes;
106 for (auto &layer : mLayers) {
107 if (layer.fx == fx) {
108 layer.blur_amount = blur_amount;
109 layer.blur_passes = blur_passes;
110 return true;
111 }
112 }
113
114 FASTLED_WARN("Fx2d not found in Blend2d::setBlurParams");
115 return false;
116}
117
118bool Blend2d::setParams(Fx2d &fx, const Params &p) {
119
120 Fx2dPtr fxPtr = fl::make_shared_no_tracking(fx);
121 return setParams(fxPtr, p);
122}
123
124} // namespace fl
XYMap xymap(WIDTH, HEIGHT, SERPENTINE)
uint8_t mGlobalBlurPasses
Definition blend.h:62
fl::string fxName() const override
Definition blend.cpp:23
Blend2d(const XYMap &xymap)
Definition blend.cpp:15
Blend2dParams Params
Definition blend.h:31
void add(Fx2dPtr layer, const Params &p=Params())
Definition blend.cpp:35
FramePtr mFrame
Definition blend.h:59
uint8_t mGlobalBlurAmount
Definition blend.h:61
bool setParams(Fx2dPtr fx, const Params &p)
Definition blend.cpp:103
FramePtr mFrameTransform
Definition blend.h:60
HeapVector< Entry > mLayers
Definition blend.h:58
void draw(DrawContext context) override
Definition blend.cpp:53
void clear()
Definition blend.cpp:101
XYMap mXyMap
Definition fx2d.h:31
XYMap & getXYMap()
Definition fx2d.h:27
uint16_t xyMap(uint16_t x, uint16_t y) const
Definition fx2d.h:21
Fx2d(const XYMap &xyMap)
Definition fx2d.h:20
_DrawContext DrawContext
Definition fx.h:21
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0)
Definition xymap.cpp:34
void blur2d(CRGB *leds, fl::u8 width, fl::u8 height, fract8 blur_amount, const XYMap &xymap)
Two-dimensional blur filter.
Definition blur.cpp:72
#define MAX(a, b)
Definition math_macros.h:37
shared_ptr< T > make_shared_no_tracking(T &obj)
Definition shared_ptr.h:379
shared_ptr< T > make_shared(Args &&... args)
Definition shared_ptr.h:348
DrawMode
Definition draw_mode.h:5
@ DRAW_MODE_BLEND_BY_MAX_BRIGHTNESS
Definition draw_mode.h:5
@ DRAW_MODE_OVERWRITE
Definition draw_mode.h:5
fl::string Str
Definition str.h:36
IMPORTANT!
Definition crgb.h:20
uint8_t blur_passes
Definition blend.h:24
uint8_t blur_amount
Definition blend.h:23
Declares classes for managing logical groups of LEDs.
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
Definition blend.h:50
#define FASTLED_WARN
Definition warn.h:7