FastLED 3.9.15
Loading...
Searching...
No Matches
tile2x2.cpp
Go to the documentation of this file.
1#include "fl/tile2x2.h"
2#include "crgb.h"
3#include "fl/draw_visitor.h"
4#include "fl/math_macros.h"
5#include "fl/raster.h"
6#include "fl/raster_sparse.h"
7#include "fl/unused.h"
8#include "fl/warn.h"
9#include "fl/xymap.h"
10#include "fl/vector.h"
11
12namespace fl {
13
14namespace {
15static vec2<u16> wrap(const vec2<u16> &v, const vec2<u16> &size) {
16 // Wrap the vector v around the size
17 return vec2<u16>(v.x % size.x, v.y % size.y);
18}
19
20static vec2<u16> wrap_x(const vec2<u16> &v, const u16 width) {
21 // Wrap the x component of the vector v around the size
22 return vec2<u16>(v.x % width, v.y);
23}
24} // namespace
25
27 mData[0][0] = {vec2<u16>(0, 0), 0};
28 mData[0][1] = {vec2<u16>(0, 1), 0};
29 mData[1][0] = {vec2<u16>(1, 0), 0};
30 mData[1][1] = {vec2<u16>(1, 1), 0};
31}
32
34 XYRasterU8Sparse *out_raster) {
35 out_raster->rasterize(tiles);
36}
37
38void Tile2x2_u8::draw(const CRGB &color, const XYMap &xymap, CRGB *out) const {
39 XYDrawComposited visitor(color, xymap, out);
40 draw(xymap, visitor);
41}
42
44 // scale the tile
45 if (scale == 255) {
46 return;
47 }
48 for (int x = 0; x < 2; ++x) {
49 for (int y = 0; y < 2; ++y) {
50 u16 value = at(x, y);
51 at(x, y) = (value * scale) >> 8;
52 }
53 }
54}
55
57 const vec2<u16> origin = from.origin();
58 at(0, 0) = {wrap_x(vec2<u16>(origin.x, origin.y), width), from.at(0, 0)};
59 at(0, 1) = {wrap_x(vec2<u16>(origin.x, origin.y + 1), width), from.at(0, 1)};
60 at(1, 0) = {wrap_x(vec2<u16>(origin.x + 1, origin.y), width), from.at(1, 0)};
61 at(1, 1) = {wrap_x(vec2<u16>(origin.x + 1, origin.y + 1), width),
62 from.at(1, 1)};
63}
64
66 // Wrap around the edges
67 x = (x + 2) % 2;
68 y = (y + 2) % 2;
69 return mData[y][x];
70}
71
72
74 mData[0][0] = data[0][0];
75 mData[0][1] = data[0][1];
76 mData[1][0] = data[1][0];
77 mData[1][1] = data[1][1];
78}
79
80
82 // Wrap around the edges
83 x = (x + 2) % 2;
84 y = (y + 2) % 2;
85 return mData[y][x];
86}
87
89 u16 height) {
90 const vec2<u16> origin = from.origin();
91 at(0, 0) = {wrap(vec2<u16>(origin.x, origin.y), vec2<u16>(width, height)),
92 from.at(0, 0)};
93 at(0, 1) = {wrap(vec2<u16>(origin.x, origin.y + 1), vec2<u16>(width, height)),
94 from.at(0, 1)};
95 at(1, 0) = {wrap(vec2<u16>(origin.x + 1, origin.y), vec2<u16>(width, height)),
96 from.at(1, 0)};
97 at(1,
98 1) = {wrap(vec2<u16>(origin.x + 1, origin.y + 1), vec2<u16>(width, height)),
99 from.at(1, 1)};
100}
101
103 u8 max = 0;
104 max = MAX(max, at(0, 0));
105 max = MAX(max, at(0, 1));
106 max = MAX(max, at(1, 0));
107 max = MAX(max, at(1, 1));
108 return max;
109}
110
113 for (int x = 0; x < 2; ++x) {
114 for (int y = 0; y < 2; ++y) {
115 result.at(x, y) = MAX(a.at(x, y), b.at(x, y));
116 }
117 }
118 return result;
119}
120
122 vec2<u16> min = mOrigin;
123 vec2<u16> max = mOrigin + vec2<u16>(2, 2);
124 return rect<u16>(min, max);
125}
126
129
130 // Clamp t to [0, 1]
131 if (t <= 0.0f) {
132 result.push_back(a);
133 return result;
134 }
135 if (t >= 1.0f) {
136 result.push_back(b);
137 return result;
138 }
139
140 // Create interpolated tile
141 Tile2x2_u8_wrap interpolated;
142
143 // Interpolate each of the 4 positions
144 for (u16 x = 0; x < 2; ++x) {
145 for (u16 y = 0; y < 2; ++y) {
146 const auto& data_a = a.at(x, y);
147 const auto& data_b = b.at(x, y);
148
149 // For now, assume positions are the same or close enough
150 // Use position from 'a' as the base
151 vec2<u16> pos = data_a.first;
152
153 // Simple linear interpolation for alpha values
154 u8 alpha_a = data_a.second;
155 u8 alpha_b = data_b.second;
156
157 // Linear interpolation: a + t * (b - a)
158 float alpha_float = alpha_a + t * (alpha_b - alpha_a);
159 u8 interpolated_alpha = static_cast<u8>(alpha_float + 0.5f); // Round to nearest
160
161 interpolated.mData[y][x] = {pos, interpolated_alpha};
162 }
163 }
164
165 result.push_back(interpolated);
166 return result;
167}
168
169} // namespace fl
int y
Definition simple.h:93
int x
Definition simple.h:92
XYMap xymap(WIDTH, HEIGHT, SERPENTINE)
uint8_t pos
Definition Blur.ino:11
Entry & at(u16 x, u16 y)
Definition tile2x2.cpp:65
fl::pair< vec2< u16 >, u8 > Entry
Definition tile2x2.h:108
static vector_fixed< Tile2x2_u8_wrap, 2 > Interpolate(const Tile2x2_u8_wrap &a, const Tile2x2_u8_wrap &b, float t)
Definition tile2x2.cpp:127
Entry[2][2] Data
Definition tile2x2.h:109
static Tile2x2_u8 MaxTile(const Tile2x2_u8 &a, const Tile2x2_u8 &b)
Definition tile2x2.cpp:111
u8 & at(int x, int y)
Definition tile2x2.h:39
void scale(u8 scale)
Definition tile2x2.cpp:43
static void Rasterize(const span< const Tile2x2_u8 > &tiles, XYRasterU8Sparse *output)
Definition tile2x2.cpp:33
u8 maxValue() const
Definition tile2x2.cpp:102
void draw(const CRGB &color, const XYMap &xymap, CRGB *out) const
Definition tile2x2.cpp:38
vec2< u16 > origin() const
Definition tile2x2.h:56
rect< u16 > bounds() const
bounds => [begin_x, end_x) (where end_x is exclusive)
Definition tile2x2.cpp:121
Tile2x2_u8()=default
vec2< u16 > mOrigin
Definition tile2x2.h:99
void rasterize(const vec2< u16 > &pt, u8 value)
Result type for promise operations.
Defines the red, green, and blue (RGB) pixel struct.
static uint32_t t
Definition Luminova.h:54
#define MAX(a, b)
Definition math_macros.h:37
static vec2< u16 > wrap_x(const vec2< u16 > &v, const u16 width)
Definition tile2x2.cpp:20
static vec2< u16 > wrap(const vec2< u16 > &v, const vec2< u16 > &size)
Definition tile2x2.cpp:15
unsigned char u8
Definition int.h:17
Slice< T > span
Definition span.h:8
FixedVector< T, INLINED_SIZE > vector_fixed
Definition vector.h:1217
IMPORTANT!
Definition crgb.h:20
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
value_type y
Definition geometry.h:191
value_type x
Definition geometry.h:190