FastLED 3.10.8
Loading...
Searching...
No Matches
blend.cpp.hpp
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 "fl/fx/2d/blend.h"
8
9#include "colorutils.h"
10#include "fl/fx/frame.h"
11#include "fl/fx/fx.h"
12#include "fl/stl/stdint.h"
13#include "fl/math/xymap.h"
14#include "fl/log/log.h"
15#include "fl/stl/noexcept.h"
16
17namespace fl {
18
20 // Warning, the xyMap will be the final transrformation applied to the
21 // frame. If the delegate Fx2d layers have their own transformation then
22 // both will be applied.
25}
26
28 fl::string out = "LayeredFx2d(";
29 for (size_t i = 0; i < mLayers.size(); ++i) {
30 out += mLayers[i].fx->fxName();
31 if (i != mLayers.size() - 1) {
32 out += ",";
33 }
34 }
35 out += ")";
36 return out;
37}
38
39void Blend2d::add(Fx2dPtr layer, const Params &p) FL_NO_EXCEPT {
40 if (!layer->getXYMap().isRectangularGrid()) {
41 if (!getXYMap().isRectangularGrid()) {
42 FL_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.");
43 layer->setXYMap(XYMap::constructRectangularGrid(layer->getWidth(), layer->getHeight()));
44 }
45 }
46 u8 blurAmount = p.blur_amount;
47 u8 blurPasses = p.blur_passes;
48 Entry entry(layer, blurAmount, blurPasses);
49 mLayers.push_back(entry);
50}
51
52void Blend2d::add(Fx2d &layer, const Params &p) FL_NO_EXCEPT {
53 Fx2dPtr fx = fl::make_shared_no_tracking(layer);
54 this->add(fx, p);
55}
56
58 mFrame->clear();
59 mFrameTransform->clear();
60
61 // Draw each layer in reverse order and applying the blending.
62 bool first = true;
63 for (auto it = mLayers.begin(); it != mLayers.end(); ++it) {
64 DrawContext tmp_ctx = context;
65 tmp_ctx.leds = mFrame->rgb();
66 auto &fx = it->fx;
67 fx->draw(tmp_ctx);
70 first = false;
71 // Apply the blur effect per effect.
72 u8 blur_amount = it->blur_amount;
73 if (blur_amount > 0) {
74 const XYMap &xyMap = fx->getXYMap();
75 u8 blur_passes = fl::max(1, it->blur_passes);
76 for (u8 i = 0; i < blur_passes; ++i) {
77 // Apply the blur effect
78 blur2d(mFrame->rgb().data(), mXyMap.getWidth(), mXyMap.getHeight(),
79 blur_amount, xyMap);
80 }
81 }
82 mFrame->draw(mFrameTransform->rgb(), mode);
83 }
84
85 if (mGlobalBlurAmount > 0) {
86 // Apply the blur effect
87 u16 width = mXyMap.getWidth();
88 u16 height = mXyMap.getHeight();
91 u8 blur_passes = fl::max(1, mGlobalBlurPasses);
92 for (u8 i = 0; i < blur_passes; ++i) {
93 // Apply the blur effect
95 }
96 }
97
98 // Copy the final result to the output
99 // memcpy(mFrameTransform->rgb(), context.leds, sizeof(CRGB) *
100 // mXyMap.getTotal());
101 mFrameTransform->drawXY(context.leds, mXyMap,
103}
104
106
107bool Blend2d::setParams(Fx2dPtr fx, const Params &p) FL_NO_EXCEPT {
108 u8 blur_amount = p.blur_amount;
109 u8 blur_passes = p.blur_passes;
110 for (auto &layer : mLayers) {
111 if (layer.fx == fx) {
112 layer.blur_amount = blur_amount;
113 layer.blur_passes = blur_passes;
114 return true;
115 }
116 }
117
118 FL_WARN("Fx2d not found in Blend2d::setBlurParams");
119 return false;
120}
121
123
124 Fx2dPtr fxPtr = fl::make_shared_no_tracking(fx);
125 return setParams(fxPtr, p);
126}
127
128} // namespace fl
XYMap xymap
fl::string fxName() const FL_NO_EXCEPT override
Definition blend.cpp.hpp:27
void add(Fx2dPtr layer, const Params &p=Params()) FL_NO_EXCEPT
Definition blend.cpp.hpp:39
void draw(DrawContext context) FL_NO_EXCEPT override
Definition blend.cpp.hpp:57
fl::shared_ptr< Frame > mFrameTransform
Definition blend.h:59
Blend2dParams Params
Definition blend.h:30
u8 mGlobalBlurAmount
Definition blend.h:60
void clear() FL_NO_EXCEPT
bool setParams(Fx2dPtr fx, const Params &p) FL_NO_EXCEPT
fl::shared_ptr< Frame > mFrame
Definition blend.h:58
u8 mGlobalBlurPasses
Definition blend.h:61
Blend2d(const XYMap &xymap) FL_NO_EXCEPT
Definition blend.cpp.hpp:19
vector< Entry > mLayers
Definition blend.h:57
XYMap & getXYMap() FL_NO_EXCEPT
Definition fx2d.h:27
XYMap mXyMap
Definition fx2d.h:31
u16 xyMap(u16 x, u16 y) const FL_NO_EXCEPT
Definition fx2d.h:21
Fx2d(const XYMap &xyMap) FL_NO_EXCEPT
Definition fx2d.h:20
static XYMap constructRectangularGrid(u16 width, u16 height, u16 offset=0) FL_NO_EXCEPT
Definition xymap.cpp.hpp:36
#define FL_WARN(...)
Definition log.h:437
Centralized logging categories for FastLED hardware interfaces and subsystems.
shared_ptr< T > make_shared(Args &&... args) FL_NO_EXCEPT
Definition shared_ptr.h:423
unsigned char u8
Definition stdint.h:131
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
void blur2d(fl::span< CRGB > leds, u8 width, u8 height, fract8 blur_amount, const XYMap &xymap) FL_NO_EXCEPT
Definition blur.h:153
u8 u8 height
Definition blur.h:186
u8 width
Definition blur.h:186
DrawMode
Definition draw_mode.h:5
@ DRAW_MODE_OVERWRITE
Definition draw_mode.h:5
@ DRAW_MODE_BLEND_BY_MAX_BRIGHTNESS
Definition draw_mode.h:5
float add(float &a, float &b) FL_NO_EXCEPT
shared_ptr< T > make_shared_no_tracking(T &obj) FL_NO_EXCEPT
Definition shared_ptr.h:468
Base definition for an LED controller.
Definition crgb.hpp:179
Definition blend.h:49
fl::span< CRGB > leds