initial commit
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / sampleApplication / ImagePanel.java
1 package eu.svjatoslav.imagesqueeze.sampleApplication;
2
3 import java.awt.BorderLayout;
4
5 import java.awt.Dimension;
6 import java.awt.image.BufferedImage;
7 import java.io.BufferedReader;
8 import java.io.ByteArrayOutputStream;
9 import java.io.File;
10 import java.io.FileInputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13
14 import javax.swing.JButton;
15
16 import javax.swing.ImageIcon;
17 import javax.swing.JPanel;
18 import javax.swing.WindowConstants;
19 import javax.swing.JFrame;
20 import javax.swing.JLabel;
21
22 import eu.svjatoslav.imagesqueeze.codec.Image;
23
24
25 public class ImagePanel extends javax.swing.JPanel {
26         private JLabel imageLabel;
27
28         public BufferedImage bufferedImage;
29
30         /**
31          * Auto-generated main method to display this 
32          * JPanel inside a new JFrame.
33          */
34         public static void main(String[] args) {
35                 JFrame frame = new JFrame();
36                 frame.getContentPane().add(new ImagePanel());
37                 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
38                 frame.pack();
39                 frame.setVisible(true);
40         }
41
42         public ImagePanel() {
43                 super();
44                 initGUI();
45         }
46
47         private void initGUI() {
48                 try {
49                         BorderLayout thisLayout = new BorderLayout();
50                         this.setLayout(thisLayout);
51                         setPreferredSize(new Dimension(660, 500));
52                         {
53                                 imageLabel = new JLabel();
54                                 this.add(getImageLabel(), BorderLayout.CENTER);
55                         }
56                 } catch (Exception e) {
57                         e.printStackTrace();
58                 }
59         }
60
61         public JLabel getImageLabel() {
62                 return imageLabel;
63         }
64
65         public void loadImage(File inputFile, boolean isImgSqz) throws IOException{
66                 FileInputStream fileInputStream = new FileInputStream(inputFile);
67         
68                 loadImage(fileInputStream, isImgSqz);           
69         }
70
71         public void loadImage(InputStream inputStream, boolean isImgSqz) throws IOException{
72                 if (isImgSqz){
73                         // load ImageSqueeze file
74
75                         Image image = new Image();
76                         image.loadImage(inputStream);
77
78                         bufferedImage = image.bufferedImage;
79
80                         ImageIcon icon = new ImageIcon(bufferedImage);
81                         //ImageIcon icon = new ImageIcon("sample data/original.png");
82
83                         imageLabel.setIcon(icon);                       
84
85                 } else {
86                         // load JPEG, PNG, GIF file
87                         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
88                         readLoop:{
89                                 for (;;){                               
90                                         int b = inputStream.read();
91                                         if (b == -1) break readLoop;
92                                         outputStream.write(b);
93                                 }
94                         }
95                         
96                         ImageIcon icon = new ImageIcon(outputStream.toByteArray());
97
98                         bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
99                         bufferedImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);
100
101                         ImageIcon displayIcon = new ImageIcon(bufferedImage);
102                         imageLabel.setIcon(displayIcon);                        
103                 }
104         }
105
106
107         public void createEmptyImage(Dimension dimension){
108
109                 bufferedImage = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_3BYTE_BGR);
110
111                 ImageIcon icon = new ImageIcon(bufferedImage);
112
113                 imageLabel.setIcon(icon);                       
114         }
115
116
117         public void saveImage(File outputFile){
118                 Image image = new Image(bufferedImage);
119                 try {
120                         image.saveImage(outputFile);
121                 } catch (Exception e) {
122                         System.out.println("Error while saving image: " + e.toString());
123                 }
124         }
125
126 }