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