Advertisement
Guest User

Untitled

a guest
Jul 16th, 2014
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.15 KB | None | 0 0
  1. import java.awt.BorderLayout;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.image.BufferedImage;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.util.Random;
  8.  
  9. import javax.imageio.ImageIO;
  10. import javax.swing.ImageIcon;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JLabel;
  14. import javax.swing.Timer;
  15.  
  16. @SuppressWarnings("serial")
  17. public class CopyColors extends JFrame {
  18.   private static final String SOURCE = "mona";
  19.   private static final String PALETTE = "spheres";
  20.   private static final int TRIES_MAX = 5000;
  21.   private static final int COUNT = 100000;
  22.   private static final int DELAY = 20;
  23.   private static final int LUM_WEIGHT = 10;
  24.  
  25.   private static final double[] F = { 0.114, 0.587, 0.299 };
  26.   private final BufferedImage source;
  27.   protected final BufferedImage dest;
  28.   private final int sw;
  29.   private final int sh;
  30.   private final int n;
  31.   private final Random r = new Random();
  32.   private final JLabel l;
  33.  
  34.   public CopyColors(final String sourceName, final String paletteName)
  35.       throws IOException {
  36.     super("CopyColors by aditsu");
  37.     source = ImageIO.read(getClass().getResourceAsStream(sourceName + ".png"));
  38.     final BufferedImage palette = ImageIO.read(getClass().getResourceAsStream(
  39.         paletteName + ".png"));
  40.     sw = source.getWidth();
  41.     sh = source.getHeight();
  42.     final int pw = palette.getWidth();
  43.     final int ph = palette.getHeight();
  44.     n = sw * sh;
  45.     if (n != pw * ph) {
  46.       throw new RuntimeException();
  47.     }
  48.     dest = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB);
  49.     for (int i = 0; i < sh; ++i) {
  50.       for (int j = 0; j < sw; ++j) {
  51.         final int x = i * sw + j;
  52.         dest.setRGB(j, i, palette.getRGB(x % pw, x / pw));
  53.       }
  54.     }
  55.     l = new JLabel(new ImageIcon(dest));
  56.     add(l);
  57.     final JButton b = new JButton("Save");
  58.     add(b, BorderLayout.SOUTH);
  59.     b.addActionListener(new ActionListener() {
  60.       @Override
  61.       public void actionPerformed(final ActionEvent e) {
  62.         try {
  63.           ImageIO.write(dest, "png", new File(sourceName + "-" + paletteName
  64.               + ".png"));
  65.         } catch (IOException ex) {
  66.           ex.printStackTrace();
  67.         }
  68.       }
  69.     });
  70.   }
  71.  
  72.   protected double dist(final int x, final int y) {
  73.     double t = 0;
  74.     double lx = 0;
  75.     double ly = 0;
  76.     for (int i = 0; i < 3; ++i) {
  77.       final double xi = ((x >> (i * 8)) & 255) * F[i];
  78.       final double yi = ((y >> (i * 8)) & 255) * F[i];
  79.       final double d = xi - yi;
  80.       t += d * d;
  81.       lx += xi;
  82.       ly += yi;
  83.     }
  84.     double l = lx - ly;
  85.     return t + l * l * LUM_WEIGHT;
  86.   }
  87.  
  88.   public boolean improve() {
  89.     final int x = r.nextInt(n);
  90.     final int y = r.nextInt(n);
  91.     final int sx = source.getRGB(x % sw, x / sw);
  92.     final int sy = source.getRGB(y % sw, y / sw);
  93.     final int dx = dest.getRGB(x % sw, x / sw);
  94.     final int dy = dest.getRGB(y % sw, y / sw);
  95.     if (dist(sx, dx) + dist(sy, dy) > dist(sx, dy) + dist(sy, dx)) {
  96.       dest.setRGB(x % sw, x / sw, dy);
  97.       dest.setRGB(y % sw, y / sw, dx);
  98.       return true;
  99.     } else {
  100.       return false;
  101.     }
  102.   }
  103.  
  104.   public void update() {
  105.     l.repaint();
  106.   }
  107.  
  108.   public static void main(final String... args) throws IOException {
  109.     final CopyColors x = new CopyColors(SOURCE, PALETTE);
  110.     x.setSize(800, 600);
  111.     x.setLocationRelativeTo(null);
  112.     x.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  113.     x.setVisible(true);
  114.     final Timer time = new Timer(DELAY, null);
  115.     final long startTime = System.currentTimeMillis();
  116.     time.addActionListener(new ActionListener() {
  117.       @Override
  118.       public void actionPerformed(final ActionEvent e) {
  119.         int swapCounter = 0;
  120.         for (int i = 0; i < COUNT; ++i) {
  121.           if (x.improve()) {
  122.             swapCounter = 0;
  123.           } else {
  124.             swapCounter++;
  125.           }
  126.           if(swapCounter > TRIES_MAX) {
  127.             System.out.println(System.currentTimeMillis() - startTime);
  128.             time.stop();
  129.             break;
  130.           }
  131.         }
  132.         x.update();
  133.       }
  134.     });
  135.     time.start();
  136.   }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement