FastLED 3.9.15
Loading...
Searching...
No Matches
bilinear_compression.cpp
Go to the documentation of this file.
1
3
4#include "crgb.h"
5#include "fl/math_macros.h"
6
7namespace fl {
8
9void downscaleBilinear(const CRGB *src, uint16_t srcWidth, uint16_t srcHeight,
10 CRGB *dst, uint16_t dstWidth, uint16_t dstHeight) {
11 // Use 8 bits for fixed-point fractional precision.
12 const uint16_t SHIFT = 8;
13 const uint16_t FP_ONE = 1 << SHIFT; // 256 in fixed-point
14 const uint16_t WEIGHT_SHIFT =
15 SHIFT * 2; // 16 bits (for the product of two fixed-point numbers)
16 const uint16_t rounding = 1 << (WEIGHT_SHIFT - 1);
17
18 // Compute scale factors in fixed-point: factor = (srcDimension << SHIFT) /
19 // dstDimension.
20 uint16_t scaleX = (srcWidth << SHIFT) / dstWidth;
21 uint16_t scaleY = (srcHeight << SHIFT) / dstHeight;
22
23 // Loop over each pixel in the destination image.
24 for (int y = 0; y < dstHeight; ++y) {
25 // Compute the corresponding source y coordinate in fixed-point.
26 int srcYFixed = y * scaleY;
27 int y0 = srcYFixed >> SHIFT; // integer part for row index
28 int yFrac = srcYFixed & (FP_ONE - 1); // fractional part
29 int y1 =
30 MIN(y0 + 1, srcHeight - 1); // ensure we don't exceed bounds
31
32 for (int x = 0; x < dstWidth; ++x) {
33 // Compute the corresponding source x coordinate in fixed-point.
34 int srcXFixed = x * scaleX;
35 int x0 = srcXFixed >> SHIFT; // integer part for column index
36 int xFrac = srcXFixed & (FP_ONE - 1); // fractional part
37 int x1 = MIN(x0 + 1, srcWidth - 1);
38
39 // Compute the weights for the four neighboring pixels.
40 int w00 = (FP_ONE - xFrac) * (FP_ONE - yFrac);
41 int w10 = xFrac * (FP_ONE - yFrac);
42 int w01 = (FP_ONE - xFrac) * yFrac;
43 int w11 = xFrac * yFrac;
44
45 // Apply fixed-point weighted sum for each color channel.
46 int r = (w00 * src[y0 * srcWidth + x0].r +
47 w10 * src[y0 * srcWidth + x1].r +
48 w01 * src[y1 * srcWidth + x0].r +
49 w11 * src[y1 * srcWidth + x1].r + rounding) >>
50 WEIGHT_SHIFT;
51
52 int g = (w00 * src[y0 * srcWidth + x0].g +
53 w10 * src[y0 * srcWidth + x1].g +
54 w01 * src[y1 * srcWidth + x0].g +
55 w11 * src[y1 * srcWidth + x1].g + rounding) >>
56 WEIGHT_SHIFT;
57
58 int b = (w00 * src[y0 * srcWidth + x0].b +
59 w10 * src[y0 * srcWidth + x1].b +
60 w01 * src[y1 * srcWidth + x0].b +
61 w11 * src[y1 * srcWidth + x1].b + rounding) >>
62 WEIGHT_SHIFT;
63
64 // Store the computed pixel in the destination image.
65 dst[y * dstWidth + x] = CRGB(static_cast<unsigned char>(r),
66 static_cast<unsigned char>(g),
67 static_cast<unsigned char>(b));
68 }
69 }
70}
71
72} // namespace fl
uint32_t x[NUM_LAYERS]
Definition Fire2023.ino:80
uint32_t y[NUM_LAYERS]
Definition Fire2023.ino:81
UISlider scaleX("ScaleX",.3, 0.1, 3,.01)
Defines the red, green, and blue (RGB) pixel struct.
#define MIN(a, b)
Definition math_macros.h:8
void downscaleBilinear(const CRGB *src, uint16_t srcWidth, uint16_t srcHeight, CRGB *dst, uint16_t dstWidth, uint16_t dstHeight)
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54