Main.java 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import javax.imageio.ImageIO;
  2. import java.io.File;
  3. import java.awt.image.BufferedImage;
  4. public class Main {
  5. private static int radius = 0;
  6. private static BufferedImage source = null;
  7. private static BufferedImage destination = null;
  8. public static void main(String[] args) {
  9. if(args.length < 3) {
  10. System.out.println("... <src> <dest> <radius>");
  11. return;
  12. }
  13. try {
  14. radius = Integer.parseInt(args[2]) + 1;
  15. } catch(NumberFormatException ex) {
  16. System.out.printf("'%s' is an invalid radius\n", args[2]);
  17. return;
  18. }
  19. try {
  20. source = ImageIO.read(new File(args[0]));
  21. } catch(Exception ex) {
  22. System.out.printf("cannot read source '%s'\n", args[0]);
  23. return;
  24. }
  25. int width = source.getWidth();
  26. int height = source.getHeight();
  27. destination = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
  28. for(int x = 0; x < width; x++) {
  29. for(int y = 0; y < height; y++) {
  30. int minX = Math.max(x - radius, 0);
  31. int maxX = Math.min(x + radius, width - 1);
  32. int minY = Math.max(y - radius, 0);
  33. int maxY = Math.min(y + radius, height - 1);
  34. int distance = radius;
  35. for(int ix = minX; ix <= maxX; ix++) {
  36. for(int iy = minY; iy <= maxY; iy++) {
  37. int c = source.getRGB(ix, iy);
  38. int sum = (c & 0xFF) + ((c >> 8) & 0xFF) + ((c >> 16) & 0xFF);
  39. if(sum < 383) {
  40. double d = Math.hypot(x - ix, y - iy);
  41. if(d > radius) {
  42. d = radius;
  43. }
  44. distance = Math.min(distance, (int) d);
  45. }
  46. }
  47. }
  48. int intensity = 180 * distance / radius + 75;
  49. intensity &= 0xFF;
  50. int c = intensity | (intensity << 8) | (intensity << 16) | 0xFF000000;
  51. destination.setRGB(x, y, c);
  52. }
  53. }
  54. try {
  55. ImageIO.write(destination, "png", new File(args[1]));
  56. } catch(Exception ex) {
  57. System.out.printf("cannot write destination '%s'\n", args[0]);
  58. }
  59. }
  60. }