Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.TreeSet;
- import java.awt.*;
- import java.awt.event.*;
- import java.awt.image.*;
- import javax.imageio.*;
- import javax.swing.*;
- import java.text.*;
- //extra
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import javax.swing.event.ChangeListener;
- import javax.swing.event.ChangeEvent;
- //path class
- import java.nio.file.*;
- import java.nio.IntBuffer;
- import java.nio.ByteBuffer;
- import java.nio.ByteOrder;
- //custom input
- import java.util.Scanner;
- public class Demo extends Component implements ActionListener, ChangeListener {
- //scanner input
- //************************************
- // List of the options(Original, Negative, Addition); correspond to the cases:
- //************************************
- static JTextField t; //editing of a single line of text
- static JButton b; //implementation a button
- static JTextField t2; //editing of a single line of text
- static JButton b2; //implementation a button
- static JSlider slider_s; //implementation of slider
- static JSlider slider_t;
- String descs[] = {
- "Original",
- "Negative",
- "Addition",
- "Subtraction",
- };
- int opIndex = 0; //option index
- int lastOp = -1;
- private BufferedImage bi, biFiltered, img2; // the input image saved as bi;//
- int w, h;
- public Demo(String inputFile) {
- this.t2 = new JTextField(16);
- this.b2 = new JButton("image2");
- if (inputFile.equals("")){
- return;
- }
- String[] fields = inputFile.split("\\.");
- if (fields[1].equals("raw")){
- print("It is a raw file");
- try {
- Path path = Paths.get(inputFile);
- byte[] content = Files.readAllBytes(path);
- IntBuffer buf
- = ByteBuffer.wrap(content)
- .order(ByteOrder.LITTLE_ENDIAN)
- .asIntBuffer();
- w = (int)Math.sqrt(content.length);
- h = w;
- int[] b = new int[w*h];
- for(int i =0; i<w*h; i++){
- b[i] = content[i];
- }
- int[][][] rgb = new int[w][h][4];
- int index = 0;
- for (int y = 0; y < h; y++) {
- for (int x = 0; x < w; x++) {
- if (b[index] < 0){
- b[index] += 255;
- }
- rgb[x][y][0]=255;
- rgb[x][y][1]=b[index];
- rgb[x][y][2]=b[index];
- rgb[x][y][3]=b[index];
- index++;
- }
- }
- bi = convertToBimage(rgb);
- } catch (IOException e) { // deal with the situation that th image has problem;/
- System.out.println("Image could not be read");
- return;
- }
- }else{
- print("It is a normal file");
- try {
- bi = ImageIO.read(new File(inputFile));
- this.img2 = null;
- w = bi.getWidth(null);
- h = bi.getHeight(null);
- System.out.print("bi type: ");
- System.out.println(bi.getType());
- System.out.print("type int rgb type: ");
- System.out.println(BufferedImage.TYPE_INT_RGB);
- 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 image has problem
- System.out.println("Image could not be read");
- return;
- }
- }
- }
- public Dimension getPreferredSize() {
- return new Dimension(w*2, 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<String> formatSet = new TreeSet<String>();
- 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){
- System.out.println(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<height; y++){
- for(int x =0; x<width; x++){
- int a = TmpArray[x][y][0];
- int r = TmpArray[x][y][1];
- int g = TmpArray[x][y][2];
- int b = TmpArray[x][y][3];
- //set RGB value
- int p = (a<<24) | (r<<16) | (g<<8) | b;
- tmpimg.setRGB(x, y, p);
- }
- }
- return tmpimg;
- }
- //************************************
- // helper functions:
- //************************************
- public static void print(String m){
- System.out.println(m);
- }
- //************************************
- // Example: Image Negative: lightest areas of the photographed subject appear darkest and the darkest areas appear lightest.
- //************************************
- public int[][][] ImageNegative(BufferedImage timg){
- int width = timg.getWidth();
- int height = timg.getHeight();
- int[][][] ImageArray = convertToArray(timg); // Convert the image to array
- // Image Negative Operation:
- for(int y=0; y<height; y++){
- for(int x =0; x<width; x++){
- ImageArray[x][y][1] = 255-ImageArray[x][y][1]; //r
- ImageArray[x][y][2] = 255-ImageArray[x][y][2]; //g
- ImageArray[x][y][3] = 255-ImageArray[x][y][3]; //b
- }
- }
- return ImageArray; // Convert the array to BufferedImage
- }
- //************************************
- // adding functions:
- //************************************
- //convert two images in one
- public BufferedImage TwoImages(int[][][] newImageArray){
- int[][][] ImageArray = convertToTwoArray(bi);
- int width = newImageArray.length;
- int height = ImageArray[0].length;
- // set rgb for the second image
- for(int y=0; y<height; y++){
- for(int x =0; x<width; x++){
- ImageArray[x+width][y][1] = newImageArray[x][y][1]; //r
- ImageArray[x+width][y][2] = newImageArray[x][y][2]; //g
- ImageArray[x+width][y][3] = newImageArray[x][y][3]; //b
- }
- }
- return convertToBimage(ImageArray);
- }
- // Convert the one Image to an array with two image size (by width)
- private static int[][][] convertToTwoArray(BufferedImage image){
- int width = image.getWidth();
- int height = image.getHeight();
- int[][][] result = new int[width*2][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;
- }
- // rescale image
- public static int[][][] Addition(int[][][] ImageArray1, int[][][] ImageArray2 ){
- int t_r = 0;
- int t_g = 0;
- int t_b = 0;
- int width = ImageArray1.length;
- int height = ImageArray1.length;
- //vars
- int rmin, rmax, gmin, gmax, bmin, bmax;
- // To shift by t and rescale by s and find the min and the max
- t_r = ImageArray1[0][0][1];
- t_g = ImageArray1[0][0][2];
- t_b = ImageArray1[0][0][3];
- rmin = (int)(ImageArray2[0][0][1]+t_r); rmax = rmin;
- gmin = (int)(ImageArray2[0][0][2]+t_g); gmax = gmin;
- bmin = (int)(ImageArray2[0][0][3]+t_b); bmax = bmin;
- for(int y=0; y<height; y++){
- for(int x=0; x<width; x++){
- t_r = ImageArray1[x][y][1];
- t_g = ImageArray1[x][y][2];
- t_b = ImageArray1[x][y][3];
- ImageArray2[x][y][1] = (int)(ImageArray2[x][y][1]+t_r); //r
- ImageArray2[x][y][2] = (int)(ImageArray2[x][y][2]+t_g); //g
- ImageArray2[x][y][3] = (int)(ImageArray2[x][y][3]+t_b); //b
- if (rmin>ImageArray2[x][y][1]) { rmin = ImageArray2[x][y][1]; }
- if (gmin>ImageArray2[x][y][2]) { gmin = ImageArray2[x][y][2]; }
- if (bmin>ImageArray2[x][y][3]) { bmin = ImageArray2[x][y][3]; }
- if (rmax<ImageArray2[x][y][1]) { rmax = ImageArray2[x][y][1]; }
- if (gmax<ImageArray2[x][y][2]) { gmax = ImageArray2[x][y][2]; }
- if (bmax<ImageArray2[x][y][3]) { bmax = ImageArray2[x][y][3]; }
- }
- }
- for(int y=0; y<height; y++){
- for(int x =0; x<width; x++){
- ImageArray2[x][y][1]=255*(ImageArray2[x][y][1]-rmin)/(rmax-rmin);
- ImageArray2[x][y][2]=255*(ImageArray2[x][y][2]-gmin)/(gmax-gmin);
- ImageArray2[x][y][3]=255*(ImageArray2[x][y][3]-bmin)/(bmax-bmin);
- }
- }
- return ImageArray2;
- }
- // rescale image 2
- public int[][][] rescale2(int[][][] ImageArray1, double s, int t){
- int[][][] ImageArray2 = ImageArray1;
- int width = w;
- int height = h;
- // To shift by t and rescale by s without finding the min and the max
- for(int y=0; y<height; y++){
- for(int x=0; x<width; x++){
- //System.out.print(x);
- //System.out.print(" ");
- //System.out.println(y);
- ImageArray2[x][y][1] = (int)(s*(ImageArray1[x][y][1]+t)); //r
- ImageArray2[x][y][2] = (int)(s*(ImageArray1[x][y][2]+t)); //g
- ImageArray2[x][y][3] = (int)(s*(ImageArray1[x][y][3]+t)); //b
- if (ImageArray2[x][y][1]<0) { ImageArray2[x][y][1] = 0; }
- if (ImageArray2[x][y][2]<0) { ImageArray2[x][y][2] = 0; }
- if (ImageArray2[x][y][3]<0) { ImageArray2[x][y][3] = 0; }
- if (ImageArray2[x][y][1]>255) { ImageArray2[x][y][1] = 255; }
- if (ImageArray2[x][y][2]>255) { ImageArray2[x][y][2] = 255; }
- if (ImageArray2[x][y][3]>255) { ImageArray2[x][y][3] = 255; }
- }
- }
- return ImageArray2;
- }
- //************************************
- // Image arithmetic: Addition
- //************************************
- //************************************
- // You need to register your function here
- //************************************
- public void filterImage() {
- if (opIndex == lastOp) {
- return;
- }
- lastOp = opIndex;
- switch (opIndex) {
- case 0: biFiltered = bi; /* original */
- return;
- case 1: biFiltered = TwoImages(ImageNegative(bi)); /* Image Negative */
- return;
- case 2: biFiltered = convertToBimage(Addition(convertToArray(this.bi), convertToArray(this.img2))); /* Addition arithmetic operation */
- return;
- //case 3: biFiltered = convertToBimage(gen()); /* add pink tint to the image */
- //return;
- }
- }
- public void actionPerformed(ActionEvent e) {
- if (e.getSource() instanceof JButton){
- if(e.getActionCommand().equals("Submit")){
- newWindow(t.getText());
- }else if (e.getActionCommand().equals("image2")){
- String newImageName = this.t2.getText();
- System.out.println("Button clicked");
- try{
- System.out.println(newImageName);
- this.img2 = ImageIO.read(new File(newImageName));
- if (this.img2.getType() != BufferedImage.TYPE_INT_RGB) {
- BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
- Graphics big = bi2.getGraphics();
- big.drawImage(this.img2, 0, 0, null);
- this.img2 = bi2;
- }
- }catch(Exception e2){
- System.out.println(e2);
- }
- this.biFiltered = TwoImages(this.convertToArray(this.img2));
- this.repaint();
- }
- }
- else if(e.getSource() instanceof JComboBox){
- JComboBox cb = (JComboBox)e.getSource();
- if (cb.getActionCommand().equals("SetFilter")) {
- setOpIndex(cb.getSelectedIndex());
- repaint();
- } else if (cb.getActionCommand().equals("Formats")) {
- String format = (String)cb.getSelectedItem();
- File saveFile = new File("savedimage."+format);
- JFileChooser chooser = new JFileChooser();
- chooser.setSelectedFile(saveFile);
- int rval = chooser.showSaveDialog(cb);
- if (rval == JFileChooser.APPROVE_OPTION) {
- saveFile = chooser.getSelectedFile();
- try {
- ImageIO.write(biFiltered, format, saveFile);
- } catch (IOException ex) {
- }
- }
- }
- }
- System.out.println(e.getSource());
- };
- // if JSlider value is changed
- public void stateChanged(ChangeEvent e){
- Object source = e.getSource();
- if (source instanceof JSlider) {
- double s;
- int t;
- s = slider_s.getValue()/100.0;
- t = slider_t.getValue();
- System.out.print(s);
- System.out.print(" ");
- System.out.println(t);
- biFiltered = convertToBimage(rescale2(convertToArray(this.biFiltered), s, t));
- repaint();
- }
- }
- public void newWindow(String inputFile){
- Demo de = new Demo("");
- JFrame f = new JFrame("File: //" + inputFile);
- try{
- de = new Demo(inputFile);
- }catch (Exception e) {
- System.out.println(e);
- System.out.println("The File does not exist!");
- System.exit(0);
- }
- // ==
- f.add("Center", de);
- slider_s = new JSlider(0, 200, 10);
- slider_s.setValue(100);
- slider_s.setName("s_s");
- // paint the ticks and tarcks
- slider_s.setPaintTrack(true);
- slider_s.setPaintTicks(true);
- slider_s.setPaintLabels(true);
- // set spacing
- slider_s.setMajorTickSpacing(50);
- slider_s.setMinorTickSpacing(10);
- slider_s.addChangeListener(de);
- // ==
- slider_t = new JSlider(0, 255, 120);
- slider_t.setName("s_t");
- slider_t.setValue(0);
- // paint the ticks and tarcks
- slider_t.setPaintTrack(true);
- slider_t.setPaintTicks(true);
- slider_t.setPaintLabels(true);
- // set spacing
- slider_t.setMajorTickSpacing(50);
- slider_t.setMinorTickSpacing(10);
- slider_t.addChangeListener(de);
- // ==
- JComboBox choices = new JComboBox(de.getDescriptions());
- choices.setActionCommand("SetFilter");
- choices.addActionListener(de);
- JComboBox formats = new JComboBox(de.getFormats());
- formats.setActionCommand("Formats");
- formats.addActionListener(new Demo(""));
- this.t2 = new JTextField(16);
- this.b2 = new JButton("image2");
- this.b2.addActionListener(de);
- // ==
- JPanel panel = new JPanel();
- panel.add(this.t2);
- panel.add(this.b2);
- panel.add(new JLabel("Scale:"));
- panel.add(slider_s);
- panel.add(new JLabel("Shifting:"));
- panel.add(slider_t);
- panel.add(choices);
- panel.add(new JLabel("Save As"));
- panel.add(formats);
- f.add("North", panel);
- f.pack();
- f.setVisible(true);
- }
- public static void main(String s[]) {
- JFrame f = new JFrame("Main Window");
- f.addWindowListener(new WindowAdapter() {
- public void windowClosing(WindowEvent e) {System.exit(0);}
- });
- Demo de = new Demo("");
- // scanner obj
- Scanner scanner = new Scanner(System.in);
- //System.out.println("Open file: ");
- //String inputFile = scanner.nextLine();
- t = new JTextField(16);
- t.setText("Peppers.bmp");
- b = new JButton("Submit");
- JPanel j = new JPanel();
- b.addActionListener(de);
- j.add(t);
- j.add(b);
- f.add(j);
- f.setSize(300,300);
- f.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement