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