FastLED 3.9.13
Loading...
Searching...
No Matches
scale_up.cpp
1
2#include <stdint.h>
3#include "scale_up.h"
4
5#define FASTLED_INTERNAL
6#include "FastLED.h"
8#include "fl/ptr.h"
9#include "fx/fx2d.h"
10#include "lib8tion/random8.h"
11#include "noise.h"
12#include "fl/xymap.h"
13
14
15
16
17// Optimized for 2^n grid sizes in terms of both memory and performance.
18// If you are somehow running this on AVR then you probably want this if
19// you can make your grid size a power of 2.
20#define FASTLED_SCALE_UP_ALWAYS_POWER_OF_2 0 // 0 for always power of 2.
21// Uses more memory than FASTLED_SCALE_UP_ALWAYS_POWER_OF_2 but can handle
22// arbitrary grid sizes.
23#define FASTLED_SCALE_UP_HIGH_PRECISION 1 // 1 for always choose high precision.
24// Uses the most executable memory because both low and high precision versions
25// are compiled in. If the grid size is a power of 2 then the faster version is
26// used. Note that the floating point version has to be directly specified
27// because in testing it offered no benefits over the integer versions.
28#define FASTLED_SCALE_UP_DECIDE_AT_RUNTIME 2 // 2 for runtime decision.
29
30#define FASTLED_SCALE_UP_FORCE_FLOATING_POINT 3 // Warning, this is slow.
31
32#ifndef FASTLED_SCALE_UP
33#define FASTLED_SCALE_UP FASTLED_SCALE_UP_DECIDE_AT_RUNTIME
34#endif
35
36namespace fl {
37
38ScaleUp::ScaleUp(XYMap xymap, Fx2dPtr fx) : Fx2d(xymap), mDelegate(fx) {
39 // Turn off re-mapping of the delegate's XYMap, since bilinearExpand needs
40 // to work in screen coordinates. The final mapping will for this class will
41 // still be performed.
42 mDelegate->getXYMap().setRectangularGrid();
43}
44
45void ScaleUp::draw(DrawContext context) {
46 if (!mSurface) {
47 mSurface.reset(new CRGB[mDelegate->getNumLeds()]);
48 }
49 DrawContext delegateContext = context;
50 delegateContext.leds = mSurface.get();
51 mDelegate->draw(delegateContext);
52
53 uint16_t in_w = mDelegate->getWidth();
54 uint16_t in_h = mDelegate->getHeight();
55 uint16_t out_w = getWidth();
56 uint16_t out_h = getHeight();
57 ;
58 if (in_w == out_w && in_h == out_h) {
59 noExpand(mSurface.get(), context.leds, in_w, in_h);
60 } else {
61 expand(mSurface.get(), context.leds, in_w, in_h, mXyMap);
62 }
63}
64
65void ScaleUp::expand(const CRGB *input, CRGB *output, uint16_t width,
66 uint16_t height, XYMap mXyMap) {
67#if FASTLED_SCALE_UP == FASTLED_SCALE_UP_ALWAYS_POWER_OF_2
68 bilinearExpandPowerOf2(input, output, width, height, mXyMap);
69#elif FASTLED_SCALE_UP == FASTLED_SCALE_UP_HIGH_PRECISION
70 bilinearExpandArbitrary(input, output, width, height, mXyMap);
71#elif FASTLED_SCALE_UP == FASTLED_SCALE_UP_DECIDE_AT_RUNTIME
72 bilinearExpand(input, output, width, height, mXyMap);
73#elif FASTLED_SCALE_UP == FASTLED_SCALE_UP_FORCE_FLOATING_POINT
74 bilinearExpandFloat(input, output, width, height, mXyMap);
75#else
76#error "Invalid FASTLED_SCALE_UP"
77#endif
78}
79
80void ScaleUp::noExpand(const CRGB *input, CRGB *output, uint16_t width,
81 uint16_t height) {
82 uint16_t n = mXyMap.getTotal();
83 for (uint16_t w = 0; w < width; w++) {
84 for (uint16_t h = 0; h < height; h++) {
85 uint16_t idx = mXyMap.mapToIndex(w, h);
86 if (idx < n) {
87 output[idx] = input[w * height + h];
88 }
89 }
90 }
91}
92
93} // namespace fl
central include file for FastLED, defines the CFastLED class/object
Demonstrates how to mix noise generation with color palettes on a 2D LED matrix.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
void bilinearExpandArbitrary(const CRGB *input, CRGB *output, uint16_t inputWidth, uint16_t inputHeight, XYMap xyMap)
Performs bilinear interpolation for upscaling an image.
void bilinearExpandPowerOf2(const CRGB *input, CRGB *output, uint8_t inputWidth, uint8_t inputHeight, XYMap xyMap)
Performs bilinear interpolation for upscaling an image.
Functions to generate and fill arrays with noise.
Fast, efficient random number generators specifically designed for high-performance LED programming.
Expands a grid using bilinear interpolation and scaling up.
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54