/*
- * Imagesqueeze - Image codec optimized for photos.
- * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
+ * Imagesqueeze - Image codec. Copyright ©2012-2019, 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.
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later 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.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());
+ }
+ }
}