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/memory.h"
10#include "fl/stdint.h"
11
12#include "fl/int.h"
13#include "fl/geometry.h"
14#include "fl/namespace.h"
15
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 a Referent 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) : length(length) { this->data = data; }
44 ~LUT() {
46 data = mDataHandle.get();
47 }
48
49 const T &operator[](u32 index) const { return data[index]; }
50
51 const T &operator[](u16 index) const { return data[index]; }
52
53 T *getDataMutable() { return data; }
54
55 const T *getData() const { return data; }
56
57 u32 size() const { return length; }
58
59 T interp8(u8 alpha) {
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) {
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[](u32 index) const
Definition lut.h:49
T * getDataMutable()
Definition lut.h:53
fl::unique_ptr< u16 > mDataHandle
Definition lut.h:102
u32 size() const
Definition lut.h:57
LUT(u32 length, T *data)
Definition lut.h:43
T interp16(u16 alpha)
Definition lut.h:80
u16 * data
Definition lut.h:103
const T & operator[](u16 index) const
Definition lut.h:51
LUT(u32 length)
Definition lut.h:37
T interp8(u8 alpha)
Definition lut.h:59
~LUT()
Definition lut.h:44
const T * getData() const
Definition lut.h:55
u32 length
Definition lut.h:104
Definition lut.h:35
static T * Alloc(fl::size n)
Definition allocator.h:44
static void Free(T *p)
Definition allocator.h:49
Implements the FastLED namespace macros.
unsigned char u8
Definition int.h:17
LUT< vec2< u16 > > LUTXY16
Definition lut.h:25
LUT< vec3f > LUTXYZFLOAT
Definition lut.h:27
LUT< vec2f > LUTXYFLOAT
Definition lut.h:26
LUT< u16 > LUT16
Definition lut.h:24
CRGB blend(const CRGB &p1, const CRGB &p2, fract8 amountOfP2)
IMPORTANT!
Definition crgb.h:20
#define FASTLED_SMART_PTR_NO_FWD(type)
Definition ptr.h:41