Skip to content

Commit 4aa38f2

Browse files
committed
Revert fixed-point and reintroduce clamping
- We found that int16_t was not enough range for the fixed point changes, especially in combination with not clamping for error.
1 parent cd823ad commit 4aa38f2

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/include/ImageDitherColor.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,18 @@ uint8_t Image::ditherGetPixelBmp(uint32_t px, int i, int j, int w, bool paletted
106106
if (paletted)
107107
px = ditherPalette[px];
108108

109-
int16_t r = RED8(px) + ditherBuffer[0][j % 8][i] / coef;
110-
int16_t g = GREEN8(px) + ditherBuffer[1][j % 8][i] / coef;
111-
int16_t b = BLUE8(px) + ditherBuffer[2][j % 8][i] / coef;
109+
int16_t r = RED8(px) + ditherBuffer[0][j % 8][i];
110+
int16_t g = GREEN8(px) + ditherBuffer[1][j % 8][i];
111+
int16_t b = BLUE8(px) + ditherBuffer[2][j % 8][i];
112112

113113
ditherBuffer[0][j % 8][i] = 0;
114114
ditherBuffer[1][j % 8][i] = 0;
115115
ditherBuffer[2][j % 8][i] = 0;
116116

117+
r = max((int16_t)0, min((int16_t)255, r));
118+
g = max((int16_t)0, min((int16_t)255, g));
119+
b = max((int16_t)0, min((int16_t)255, b));
120+
117121
int closest = findClosestPalette(r, g, b);
118122

119123
int32_t rErr = r - (int32_t)((pallete[closest] >> 16) & 0xFF);
@@ -126,9 +130,9 @@ uint8_t Image::ditherGetPixelBmp(uint32_t px, int i, int j, int w, bool paletted
126130
{
127131
if (!(0 <= i + l && i + l < w))
128132
continue;
129-
ditherBuffer[0][(j + k) % 8][i + l] += (kernel[k][l + kernelX] * rErr);
130-
ditherBuffer[1][(j + k) % 8][i + l] += (kernel[k][l + kernelX] * gErr);
131-
ditherBuffer[2][(j + k) % 8][i + l] += (kernel[k][l + kernelX] * bErr);
133+
ditherBuffer[0][(j + k) % 8][i + l] += (kernel[k][l + kernelX] * rErr) / coef;
134+
ditherBuffer[1][(j + k) % 8][i + l] += (kernel[k][l + kernelX] * gErr) / coef;
135+
ditherBuffer[2][(j + k) % 8][i + l] += (kernel[k][l + kernelX] * bErr) / coef;
132136
}
133137
}
134138

0 commit comments

Comments
 (0)