FastLED 3.9.3
Loading...
Searching...
No Matches
scale_up.hpp
1
6
7#pragma once
8
9#include <stdint.h>
10
11#include "FastLED.h"
12#include "bilinear_expansion.h"
13#include "fx/fx2d.h"
14#include "lib8tion/random8.h"
15#include "noise.h"
16#include "ref.h"
17#include "xymap.h"
18
19// Optimized for 2^n grid sizes in terms of both memory and performance.
20// If you are somehow running this on AVR then you probably want this if
21// you can make your grid size a power of 2.
22#define FASTLED_SCALE_UP_ALWAYS_POWER_OF_2 0 // 0 for always power of 2.
23// Uses more memory than FASTLED_SCALE_UP_ALWAYS_POWER_OF_2 but can handle
24// arbitrary grid sizes.
25#define FASTLED_SCALE_UP_HIGH_PRECISION 1 // 1 for always choose high precision.
26// Uses the most executable memory because both low and high precision versions are compiled in.
27// If the grid size is a power of 2 then the faster version is used. Note that the floating point
28// version has to be directly specified because in testing it offered no benefits over the integer
29// versions.
30#define FASTLED_SCALE_UP_DECIDE_AT_RUNTIME 2 // 2 for runtime decision.
31
32#define FASTLED_SCALE_UP_FORCE_FLOATING_POINT 3 // Warning, this is slow.
33
34#ifndef FASTLED_SCALE_UP
35#define FASTLED_SCALE_UP FASTLED_SCALE_UP_DECIDE_AT_RUNTIME
36#endif
37
38FASTLED_NAMESPACE_BEGIN
39
40FASTLED_SMART_REF(ScaleUp);
41
42// Uses bilearn filtering to double the size of the grid.
43class ScaleUp : public FxGrid {
44 public:
45 ScaleUp(XYMap xymap, FxGridRef fx) : FxGrid(xymap), mDelegate(fx) {
46 // Turn off re-mapping of the delegate's XYMap, since bilinearExpand needs to
47 // work in screen coordinates. The final mapping will for this class will
48 // still be performed.
49 mDelegate->getXYMap().setRectangularGrid();
50 }
51 void lazyInit() override {}
52 void draw(DrawContext context) override {
53 if (!mSurface) {
54 mSurface.reset(new CRGB[mDelegate->getNumLeds()]);
55 }
56 DrawContext delegateContext = context;
57 delegateContext.leds = mSurface.get();
58 mDelegate->draw(delegateContext);
59
60 uint16_t in_w = mDelegate->getWidth();
61 uint16_t in_h = mDelegate->getHeight();
62 uint16_t out_w = getWidth();
63 uint16_t out_h = getHeight();;
64 if (in_w == out_w && in_h == out_h) {
65 noExpand(mSurface.get(), context.leds, in_w, in_h);
66 } else {
67 expand(mSurface.get(), context.leds, in_w, in_h, mXyMap);
68 }
69 }
70
71 void expand(const CRGB *input, CRGB *output, uint16_t width,
72 uint16_t height, XYMap mXyMap) {
73#if FASTLED_SCALE_UP == FASTLED_SCALE_UP_ALWAYS_POWER_OF_2
74 bilinearExpandPowerOf2(input, output, width, height, mXyMap);
75#elif FASTLED_SCALE_UP == FASTLED_SCALE_UP_HIGH_PRECISION
76 bilinearExpandArbitrary(input, output, width, height, mXyMap);
77#elif FASTLED_SCALE_UP == FASTLED_SCALE_UP_DECIDE_AT_RUNTIME
78 bilinearExpand(input, output, width, height, mXyMap);
79#elif FASTLED_SCALE_UP == FASTLED_SCALE_UP_FORCE_FLOATING_POINT
80 bilinearExpandFloat(input, output, width, height, mXyMap);
81#else
82#error "Invalid FASTLED_SCALE_UP"
83#endif
84 }
85
86 const char *fxName(int) const override { return "scale_up"; }
87
88 private:
89 // No expansion needed. Also useful for debugging.
90 void noExpand(const CRGB *input, CRGB *output, uint16_t width,
91 uint16_t height) {
92 uint16_t n = mXyMap.getTotal();
93 for (uint16_t w = 0; w < width; w++) {
94 for (uint16_t h = 0; h < height; h++) {
95 uint16_t idx = mXyMap.mapToIndex(w, h);
96 if (idx < n) {
97 output[idx] = input[w * height + h];
98 }
99 }
100 }
101 }
102 FxGridRef mDelegate;
103 scoped_array<CRGB> mSurface;
104};
105
106FASTLED_NAMESPACE_END
central include file for FastLED, defines the CFastLED class/object
Definition fx2d.h:16
void draw(DrawContext context) override
Definition scale_up.hpp:52
Definition xymap.h:39
Functions to generate and fill arrays with noise.
Fast, efficient random number generators specifically designed for high-performance LED programming.
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:39