Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.image.*;
- import javax.imageio.*;
- import java.io.*;
- import java.awt.*;
- import java.lang.Math;
- class Main {
- static int w = 1920;
- static int h = 1200;
- static int iterations = 10;
- public static void main(String[] args) {
- BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- bi.setRGB(x, y, (int)(Math.random()*-16777217));
- }
- }
- try {
- ImageIO.write(bi, "png", new File("C:\\Users\\Connor\\Desktop\\pic_0.png"));
- } catch(IOException e) {
- }
- for (int n = 0; n < 10; n++) {
- System.out.println();
- for (int i = 0; i < iterations; i++) {
- bi = smooth(bi);
- }
- try {
- ImageIO.write(bi, "png", new File("C:\\Users\\Connor\\Desktop\\pic_" + (100*(n+1)) + ".png"));
- } catch(IOException e) {
- }
- }
- }
- public static BufferedImage smooth(BufferedImage bi) {
- BufferedImage img = new BufferedImage(bi.getWidth(), bi.getHeight(), bi.getType());
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- img.setRGB(x, y, getAverageColor(x, y, bi));
- }
- }
- return img;
- }
- public static int getAverageColor(int x, int y, BufferedImage template) {
- int n = 0;
- int d = 0;
- if (x > 0) {
- n+=template.getRGB(x-1, y);
- d++;
- }
- if (y > 0) {
- n+=template.getRGB(x, y-1);
- d++;
- }
- if (x < template.getWidth()-1) {
- n+=template.getRGB(x+1, y);
- d++;
- }
- if (y < template.getHeight()-1) {
- n+=template.getRGB(x, y+1);
- d++;
- }
- return (int)(n*1f/d+.5f);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement