FastLED 3.9.15
Loading...
Searching...
No Matches

◆ downscaleArbitrary()

void fl::downscaleArbitrary ( const CRGB * src,
const XYMap & srcXY,
CRGB * dst,
const XYMap & dstXY )

Definition at line 82 of file downscale.cpp.

83 {
84 const fl::u16 srcWidth = srcXY.getWidth();
85 const fl::u16 srcHeight = srcXY.getHeight();
86 const fl::u16 dstWidth = dstXY.getWidth();
87 const fl::u16 dstHeight = dstXY.getHeight();
88
89 const fl::u32 FP_ONE = 256; // Q8.8 fixed-point multiplier
90
91 FASTLED_ASSERT(dstWidth <= srcWidth,
92 "Destination width must be <= source width");
93 FASTLED_ASSERT(dstHeight <= srcHeight,
94 "Destination height must be <= source height");
95
96 for (fl::u16 dy = 0; dy < dstHeight; ++dy) {
97 // Fractional boundaries in Q8.8
98 fl::u32 dstY0 = (dy * srcHeight * FP_ONE) / dstHeight;
99 fl::u32 dstY1 = ((dy + 1) * srcHeight * FP_ONE) / dstHeight;
100
101 for (fl::u16 dx = 0; dx < dstWidth; ++dx) {
102 fl::u32 dstX0 = (dx * srcWidth * FP_ONE) / dstWidth;
103 fl::u32 dstX1 = ((dx + 1) * srcWidth * FP_ONE) / dstWidth;
104
105 fl::u64 rSum = 0, gSum = 0, bSum = 0;
106 fl::u32 totalWeight = 0;
107
108 // Find covered source pixels
109 fl::u16 srcY_start = dstY0 / FP_ONE;
110 fl::u16 srcY_end = (dstY1 + FP_ONE - 1) / FP_ONE; // ceil
111
112 fl::u16 srcX_start = dstX0 / FP_ONE;
113 fl::u16 srcX_end = (dstX1 + FP_ONE - 1) / FP_ONE; // ceil
114
115 for (fl::u16 sy = srcY_start; sy < srcY_end; ++sy) {
116 // Calculate vertical overlap in Q8.8
117 fl::u32 sy0 = sy * FP_ONE;
118 fl::u32 sy1 = (sy + 1) * FP_ONE;
119 fl::u32 y_overlap = MIN(dstY1, sy1) - MAX(dstY0, sy0);
120 if (y_overlap == 0)
121 continue;
122
123 for (fl::u16 sx = srcX_start; sx < srcX_end; ++sx) {
124 fl::u32 sx0 = sx * FP_ONE;
125 fl::u32 sx1 = (sx + 1) * FP_ONE;
126 fl::u32 x_overlap = MIN(dstX1, sx1) - MAX(dstX0, sx0);
127 if (x_overlap == 0)
128 continue;
129
130 fl::u32 weight = (x_overlap * y_overlap + (FP_ONE >> 1)) >>
131 8; // Q8.8 * Q8.8 → Q16.16 → Q8.8
132
133 const CRGB &p = src[srcXY.mapToIndex(sx, sy)];
134 rSum += p.r * weight;
135 gSum += p.g * weight;
136 bSum += p.b * weight;
137 totalWeight += weight;
138 }
139 }
140
141 // Final division, rounding
142 fl::u8 r =
143 totalWeight ? (rSum + (totalWeight >> 1)) / totalWeight : 0;
144 fl::u8 g =
145 totalWeight ? (gSum + (totalWeight >> 1)) / totalWeight : 0;
146 fl::u8 b =
147 totalWeight ? (bSum + (totalWeight >> 1)) / totalWeight : 0;
148
149 dst[dstXY.mapToIndex(dx, dy)] = CRGB(r, g, b);
150 }
151 }
152}
u16 getWidth() const
Definition xymap.cpp:122
u16 mapToIndex(const u16 &x, const u16 &y) const
Definition xymap.cpp:95
u16 getHeight() const
Definition xymap.cpp:124
#define MIN(a, b)
Definition math_macros.h:41
#define MAX(a, b)
Definition math_macros.h:37
unsigned char u8
Definition int.h:17
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86

References fl::XYMap::getHeight(), fl::XYMap::getWidth(), fl::XYMap::mapToIndex(), MAX, and MIN.

Referenced by downscale().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: