Advertisement
daaca

RandomJitter

Apr 1st, 2015
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.31 KB | None | 0 0
  1. import java.awt.image.BufferedImage;
  2. import java.util.Random;
  3.  
  4. @SuppressWarnings("unused")
  5. public class RandomJitter implements BaseFilter {
  6.     private static final int DEGREE = 4;
  7.     private static final int ITERATIONS = 3;
  8.     private Random rnd;
  9.  
  10.     public RandomJitter() {
  11.         rnd = new Random();
  12.     }
  13.  
  14.     @Override
  15.     public BufferedImage process(BufferedImage source) {
  16.         BufferedImage slika = new BufferedImage(source.getWidth(), source.getHeight(), BufferedImage.TYPE_INT_RGB);
  17.         for (int x = 0; x < source.getWidth(); x++)
  18.         {
  19.             for (int y = 0; y < source.getHeight(); y++)
  20.             {
  21.                 slika.setRGB(x, y, source.getRGB(x, y));
  22.             }
  23.         }
  24.         int c = 0;
  25.         Random rnd = new Random();
  26.         while (c++ < ITERATIONS)
  27.         {
  28.             for (int x = 0; x < source.getWidth(); x++)
  29.             {
  30.                 for (int y = 0; y < source.getHeight(); y++)
  31.                 {
  32.                     int degree = rnd.nextInt(DEGREE)-2;
  33.                     int noviX = x + degree;
  34.                     int noviY = y + degree;
  35.                     if (noviX < 0)
  36.                     {
  37.                         noviX = 0;
  38.                     }
  39.                     else if (noviX >= source.getWidth())
  40.                     {
  41.                         noviX = source.getWidth() - 1;
  42.                     }
  43.                     if (noviY < 0)
  44.                     {
  45.                         noviY = 0;
  46.                     }
  47.                     else if (noviY >= source.getHeight())
  48.                     {
  49.                         noviY = source.getHeight() - 1;
  50.                     }
  51.                     slika.setRGB(noviX, noviY, source.getRGB(x, y));
  52.                 }
  53.             }
  54.         }
  55.         return slika;
  56.     }
  57.  
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement