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