Advertisement
Guest User

Untitled

a guest
Apr 7th, 2020
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 19.65 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.TreeSet;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.awt.image.*;
  6. import javax.imageio.*;
  7. import javax.swing.*;
  8. import java.text.*;
  9. //extra
  10. import java.awt.event.ActionListener;
  11. import java.awt.event.ActionEvent;
  12. import javax.swing.event.ChangeListener;
  13. import javax.swing.event.ChangeEvent;
  14.  
  15. //path class
  16. import java.nio.file.*;
  17. import java.nio.IntBuffer;
  18. import java.nio.ByteBuffer;
  19. import java.nio.ByteOrder;
  20. //custom input
  21. import java.util.Scanner;
  22.  
  23.  
  24. public class Demo extends Component implements ActionListener, ChangeListener {
  25.     //scanner input
  26.     //************************************
  27.     // List of the options(Original, Negative, Addition); correspond to the cases:
  28.     //************************************
  29.     static JTextField t; //editing of a single line of text
  30.     static JButton b; //implementation a button
  31.    
  32.    
  33.     static JTextField t2; //editing of a single line of text
  34.     static JButton b2; //implementation a button
  35.    
  36.     static JSlider slider_s; //implementation of slider
  37.     static JSlider slider_t;
  38.    
  39.     String descs[] = {
  40.             "Original",
  41.             "Negative",
  42.             "Addition",
  43.             "Subtraction",
  44.     };
  45.  
  46.     int opIndex = 0;  //option index
  47.     int lastOp = -1;
  48.  
  49.     private BufferedImage bi, biFiltered, img2;   // the input image saved as bi;//
  50.     int w, h;
  51.      
  52.     public Demo(String inputFile) {
  53.         this.t2 = new JTextField(16);
  54.         this.b2 = new JButton("image2");
  55.         if (inputFile.equals("")){
  56.             return;
  57.         }
  58.         String[] fields = inputFile.split("\\.");
  59.         if (fields[1].equals("raw")){
  60.             print("It is a raw file");
  61.             try {
  62.                 Path path = Paths.get(inputFile);
  63.                 byte[] content = Files.readAllBytes(path);
  64.                
  65.                 IntBuffer buf
  66.                     = ByteBuffer.wrap(content)
  67.                     .order(ByteOrder.LITTLE_ENDIAN)
  68.                     .asIntBuffer();
  69.                
  70.                 w = (int)Math.sqrt(content.length);
  71.                 h = w;
  72.                 int[] b = new int[w*h];
  73.                 for(int i =0; i<w*h; i++){
  74.                     b[i] = content[i];
  75.                 }
  76.                
  77.                
  78.                 int[][][] rgb = new int[w][h][4];
  79.                 int index = 0;
  80.                 for (int y = 0; y < h; y++) {
  81.                      for (int x = 0; x < w; x++) {
  82.                             if (b[index] < 0){
  83.                                 b[index] += 255;
  84.                             }
  85.                             rgb[x][y][0]=255;
  86.                             rgb[x][y][1]=b[index];
  87.                             rgb[x][y][2]=b[index];
  88.                             rgb[x][y][3]=b[index];
  89.                             index++;
  90.                      }
  91.                 }
  92.                 bi = convertToBimage(rgb);
  93.        
  94.             } catch (IOException e) {      // deal with the situation that th image has problem;/
  95.                     System.out.println("Image could not be read");
  96.                     return;
  97.             }
  98.            
  99.         }else{  
  100.             print("It is a normal file");  
  101.             try {
  102.                     bi = ImageIO.read(new File(inputFile));
  103.                     this.img2 = null;
  104.  
  105.                     w = bi.getWidth(null);
  106.                     h = bi.getHeight(null);
  107.                     System.out.print("bi type: ");
  108.                     System.out.println(bi.getType());
  109.                     System.out.print("type int rgb type: ");
  110.                     System.out.println(BufferedImage.TYPE_INT_RGB);
  111.                     if (bi.getType() != BufferedImage.TYPE_INT_RGB) {
  112.                             BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  113.                             Graphics big = bi2.getGraphics();
  114.                             big.drawImage(bi, 0, 0, null);
  115.                             biFiltered = bi = bi2;
  116.                     }
  117.             } catch (IOException e) {      // deal with the situation that image has problem
  118.                     System.out.println("Image could not be read");
  119.                     return;
  120.             }  
  121.         }
  122.        
  123.     }      
  124.     public Dimension getPreferredSize() {
  125.             return new Dimension(w*2, h);
  126.     }
  127.  
  128.     String[] getDescriptions() {
  129.             return descs;
  130.     }
  131.  
  132.     // Return the formats sorted alphabetically and in lower case
  133.     public String[] getFormats() {
  134.             String[] formats = {"bmp","gif","jpeg","jpg","png"};
  135.             TreeSet<String> formatSet = new TreeSet<String>();
  136.             for (String s : formats) {
  137.                     formatSet.add(s.toLowerCase());
  138.             }
  139.             return formatSet.toArray(new String[0]);
  140.     }
  141.  
  142.  
  143.  
  144.     void setOpIndex(int i) {
  145.             opIndex = i;
  146.     }
  147.  
  148.     public void paint(Graphics g) { //  Repaint will call this function so the image will change.
  149.             filterImage();      
  150.  
  151.             g.drawImage(biFiltered, 0, 0, null);
  152.     }
  153.  
  154.  
  155.     //************************************
  156.     //  Convert the Buffered Image to Array
  157.     //************************************
  158.     private static int[][][] convertToArray(BufferedImage image){
  159.         System.out.println(image);
  160.         int width = image.getWidth();
  161.         int height = image.getHeight();
  162.  
  163.         int[][][] result = new int[width][height][4];
  164.  
  165.         for (int y = 0; y < height; y++) {
  166.              for (int x = 0; x < width; x++) {
  167.                     int p = image.getRGB(x,y);
  168.                     int a = (p>>24)&0xff;
  169.                     int r = (p>>16)&0xff;
  170.                     int g = (p>>8)&0xff;
  171.                     int b = p&0xff;
  172.  
  173.                     result[x][y][0]=a;
  174.                     result[x][y][1]=r;
  175.                     result[x][y][2]=g;
  176.                     result[x][y][3]=b;
  177.              }
  178.         }
  179.         return result;
  180.     }
  181.  
  182.     //************************************
  183.     //  Convert the  Array to BufferedImage
  184.     //************************************
  185.     public BufferedImage convertToBimage(int[][][] TmpArray){
  186.  
  187.             int width = TmpArray.length;
  188.             int height = TmpArray[0].length;
  189.  
  190.             BufferedImage tmpimg=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
  191.  
  192.             for(int y=0; y<height; y++){
  193.                     for(int x =0; x<width; x++){
  194.                             int a = TmpArray[x][y][0];
  195.                             int r = TmpArray[x][y][1];
  196.                             int g = TmpArray[x][y][2];
  197.                             int b = TmpArray[x][y][3];
  198.                            
  199.                             //set RGB value
  200.  
  201.                             int p = (a<<24) | (r<<16) | (g<<8) | b;
  202.                             tmpimg.setRGB(x, y, p);
  203.  
  204.                     }
  205.             }
  206.             return tmpimg;
  207.     }
  208.     //************************************
  209.     //  helper functions:
  210.     //************************************
  211.     public static void print(String m){
  212.         System.out.println(m);
  213.     }
  214.     //************************************
  215.     //  Example:  Image Negative: lightest areas of the photographed subject appear darkest and the darkest areas appear lightest.
  216.     //************************************
  217.     public int[][][] ImageNegative(BufferedImage timg){
  218.             int width = timg.getWidth();
  219.             int height = timg.getHeight();
  220.  
  221.             int[][][] ImageArray = convertToArray(timg);          //  Convert the image to array
  222.  
  223.             // Image Negative Operation:
  224.             for(int y=0; y<height; y++){
  225.                     for(int x =0; x<width; x++){
  226.                         ImageArray[x][y][1] = 255-ImageArray[x][y][1];  //r
  227.                         ImageArray[x][y][2] = 255-ImageArray[x][y][2];  //g
  228.                         ImageArray[x][y][3] = 255-ImageArray[x][y][3];  //b
  229.                     }
  230.             }
  231.            
  232.             return ImageArray;  // Convert the array to BufferedImage
  233.     }
  234.  
  235.  
  236.  
  237.  
  238.     //************************************
  239.     //  adding functions:
  240.     //************************************
  241.     //convert two images in one
  242.     public BufferedImage TwoImages(int[][][] newImageArray){
  243.             int[][][] ImageArray = convertToTwoArray(bi);
  244.            
  245.             int width = newImageArray.length;
  246.             int height = ImageArray[0].length;
  247.            
  248.             // set rgb for the second image
  249.             for(int y=0; y<height; y++){
  250.                     for(int x =0; x<width; x++){
  251.                         ImageArray[x+width][y][1] = newImageArray[x][y][1];  //r
  252.                         ImageArray[x+width][y][2] = newImageArray[x][y][2];  //g
  253.                         ImageArray[x+width][y][3] = newImageArray[x][y][3];  //b
  254.                     }
  255.             }
  256.             return convertToBimage(ImageArray);
  257.     }
  258.     //  Convert the one Image to an array with two image size (by width)
  259.     private static int[][][] convertToTwoArray(BufferedImage image){
  260.         int width = image.getWidth();
  261.         int height = image.getHeight();
  262.  
  263.         int[][][] result = new int[width*2][height][4];
  264.  
  265.         for (int y = 0; y < height; y++) {
  266.              for (int x = 0; x < width; x++) {
  267.                     int p = image.getRGB(x,y);
  268.                     int a = (p>>24)&0xff;
  269.                     int r = (p>>16)&0xff;
  270.                     int g = (p>>8)&0xff;
  271.                     int b = p&0xff;
  272.  
  273.                     result[x][y][0]=a;
  274.                     result[x][y][1]=r;
  275.                     result[x][y][2]=g;
  276.                     result[x][y][3]=b;
  277.              }
  278.         }
  279.         return result;
  280.     }
  281.    
  282.     // rescale image
  283.     public static int[][][] Addition(int[][][] ImageArray1, int[][][] ImageArray2 ){
  284.        
  285.         int t_r = 0;
  286.         int t_g = 0;
  287.         int t_b = 0;
  288.        
  289.         int width = ImageArray1.length;
  290.         int height = ImageArray1.length;
  291.         //vars
  292.         int rmin, rmax, gmin, gmax, bmin, bmax;
  293.         // To shift by t and rescale by s and find the min and the max
  294.         t_r = ImageArray1[0][0][1];
  295.         t_g = ImageArray1[0][0][2];
  296.         t_b = ImageArray1[0][0][3];
  297.        
  298.         rmin = (int)(ImageArray2[0][0][1]+t_r); rmax = rmin;
  299.         gmin = (int)(ImageArray2[0][0][2]+t_g); gmax = gmin;
  300.         bmin = (int)(ImageArray2[0][0][3]+t_b); bmax = bmin;
  301.         for(int y=0; y<height; y++){
  302.             for(int x=0; x<width; x++){
  303.                 t_r = ImageArray1[x][y][1];
  304.                 t_g = ImageArray1[x][y][2];
  305.                 t_b = ImageArray1[x][y][3];
  306.                 ImageArray2[x][y][1] = (int)(ImageArray2[x][y][1]+t_r); //r
  307.                 ImageArray2[x][y][2] = (int)(ImageArray2[x][y][2]+t_g); //g
  308.                 ImageArray2[x][y][3] = (int)(ImageArray2[x][y][3]+t_b); //b
  309.                 if (rmin>ImageArray2[x][y][1]) { rmin = ImageArray2[x][y][1]; }
  310.                 if (gmin>ImageArray2[x][y][2]) { gmin = ImageArray2[x][y][2]; }
  311.                 if (bmin>ImageArray2[x][y][3]) { bmin = ImageArray2[x][y][3]; }
  312.                 if (rmax<ImageArray2[x][y][1]) { rmax = ImageArray2[x][y][1]; }
  313.                 if (gmax<ImageArray2[x][y][2]) { gmax = ImageArray2[x][y][2]; }
  314.                 if (bmax<ImageArray2[x][y][3]) { bmax = ImageArray2[x][y][3]; }
  315.             }
  316.         }
  317.         for(int y=0; y<height; y++){
  318.             for(int x =0; x<width; x++){
  319.                 ImageArray2[x][y][1]=255*(ImageArray2[x][y][1]-rmin)/(rmax-rmin);
  320.                 ImageArray2[x][y][2]=255*(ImageArray2[x][y][2]-gmin)/(gmax-gmin);
  321.                 ImageArray2[x][y][3]=255*(ImageArray2[x][y][3]-bmin)/(bmax-bmin);
  322.             }
  323.         }  
  324.         return ImageArray2;
  325.     }
  326.    
  327.        // rescale image 2
  328.     public int[][][] rescale2(int[][][] ImageArray1, double s, int t){
  329.         int[][][] ImageArray2 = ImageArray1;
  330.        
  331.        
  332.         int width = w;
  333.         int height = h;
  334.        
  335.         // To shift by t and rescale by s without finding the min and the max
  336.         for(int y=0; y<height; y++){
  337.             for(int x=0; x<width; x++){
  338.                 //System.out.print(x);
  339.                 //System.out.print(" ");
  340.                 //System.out.println(y);
  341.                 ImageArray2[x][y][1] = (int)(s*(ImageArray1[x][y][1]+t)); //r
  342.                 ImageArray2[x][y][2] = (int)(s*(ImageArray1[x][y][2]+t)); //g
  343.                 ImageArray2[x][y][3] = (int)(s*(ImageArray1[x][y][3]+t)); //b
  344.                 if (ImageArray2[x][y][1]<0) { ImageArray2[x][y][1] = 0; }
  345.                 if (ImageArray2[x][y][2]<0) { ImageArray2[x][y][2] = 0; }
  346.                 if (ImageArray2[x][y][3]<0) { ImageArray2[x][y][3] = 0; }
  347.                 if (ImageArray2[x][y][1]>255) { ImageArray2[x][y][1] = 255; }
  348.                 if (ImageArray2[x][y][2]>255) { ImageArray2[x][y][2] = 255; }
  349.                 if (ImageArray2[x][y][3]>255) { ImageArray2[x][y][3] = 255; }
  350.             }
  351.         }
  352.        
  353.         return ImageArray2;
  354.     }
  355.  
  356.     //************************************
  357.     //  Image arithmetic: Addition
  358.     //************************************
  359.  
  360.    
  361.    
  362.    
  363.     //************************************
  364.     //  You need to register your function here
  365.     //************************************
  366.     public void filterImage() {
  367.             if (opIndex == lastOp) {
  368.                     return;
  369.             }
  370.  
  371.             lastOp = opIndex;
  372.             switch (opIndex) {
  373.             case 0: biFiltered = bi; /* original */
  374.                 return;
  375.             case 1: biFiltered = TwoImages(ImageNegative(bi)); /* Image Negative */
  376.                 return;
  377.             case 2: biFiltered = convertToBimage(Addition(convertToArray(this.bi), convertToArray(this.img2))); /* Addition arithmetic operation */
  378.                 return;
  379.             //case 3: biFiltered = convertToBimage(gen()); /* add pink tint to the image */
  380.                 //return;
  381.            
  382.  
  383.             }
  384.  
  385.     }
  386.  
  387.  
  388.  
  389.      public void actionPerformed(ActionEvent e) {
  390.         if (e.getSource() instanceof JButton){
  391.             if(e.getActionCommand().equals("Submit")){
  392.                 newWindow(t.getText());
  393.             }else if (e.getActionCommand().equals("image2")){
  394.                 String newImageName = this.t2.getText();
  395.                 System.out.println("Button clicked");
  396.                 try{
  397.                     System.out.println(newImageName);
  398.                     this.img2 = ImageIO.read(new File(newImageName));
  399.                     if (this.img2.getType() != BufferedImage.TYPE_INT_RGB) {
  400.                             BufferedImage bi2 = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
  401.                             Graphics big = bi2.getGraphics();
  402.                             big.drawImage(this.img2, 0, 0, null);
  403.                             this.img2 = bi2;
  404.                     }
  405.                 }catch(Exception e2){
  406.                     System.out.println(e2);
  407.                 }
  408.                
  409.                 this.biFiltered = TwoImages(this.convertToArray(this.img2));
  410.                 this.repaint();
  411.                
  412.  
  413.  
  414.             }
  415.         }
  416.        
  417.        
  418.         else if(e.getSource() instanceof JComboBox){
  419.             JComboBox cb = (JComboBox)e.getSource();
  420.             if (cb.getActionCommand().equals("SetFilter")) {
  421.                  setOpIndex(cb.getSelectedIndex());
  422.                  repaint();
  423.             } else if (cb.getActionCommand().equals("Formats")) {
  424.                  String format = (String)cb.getSelectedItem();
  425.                  File saveFile = new File("savedimage."+format);
  426.                  JFileChooser chooser = new JFileChooser();
  427.                  chooser.setSelectedFile(saveFile);
  428.                  int rval = chooser.showSaveDialog(cb);
  429.                  if (rval == JFileChooser.APPROVE_OPTION) {
  430.                          saveFile = chooser.getSelectedFile();
  431.                          try {
  432.                                  ImageIO.write(biFiltered, format, saveFile);
  433.                          } catch (IOException ex) {
  434.                          }
  435.                  }
  436.             }
  437.         }
  438.        
  439.         System.out.println(e.getSource());
  440.        
  441.        
  442.     };
  443.     // if JSlider value is changed
  444.     public void stateChanged(ChangeEvent e){    
  445.         Object source = e.getSource();
  446.         if (source instanceof JSlider) {
  447.             double s;
  448.             int t;
  449.             s = slider_s.getValue()/100.0;
  450.             t = slider_t.getValue();
  451.             System.out.print(s);
  452.             System.out.print(" ");
  453.             System.out.println(t);
  454.             biFiltered = convertToBimage(rescale2(convertToArray(this.biFiltered), s, t));
  455.             repaint();
  456.  
  457.         }
  458.     }
  459.    
  460.     public  void newWindow(String inputFile){
  461.         Demo de = new Demo("");
  462.         JFrame f = new JFrame("File: //" + inputFile);
  463.        
  464.         try{
  465.            
  466.            
  467.             de = new Demo(inputFile);
  468.            
  469.            
  470.         }catch (Exception e) {
  471.             System.out.println(e);
  472.             System.out.println("The File does not exist!");
  473.             System.exit(0);
  474.         }
  475.         // ==
  476.         f.add("Center", de);
  477.         slider_s = new JSlider(0, 200, 10);
  478.         slider_s.setValue(100);
  479.         slider_s.setName("s_s");
  480.        
  481.         // paint the ticks and tarcks
  482.         slider_s.setPaintTrack(true);
  483.         slider_s.setPaintTicks(true);
  484.         slider_s.setPaintLabels(true);
  485.         // set spacing
  486.         slider_s.setMajorTickSpacing(50);
  487.         slider_s.setMinorTickSpacing(10);
  488.         slider_s.addChangeListener(de);
  489.         // ==
  490.         slider_t = new JSlider(0, 255, 120);
  491.         slider_t.setName("s_t");
  492.         slider_t.setValue(0);
  493.         // paint the ticks and tarcks
  494.         slider_t.setPaintTrack(true);
  495.         slider_t.setPaintTicks(true);
  496.         slider_t.setPaintLabels(true);
  497.         // set spacing
  498.         slider_t.setMajorTickSpacing(50);
  499.         slider_t.setMinorTickSpacing(10);
  500.         slider_t.addChangeListener(de);
  501.         // ==
  502.         JComboBox choices = new JComboBox(de.getDescriptions());
  503.         choices.setActionCommand("SetFilter");
  504.         choices.addActionListener(de);
  505.         JComboBox formats = new JComboBox(de.getFormats());
  506.         formats.setActionCommand("Formats");
  507.         formats.addActionListener(new Demo(""));
  508.        
  509.         this.t2 = new JTextField(16);
  510.         this.b2 = new JButton("image2");
  511.         this.b2.addActionListener(de);
  512.         // ==
  513.         JPanel panel = new JPanel();
  514.        
  515.         panel.add(this.t2);
  516.         panel.add(this.b2);
  517.         panel.add(new JLabel("Scale:"));
  518.         panel.add(slider_s);
  519.         panel.add(new JLabel("Shifting:"));
  520.         panel.add(slider_t);
  521.         panel.add(choices);
  522.         panel.add(new JLabel("Save As"));
  523.         panel.add(formats);
  524.        
  525.         f.add("North", panel);
  526.         f.pack();
  527.         f.setVisible(true);
  528.     }
  529.     public static void main(String s[]) {
  530.         JFrame f = new JFrame("Main Window");
  531.         f.addWindowListener(new WindowAdapter() {
  532.                 public void windowClosing(WindowEvent e) {System.exit(0);}
  533.         });
  534.         Demo de = new Demo("");
  535.         // scanner obj
  536.         Scanner scanner = new Scanner(System.in);
  537.         //System.out.println("Open file: ");
  538.         //String inputFile = scanner.nextLine();
  539.        
  540.         t = new JTextField(16);
  541.         t.setText("Peppers.bmp");
  542.        
  543.         b = new JButton("Submit");
  544.  
  545.         JPanel j = new JPanel();
  546.        
  547.         b.addActionListener(de);
  548.         j.add(t);
  549.         j.add(b);
  550.        
  551.         f.add(j);
  552.         f.setSize(300,300);
  553.         f.show();
  554.  
  555.     }
  556. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement