10 {
11
12 const uint16_t SHIFT = 8;
13 const uint16_t FP_ONE = 1 << SHIFT;
14 const uint16_t WEIGHT_SHIFT =
15 SHIFT * 2;
16 const uint16_t rounding = 1 << (WEIGHT_SHIFT - 1);
17
18
19
20 uint16_t
scaleX = (srcWidth << SHIFT) / dstWidth;
21 uint16_t scaleY = (srcHeight << SHIFT) / dstHeight;
22
23
24 for (
int y = 0;
y < dstHeight; ++
y) {
25
26 int srcYFixed =
y * scaleY;
27 int y0 = srcYFixed >> SHIFT;
28 int yFrac = srcYFixed & (FP_ONE - 1);
29 int y1 =
30 MIN(y0 + 1, srcHeight - 1);
31
32 for (
int x = 0;
x < dstWidth; ++
x) {
33
35 int x0 = srcXFixed >> SHIFT;
36 int xFrac = srcXFixed & (FP_ONE - 1);
37 int x1 =
MIN(x0 + 1, srcWidth - 1);
38
39
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
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
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}
UISlider scaleX("ScaleX",.3, 0.1, 3,.01)
Representation of an RGB pixel (Red, Green, Blue)