FastLED 3.9.7
Loading...
Searching...
No Matches
bilinear_expansion.cpp
Go to the documentation of this file.
1
4
5#include <stdint.h>
6
8#include "crgb.h"
9#include "fl/namespace.h"
10#include "fl/xymap.h"
11
12namespace fl {
13
14
15uint8_t bilinearInterpolate(uint8_t v00, uint8_t v10, uint8_t v01,
16 uint8_t v11, uint16_t dx, uint16_t dy);
17
18uint8_t bilinearInterpolatePowerOf2(uint8_t v00, uint8_t v10, uint8_t v01,
19 uint8_t v11, uint8_t dx, uint8_t dy);
20
21
22void bilinearExpandArbitrary(const CRGB *input, CRGB *output, uint16_t inputWidth,
23 uint16_t inputHeight, XYMap xyMap) {
24 uint16_t n = xyMap.getTotal();
25 uint16_t outputWidth = xyMap.getWidth();
26 uint16_t outputHeight = xyMap.getHeight();
27 const uint16_t scale_factor = 256; // Using 8 bits for the fractional part
28
29 for (uint16_t y = 0; y < outputHeight; y++) {
30 for (uint16_t x = 0; x < outputWidth; x++) {
31 // Calculate the corresponding position in the input grid
32 uint32_t fx = ((uint32_t)x * (inputWidth - 1) * scale_factor) /
33 (outputWidth - 1);
34 uint32_t fy = ((uint32_t)y * (inputHeight - 1) * scale_factor) /
35 (outputHeight - 1);
36
37 uint16_t ix = fx / scale_factor; // Integer part of x
38 uint16_t iy = fy / scale_factor; // Integer part of y
39 uint16_t dx = fx % scale_factor; // Fractional part of x
40 uint16_t dy = fy % scale_factor; // Fractional part of y
41
42 uint16_t ix1 = (ix + 1 < inputWidth) ? ix + 1 : ix;
43 uint16_t iy1 = (iy + 1 < inputHeight) ? iy + 1 : iy;
44
45 uint16_t i00 = iy * inputWidth + ix;
46 uint16_t i10 = iy * inputWidth + ix1;
47 uint16_t i01 = iy1 * inputWidth + ix;
48 uint16_t i11 = iy1 * inputWidth + ix1;
49
50 CRGB c00 = input[i00];
51 CRGB c10 = input[i10];
52 CRGB c01 = input[i01];
53 CRGB c11 = input[i11];
54
55 CRGB result;
56 result.r = bilinearInterpolate(c00.r, c10.r, c01.r, c11.r, dx, dy);
57 result.g = bilinearInterpolate(c00.g, c10.g, c01.g, c11.g, dx, dy);
58 result.b = bilinearInterpolate(c00.b, c10.b, c01.b, c11.b, dx, dy);
59
60 uint16_t idx = xyMap.mapToIndex(x, y);
61 if (idx < n) {
62 output[idx] = result;
63 }
64 }
65 }
66}
67uint8_t bilinearInterpolate(uint8_t v00, uint8_t v10, uint8_t v01, uint8_t v11,
68 uint16_t dx, uint16_t dy) {
69 uint16_t dx_inv = 256 - dx;
70 uint16_t dy_inv = 256 - dy;
71
72 uint32_t w00 = (uint32_t)dx_inv * dy_inv;
73 uint32_t w10 = (uint32_t)dx * dy_inv;
74 uint32_t w01 = (uint32_t)dx_inv * dy;
75 uint32_t w11 = (uint32_t)dx * dy;
76
77 uint32_t sum = v00 * w00 + v10 * w10 + v01 * w01 + v11 * w11;
78
79 // Normalize the result by dividing by 65536 (shift right by 16 bits),
80 // with rounding
81 uint8_t result = (uint8_t)((sum + 32768) >> 16);
82
83 return result;
84}
85
86void bilinearExpandPowerOf2(const CRGB *input, CRGB *output, uint8_t inputWidth, uint8_t inputHeight, XYMap xyMap) {
87 uint8_t width = xyMap.getWidth();
88 uint8_t height = xyMap.getHeight();
89 if (width != xyMap.getWidth() || height != xyMap.getHeight()) {
90 // xyMap has width and height that do not fit in an uint16_t.
91 return;
92 }
93 uint16_t n = xyMap.getTotal();
94
95 for (uint8_t y = 0; y < height; y++) {
96 for (uint8_t x = 0; x < width; x++) {
97 // Use 8-bit fixed-point arithmetic with 8 fractional bits
98 // (scale factor of 256)
99 uint16_t fx = ((uint16_t)x * (inputWidth - 1) * 256) / (width - 1);
100 uint16_t fy =
101 ((uint16_t)y * (inputHeight - 1) * 256) / (height - 1);
102
103 uint8_t ix = fx >> 8; // Integer part
104 uint8_t iy = fy >> 8;
105 uint8_t dx = fx & 0xFF; // Fractional part
106 uint8_t dy = fy & 0xFF;
107
108 uint8_t ix1 = (ix + 1 < inputWidth) ? ix + 1 : ix;
109 uint8_t iy1 = (iy + 1 < inputHeight) ? iy + 1 : iy;
110
111 uint16_t i00 = iy * inputWidth + ix;
112 uint16_t i10 = iy * inputWidth + ix1;
113 uint16_t i01 = iy1 * inputWidth + ix;
114 uint16_t i11 = iy1 * inputWidth + ix1;
115
116 CRGB c00 = input[i00];
117 CRGB c10 = input[i10];
118 CRGB c01 = input[i01];
119 CRGB c11 = input[i11];
120
121 CRGB result;
122 result.r = bilinearInterpolatePowerOf2(c00.r, c10.r, c01.r, c11.r, dx, dy);
123 result.g = bilinearInterpolatePowerOf2(c00.g, c10.g, c01.g, c11.g, dx, dy);
124 result.b = bilinearInterpolatePowerOf2(c00.b, c10.b, c01.b, c11.b, dx, dy);
125
126 uint16_t idx = xyMap.mapToIndex(x, y);
127 if (idx < n) {
128 output[idx] = result;
129 }
130 }
131 }
132}
133
134uint8_t bilinearInterpolatePowerOf2(uint8_t v00, uint8_t v10, uint8_t v01,
135 uint8_t v11, uint8_t dx, uint8_t dy) {
136 uint16_t dx_inv = 256 - dx; // 0 to 256
137 uint16_t dy_inv = 256 - dy; // 0 to 256
138
139 // Scale down weights to fit into uint16_t
140 uint16_t w00 = (dx_inv * dy_inv) >> 8; // Max value 256
141 uint16_t w10 = (dx * dy_inv) >> 8;
142 uint16_t w01 = (dx_inv * dy) >> 8;
143 uint16_t w11 = (dx * dy) >> 8;
144
145 // Sum of weights should be approximately 256
146 uint16_t weight_sum = w00 + w10 + w01 + w11;
147
148 // Compute the weighted sum of pixel values
149 uint16_t sum = v00 * w00 + v10 * w10 + v01 * w01 + v11 * w11;
150
151 // Normalize the result
152 uint8_t result = (sum + (weight_sum >> 1)) / weight_sum;
153
154 return result;
155}
156
157
158// Floating-point version of bilinear interpolation
159uint8_t bilinearInterpolateFloat(uint8_t v00, uint8_t v10, uint8_t v01,
160 uint8_t v11, float dx, float dy) {
161 float dx_inv = 1.0f - dx;
162 float dy_inv = 1.0f - dy;
163
164 // Calculate the weights for each corner
165 float w00 = dx_inv * dy_inv;
166 float w10 = dx * dy_inv;
167 float w01 = dx_inv * dy;
168 float w11 = dx * dy;
169
170 // Compute the weighted sum
171 float sum = v00 * w00 + v10 * w10 + v01 * w01 + v11 * w11;
172
173 // Clamp the result to [0, 255] and round
174 uint8_t result = static_cast<uint8_t>(sum + 0.5f);
175
176 return result;
177}
178
179// Floating-point version for arbitrary grid sizes
180void bilinearExpandArbitraryFloat(const CRGB *input, CRGB *output,
181 uint16_t inputWidth, uint16_t inputHeight,
182 XYMap xyMap) {
183 uint16_t n = xyMap.getTotal();
184 uint16_t outputWidth = xyMap.getWidth();
185 uint16_t outputHeight = xyMap.getHeight();
186
187 for (uint16_t y = 0; y < outputHeight; y++) {
188 for (uint16_t x = 0; x < outputWidth; x++) {
189 // Map output pixel to input grid position
190 float fx = static_cast<float>(x) * (inputWidth - 1) / (outputWidth - 1);
191 float fy = static_cast<float>(y) * (inputHeight - 1) / (outputHeight - 1);
192
193 uint16_t ix = static_cast<uint16_t>(fx);
194 uint16_t iy = static_cast<uint16_t>(fy);
195 float dx = fx - ix;
196 float dy = fy - iy;
197
198 uint16_t ix1 = (ix + 1 < inputWidth) ? ix + 1 : ix;
199 uint16_t iy1 = (iy + 1 < inputHeight) ? iy + 1 : iy;
200
201 uint16_t i00 = iy * inputWidth + ix;
202 uint16_t i10 = iy * inputWidth + ix1;
203 uint16_t i01 = iy1 * inputWidth + ix;
204 uint16_t i11 = iy1 * inputWidth + ix1;
205
206 CRGB c00 = input[i00];
207 CRGB c10 = input[i10];
208 CRGB c01 = input[i01];
209 CRGB c11 = input[i11];
210
211 CRGB result;
212 result.r = bilinearInterpolateFloat(c00.r, c10.r, c01.r, c11.r, dx, dy);
213 result.g = bilinearInterpolateFloat(c00.g, c10.g, c01.g, c11.g, dx, dy);
214 result.b = bilinearInterpolateFloat(c00.b, c10.b, c01.b, c11.b, dx, dy);
215
216 uint16_t idx = xyMap.mapToIndex(x, y);
217 if (idx < n) {
218 output[idx] = result;
219 }
220 }
221 }
222}
223
224// Floating-point version for power-of-two grid sizes
225void bilinearExpandFloat(const CRGB *input, CRGB *output,
226 uint8_t inputWidth, uint8_t inputHeight,
227 XYMap xyMap) {
228 uint8_t outputWidth = xyMap.getWidth();
229 uint8_t outputHeight = xyMap.getHeight();
230 if (outputWidth != xyMap.getWidth() || outputHeight != xyMap.getHeight()) {
231 // xyMap has width and height that do not fit in a uint8_t.
232 return;
233 }
234 uint16_t n = xyMap.getTotal();
235
236 for (uint8_t y = 0; y < outputHeight; y++) {
237 for (uint8_t x = 0; x < outputWidth; x++) {
238 // Map output pixel to input grid position
239 float fx = static_cast<float>(x) * (inputWidth - 1) / (outputWidth - 1);
240 float fy = static_cast<float>(y) * (inputHeight - 1) / (outputHeight - 1);
241
242 uint8_t ix = static_cast<uint8_t>(fx);
243 uint8_t iy = static_cast<uint8_t>(fy);
244 float dx = fx - ix;
245 float dy = fy - iy;
246
247 uint8_t ix1 = (ix + 1 < inputWidth) ? ix + 1 : ix;
248 uint8_t iy1 = (iy + 1 < inputHeight) ? iy + 1 : iy;
249
250 uint16_t i00 = iy * inputWidth + ix;
251 uint16_t i10 = iy * inputWidth + ix1;
252 uint16_t i01 = iy1 * inputWidth + ix;
253 uint16_t i11 = iy1 * inputWidth + ix1;
254
255 CRGB c00 = input[i00];
256 CRGB c10 = input[i10];
257 CRGB c01 = input[i01];
258 CRGB c11 = input[i11];
259
260 CRGB result;
261 result.r = bilinearInterpolateFloat(c00.r, c10.r, c01.r, c11.r, dx, dy);
262 result.g = bilinearInterpolateFloat(c00.g, c10.g, c01.g, c11.g, dx, dy);
263 result.b = bilinearInterpolateFloat(c00.b, c10.b, c01.b, c11.b, dx, dy);
264
265 uint16_t idx = xyMap.mapToIndex(x, y);
266 if (idx < n) {
267 output[idx] = result;
268 }
269 }
270 }
271}
272
273} // namespace fl
Demonstrates how to mix noise generation with color palettes on a 2D LED matrix.
Defines the red, green, and blue (RGB) pixel struct.
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
void bilinearExpandArbitrary(const CRGB *input, CRGB *output, uint16_t inputWidth, uint16_t inputHeight, XYMap xyMap)
Performs bilinear interpolation for upscaling an image.
void bilinearExpandPowerOf2(const CRGB *input, CRGB *output, uint8_t inputWidth, uint8_t inputHeight, XYMap xyMap)
Performs bilinear interpolation for upscaling an image.
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54
uint8_t r
Red channel value.
Definition crgb.h:58
uint8_t g
Green channel value.
Definition crgb.h:62
uint8_t b
Blue channel value.
Definition crgb.h:66