Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.28 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.        
  17.         BufferedImage result = clone(source);
  18.        
  19.         int width = source.getWidth();
  20.         int height = source.getHeight();
  21.        
  22.         for (int i= 0; i < ITERATIONS; i++) {
  23.             for (int x = 0; x < width; x++)
  24.                 for (int y = 0; y < height; y++) {
  25.                     int xDelta = rnd.nextInt(DEGREE + 1) - 2;
  26.                     int yDelta = rnd.nextInt(DEGREE + 1) - 2;
  27.                    
  28.                     if (x + xDelta >= 0 && x + xDelta < width && y + yDelta >= 0 && y + yDelta <height){
  29.                        result.setRGB(x + xDelta, y + yDelta, source.getRGB(x, y));
  30.                     }
  31.                 }
  32.         }
  33.        
  34.         return result;
  35.     }
  36.    
  37.     private BufferedImage clone(BufferedImage source) {
  38.         int width = source.getWidth();
  39.         int height = source.getHeight();
  40.        
  41.         BufferedImage result = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  42.        
  43.         for (int i = 0; i < width; i++)
  44.             for (int j = 0; j < height; j++) {
  45.                 result.setRGB(i, j, source.getRGB(i, j));
  46.             }
  47.        
  48.         return result;
  49.     }
  50.    
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement