Pixmap antialias utility
@@ -8,6 +8,7 @@ import arc.struct.*;
import arc.util.*;
import java.nio.*;
+import java.util.*;
/** Various pixmap utilities. */
public class Pixmaps{
@@ -273,6 +274,75 @@ public class Pixmaps{
return image;
}
+ public static void antialias(Pixmap pixmap){
+ Pixmap prev = pixmap.copy();
+
+ Color color = new Color();
+ Color sum = new Color();
+ Color suma = new Color();
+ int[] p = new int[9];
+
+ for(int x = 0; x < prev.width; x++){
+ for(int y = 0; y < prev.height; y++){
+ int A = prev.get(x - 1, y + 1),
+ B = prev.get(x, y + 1),
+ C = prev.get(x + 1, y + 1),
+ D = prev.get(x - 1, y),
+ E = prev.get(x, y),
+ F = prev.get(x + 1, y),
+ G = prev.get(x - 1, y - 1),
+ H = prev.get(x, y - 1),
+ I = prev.get(x + 1, y - 1);
+
+ Arrays.fill(p, E);
+
+ if(D == B && D != H && B != F) p[0] = D;
+ if((D == B && D != H && B != F && E != C) || (B == F && B != D && F != H && E != A)) p[1] = B;
+ if(B == F && B != D && F != H) p[2] = F;
+ if((H == D && H != F && D != B && E != A) || (D == B && D != H && B != F && E != G)) p[3] = D;
+ if((B == F && B != D && F != H && E != I) || (F == H && F != B && H != D && E != C)) p[5] = F;
+ if(H == D && H != F && D != B) p[6] = D;
+ if((F == H && F != B && H != D && E != G) || (H == D && H != F && D != B && E != I)) p[7] = H;
+ if(F == H && F != B && H != D) p[8] = F;
+
+ suma.set(0);
+
+ for(int val : p){
+ color.rgba8888(val);
+ color.premultiplyAlpha();
+ suma.r += color.r;
+ suma.g += color.g;
+ suma.b += color.b;
+ suma.a += color.a;
+ }
+
+ float fm = suma.a <= 0.001f ? 0f : (1f / suma.a);
+ suma.mul(fm, fm, fm, fm);
+
+ float total = 0;
+ sum.set(0);
+
+ for(int val : p){
+ color.rgba8888(val);
+ float a = color.a;
+ color.lerp(suma, (1f - a));
+ sum.r += color.r;
+ sum.g += color.g;
+ sum.b += color.b;
+ sum.a += a;
+ total += 1f;
+ }
+
+ fm = (1f / total);
+ sum.mul(fm, fm, fm, fm);
+ pixmap.setRaw(x, y, sum.rgba8888());
+ sum.set(0);
+ }
+ }
+
+ prev.dispose();
+ }
+
/**
* Applies alpha bleeding to the target pixmap.
* @return the input pixmap with its pixels modified.
