1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- 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]);
- }
- }
- }
|