Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.BorderLayout;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import java.util.Random;
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.Timer;
- @SuppressWarnings("serial")
- public class CopyColors extends JFrame {
- private static final String SOURCE = "mona";
- private static final String PALETTE = "spheres";
- private static final int TRIES_MAX = 5000;
- private static final int COUNT = 100000;
- private static final int DELAY = 20;
- private static final int LUM_WEIGHT = 10;
- private static final double[] F = { 0.114, 0.587, 0.299 };
- private final BufferedImage source;
- protected final BufferedImage dest;
- private final int sw;
- private final int sh;
- private final int n;
- private final Random r = new Random();
- private final JLabel l;
- public CopyColors(final String sourceName, final String paletteName)
- throws IOException {
- super("CopyColors by aditsu");
- source = ImageIO.read(getClass().getResourceAsStream(sourceName + ".png"));
- final BufferedImage palette = ImageIO.read(getClass().getResourceAsStream(
- paletteName + ".png"));
- sw = source.getWidth();
- sh = source.getHeight();
- final int pw = palette.getWidth();
- final int ph = palette.getHeight();
- n = sw * sh;
- if (n != pw * ph) {
- throw new RuntimeException();
- }
- dest = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);
- for (int i = 0; i < sh; ++i) {
- for (int j = 0; j < sw; ++j) {
- final int x = i * sw + j;
- dest.setRGB(j, i, palette.getRGB(x % pw, x / pw));
- }
- }
- l = new JLabel(new ImageIcon(dest));
- add(l);
- final JButton b = new JButton("Save");
- add(b, BorderLayout.SOUTH);
- b.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(final ActionEvent e) {
- try {
- ImageIO.write(dest, "png", new File(sourceName + "-" + paletteName
- + ".png"));
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- }
- });
- }
- protected double dist(final int x, final int y) {
- double t = 0;
- double lx = 0;
- double ly = 0;
- for (int i = 0; i < 3; ++i) {
- final double xi = ((x >> (i * 8)) & 255) * F[i];
- final double yi = ((y >> (i * 8)) & 255) * F[i];
- final double d = xi - yi;
- t += d * d;
- lx += xi;
- ly += yi;
- }
- double l = lx - ly;
- return t + l * l * LUM_WEIGHT;
- }
- public boolean improve() {
- final int x = r.nextInt(n);
- final int y = r.nextInt(n);
- final int sx = source.getRGB(x % sw, x / sw);
- final int sy = source.getRGB(y % sw, y / sw);
- final int dx = dest.getRGB(x % sw, x / sw);
- final int dy = dest.getRGB(y % sw, y / sw);
- if (dist(sx, dx) + dist(sy, dy) > dist(sx, dy) + dist(sy, dx)) {
- dest.setRGB(x % sw, x / sw, dy);
- dest.setRGB(y % sw, y / sw, dx);
- return true;
- } else {
- return false;
- }
- }
- public void update() {
- l.repaint();
- }
- public static void main(final String... args) throws IOException {
- final CopyColors x = new CopyColors(SOURCE, PALETTE);
- x.setSize(800, 600);
- x.setLocationRelativeTo(null);
- x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- x.setVisible(true);
- final Timer time = new Timer(DELAY, null);
- final long startTime = System.currentTimeMillis();
- time.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(final ActionEvent e) {
- int swapCounter = 0;
- for (int i = 0; i < COUNT; ++i) {
- if (x.improve()) {
- swapCounter = 0;
- } else {
- swapCounter++;
- }
- if(swapCounter > TRIES_MAX) {
- System.out.println(System.currentTimeMillis() - startTime);
- time.stop();
- break;
- }
- }
- x.update();
- }
- });
- time.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement