FastLED 3.9.15
Loading...
Searching...
No Matches
scale_up.cpp.hpp
Go to the documentation of this file.
1
2#include "fl/fx/2d/scale_up.h"
3
4#include "crgb.h"
5#include "fl/stl/stdint.h"
6#include "fl/gfx/upscale.h"
7#include "fl/stl/memory.h"
8#include "fl/math/xymap.h"
9#include "fl/stl/vector.h"
10#include "fl/stl/allocator.h"
11#include "fl/fx/fx2d.h"
12
13// Optimized for 2^n grid sizes in terms of both memory and performance.
14// If you are somehow running this on AVR then you probably want this if
15// you can make your grid size a power of 2.
16#define FASTLED_SCALE_UP_ALWAYS_POWER_OF_2 0 // 0 for always power of 2.
17// Uses more memory than FASTLED_SCALE_UP_ALWAYS_POWER_OF_2 but can handle
18// arbitrary grid sizes.
19#define FASTLED_SCALE_UP_HIGH_PRECISION 1 // 1 for always choose high precision.
20// Uses the most executable memory because both low and high precision versions
21// are compiled in. If the grid size is a power of 2 then the faster version is
22// used. Note that the floating point version has to be directly specified
23// because in testing it offered no benefits over the integer versions.
24#define FASTLED_SCALE_UP_DECIDE_AT_RUNTIME 2 // 2 for runtime decision.
25
26#define FASTLED_SCALE_UP_FORCE_FLOATING_POINT 3 // Warning, this is slow.
27
28#ifndef FASTLED_SCALE_UP
29#define FASTLED_SCALE_UP FASTLED_SCALE_UP_DECIDE_AT_RUNTIME
30#endif
31
32namespace fl {
33
34ScaleUp::ScaleUp(const XYMap& xymap, Fx2dPtr fx) : Fx2d(xymap), mDelegate(fx) {
35 // Turn off re-mapping of the delegate's XYMap, since bilinearExpand needs
36 // to work in screen coordinates. The final mapping will for this class will
37 // still be performed.
38 mDelegate->getXYMap().setRectangularGrid();
39}
40
42 if (mSurface.empty()) {
43 mSurface.resize(mDelegate->getNumLeds());
44 }
45 DrawContext delegateContext = context;
46 delegateContext.leds = fl::span<CRGB>(mSurface);
47 mDelegate->draw(delegateContext);
48
49 u16 in_w = mDelegate->getWidth();
50 u16 in_h = mDelegate->getHeight();
51 u16 out_w = getWidth();
52 u16 out_h = getHeight();
53 ;
54 if (in_w == out_w && in_h == out_h) {
55 noExpand(fl::span<const CRGB>(mSurface), context.leds, in_w, in_h);
56 } else {
57 expand(fl::span<const CRGB>(mSurface), context.leds, in_w, in_h, mXyMap);
58 }
59}
60
62 u16 height, const XYMap& mXyMap) {
63#if FASTLED_SCALE_UP == FASTLED_SCALE_UP_ALWAYS_POWER_OF_2
64 fl::upscalePowerOf2(input.data(), output.data(), static_cast<u8>(width), static_cast<u8>(height), mXyMap);
65#elif FASTLED_SCALE_UP == FASTLED_SCALE_UP_HIGH_PRECISION
66 fl::upscaleArbitrary(input.data(), output.data(), width, height, mXyMap);
67#elif FASTLED_SCALE_UP == FASTLED_SCALE_UP_DECIDE_AT_RUNTIME
68 fl::upscale(input.data(), output.data(), width, height, mXyMap);
69#elif FASTLED_SCALE_UP == FASTLED_SCALE_UP_FORCE_FLOATING_POINT
70 fl::upscaleFloat(input.data(), output.data(), static_cast<u8>(width), static_cast<u8>(height), mXyMap);
71#else
72#error "Invalid FASTLED_SCALE_UP"
73#endif
74}
75
77 u16 height) {
78 u16 n = mXyMap.getTotal();
79 for (u16 w = 0; w < width; w++) {
80 for (u16 h = 0; h < height; h++) {
81 u16 idx = mXyMap.mapToIndex(w, h);
82 if (idx < n) {
83 output[idx] = input[w * height + h];
84 }
85 }
86 }
87}
88
89} // namespace fl
XYMap xymap
XYMap mXyMap
Definition fx2d.h:30
Fx2d(const XYMap &xyMap)
Definition fx2d.h:19
u16 getWidth() const
Definition fx2d.h:24
u16 getHeight() const
Definition fx2d.h:23
void noExpand(fl::span< const CRGB > input, fl::span< CRGB > output, u16 width, u16 height)
Direct copy without expansion (used when resolutions match)
ScaleUp(const XYMap &xymap, Fx2dPtr fx)
Construct a ScaleUp effect wrapper.
void draw(DrawContext context) override
Render the effect by drawing delegate at low-res and scaling up.
fl::vector_psram< CRGB > mSurface
Low-resolution render buffer.
Definition scale_up.h:115
void expand(fl::span< const CRGB > input, fl::span< CRGB > output, u16 width, u16 height, const XYMap &mXyMap)
Expand a low-resolution buffer to high-resolution using bilinear interpolation.
Fx2dPtr mDelegate
The wrapped effect that renders at low resolution.
Definition scale_up.h:114
const T * data() const FL_NOEXCEPT
Definition span.h:461
unsigned char u8
Definition stdint.h:131
u8 u8 height
Definition blur.h:186
void upscaleArbitrary(const CRGB *input, CRGB *output, u16 inputWidth, u16 inputHeight, const XYMap &xyMap)
Performs bilinear interpolation for upscaling an image.
u8 width
Definition blur.h:186
u8 upscaleFloat(u8 v00, u8 v10, u8 v01, u8 v11, float dx, float dy)
void upscale(const CRGB *input, CRGB *output, u16 inputWidth, u16 inputHeight, const fl::XYMap &xyMap)
Definition upscale.h:56
void upscalePowerOf2(const CRGB *input, CRGB *output, u8 inputWidth, u8 inputHeight, const XYMap &xyMap)
Performs bilinear interpolation for upscaling an image.
Base definition for an LED controller.
Definition crgb.hpp:179
Expands a grid using bilinear interpolation for upscaling effects.
fl::span< CRGB > leds