X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=imagesqueeze.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fimagesqueeze%2FsampleApplication%2FImagePanel.java;h=0748d8fb938385f6f3fd3f089ef3e16ca7997515;hp=c2ccead787943ccae3d52191088cd887a7302ee9;hb=HEAD;hpb=4bcffe8896c08c9f60b2707da71bb39a64618d93 diff --git a/src/main/java/eu/svjatoslav/imagesqueeze/sampleApplication/ImagePanel.java b/src/main/java/eu/svjatoslav/imagesqueeze/sampleApplication/ImagePanel.java index c2ccead..0748d8f 100755 --- a/src/main/java/eu/svjatoslav/imagesqueeze/sampleApplication/ImagePanel.java +++ b/src/main/java/eu/svjatoslav/imagesqueeze/sampleApplication/ImagePanel.java @@ -1,131 +1,118 @@ /* - * 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. + * Image codec. Author: Svjatoslav Agejenko, svjatoslav@svjatoslav.eu + * This project is released under Creative Commons Zero (CC0) license. */ - package eu.svjatoslav.imagesqueeze.sampleApplication; -import java.awt.BorderLayout; -import java.awt.Dimension; -import java.awt.image.BufferedImage; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; - -import javax.swing.ImageIcon; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.WindowConstants; - import eu.svjatoslav.imagesqueeze.codec.Image; +import javax.swing.*; +import java.awt.*; +import java.awt.image.BufferedImage; +import java.io.*; + public class ImagePanel extends javax.swing.JPanel { - private JLabel imageLabel; - - public BufferedImage bufferedImage; - - 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 { - final BorderLayout thisLayout = new BorderLayout(); - setLayout(thisLayout); - setPreferredSize(new Dimension(660, 500)); - { - imageLabel = new JLabel(); - this.add(getImageLabel(), BorderLayout.CENTER); - } - } catch (final Exception e) { - e.printStackTrace(); - } - } - - public void loadImage(final File inputFile, final boolean isImgSqz) - throws IOException { - final FileInputStream fileInputStream = new FileInputStream(inputFile); - - loadImage(fileInputStream, isImgSqz); - } - - public void loadImage(final InputStream inputStream, final boolean isImgSqz) - throws IOException { - if (isImgSqz) { - // load ImageSqueeze file - - final Image image = new Image(); - image.loadImage(inputStream); - - bufferedImage = image.bufferedImage; - - final ImageIcon icon = new ImageIcon(bufferedImage); - // ImageIcon icon = new ImageIcon("sample data/original.png"); - - imageLabel.setIcon(icon); - - } else { - // load JPEG, PNG, GIF file - final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - readLoop: { - for (;;) { - final int b = inputStream.read(); - if (b == -1) - break readLoop; - outputStream.write(b); - } - } - - 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); - - final ImageIcon displayIcon = new ImageIcon(bufferedImage); - imageLabel.setIcon(displayIcon); - } - } - - public void saveImage(final File outputFile) { - final Image image = new Image(bufferedImage); - try { - image.saveImage(outputFile); - } 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); - } + private BufferedImage bufferedImage; + private JLabel imageLabel; + + public ImagePanel() { + super(); + initGUI(); + } + + /** + * 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); + } + + 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); + } + + private JLabel getImageLabel() { + return imageLabel; + } + + private void initGUI() { + try { + final BorderLayout thisLayout = new BorderLayout(); + setLayout(thisLayout); + setPreferredSize(new Dimension(660, 500)); + { + imageLabel = new JLabel(); + this.add(getImageLabel(), BorderLayout.CENTER); + } + } catch (final Exception e) { + e.printStackTrace(); + } + } + + public void loadImage(final File inputFile, final boolean isImgSqz) + throws IOException { + + try (final FileInputStream fileInputStream = new FileInputStream(inputFile)) { + loadImage(fileInputStream, isImgSqz); + } + } + + public void loadImage(final InputStream inputStream, final boolean isImgSqz) + throws IOException { + if (isImgSqz) { + // load ImageSqueeze file + + final Image image = new Image(); + image.loadImage(inputStream); + + bufferedImage = image.bufferedImage; + + final ImageIcon icon = new ImageIcon(bufferedImage); + // ImageIcon icon = new ImageIcon("sample data/original.png"); + + imageLabel.setIcon(icon); + + } else { + // load JPEG, PNG, GIF file + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + readLoop: + { + for (; ; ) { + final int b = inputStream.read(); + if (b == -1) + break readLoop; + outputStream.write(b); + } + } + + 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); + + final ImageIcon displayIcon = new ImageIcon(bufferedImage); + imageLabel.setIcon(displayIcon); + } + } + + public void saveImage(final File outputFile) { + final Image image = new Image(bufferedImage); + try { + image.saveImage(outputFile); + } catch (final Exception e) { + System.out.println("Error while saving image: " + e.toString()); + } + } }