FastLED 3.10.8
Loading...
Searching...
No Matches
sample.cpp.hpp
Go to the documentation of this file.
1#include "fl/gfx/sample.h"
2
3#include "fl/math/math.h"
4#include "fl/math/xymap.h"
5#include "crgb.h"
6#include "fl/stl/noexcept.h"
7
8namespace fl {
9
10CRGB sample(const CRGB *grid, const XYMap &xyMap, float x, float y,
12 if (mode == SampleMode::SAMPLE_BILINEAR) {
13 return sampleBilinear(grid, xyMap, x, y);
14 }
15 return sampleNearest(grid, xyMap, x, y);
16}
17
18CRGB sampleBilinear(const CRGB *grid, const XYMap &xyMap, float x, float y) FL_NO_EXCEPT {
19 int x0 = static_cast<int>(x);
20 int y0 = static_cast<int>(y);
21 int x1 = min(x0 + 1, static_cast<int>(xyMap.getWidth()) - 1);
22 int y1 = min(y0 + 1, static_cast<int>(xyMap.getHeight()) - 1);
23
24 // Clamp x0 and y0 to valid range
25 x0 = max(0, min(x0, static_cast<int>(xyMap.getWidth()) - 1));
26 y0 = max(0, min(y0, static_cast<int>(xyMap.getHeight()) - 1));
27
28 float fx = x - x0;
29 float fy = y - y0;
30
31 // Get four neighboring pixels
32 CRGB c00 = grid[xyMap.mapToIndex(x0, y0)];
33 CRGB c10 = grid[xyMap.mapToIndex(x1, y0)];
34 CRGB c01 = grid[xyMap.mapToIndex(x0, y1)];
35 CRGB c11 = grid[xyMap.mapToIndex(x1, y1)];
36
37 // Interpolate
38 CRGB c0 = CRGB::blend(c00, c10, static_cast<u8>(fx * 255));
39 CRGB c1 = CRGB::blend(c01, c11, static_cast<u8>(fx * 255));
40 return CRGB::blend(c0, c1, static_cast<u8>(fy * 255));
41}
42
43CRGB sampleNearest(const CRGB *grid, const XYMap &xyMap, float x, float y) FL_NO_EXCEPT {
44 int xi = static_cast<int>(x + 0.5f); // Round to nearest
45 int yi = static_cast<int>(y + 0.5f);
46 xi = max(0, min(xi, static_cast<int>(xyMap.getWidth()) - 1));
47 yi = max(0, min(yi, static_cast<int>(xyMap.getHeight()) - 1));
48 return grid[xyMap.mapToIndex(xi, yi)];
49}
50
51} // namespace fl
fl::XYMap xyMap
Defines the 8-bit red, green, and blue (RGB) pixel type in the fl namespace.
unsigned char u8
Definition stdint.h:131
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
CRGB sample(const CRGB *grid, const XYMap &xyMap, float x, float y, SampleMode mode) FL_NO_EXCEPT
Sample a pixel from a 2D CRGB grid at floating-point coordinates.
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NO_EXCEPT
Definition math.h:71
CRGB sampleBilinear(const CRGB *grid, const XYMap &xyMap, float x, float y) FL_NO_EXCEPT
Bilinear interpolation sample from a 2D CRGB grid.
SampleMode
Interpolation mode for sampling a 2D grid.
Definition sample.h:14
@ SAMPLE_BILINEAR
Bilinear interpolation (smooth)
Definition sample.h:16
CRGB sampleNearest(const CRGB *grid, const XYMap &xyMap, float x, float y) FL_NO_EXCEPT
Nearest-neighbor sample from a 2D CRGB grid.
Base definition for an LED controller.
Definition crgb.hpp:179
2D grid sampling with bilinear and nearest-neighbor interpolation
static CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2) FL_NO_EXCEPT
Definition crgb.cpp.hpp:55
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38