Advertisement
Guest User

WarpEffect

a guest
Apr 12th, 2013
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. import java.awt.Point;
  2. import java.awt.image.BufferedImage;
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.util.ArrayList;
  6. import java.util.Scanner;
  7.  
  8. import javax.imageio.ImageIO;
  9.  
  10.  
  11. public class Main
  12. {
  13.     public static final int RADIUS = 75;
  14.     public static final double SWIRL = -0.63;
  15.     public static final int STABILITY = 10;
  16.    
  17.     @SuppressWarnings("unchecked")
  18.     public static final ArrayList<Point>[] CURVES = (ArrayList<Point>[]) new ArrayList[360];
  19.     @SuppressWarnings("unchecked")
  20.     private static ArrayList<Integer>[][] CACHED_ARRAY = (ArrayList<Integer>[][]) new ArrayList[RADIUS*2+1][RADIUS*2+1];
  21.     private static final int[] CIRCUMFERENCE_ANGLES;
  22.    
  23.     static
  24.     {
  25.         ArrayList<Point> pl = new ArrayList<Point>();
  26.         ArrayList<Integer> al = new ArrayList<Integer>();
  27.         for (int i = 0; i < 360; i++)
  28.         {
  29.             double angle = Math.toRadians(i);
  30.             int ox = (int) (RADIUS * Math.cos(angle));
  31.             int oy = (int) (RADIUS * Math.sin(angle));
  32.             int mx = ox / 2;
  33.             int my = oy / 2;
  34.             int cx = mx - (int) (Math.abs(oy) * SWIRL * Math.sin(angle));
  35.             int cy = my - (int) (Math.abs(ox) * SWIRL * Math.sin(angle - Math.PI / 2));
  36.            
  37.             CURVES[i] = new ArrayList<Point>();
  38.             Utils.getQuadCurve(CURVES[i], ox, oy, cx, cy, 0, 0, 0.00005);
  39.            
  40.             if (pl.isEmpty() || ox != pl.get(pl.size() - 1).x || oy != pl.get(pl.size() - 1).y)
  41.             {
  42.                 pl.add(new Point(ox, oy));
  43.                 al.add(i);
  44.             }
  45.         }
  46.         int[] arr = new int[al.size()];
  47.         for (int i = 0; i < arr.length; i++)
  48.             arr[i] = al.get(i);
  49.        
  50.         CIRCUMFERENCE_ANGLES = arr;
  51.     }
  52.    
  53.     public static void applyWarp(BufferedImage img, int x, int y)
  54.     {
  55.         if (x + RADIUS <= 0 || y + RADIUS <= 0)
  56.             return;
  57.         if (x - RADIUS >= img.getWidth() || y - RADIUS >= img.getHeight())
  58.             return;
  59.        
  60.         int w = img.getWidth();
  61.         int h = img.getHeight();
  62.        
  63.         ArrayList<Integer>[][] arr = CACHED_ARRAY;
  64.         for (int i = 0; i <= RADIUS * 2; i++)
  65.         {
  66.             for (int j = 0; j <= RADIUS * 2; j++)
  67.             {
  68.                 ArrayList<Integer> a = new ArrayList<Integer>();
  69.                 arr[i][j] = a;
  70.             }
  71.         }
  72.        
  73.         for (int n = 0; n < CIRCUMFERENCE_ANGLES.length; n++)
  74.         {
  75.             int a = CIRCUMFERENCE_ANGLES[n];
  76.             ArrayList<Point> curve = CURVES[a];
  77.             ArrayList<Integer> colors = new ArrayList<Integer>();
  78.            
  79.             for (Point cp : curve)
  80.             {
  81.                 int nx = x + cp.x;
  82.                 int ny = y + cp.y;
  83.                 if (nx < 0 || ny < 0 || nx >= w || ny >= h)
  84.                     continue;
  85.                
  86.                 arr[cp.x+RADIUS][cp.y+RADIUS].addAll(colors);
  87.                
  88.                 int c = img.getRGB(nx, ny) & 0xFFFFFF;
  89.                 if (c != 0)
  90.                     colors.add(c);
  91.             }
  92.         }
  93.  
  94.         for (int i = 0; i <= RADIUS * 2; i++)
  95.         {
  96.             for (int j = 0; j <= RADIUS * 2; j++)
  97.             {
  98.                 int rx = x + i - RADIUS;
  99.                 int ry = y + j - RADIUS;
  100.                 if (rx >= 0 && ry >= 0 && rx < w && ry < h)
  101.                 {
  102.                     int r = 0;
  103.                     int g = 0;
  104.                     int b = 0;
  105.                     ArrayList<Integer> list = arr[i][j];
  106.                    
  107.                     for (Integer n : list)
  108.                     {
  109.                         r += Utils.red(n);
  110.                         g += Utils.green(n);
  111.                         b += Utils.blue(n);
  112.                     }
  113.                     int c = img.getRGB(rx, ry);
  114.                     r += Utils.red(c) * STABILITY;
  115.                     g += Utils.green(c) * STABILITY;
  116.                     b += Utils.blue(c) * STABILITY;
  117.                    
  118.                     r /= list.size() + STABILITY;
  119.                     g /= list.size() + STABILITY;
  120.                     b /= list.size() + STABILITY;
  121.                     img.setRGB(rx, ry, Utils.color(0xFF, r, g, b));
  122.                 }
  123.             }
  124.         }
  125.     }
  126.    
  127.     // simple test for the effect
  128.     public static void main(String[] args) throws IOException
  129.     {
  130.         Scanner sc = new Scanner(System.in);
  131.         int x = sc.nextInt();
  132.         int y = sc.nextInt();
  133.         BufferedImage img = ImageIO.read(new File("test.jpg"));
  134.        
  135.         long time = System.nanoTime();
  136.         applyWarp(img, x, y);
  137.         System.out.println((double) (System.nanoTime() - time) / 1000000);
  138.        
  139.         ImageIO.write(img, "JPG", new File("product.jpg"));
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement