Advertisement
Guest User

Pixelator

a guest
Nov 7th, 2016
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.68 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import java.awt.geom.AffineTransform;
  5. import java.awt.image.BufferedImage;
  6. import java.io.File;
  7. import java.io.IOException;
  8. import javax.imageio.ImageIO;
  9. import javax.swing.*;
  10.  
  11. public class pixels extends JPanel{
  12.     public static JFrame g;
  13.     public static JProgressBar p;
  14.     public static JLabel direct;
  15.     public static JLabel eta;
  16.     public static ImageIcon imageIn;
  17.     public static ImageIcon imageOut;
  18.    
  19.     public static JFrame in;
  20.     public static JTextField gid;
  21.     public static JTextField scalei;
  22.     public static JLabel warning1;
  23.     public static JLabel warning2;
  24.     public static JButton select;
  25.    
  26.     public static String id = null;
  27.     public static float scale = 0.0f;
  28.    
  29.    
  30.     public static BufferedImage load(String filename, String id){
  31.         //Initialize BufferedImage input
  32.         BufferedImage input = null;
  33.         try{
  34.             //Set the BufferedImage's path
  35.             input = ImageIO.read(new File("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+id+"\\"+filename));
  36.             return input;
  37.         } catch (IOException e){
  38.             //Exit the program if error occurs.
  39.             System.out.println("Sorry! Here is the exception: "+e);
  40.             return input;
  41.         }
  42.     } public static BufferedImage pixelate(BufferedImage imgIn, float scale){
  43.         //Create BufferedImage type RGB with Alpha.
  44.         int imageType = BufferedImage.TYPE_INT_ARGB;
  45.         //Initializes the new scaled BufferedImage with the scaled dimensions.
  46.         BufferedImage scaled = new BufferedImage(Math.round(imgIn.getWidth()*scale), Math.round(imgIn.getHeight()*scale), imageType);
  47.         //Initialize Grpahics2D for the scaled BufferedImage.
  48.         Graphics2D g = scaled.createGraphics();
  49.         //Set Graphics2D to retain Alpha.
  50.         g.setComposite(AlphaComposite.Src);
  51.         //Create the modifier for drawRenderedImage() and executes it.
  52.         AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
  53.         g.drawRenderedImage(imgIn, at);
  54.         //Frees up system resources and returns the scaled image.
  55.         g.dispose();
  56.         return scaled;
  57.     } public static void save(BufferedImage save, String name){
  58.         try{
  59.             //Saves image with specified name, directory, and .png extension.
  60.             File output = new File (name);
  61.             ImageIO.write(save, "png", output);
  62.         } catch (IOException e){
  63.             //Exits if there is an error
  64.             System.out.println("Sorry! Here is the exception: "+e);
  65.         }
  66.     } public static void createMainGUI (){
  67.         g = new JFrame("Pixelator");
  68.         g.setSize(800, 300);
  69.         g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  70.         g.setLayout(new BorderLayout());
  71.         g.setLocationRelativeTo(null);
  72.        
  73.         direct = new JLabel("");
  74.         direct.setHorizontalAlignment(JLabel.CENTER);
  75.         direct.setToolTipText("Directory working in.");
  76.        
  77.         eta = new JLabel("");
  78.         eta.setHorizontalAlignment(JLabel.CENTER);
  79.        
  80.         p = new JProgressBar(0, 500);
  81.         p.setValue(0);
  82.         p.setStringPainted(true);
  83.         p.setMinimum(0);
  84.        
  85.         imageIn = new ImageIcon();
  86.         imageOut = new ImageIcon();
  87.        
  88.         g.add(new JLabel(imageIn), BorderLayout.WEST);
  89.         g.add(new JLabel(imageOut), BorderLayout.EAST);
  90.         g.add(eta, BorderLayout.CENTER);
  91.         g.add(direct, BorderLayout.NORTH);
  92.         g.add(p, BorderLayout.SOUTH);
  93.        
  94.        
  95.         g.setVisible(true);
  96.     } public static void createInputGUI(){
  97.         in = new JFrame ("Pixelator");
  98.         in.setSize(300, 150);
  99.         in.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  100.         in.setLayout(new GridLayout(3,2));
  101.         in.setLocationRelativeTo(null);
  102.        
  103.         gid = new JTextField(20);
  104.         gid.setText("Game I.D.");
  105.         scalei = new JTextField(20);
  106.         scalei.setText("Scale");
  107.        
  108.         warning1 = new JLabel("Invalid ID.");
  109.         warning2 = new JLabel("Invalid scale.");
  110.         warning1.setForeground(Color.RED);
  111.         warning2.setForeground(Color.RED);
  112.         warning1.setVisible(false);
  113.         warning2.setVisible(false);
  114.        
  115.         select = new JButton("O.K.");
  116.         select.setMnemonic('O');
  117.         select.addActionListener(new ActionListener() {
  118.             public void actionPerformed(ActionEvent e){
  119.                 File path = new File("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+gid.getText()+"\\");
  120.                 if (!path.exists() || gid.getText() == ""){
  121.                     warning1.setVisible(true);
  122.                     gid.setBackground(new Color(249, 144, 144));
  123.                 } else {
  124.                     try{
  125.                         Float.parseFloat(scalei.getText());
  126.                     } catch(NumberFormatException e1){
  127.                         warning2.setVisible(true);
  128.                         scalei.setBackground(new Color(249, 144, 144));
  129.                     }
  130.                     id = gid.getText();
  131.                     scale = Float.parseFloat(scalei.getText());
  132.                     in.setVisible(false);
  133.                 }
  134.             }
  135.         });
  136.        
  137.         in.add(gid);
  138.         in.add(warning1);
  139.         in.add(scalei);
  140.         in.add(warning2);
  141.         in.add(select);
  142.        
  143.         in.setVisible(true);
  144.     }
  145.     public static void main (String[] args){
  146.         //Ask for various inputs
  147.         createInputGUI();
  148.         while(id == null || scale == 0.0f){
  149.             try {
  150.                 Thread.sleep(1000);
  151.             } catch (InterruptedException e) {
  152.                 e.printStackTrace();
  153.             }
  154.         }
  155.         int recip = (int)(1.0/scale);
  156.         //Create folder that it will be accessing and create a list of its files.
  157.         File infolder = new File("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+id+"\\");
  158.         File outfolder = new File("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Load\\Textures\\"+id+"\\");
  159.         if (!outfolder.exists()){
  160.             outfolder.mkdir();
  161.         }
  162.         File[] files = infolder.listFiles();
  163.         createMainGUI();
  164.         direct.setText("Current Directory: C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+id+"\\");
  165.         p.setMaximum(files.length);
  166.        
  167.         for (int i = 0; i < files.length; i++){
  168.             //For each file, load it, then scale it down then up (MS Paint-esque). Finally, save file in the Load folder to be loaded by Dolphin.
  169.             long startTime = 0;
  170.             if (i%50 == 0){
  171.                 startTime = System.nanoTime();
  172.             }
  173.             BufferedImage input = load(files[i].getName(), id);
  174.            
  175.             p.setString("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+id+"\\"+files[i].getName() + " (" + i + " out of " + files.length + ")");
  176.             imageIn.setImage(input);
  177.             g.setSize((input.getWidth()*2)+200, g.getHeight());
  178.             g.setLocationRelativeTo(null);
  179.             g.repaint();
  180.            
  181.             input = pixelate(input, scale);
  182.             input = pixelate(input, recip);
  183.            
  184.             imageOut.setImage(input);
  185.             g.repaint();
  186.             save(input, "C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Load\\Textures\\"+id+"\\"+files[i].getName());
  187.             p.setValue(i);
  188.            
  189.             if(i%100 == 0){
  190.                 long endTime = System.nanoTime();
  191.                 double duration = (double)(endTime-startTime) / 1000000000.0;
  192.                 eta.setText("-> ETA " + Math.floor(duration * ((double)(files.length-i))) + " secs");
  193.             }
  194.         }
  195.     }
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement