15uint8_t bilinearInterpolate(uint8_t v00, uint8_t v10, uint8_t v01,
16 uint8_t v11, uint16_t dx, uint16_t dy);
18uint8_t bilinearInterpolatePowerOf2(uint8_t v00, uint8_t v10, uint8_t v01,
19 uint8_t v11, uint8_t dx, uint8_t dy);
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;
29 for (uint16_t y = 0; y < outputHeight; y++) {
30 for (uint16_t x = 0; x < outputWidth; x++) {
32 uint32_t fx = ((uint32_t)x * (inputWidth - 1) * scale_factor) /
34 uint32_t fy = ((uint32_t)y * (inputHeight - 1) * scale_factor) /
37 uint16_t ix = fx / scale_factor;
38 uint16_t iy = fy / scale_factor;
39 uint16_t dx = fx % scale_factor;
40 uint16_t dy = fy % scale_factor;
42 uint16_t ix1 = (ix + 1 < inputWidth) ? ix + 1 : ix;
43 uint16_t iy1 = (iy + 1 < inputHeight) ? iy + 1 : iy;
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;
50 CRGB c00 = input[i00];
51 CRGB c10 = input[i10];
52 CRGB c01 = input[i01];
53 CRGB c11 = input[i11];
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);
60 uint16_t idx = xyMap.mapToIndex(x, y);
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;
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;
77 uint32_t sum = v00 * w00 + v10 * w10 + v01 * w01 + v11 * w11;
81 uint8_t result = (uint8_t)((sum + 32768) >> 16);
87 uint8_t width = xyMap.getWidth();
88 uint8_t height = xyMap.getHeight();
89 if (width != xyMap.getWidth() || height != xyMap.getHeight()) {
93 uint16_t n = xyMap.getTotal();
95 for (uint8_t y = 0; y < height; y++) {
96 for (uint8_t x = 0; x < width; x++) {
99 uint16_t fx = ((uint16_t)x * (inputWidth - 1) * 256) / (width - 1);
101 ((uint16_t)y * (inputHeight - 1) * 256) / (height - 1);
103 uint8_t ix = fx >> 8;
104 uint8_t iy = fy >> 8;
105 uint8_t dx = fx & 0xFF;
106 uint8_t dy = fy & 0xFF;
108 uint8_t ix1 = (ix + 1 < inputWidth) ? ix + 1 : ix;
109 uint8_t iy1 = (iy + 1 < inputHeight) ? iy + 1 : iy;
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;
116 CRGB c00 = input[i00];
117 CRGB c10 = input[i10];
118 CRGB c01 = input[i01];
119 CRGB c11 = input[i11];
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);
126 uint16_t idx = xyMap.mapToIndex(x, y);
128 output[idx] = result;
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;
137 uint16_t dy_inv = 256 - dy;
140 uint16_t w00 = (dx_inv * dy_inv) >> 8;
141 uint16_t w10 = (dx * dy_inv) >> 8;
142 uint16_t w01 = (dx_inv * dy) >> 8;
143 uint16_t w11 = (dx * dy) >> 8;
146 uint16_t weight_sum = w00 + w10 + w01 + w11;
149 uint16_t sum = v00 * w00 + v10 * w10 + v01 * w01 + v11 * w11;
152 uint8_t result = (sum + (weight_sum >> 1)) / weight_sum;
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;
165 float w00 = dx_inv * dy_inv;
166 float w10 = dx * dy_inv;
167 float w01 = dx_inv * dy;
171 float sum = v00 * w00 + v10 * w10 + v01 * w01 + v11 * w11;
174 uint8_t result =
static_cast<uint8_t
>(sum + 0.5f);
180void bilinearExpandArbitraryFloat(
const CRGB *input,
CRGB *output,
181 uint16_t inputWidth, uint16_t inputHeight,
183 uint16_t n = xyMap.getTotal();
184 uint16_t outputWidth = xyMap.getWidth();
185 uint16_t outputHeight = xyMap.getHeight();
187 for (uint16_t y = 0; y < outputHeight; y++) {
188 for (uint16_t x = 0; x < outputWidth; x++) {
190 float fx =
static_cast<float>(x) * (inputWidth - 1) / (outputWidth - 1);
191 float fy =
static_cast<float>(y) * (inputHeight - 1) / (outputHeight - 1);
193 uint16_t ix =
static_cast<uint16_t
>(fx);
194 uint16_t iy =
static_cast<uint16_t
>(fy);
198 uint16_t ix1 = (ix + 1 < inputWidth) ? ix + 1 : ix;
199 uint16_t iy1 = (iy + 1 < inputHeight) ? iy + 1 : iy;
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;
206 CRGB c00 = input[i00];
207 CRGB c10 = input[i10];
208 CRGB c01 = input[i01];
209 CRGB c11 = input[i11];
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);
216 uint16_t idx = xyMap.mapToIndex(x, y);
218 output[idx] = result;
225void bilinearExpandFloat(
const CRGB *input,
CRGB *output,
226 uint8_t inputWidth, uint8_t inputHeight,
228 uint8_t outputWidth = xyMap.getWidth();
229 uint8_t outputHeight = xyMap.getHeight();
230 if (outputWidth != xyMap.getWidth() || outputHeight != xyMap.getHeight()) {
234 uint16_t n = xyMap.getTotal();
236 for (uint8_t y = 0; y < outputHeight; y++) {
237 for (uint8_t x = 0; x < outputWidth; x++) {
239 float fx =
static_cast<float>(x) * (inputWidth - 1) / (outputWidth - 1);
240 float fy =
static_cast<float>(y) * (inputHeight - 1) / (outputHeight - 1);
242 uint8_t ix =
static_cast<uint8_t
>(fx);
243 uint8_t iy =
static_cast<uint8_t
>(fy);
247 uint8_t ix1 = (ix + 1 < inputWidth) ? ix + 1 : ix;
248 uint8_t iy1 = (iy + 1 < inputHeight) ? iy + 1 : iy;
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;
255 CRGB c00 = input[i00];
256 CRGB c10 = input[i10];
257 CRGB c01 = input[i01];
258 CRGB c11 = input[i11];
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);
265 uint16_t idx = xyMap.mapToIndex(x, y);
267 output[idx] = result;
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.
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)
uint8_t r
Red channel value.
uint8_t g
Green channel value.
uint8_t b
Blue channel value.