FastLED 3.9.15
Loading...
Searching...
No Matches
lut.h
Go to the documentation of this file.
1#pragma once
2
3/*
4LUT - Look up table implementation for various types.
5*/
6
7#include "fl/stl/allocator.h"
8#include "fl/stl/shared_ptr.h" // For FASTLED_SHARED_PTR macros
9// IWYU pragma: begin_keep
10#include "fl/stl/unique_ptr.h"
11// IWYU pragma: end_keep // For fl::unique_ptr<T>
12
13#include "fl/stl/int.h"
14#include "fl/math/geometry.h"
15#include "fl/stl/noexcept.h"
16namespace fl {
17
18// LUT holds a look up table to map data from one
19// value to another. This can be quite big (1/3rd of the frame buffer)
20// so fl::shared_ptr is used to allow memory sharing.
21
22template <typename T> class LUT;
23
28
33
34// Templated lookup table.
35template <typename T> class LUT {
36 public:
39 mDataHandle.reset(ptr);
40 data = ptr;
41 }
42 // In this version the data is passed in but not managed by this object.
43 LUT(u32 length, T *data) FL_NOEXCEPT : length(length) { this->data = data; }
48
49 const T &operator[](u32 index) const FL_NOEXCEPT { return data[index]; }
50
51 const T &operator[](u16 index) const FL_NOEXCEPT { return data[index]; }
52
54
55 const T *getData() const FL_NOEXCEPT { return data; }
56
57 u32 size() const FL_NOEXCEPT { return length; }
58
60 if (length == 0)
61 return T();
62 if (alpha == 0)
63 return data[0];
64 if (alpha == 255)
65 return data[length - 1];
66
67 // treat alpha/255 as fraction, scale to [0..length-1]
68 u32 maxIndex = length - 1;
69 u32 pos = u32(alpha) * maxIndex; // numerator
70 u32 idx0 = pos / 255; // floor(position)
71 u32 idx1 = idx0 < maxIndex ? idx0 + 1 : maxIndex;
72 u8 blend = pos % 255; // fractional part
73
74 const T &a = data[idx0];
75 const T &b = data[idx1];
76 // a + (b-a) * blend/255
77 return a + (b - a) * blend / 255;
78 }
79
80 T interp16(u16 alpha) FL_NOEXCEPT {
81 if (length == 0)
82 return T();
83 if (alpha == 0)
84 return data[0];
85 if (alpha == 65535)
86 return data[length - 1];
87
88 // treat alpha/65535 as fraction, scale to [0..length-1]
89 u32 maxIndex = length - 1;
90 u32 pos = u32(alpha) * maxIndex; // numerator
91 u32 idx0 = pos / 65535; // floor(position)
92 u32 idx1 = idx0 < maxIndex ? idx0 + 1 : maxIndex;
93 u16 blend = pos % 65535; // fractional part
94
95 const T &a = data[idx0];
96 const T &b = data[idx1];
97 // a + (b-a) * blend/65535
98 return a + (b - a) * blend / 65535;
99 }
100
101 private:
103 T *data = nullptr;
105};
106
107} // namespace fl
uint8_t pos
Definition Blur.ino:11
const T & operator[](u16 index) const FL_NOEXCEPT
Definition lut.h:51
T interp16(u16 alpha) FL_NOEXCEPT
Definition lut.h:80
LUT(u32 length, T *data) FL_NOEXCEPT
Definition lut.h:43
const T & operator[](u32 index) const FL_NOEXCEPT
Definition lut.h:49
fl::unique_ptr< u16 > mDataHandle
Definition lut.h:102
T * getDataMutable() FL_NOEXCEPT
Definition lut.h:53
const T * getData() const FL_NOEXCEPT
Definition lut.h:55
u16 * data
Definition lut.h:103
~LUT() FL_NOEXCEPT
Definition lut.h:44
u32 size() const FL_NOEXCEPT
Definition lut.h:57
T interp8(u8 alpha) FL_NOEXCEPT
Definition lut.h:59
LUT(u32 length) FL_NOEXCEPT
Definition lut.h:37
u32 length
Definition lut.h:104
Definition lut.h:35
static void Free(T *p) FL_NOEXCEPT
Definition allocator.h:124
static T * Alloc(fl::size n) FL_NOEXCEPT
Definition allocator.h:119
unsigned char u8
Definition stdint.h:131
LUT< vec2< u16 > > LUTXY16
Definition lut.h:25
LUT< vec3f > LUTXYZFLOAT
Definition lut.h:27
LUT< u16 > LUT16
Definition lut.h:24
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
LUT< vec2f > LUTXYFLOAT
Definition lut.h:26
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT
#define FASTLED_SHARED_PTR_NO_FWD(type)
Definition shared_ptr.h:546