Advertisement
CaptainSpaceCat

Smooooooth...

Jul 26th, 2016
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.72 KB | None | 0 0
  1. import java.awt.image.*;
  2. import javax.imageio.*;
  3. import java.io.*;
  4. import java.awt.*;
  5. import java.lang.Math;
  6.  
  7. class Main {
  8.  
  9.   static int w = 1920;
  10.   static int h = 1200;
  11.   static int iterations = 10;
  12.  
  13.   public static void main(String[] args) {
  14.     BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
  15.    
  16.     for (int y = 0; y < h; y++) {
  17.       for (int x = 0; x < w; x++) {
  18.         bi.setRGB(x, y, (int)(Math.random()*-16777217));
  19.       }
  20.     }
  21.    
  22.     try {
  23.       ImageIO.write(bi, "png", new File("C:\\Users\\Connor\\Desktop\\pic_0.png"));
  24.     } catch(IOException e) {
  25.     }
  26.    
  27.     for (int n = 0; n < 10; n++) {
  28.       System.out.println();
  29.       for (int i = 0; i < iterations; i++) {
  30.         bi = smooth(bi);
  31.       }
  32.      
  33.       try {
  34.         ImageIO.write(bi, "png", new File("C:\\Users\\Connor\\Desktop\\pic_" + (100*(n+1)) + ".png"));
  35.       } catch(IOException e) {
  36.       }
  37.     }
  38.    
  39.   }
  40.  
  41.   public static BufferedImage smooth(BufferedImage bi) {
  42.     BufferedImage img = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
  43.     for (int y = 0; y < h; y++) {
  44.       for (int x = 0; x < w; x++) {
  45.         img.setRGB(x, y, getAverageColor(x, y, bi));
  46.       }
  47.     }
  48.     return img;
  49.   }
  50.  
  51.   public static int getAverageColor(int x, int y, BufferedImage template) {
  52.     int n = 0;
  53.     int d = 0;
  54.     if (x > 0) {
  55.       n+=template.getRGB(x-1, y);
  56.       d++;
  57.     }
  58.     if (y > 0) {
  59.       n+=template.getRGB(x, y-1);
  60.       d++;
  61.     }
  62.     if (x < template.getWidth()-1) {
  63.       n+=template.getRGB(x+1, y);
  64.       d++;
  65.     }
  66.     if (y < template.getHeight()-1) {
  67.       n+=template.getRGB(x, y+1);
  68.       d++;
  69.     }
  70.     return (int)(n*1f/d+.5f);
  71.   }
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement