Moved bit input and output streams into Svjatoslav Commons library.
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / sampleApplication / ImagePanel.java
index 749d558..c2ccead 100755 (executable)
+/*
+ * Imagesqueeze - Image codec optimized for photos.
+ * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ * 
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
 package eu.svjatoslav.imagesqueeze.sampleApplication;
 
 import java.awt.BorderLayout;
-
 import java.awt.Dimension;
 import java.awt.image.BufferedImage;
-import java.io.BufferedReader;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 
-import javax.swing.JButton;
-
 import javax.swing.ImageIcon;
-import javax.swing.JPanel;
-import javax.swing.WindowConstants;
 import javax.swing.JFrame;
 import javax.swing.JLabel;
+import javax.swing.WindowConstants;
 
 import eu.svjatoslav.imagesqueeze.codec.Image;
 
-
 public class ImagePanel extends javax.swing.JPanel {
        private JLabel imageLabel;
 
        public BufferedImage bufferedImage;
 
-       /**
-        * Auto-generated main method to display this 
-        * JPanel inside a new JFrame.
-        */
-       public static void main(String[] args) {
-               JFrame frame = new JFrame();
-               frame.getContentPane().add(new ImagePanel());
-               frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
-               frame.pack();
-               frame.setVisible(true);
-       }
-
        public ImagePanel() {
                super();
                initGUI();
        }
 
+       public void createEmptyImage(final Dimension dimension) {
+
+               bufferedImage = new BufferedImage(dimension.width, dimension.height,
+                               BufferedImage.TYPE_3BYTE_BGR);
+
+               final ImageIcon icon = new ImageIcon(bufferedImage);
+
+               imageLabel.setIcon(icon);
+       }
+
+       public JLabel getImageLabel() {
+               return imageLabel;
+       }
+
        private void initGUI() {
                try {
-                       BorderLayout thisLayout = new BorderLayout();
-                       this.setLayout(thisLayout);
+                       final BorderLayout thisLayout = new BorderLayout();
+                       setLayout(thisLayout);
                        setPreferredSize(new Dimension(660, 500));
                        {
                                imageLabel = new JLabel();
                                this.add(getImageLabel(), BorderLayout.CENTER);
                        }
-               } catch (Exception e) {
+               } catch (final Exception e) {
                        e.printStackTrace();
                }
        }
 
-       public JLabel getImageLabel() {
-               return imageLabel;
-       }
+       public void loadImage(final File inputFile, final boolean isImgSqz)
+                       throws IOException {
+               final FileInputStream fileInputStream = new FileInputStream(inputFile);
 
-       public void loadImage(File inputFile, boolean isImgSqz) throws IOException{
-               FileInputStream fileInputStream = new FileInputStream(inputFile);
-       
-               loadImage(fileInputStream, isImgSqz);           
+               loadImage(fileInputStream, isImgSqz);
        }
 
-       public void loadImage(InputStream inputStream, boolean isImgSqz) throws IOException{
-               if (isImgSqz){
+       public void loadImage(final InputStream inputStream, final boolean isImgSqz)
+                       throws IOException {
+               if (isImgSqz) {
                        // load ImageSqueeze file
 
-                       Image image = new Image();
+                       final Image image = new Image();
                        image.loadImage(inputStream);
 
                        bufferedImage = image.bufferedImage;
 
-                       ImageIcon icon = new ImageIcon(bufferedImage);
-                       //ImageIcon icon = new ImageIcon("sample data/original.png");
+                       final ImageIcon icon = new ImageIcon(bufferedImage);
+                       // ImageIcon icon = new ImageIcon("sample data/original.png");
 
-                       imageLabel.setIcon(icon);                       
+                       imageLabel.setIcon(icon);
 
                } else {
                        // load JPEG, PNG, GIF file
-                       ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-                       readLoop:{
-                               for (;;){                               
-                                       int b = inputStream.read();
-                                       if (b == -1) break readLoop;
+                       final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+                       readLoop: {
+                               for (;;) {
+                                       final int b = inputStream.read();
+                                       if (b == -1)
+                                               break readLoop;
                                        outputStream.write(b);
                                }
                        }
-                       
-                       ImageIcon icon = new ImageIcon(outputStream.toByteArray());
 
-                       bufferedImage = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
+                       final ImageIcon icon = new ImageIcon(outputStream.toByteArray());
+
+                       bufferedImage = new BufferedImage(icon.getIconWidth(),
+                                       icon.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
                        bufferedImage.getGraphics().drawImage(icon.getImage(), 0, 0, null);
 
-                       ImageIcon displayIcon = new ImageIcon(bufferedImage);
-                       imageLabel.setIcon(displayIcon);                        
+                       final ImageIcon displayIcon = new ImageIcon(bufferedImage);
+                       imageLabel.setIcon(displayIcon);
                }
        }
 
-
-       public void createEmptyImage(Dimension dimension){
-
-               bufferedImage = new BufferedImage(dimension.width, dimension.height, BufferedImage.TYPE_3BYTE_BGR);
-
-               ImageIcon icon = new ImageIcon(bufferedImage);
-
-               imageLabel.setIcon(icon);                       
-       }
-
-
-       public void saveImage(File outputFile){
-               Image image = new Image(bufferedImage);
+       public void saveImage(final File outputFile) {
+               final Image image = new Image(bufferedImage);
                try {
                        image.saveImage(outputFile);
-               } catch (Exception e) {
+               } catch (final Exception e) {
                        System.out.println("Error while saving image: " + e.toString());
                }
        }
 
+       /**
+        * Auto-generated main method to display this JPanel inside a new JFrame.
+        */
+       public static void main(final String[] args) {
+               final JFrame frame = new JFrame();
+               frame.getContentPane().add(new ImagePanel());
+               frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
+               frame.pack();
+               frame.setVisible(true);
+       }
+
 }