| 103 | } |
| 104 | |
| 105 | void upscaleArbitrary(const CRGB *input, CRGB *output, u16 inputWidth, |
| 106 | u16 inputHeight, const XYMap& xyMap) { |
| 107 | u16 n = xyMap.getTotal(); |
| 108 | u16 outputWidth = xyMap.getWidth(); |
| 109 | u16 outputHeight = xyMap.getHeight(); |
| 110 | const u16 scale_factor = 256; // Using 8 bits for the fractional part |
| 111 | |
| 112 | for (u16 y = 0; y < outputHeight; y++) { |
| 113 | for (u16 x = 0; x < outputWidth; x++) { |
| 114 | // Calculate the corresponding position in the input grid |
| 115 | u32 fx = ((u32)x * (inputWidth - 1) * scale_factor) / |
| 116 | (outputWidth - 1); |
| 117 | u32 fy = ((u32)y * (inputHeight - 1) * scale_factor) / |
| 118 | (outputHeight - 1); |
| 119 | |
| 120 | u16 ix = fx / scale_factor; // Integer part of x |
| 121 | u16 iy = fy / scale_factor; // Integer part of y |
| 122 | u16 dx = fx % scale_factor; // Fractional part of x |
| 123 | u16 dy = fy % scale_factor; // Fractional part of y |
| 124 | |
| 125 | u16 ix1 = (ix + 1 < inputWidth) ? ix + 1 : ix; |
| 126 | u16 iy1 = (iy + 1 < inputHeight) ? iy + 1 : iy; |
| 127 | |
| 128 | u16 i00 = iy * inputWidth + ix; |
| 129 | u16 i10 = iy * inputWidth + ix1; |
| 130 | u16 i01 = iy1 * inputWidth + ix; |
| 131 | u16 i11 = iy1 * inputWidth + ix1; |
| 132 | |
| 133 | CRGB c00 = input[i00]; |
| 134 | CRGB c10 = input[i10]; |
| 135 | CRGB c01 = input[i01]; |
| 136 | CRGB c11 = input[i11]; |
| 137 | |
| 138 | CRGB result; |
| 139 | result.r = bilinearInterpolate(c00.r, c10.r, c01.r, c11.r, dx, dy); |
| 140 | result.g = bilinearInterpolate(c00.g, c10.g, c01.g, c11.g, dx, dy); |
| 141 | result.b = bilinearInterpolate(c00.b, c10.b, c01.b, c11.b, dx, dy); |
| 142 | |
| 143 | u16 idx = xyMap.mapToIndex(x, y); |
| 144 | if (idx < n) { |
| 145 | output[idx] = result; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | u8 bilinearInterpolate(u8 v00, u8 v10, u8 v01, u8 v11, |
| 151 | u16 dx, u16 dy) { |
| 152 | u16 dx_inv = 256 - dx; |
no test coverage detected