Home | History | Annotate | Download | only in flame
      1 /*******************************************************************************
      2  * Copyright 2011 See AUTHORS file.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *   http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  ******************************************************************************/
     16 package com.badlogic.gdx.tools.flame;
     17 
     18 import java.awt.EventQueue;
     19 import java.awt.FileDialog;
     20 import java.awt.event.ActionEvent;
     21 import java.awt.event.ActionListener;
     22 import java.awt.event.WindowAdapter;
     23 import java.awt.event.WindowEvent;
     24 import java.awt.image.BufferedImage;
     25 import java.awt.image.WritableRaster;
     26 import java.io.File;
     27 import java.io.IOException;
     28 
     29 import javax.imageio.ImageIO;
     30 import javax.swing.JFrame;
     31 import javax.swing.JMenu;
     32 import javax.swing.JMenuBar;
     33 import javax.swing.JMenuItem;
     34 import javax.swing.JOptionPane;
     35 import javax.swing.UIManager;
     36 import javax.swing.UIManager.LookAndFeelInfo;
     37 
     38 /** @author Inferno */
     39 public class PreAlpha extends JFrame {
     40 	BufferedImage image;
     41 	ImagePanel imagePanel;
     42 	String lastDir;
     43 
     44 	public PreAlpha () {
     45 		super("Premultiply alpha converter");
     46 		addWindowListener(new WindowAdapter() {
     47 			public void windowClosed (WindowEvent event) {
     48 				System.exit(0);
     49 			}
     50 		});
     51 
     52 		initializeComponents();
     53 		pack();
     54 		setLocationRelativeTo(null);
     55 		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
     56 		setVisible(true);
     57 	}
     58 
     59 	private void initializeComponents () {
     60 		//Create the menu bar.
     61 		JMenuBar menuBar = new JMenuBar();
     62 
     63 		//Build the first menu.
     64 		JMenu menu = new JMenu("File");
     65 		menuBar.add(menu);
     66 
     67 		//a group of JMenuItems
     68 		JMenuItem menuItem = new JMenuItem("Open");
     69 		menuItem.addActionListener(new ActionListener() {
     70 
     71 			@Override
     72 			public void actionPerformed (ActionEvent arg0) {
     73 				open();
     74 			}
     75 		});
     76 		menu.add(menuItem);
     77 
     78 		menuItem = new JMenuItem("Save");
     79 		menuItem.addActionListener(new ActionListener() {
     80 
     81 			@Override
     82 			public void actionPerformed (ActionEvent arg0) {
     83 				save();
     84 			}
     85 		});
     86 		menu.add(menuItem);
     87 		setJMenuBar(menuBar);
     88 
     89 		imagePanel = new ImagePanel();
     90 		getContentPane().add(imagePanel);
     91 	}
     92 
     93 	protected void save () {
     94 		FileDialog dialog = new FileDialog(this, "Save Image", FileDialog.SAVE);
     95 		if (lastDir != null) dialog.setDirectory(lastDir);
     96 		dialog.setVisible(true);
     97 		final String file = dialog.getFile();
     98 		final String dir = dialog.getDirectory();
     99 		if (dir == null || file == null || file.trim().length() == 0) return;
    100 		lastDir = dir;
    101 		try {
    102 			generatePremultiplyAlpha(new File(dir, file));
    103 			JOptionPane.showMessageDialog(this, "Conversion complete!");
    104 		} catch (Exception ex) {
    105 			JOptionPane.showMessageDialog(this, "Error saving image.");
    106 			return;
    107 		}
    108 	}
    109 
    110 	protected void open () {
    111 		FileDialog dialog = new FileDialog(this, "Open Image", FileDialog.LOAD);
    112 		if (lastDir != null) dialog.setDirectory(lastDir);
    113 		dialog.setVisible(true);
    114 		final String file = dialog.getFile();
    115 		final String dir = dialog.getDirectory();
    116 		if (dir == null || file == null || file.trim().length() == 0) return;
    117 		lastDir = dir;
    118 		try {
    119 			image = ImageIO.read(new File(dir, file));
    120 			imagePanel.setImage(image);
    121 			imagePanel.revalidate();
    122 			imagePanel.repaint();
    123 			pack();
    124 		} catch (Exception ex) {
    125 			JOptionPane.showMessageDialog(this, "Error opening image.");
    126 			return;
    127 		}
    128 	}
    129 
    130 	private void generatePremultiplyAlpha(File out){
    131 		try {
    132 			BufferedImage outImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
    133 			float[] color = new float[4];
    134 			WritableRaster raster = image.getRaster();
    135 			WritableRaster outRaster = outImage.getRaster();
    136 			for(int x =0, w = image.getWidth(); x< w; ++x)
    137 				for(int y =0, h = image.getHeight(); y< h; ++y){
    138 					raster.getPixel(x, y, color);
    139 					float alpha = color[3]/255f;
    140 					for(int i=0;i < 3; ++i)
    141 						color[i] *= alpha;
    142 					outRaster.setPixel(x, y, color);
    143 				}
    144 			ImageIO.write(outImage, "png", out);
    145 		} catch (IOException e) {
    146 			e.printStackTrace();
    147 		}
    148 	}
    149 
    150 
    151 	public static void main (String[] args) {
    152 		for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
    153 			if ("Nimbus".equals(info.getName())) {
    154 				try {
    155 					UIManager.setLookAndFeel(info.getClassName());
    156 				} catch (Throwable ignored) {
    157 				}
    158 				break;
    159 			}
    160 		}
    161 		EventQueue.invokeLater(new Runnable() {
    162 			public void run () {
    163 				new PreAlpha();
    164 			}
    165 		});
    166 	}
    167 }
    168