Moved to java 1.8. Code cleanup and formatting.
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / Image.java
index c644eaa..256ac26 100755 (executable)
@@ -1,14 +1,19 @@
+/*
+ * 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.codec;
 
+import eu.svjatoslav.commons.data.BitInputStream;
+import eu.svjatoslav.commons.data.BitOutputStream;
+
 import java.awt.image.BufferedImage;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
+import java.io.*;
 
 /**
  * Main class representing compressed image.
@@ -16,105 +21,108 @@ import java.io.OutputStream;
 
 public class Image {
 
-       public ImageMetaData metaData;
-
-       public BufferedImage bufferedImage;
-
-       ImageEncoder encoder;
-       
-       public Image(){};
+    public ImageMetaData metaData;
 
-       /**
-        * Initialize imagesqueeze image based on {@link BufferedImage}. {@link BufferedImage} must be of type BufferedImage.TYPE_3BYTE_BGR .
-        */
-       public Image(BufferedImage image){
+    public BufferedImage bufferedImage;
 
-               this.bufferedImage = image;
-               metaData = new ImageMetaData();
+    private ImageEncoder encoder;
 
-               metaData.version = 1;
-               metaData.width = image.getWidth();
-               metaData.height = image.getHeight();            
-       }
+    public Image() {
+    }
 
-       /**
-        * Initialize empty imagesqueeze image.
-        */
-       public Image(int width, int height){
+    /**
+     * Initialize imagesqueeze image based on {@link BufferedImage}.
+     * {@link BufferedImage} must be of type BufferedImage.TYPE_3BYTE_BGR .
+     */
+    public Image(final BufferedImage image) {
 
-               this.bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
-               metaData = new ImageMetaData();
+        bufferedImage = image;
+        metaData = new ImageMetaData();
 
-               metaData.version = 1;
-               metaData.width = width;
-               metaData.height = height;               
-       }
+        metaData.version = 1;
+        metaData.width = image.getWidth();
+        metaData.height = image.getHeight();
+    }
 
-       /**
-        * Load ImgSqz image from {@link InputStream}.
-        */
-       public void loadImage(InputStream source) throws IOException{
-               BitInputStream bitInputStream = new BitInputStream(source);
+    /**
+     * Initialize empty imagesqueeze image.
+     */
+    public Image(final int width, final int height) {
 
-               metaData = new ImageMetaData();
-               metaData.load(bitInputStream);
+        bufferedImage = new BufferedImage(width, height,
+                BufferedImage.TYPE_3BYTE_BGR);
+        metaData = new ImageMetaData();
 
-               bufferedImage = new BufferedImage(metaData.width, metaData.height, BufferedImage.TYPE_3BYTE_BGR);
+        metaData.version = 1;
+        metaData.width = width;
+        metaData.height = height;
+    }
 
-               ImageDecoder imageDecoder = new ImageDecoder(this, bitInputStream);
+    /**
+     * Load ImgSqz image from {@link File}.
+     */
+    public void loadImage(final File source) throws IOException {
 
-               imageDecoder.decode();
-       }
+        final byte[] fileContent = new byte[(int) source.length()];
 
-       /**
-        * Load ImgSqz image from {@link File}.
-        */
-       public void loadImage(File source) throws IOException{
+        try (FileInputStream fileInputStream = new FileInputStream(source)) {
+            if (fileInputStream.read(fileContent) != fileContent.length)
+                throw new RuntimeException("Failed to read file content");
+        }
 
-               byte [] fileContent = new byte[(int)source.length()];
+        final ByteArrayInputStream inputStream = new ByteArrayInputStream(
+                fileContent);
 
-               FileInputStream fileInputStream = new FileInputStream(source);
+        loadImage(inputStream);
+    }
 
-               fileInputStream.read(fileContent);
+    /**
+     * Load ImgSqz image from {@link InputStream}.
+     */
+    public void loadImage(final InputStream source) throws IOException {
+        final BitInputStream bitInputStream = new BitInputStream(source);
 
-               fileInputStream.close();
+        metaData = new ImageMetaData();
+        metaData.load(bitInputStream);
 
-               ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContent);
+        bufferedImage = new BufferedImage(metaData.width, metaData.height,
+                BufferedImage.TYPE_3BYTE_BGR);
 
-               loadImage(inputStream);
-       }
+        final ImageDecoder imageDecoder = new ImageDecoder(this, bitInputStream);
 
-       /**
-        * Save image into ImgSqz file format.
-        */
-       public void saveImage(OutputStream outputStream) throws IOException{
+        imageDecoder.decode();
+    }
 
-               BitOutputStream bitOutputStream = new BitOutputStream(outputStream);
+    /**
+     * Save image into ImgSqz file format.
+     */
+    public void saveImage(final File file) throws IOException {
+        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        saveImage(outputStream);
 
-               metaData.save(bitOutputStream);
-               
-               if (encoder == null){
-                       encoder = new ImageEncoder(this);                       
-               }
+        final byte[] buffer = outputStream.toByteArray();
+        try (FileOutputStream fileOutputStream = new FileOutputStream(file)) {
+            fileOutputStream.write(buffer);
+        }
+    }
 
-               encoder.encode(bitOutputStream);
+    /**
+     * Save image into ImgSqz file format.
+     */
+    private void saveImage(final OutputStream outputStream) throws IOException {
 
-               bitOutputStream.finishByte();
-       }
+        final BitOutputStream bitOutputStream = new BitOutputStream(
+                outputStream);
 
+        metaData.save(bitOutputStream);
 
-       /**
-        * Save image into ImgSqz file format.
-        */
-       public void saveImage(File file) throws IOException{
-               ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
-               saveImage(outputStream);
+        if (encoder == null) {
+            encoder = new ImageEncoder(this);
+        }
 
-               byte [] buffer = outputStream.toByteArray();
-               FileOutputStream fileOutputStream = new FileOutputStream(file);
-               fileOutputStream.write(buffer);
-               fileOutputStream.close();
-       }
+        encoder.encode(bitOutputStream);
 
+        bitOutputStream.finishByte();
+    }
 
 }