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/allocator.h"
8#include "fl/force_inline.h"
9#include "fl/ptr.h"
10#include <stdint.h>
11
12#include "fl/geometry.h"
13#include "fl/namespace.h"
14
15namespace fl {
16
17// LUT holds a look up table to map data from one
18// value to another. This can be quite big (1/3rd of the frame buffer)
19// so a Referent is used to allow memory sharing.
20
21template <typename T> class LUT;
22
27
32
33// Templated lookup table.
34template <typename T> class LUT : public fl::Referent {
35 public:
36 LUT(uint32_t length) : length(length) {
38 mDataHandle.reset(ptr);
39 data = ptr;
40 }
41 // In this version the data is passed in but not managed by this object.
42 LUT(uint32_t length, T *data) : length(length) { this->data = data; }
43 ~LUT() {
45 data = mDataHandle.get();
46 }
47
48 const T &operator[](uint32_t index) const { return data[index]; }
49
50 const T &operator[](uint16_t index) const { return data[index]; }
51
52 T *getDataMutable() { return data; }
53
54 const T *getData() const { return data; }
55
56 uint32_t size() const { return length; }
57
58 T interp8(uint8_t alpha) {
59 if (length == 0)
60 return T();
61 if (alpha == 0)
62 return data[0];
63 if (alpha == 255)
64 return data[length - 1];
65
66 // treat alpha/255 as fraction, scale to [0..length-1]
67 uint32_t maxIndex = length - 1;
68 uint32_t pos = uint32_t(alpha) * maxIndex; // numerator
69 uint32_t idx0 = pos / 255; // floor(position)
70 uint32_t idx1 = idx0 < maxIndex ? idx0 + 1 : maxIndex;
71 uint8_t blend = pos % 255; // fractional part
72
73 const T &a = data[idx0];
74 const T &b = data[idx1];
75 // a + (b-a) * blend/255
76 return a + (b - a) * blend / 255;
77 }
78
79 T interp16(uint16_t alpha) {
80 if (length == 0)
81 return T();
82 if (alpha == 0)
83 return data[0];
84 if (alpha == 65535)
85 return data[length - 1];
86
87 // treat alpha/65535 as fraction, scale to [0..length-1]
88 uint32_t maxIndex = length - 1;
89 uint32_t pos = uint32_t(alpha) * maxIndex; // numerator
90 uint32_t idx0 = pos / 65535; // floor(position)
91 uint32_t idx1 = idx0 < maxIndex ? idx0 + 1 : maxIndex;
92 uint16_t blend = pos % 65535; // fractional part
93
94 const T &a = data[idx0];
95 const T &b = data[idx1];
96 // a + (b-a) * blend/65535
97 return a + (b - a) * blend / 65535;
98 }
99
100 private:
102 T *data = nullptr;
103 uint32_t length;
104};
105
106} // namespace fl
uint8_t pos
Definition Blur.ino:11
LUT(uint32_t length, T *data)
Definition lut.h:42
T * getDataMutable()
Definition lut.h:52
T interp8(uint8_t alpha)
Definition lut.h:58
const T & operator[](uint32_t index) const
Definition lut.h:48
uint16_t * data
Definition lut.h:102
fl::scoped_ptr< uint16_t > mDataHandle
Definition lut.h:101
T interp16(uint16_t alpha)
Definition lut.h:79
LUT(uint32_t length)
Definition lut.h:36
~LUT()
Definition lut.h:43
const T * getData() const
Definition lut.h:54
uint32_t length
Definition lut.h:103
uint32_t size() const
Definition lut.h:56
const T & operator[](uint16_t index) const
Definition lut.h:50
Definition lut.h:34
static T * Alloc(size_t n)
Definition allocator.h:15
static void Free(T *p)
Definition allocator.h:20
Implements the FastLED namespace macros.
LUT< vec3f > LUTXYZFLOAT
Definition lut.h:26
LUT< vec2f > LUTXYFLOAT
Definition lut.h:25
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
LUT< vec2< uint16_t > > LUTXY16
Definition lut.h:24
LUT< uint16_t > LUT16
Definition lut.h:23
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
#define FASTLED_SMART_PTR_NO_FWD(type)
Definition ptr.h:39