Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.geom.AffineTransform;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.IOException;
- import javax.imageio.ImageIO;
- import javax.swing.*;
- public class pixels extends JPanel{
- public static JFrame g;
- public static JProgressBar p;
- public static JLabel direct;
- public static JLabel eta;
- public static ImageIcon imageIn;
- public static ImageIcon imageOut;
- public static JFrame in;
- public static JTextField gid;
- public static JTextField scalei;
- public static JLabel warning1;
- public static JLabel warning2;
- public static JButton select;
- public static String id = null;
- public static float scale = 0.0f;
- public static BufferedImage load(String filename, String id){
- //Initialize BufferedImage input
- BufferedImage input = null;
- try{
- //Set the BufferedImage's path
- input = ImageIO.read(new File("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+id+"\\"+filename));
- return input;
- } catch (IOException e){
- //Exit the program if error occurs.
- System.out.println("Sorry! Here is the exception: "+e);
- return input;
- }
- } public static BufferedImage pixelate(BufferedImage imgIn, float scale){
- //Create BufferedImage type RGB with Alpha.
- int imageType = BufferedImage.TYPE_INT_ARGB;
- //Initializes the new scaled BufferedImage with the scaled dimensions.
- BufferedImage scaled = new BufferedImage(Math.round(imgIn.getWidth()*scale), Math.round(imgIn.getHeight()*scale), imageType);
- //Initialize Grpahics2D for the scaled BufferedImage.
- Graphics2D g = scaled.createGraphics();
- //Set Graphics2D to retain Alpha.
- g.setComposite(AlphaComposite.Src);
- //Create the modifier for drawRenderedImage() and executes it.
- AffineTransform at = AffineTransform.getScaleInstance(scale, scale);
- g.drawRenderedImage(imgIn, at);
- //Frees up system resources and returns the scaled image.
- g.dispose();
- return scaled;
- } public static void save(BufferedImage save, String name){
- try{
- //Saves image with specified name, directory, and .png extension.
- File output = new File (name);
- ImageIO.write(save, "png", output);
- } catch (IOException e){
- //Exits if there is an error
- System.out.println("Sorry! Here is the exception: "+e);
- }
- } public static void createMainGUI (){
- g = new JFrame("Pixelator");
- g.setSize(800, 300);
- g.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- g.setLayout(new BorderLayout());
- g.setLocationRelativeTo(null);
- direct = new JLabel("");
- direct.setHorizontalAlignment(JLabel.CENTER);
- direct.setToolTipText("Directory working in.");
- eta = new JLabel("");
- eta.setHorizontalAlignment(JLabel.CENTER);
- p = new JProgressBar(0, 500);
- p.setValue(0);
- p.setStringPainted(true);
- p.setMinimum(0);
- imageIn = new ImageIcon();
- imageOut = new ImageIcon();
- g.add(new JLabel(imageIn), BorderLayout.WEST);
- g.add(new JLabel(imageOut), BorderLayout.EAST);
- g.add(eta, BorderLayout.CENTER);
- g.add(direct, BorderLayout.NORTH);
- g.add(p, BorderLayout.SOUTH);
- g.setVisible(true);
- } public static void createInputGUI(){
- in = new JFrame ("Pixelator");
- in.setSize(300, 150);
- in.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- in.setLayout(new GridLayout(3,2));
- in.setLocationRelativeTo(null);
- gid = new JTextField(20);
- gid.setText("Game I.D.");
- scalei = new JTextField(20);
- scalei.setText("Scale");
- warning1 = new JLabel("Invalid ID.");
- warning2 = new JLabel("Invalid scale.");
- warning1.setForeground(Color.RED);
- warning2.setForeground(Color.RED);
- warning1.setVisible(false);
- warning2.setVisible(false);
- select = new JButton("O.K.");
- select.setMnemonic('O');
- select.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e){
- File path = new File("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+gid.getText()+"\\");
- if (!path.exists() || gid.getText() == ""){
- warning1.setVisible(true);
- gid.setBackground(new Color(249, 144, 144));
- } else {
- try{
- Float.parseFloat(scalei.getText());
- } catch(NumberFormatException e1){
- warning2.setVisible(true);
- scalei.setBackground(new Color(249, 144, 144));
- }
- id = gid.getText();
- scale = Float.parseFloat(scalei.getText());
- in.setVisible(false);
- }
- }
- });
- in.add(gid);
- in.add(warning1);
- in.add(scalei);
- in.add(warning2);
- in.add(select);
- in.setVisible(true);
- }
- public static void main (String[] args){
- //Ask for various inputs
- createInputGUI();
- while(id == null || scale == 0.0f){
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- int recip = (int)(1.0/scale);
- //Create folder that it will be accessing and create a list of its files.
- File infolder = new File("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+id+"\\");
- File outfolder = new File("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Load\\Textures\\"+id+"\\");
- if (!outfolder.exists()){
- outfolder.mkdir();
- }
- File[] files = infolder.listFiles();
- createMainGUI();
- direct.setText("Current Directory: C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+id+"\\");
- p.setMaximum(files.length);
- for (int i = 0; i < files.length; i++){
- //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.
- long startTime = 0;
- if (i%50 == 0){
- startTime = System.nanoTime();
- }
- BufferedImage input = load(files[i].getName(), id);
- p.setString("C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Dump\\Textures\\"+id+"\\"+files[i].getName() + " (" + i + " out of " + files.length + ")");
- imageIn.setImage(input);
- g.setSize((input.getWidth()*2)+200, g.getHeight());
- g.setLocationRelativeTo(null);
- g.repaint();
- input = pixelate(input, scale);
- input = pixelate(input, recip);
- imageOut.setImage(input);
- g.repaint();
- save(input, "C:\\Users\\"+System.getProperty("user.name")+"\\Documents\\Dolphin Emulator\\Load\\Textures\\"+id+"\\"+files[i].getName());
- p.setValue(i);
- if(i%100 == 0){
- long endTime = System.nanoTime();
- double duration = (double)(endTime-startTime) / 1000000000.0;
- eta.setText("-> ETA " + Math.floor(duration * ((double)(files.length-i))) + " secs");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement