Kajetan Johannes Hammerle 2 years ago
commit
1f27dff4fb
3 changed files with 93 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 27 0
      build.xml
  3. 65 0
      src/Main.java

+ 1 - 0
.gitignore

@@ -0,0 +1 @@
+/build

+ 27 - 0
build.xml

@@ -0,0 +1,27 @@
+<project default="run">
+    <target name="clean">
+        <delete dir="build"/>
+    </target>
+
+    <target name="compile">
+        <mkdir dir="build/classes"/>
+        <javac includeantruntime="false" srcdir="src" destdir="build/classes"/>
+    </target>
+
+    <target name="jar" depends="compile">
+        <javac includeantruntime="false" srcdir="src" destdir="build/classes"/>
+        <jar destfile="build/SignedDistanceFieldGenerator.jar" basedir="build/classes">
+            <manifest>
+                <attribute name="Main-Class" value="Main"/>
+            </manifest>
+        </jar>
+    </target>
+
+    <target name="run" depends="jar">
+        <java jar="build/SignedDistanceFieldGenerator.jar" fork="true">
+            <arg value="orig_abilities.png"/>
+            <arg value="abilities.png"/>
+            <arg value="7"/>
+        </java>
+    </target>
+</project>

+ 65 - 0
src/Main.java

@@ -0,0 +1,65 @@
+import javax.imageio.ImageIO;
+import java.io.File;
+import java.awt.image.BufferedImage;
+
+public class Main {
+    private static int radius = 0;
+    private static BufferedImage source = null;
+    private static BufferedImage destination = null;
+
+    public static void main(String[] args) {
+        if(args.length < 3) {
+            System.out.println("... <src> <dest> <radius>");
+            return;
+        }
+        try {
+            radius = Integer.parseInt(args[2]) + 1;
+        } catch(NumberFormatException ex) {
+            System.out.printf("'%s' is an invalid radius\n", args[2]);
+            return;
+        }
+        try {
+            source = ImageIO.read(new File(args[0]));
+        } catch(Exception ex) {
+            System.out.printf("cannot read source '%s'\n", args[0]);
+            return;
+        }
+        int width = source.getWidth();
+        int height = source.getHeight();
+        destination = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
+
+        for(int x = 0; x < width; x++) {
+            for(int y = 0; y < height; y++) {
+                int minX = Math.max(x - radius, 0);
+                int maxX = Math.min(x + radius, width - 1);
+                int minY = Math.max(y - radius, 0);
+                int maxY = Math.min(y + radius, height - 1);
+
+                int distance = radius;
+                for(int ix = minX; ix <= maxX; ix++) {
+                    for(int iy = minY; iy <= maxY; iy++) {
+                        int c = source.getRGB(ix, iy);
+                        int sum = (c & 0xFF) + ((c >> 8) & 0xFF) + ((c >> 16) & 0xFF);
+                        if(sum < 383) {
+                            double d = Math.hypot(x - ix, y - iy);
+                            if(d > radius) {
+                                d = radius;
+                            }
+                            distance = Math.min(distance, (int) d);
+                        }
+                    }
+                }
+                int intensity = 180 * distance / radius + 75;
+                intensity &= 0xFF;
+                int c = intensity | (intensity << 8) | (intensity << 16) | 0xFF000000;
+                destination.setRGB(x, y, c);
+            }
+        }
+
+        try {
+            ImageIO.write(destination, "png", new File(args[1]));
+        } catch(Exception ex) {
+            System.out.printf("cannot write destination '%s'\n", args[0]);
+        }
+    }
+}