import java.io.*; import java.util.TreeSet; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.imageio.*; import javax.swing.*; public class Demo extends Component implements ActionListener { //************************************ // List of the options(Original, Negative); correspond to the cases: //************************************ String descs[] = { "Original", "Negative", }; int opIndex; //option index for int lastOp; private BufferedImage bi, biFiltered; // the input image saved as bi;// int w, h; public Demo() { try { bi = ImageIO.read(new File("default.jpg")); w = bi.getWidth(null); h = bi.getHeight(null); System.out.println(bi.getType()); if (bi.getType() != BufferedImage.TYPE_INT_RGB) { BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); Graphics big = bi2.getGraphics(); big.drawImage(bi, 0, 0, null); biFiltered = bi = bi2; } } catch (IOException e) { // deal with the situation that th image has problem;/ System.out.println("Image could not be read"); System.exit(1); } } public Dimension getPreferredSize() { return new Dimension(w, h); } String[] getDescriptions() { return descs; } // Return the formats sorted alphabetically and in lower case public String[] getFormats() { String[] formats = {"bmp","gif","jpeg","jpg","png"}; TreeSet formatSet = new TreeSet(); for (String s : formats) { formatSet.add(s.toLowerCase()); } return formatSet.toArray(new String[0]); } void setOpIndex(int i) { opIndex = i; } public void paint(Graphics g) { // Repaint will call this function so the image will change. filterImage(); g.drawImage(biFiltered, 0, 0, null); } //************************************ // Convert the Buffered Image to Array //************************************ private static int[][][] convertToArray(BufferedImage image){ int width = image.getWidth(); int height = image.getHeight(); int[][][] result = new int[width][height][4]; for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int p = image.getRGB(x,y); int a = (p>>24)&0xff; int r = (p>>16)&0xff; int g = (p>>8)&0xff; int b = p&0xff; result[x][y][0]=a; result[x][y][1]=r; result[x][y][2]=g; result[x][y][3]=b; } } return result; } //************************************ // Convert the Array to BufferedImage //************************************ public BufferedImage convertToBimage(int[][][] TmpArray){ int width = TmpArray.length; int height = TmpArray[0].length; BufferedImage tmpimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); for(int y=0; y