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