Moved bit input and output streams into Svjatoslav Commons library.
[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 java.awt.BorderLayout;
13 import java.awt.Dimension;
14 import java.awt.image.BufferedImage;
15 import java.io.ByteArrayOutputStream;
16 import java.io.File;
17 import java.io.FileInputStream;
18 import java.io.IOException;
19 import java.io.InputStream;
20
21 import javax.swing.ImageIcon;
22 import javax.swing.JFrame;
23 import javax.swing.JLabel;
24 import javax.swing.WindowConstants;
25
26 import eu.svjatoslav.imagesqueeze.codec.Image;
27
28 public class ImagePanel extends javax.swing.JPanel {
29         private JLabel imageLabel;
30
31         public BufferedImage bufferedImage;
32
33         public ImagePanel() {
34                 super();
35                 initGUI();
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         public 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                 final FileInputStream fileInputStream = new FileInputStream(inputFile);
69
70                 loadImage(fileInputStream, isImgSqz);
71         }
72
73         public void loadImage(final InputStream inputStream, final boolean isImgSqz)
74                         throws IOException {
75                 if (isImgSqz) {
76                         // load ImageSqueeze file
77
78                         final Image image = new Image();
79                         image.loadImage(inputStream);
80
81                         bufferedImage = image.bufferedImage;
82
83                         final ImageIcon icon = new ImageIcon(bufferedImage);
84                         // ImageIcon icon = new ImageIcon("sample data/original.png");
85
86                         imageLabel.setIcon(icon);
87
88                 } else {
89                         // load JPEG, PNG, GIF file
90                         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
91                         readLoop: {
92                                 for (;;) {
93                                         final int b = inputStream.read();
94                                         if (b == -1)
95                                                 break readLoop;
96                                         outputStream.write(b);
97                                 }
98                         }
99
100                         final ImageIcon icon = new ImageIcon(outputStream.toByteArray());
101
102                         bufferedImage = new BufferedImage(icon.getIconWidth(),
103                                         icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
104                         bufferedImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);
105
106                         final ImageIcon displayIcon = new ImageIcon(bufferedImage);
107                         imageLabel.setIcon(displayIcon);
108                 }
109         }
110
111         public void saveImage(final File outputFile) {
112                 final Image image = new Image(bufferedImage);
113                 try {
114                         image.saveImage(outputFile);
115                 } catch (final Exception e) {
116                         System.out.println("Error while saving image: " + e.toString());
117                 }
118         }
119
120         /**
121          * Auto-generated main method to display this JPanel inside a new JFrame.
122          */
123         public static void main(final String[] args) {
124                 final JFrame frame = new JFrame();
125                 frame.getContentPane().add(new ImagePanel());
126                 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
127                 frame.pack();
128                 frame.setVisible(true);
129         }
130
131 }