Skip to content

Commit af92cc1

Browse files
authored
Implement fix for alpha channel dithering (#15)
1 parent 423bcda commit af92cc1

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

plugin/src/main/java/de/pianoman911/mapengine/core/colors/ColorPalette.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ public final byte color(Color color) {
4848
}
4949

5050
@Override
51-
public final byte color(int rgb) {
51+
public final byte color(final int rgb) {
52+
if (((rgb >> 24) & 0xFF) < 128) {
53+
return 0;
54+
}
5255
return this.colors[rgb & 0xFFFFFF];
5356
}
5457

@@ -64,7 +67,7 @@ public byte[] colors(int[] rgb, int threads) {
6467
if (finalI == threads - 1) end = rgb.length;
6568
for (int j = start; j < end; j++) {
6669
int color = rgb[j];
67-
if (((color >> 24) & 0xFF) != 255) continue;
70+
if (((color >> 24) & 0xFF) < 128) continue;
6871
result[j] = this.color(rgb[j]);
6972
}
7073
});
@@ -247,8 +250,13 @@ public final int[] toRGBs(byte[] colors) {
247250
return rgb;
248251
}
249252

250-
public final int closestColor(int rgb) {
251-
return this.reverseColors[rgb & 0xFFFFFF];
253+
public final int closestColor(final int rgb) {
254+
final int alpha = (rgb >> 24) & 0xFF;
255+
if (alpha == 0) {
256+
return 0;
257+
}
258+
final int ret = this.reverseColors[rgb & 0xFFFFFF];
259+
return (ret & 0xFFFFFF) | (alpha << 24);
252260
}
253261

254262
private void checkRetry() {

plugin/src/main/java/de/pianoman911/mapengine/core/colors/dithering/FloydSteinbergDithering.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static ColorBuffer dither(FullSpacedColorBuffer buffer, ColorPalette pale
5454

5555
int mc = palette.closestColor(rgb);
5656

57+
int a;
5758
int r = (mc >> 16) & 0xFF;
5859
int g = (mc >> 8) & 0xFF;
5960
int b = (mc) & 0xFF;
@@ -67,36 +68,40 @@ public static ColorBuffer dither(FullSpacedColorBuffer buffer, ColorPalette pale
6768
if (!(x == w - 1)) {
6869
index = x + 1 + y * w;
6970
rgb = src[index];
71+
a = (rgb >> 24) & 0xFF;
7072
r = Math.max(0, Math.min(255, (int) (((rgb >> 16) & 0xFF) + (errorR * FS_ERROR))));
7173
g = Math.max(0, Math.min(255, (int) (((rgb >> 8) & 0xFF) + (errorG * FS_ERROR))));
7274
b = Math.max(0, Math.min(255, (int) (((rgb) & 0xFF) + (errorB * FS_ERROR))));
73-
src[index] = (r << 16) | (g << 8) | b;
75+
src[index] = (a << 24) | (r << 16) | (g << 8) | b;
7476

7577
if (!(y == h - 1)) {
7678
index = x + 1 + (y + 1) * w;
7779
rgb = src[index];
80+
a = (rgb >> 24) & 0xFF;
7881
r = Math.max(0, Math.min(255, (int) (((rgb >> 16) & 0xFF) + (errorR * FS_ERROR2))));
7982
g = Math.max(0, Math.min(255, (int) (((rgb >> 8) & 0xFF) + (errorG * FS_ERROR2))));
8083
b = Math.max(0, Math.min(255, (int) (((rgb) & 0xFF) + (errorB * FS_ERROR2))));
81-
src[index] = (r << 16) | (g << 8) | b;
84+
src[index] = (a << 24) | (r << 16) | (g << 8) | b;
8285
}
8386
}
8487

8588
if (!(y == h - 1)) {
8689
index = x + (y + 1) * w;
8790
rgb = src[index];
91+
a = (rgb >> 24) & 0xFF;
8892
r = Math.max(0, Math.min(255, (int) (((rgb >> 16) & 0xFF) + (errorR * FS_ERROR3))));
8993
g = Math.max(0, Math.min(255, (int) (((rgb >> 8) & 0xFF) + (errorG * FS_ERROR3))));
9094
b = Math.max(0, Math.min(255, (int) (((rgb) & 0xFF) + (errorB * FS_ERROR3))));
91-
src[index] = (r << 16) | (g << 8) | b;
95+
src[index] = (a << 24) | (r << 16) | (g << 8) | b;
9296

9397
if (!(x == 0)) {
9498
index = x - 1 + (y + 1) * w;
9599
rgb = src[index];
100+
a = (rgb >> 24) & 0xFF;
96101
r = Math.max(0, Math.min(255, (int) (((rgb >> 16) & 0xFF) + (errorR * FS_ERROR4))));
97102
g = Math.max(0, Math.min(255, (int) (((rgb >> 8) & 0xFF) + (errorG * FS_ERROR4))));
98103
b = Math.max(0, Math.min(255, (int) (((rgb) & 0xFF) + (errorB * FS_ERROR4))));
99-
src[index] = (r << 16) | (g << 8) | b;
104+
src[index] = (a << 24) | (r << 16) | (g << 8) | b;
100105
}
101106
}
102107
}

0 commit comments

Comments
 (0)