Moved bit input and output streams into Svjatoslav Commons library.
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / Image.java
index c644eaa..6ba0d1b 100755 (executable)
@@ -1,3 +1,12 @@
+/*
+ * 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 java.awt.image.BufferedImage;
@@ -10,6 +19,9 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import eu.svjatoslav.commons.data.BitInputStream;
+import eu.svjatoslav.commons.data.BitOutputStream;
+
 /**
  * Main class representing compressed image.
  */
@@ -21,100 +33,104 @@ public class Image {
        public BufferedImage bufferedImage;
 
        ImageEncoder encoder;
-       
-       public Image(){};
+
+       public Image() {
+       };
 
        /**
-        * Initialize imagesqueeze image based on {@link BufferedImage}. {@link BufferedImage} must be of type BufferedImage.TYPE_3BYTE_BGR .
+        * Initialize imagesqueeze image based on {@link BufferedImage}.
+        * {@link BufferedImage} must be of type BufferedImage.TYPE_3BYTE_BGR .
         */
-       public Image(BufferedImage image){
+       public Image(final BufferedImage image) {
 
-               this.bufferedImage = image;
+               bufferedImage = image;
                metaData = new ImageMetaData();
 
                metaData.version = 1;
                metaData.width = image.getWidth();
-               metaData.height = image.getHeight();            
+               metaData.height = image.getHeight();
        }
 
        /**
         * Initialize empty imagesqueeze image.
         */
-       public Image(int width, int height){
+       public Image(final int width, final int height) {
 
-               this.bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
+               bufferedImage = new BufferedImage(width, height,
+                               BufferedImage.TYPE_3BYTE_BGR);
                metaData = new ImageMetaData();
 
                metaData.version = 1;
                metaData.width = width;
-               metaData.height = height;               
-       }
-
-       /**
-        * Load ImgSqz image from {@link InputStream}.
-        */
-       public void loadImage(InputStream source) throws IOException{
-               BitInputStream bitInputStream = new BitInputStream(source);
-
-               metaData = new ImageMetaData();
-               metaData.load(bitInputStream);
-
-               bufferedImage = new BufferedImage(metaData.width, metaData.height, BufferedImage.TYPE_3BYTE_BGR);
-
-               ImageDecoder imageDecoder = new ImageDecoder(this, bitInputStream);
-
-               imageDecoder.decode();
+               metaData.height = height;
        }
 
        /**
         * Load ImgSqz image from {@link File}.
         */
-       public void loadImage(File source) throws IOException{
+       public void loadImage(final File source) throws IOException {
 
-               byte [] fileContent = new byte[(int)source.length()];
+               final byte[] fileContent = new byte[(int) source.length()];
 
-               FileInputStream fileInputStream = new FileInputStream(source);
+               final FileInputStream fileInputStream = new FileInputStream(source);
 
                fileInputStream.read(fileContent);
 
                fileInputStream.close();
 
-               ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContent);
+               final ByteArrayInputStream inputStream = new ByteArrayInputStream(
+                               fileContent);
 
                loadImage(inputStream);
        }
 
        /**
-        * Save image into ImgSqz file format.
+        * Load ImgSqz image from {@link InputStream}.
         */
-       public void saveImage(OutputStream outputStream) throws IOException{
+       public void loadImage(final InputStream source) throws IOException {
+               final BitInputStream bitInputStream = new BitInputStream(source);
 
-               BitOutputStream bitOutputStream = new BitOutputStream(outputStream);
+               metaData = new ImageMetaData();
+               metaData.load(bitInputStream);
 
-               metaData.save(bitOutputStream);
-               
-               if (encoder == null){
-                       encoder = new ImageEncoder(this);                       
-               }
+               bufferedImage = new BufferedImage(metaData.width, metaData.height,
+                               BufferedImage.TYPE_3BYTE_BGR);
 
-               encoder.encode(bitOutputStream);
+               final ImageDecoder imageDecoder = new ImageDecoder(this, bitInputStream);
 
-               bitOutputStream.finishByte();
+               imageDecoder.decode();
        }
 
-
        /**
         * Save image into ImgSqz file format.
         */
-       public void saveImage(File file) throws IOException{
-               ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+       public void saveImage(final File file) throws IOException {
+               final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                saveImage(outputStream);
 
-               byte [] buffer = outputStream.toByteArray();
-               FileOutputStream fileOutputStream = new FileOutputStream(file);
+               final byte[] buffer = outputStream.toByteArray();
+               final FileOutputStream fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(buffer);
                fileOutputStream.close();
        }
 
+       /**
+        * Save image into ImgSqz file format.
+        */
+       public void saveImage(final OutputStream outputStream) throws IOException {
+
+               final BitOutputStream bitOutputStream = new BitOutputStream(
+                               outputStream);
+
+               metaData.save(bitOutputStream);
+
+               if (encoder == null) {
+                       encoder = new ImageEncoder(this);
+               }
+
+               encoder.encode(bitOutputStream);
+
+               bitOutputStream.finishByte();
+       }
 
 }