Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Point;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Scanner;
- import javax.imageio.ImageIO;
- public class Main
- {
- public static final int RADIUS = 75;
- public static final double SWIRL = -0.63;
- public static final int STABILITY = 10;
- @SuppressWarnings("unchecked")
- public static final ArrayList<Point>[] CURVES = (ArrayList<Point>[]) new ArrayList[360];
- @SuppressWarnings("unchecked")
- private static ArrayList<Integer>[][] CACHED_ARRAY = (ArrayList<Integer>[][]) new ArrayList[RADIUS*2+1][RADIUS*2+1];
- private static final int[] CIRCUMFERENCE_ANGLES;
- static
- {
- ArrayList<Point> pl = new ArrayList<Point>();
- ArrayList<Integer> al = new ArrayList<Integer>();
- for (int i = 0; i < 360; i++)
- {
- double angle = Math.toRadians(i);
- int ox = (int) (RADIUS * Math.cos(angle));
- int oy = (int) (RADIUS * Math.sin(angle));
- int mx = ox / 2;
- int my = oy / 2;
- int cx = mx - (int) (Math.abs(oy) * SWIRL * Math.sin(angle));
- int cy = my - (int) (Math.abs(ox) * SWIRL * Math.sin(angle - Math.PI / 2));
- CURVES[i] = new ArrayList<Point>();
- Utils.getQuadCurve(CURVES[i], ox, oy, cx, cy, 0, 0, 0.00005);
- if (pl.isEmpty() || ox != pl.get(pl.size() - 1).x || oy != pl.get(pl.size() - 1).y)
- {
- pl.add(new Point(ox, oy));
- al.add(i);
- }
- }
- int[] arr = new int[al.size()];
- for (int i = 0; i < arr.length; i++)
- arr[i] = al.get(i);
- CIRCUMFERENCE_ANGLES = arr;
- }
- public static void applyWarp(BufferedImage img, int x, int y)
- {
- if (x + RADIUS <= 0 || y + RADIUS <= 0)
- return;
- if (x - RADIUS >= img.getWidth() || y - RADIUS >= img.getHeight())
- return;
- int w = img.getWidth();
- int h = img.getHeight();
- ArrayList<Integer>[][] arr = CACHED_ARRAY;
- for (int i = 0; i <= RADIUS * 2; i++)
- {
- for (int j = 0; j <= RADIUS * 2; j++)
- {
- ArrayList<Integer> a = new ArrayList<Integer>();
- arr[i][j] = a;
- }
- }
- for (int n = 0; n < CIRCUMFERENCE_ANGLES.length; n++)
- {
- int a = CIRCUMFERENCE_ANGLES[n];
- ArrayList<Point> curve = CURVES[a];
- ArrayList<Integer> colors = new ArrayList<Integer>();
- for (Point cp : curve)
- {
- int nx = x + cp.x;
- int ny = y + cp.y;
- if (nx < 0 || ny < 0 || nx >= w || ny >= h)
- continue;
- arr[cp.x+RADIUS][cp.y+RADIUS].addAll(colors);
- int c = img.getRGB(nx, ny) & 0xFFFFFF;
- if (c != 0)
- colors.add(c);
- }
- }
- for (int i = 0; i <= RADIUS * 2; i++)
- {
- for (int j = 0; j <= RADIUS * 2; j++)
- {
- int rx = x + i - RADIUS;
- int ry = y + j - RADIUS;
- if (rx >= 0 && ry >= 0 && rx < w && ry < h)
- {
- int r = 0;
- int g = 0;
- int b = 0;
- ArrayList<Integer> list = arr[i][j];
- for (Integer n : list)
- {
- r += Utils.red(n);
- g += Utils.green(n);
- b += Utils.blue(n);
- }
- int c = img.getRGB(rx, ry);
- r += Utils.red(c) * STABILITY;
- g += Utils.green(c) * STABILITY;
- b += Utils.blue(c) * STABILITY;
- r /= list.size() + STABILITY;
- g /= list.size() + STABILITY;
- b /= list.size() + STABILITY;
- img.setRGB(rx, ry, Utils.color(0xFF, r, g, b));
- }
- }
- }
- }
- // simple test for the effect
- public static void main(String[] args) throws IOException
- {
- Scanner sc = new Scanner(System.in);
- int x = sc.nextInt();
- int y = sc.nextInt();
- BufferedImage img = ImageIO.read(new File("test.jpg"));
- long time = System.nanoTime();
- applyWarp(img, x, y);
- System.out.println((double) (System.nanoTime() - time) / 1000000);
- ImageIO.write(img, "JPG", new File("product.jpg"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement