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