moved compressed integer support out of svjatoslavcommons
[imagesqueeze.git] / src / main / java / eu / svjatoslav / imagesqueeze / codec / ImageDecoder.java
index 78bf715..53f2d91 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Imagesqueeze - Image codec optimized for photos.
  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 /*
  * 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.
  * 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.
@@ -21,24 +21,34 @@ import eu.svjatoslav.commons.data.BitInputStream;
 
 public class ImageDecoder {
 
 
 public class ImageDecoder {
 
+       public static int readIntegerCompressed8(final BitInputStream inputStream)
+                       throws IOException {
+
+               if (inputStream.readBits(1) == 0)
+                       return inputStream.readBits(8);
+               else
+                       return inputStream.readBits(32);
+       }
+
        int width, height;
        int width, height;
-       Image image;
 
 
+       Image image;
        byte[] decodedYRangeMap;
        byte[] decodedYRangeMap;
-       byte[] decodedYMap;
 
 
+       byte[] decodedYMap;
        byte[] decodedURangeMap;
        byte[] decodedURangeMap;
-       byte[] decodedUMap;
 
 
+       byte[] decodedUMap;
        byte[] decodedVRangeMap;
        byte[] decodedVRangeMap;
+
        byte[] decodedVMap;
 
        Color tmpColor = new Color();
        byte[] decodedVMap;
 
        Color tmpColor = new Color();
-
        Approximator approximator;
        Approximator approximator;
-       BitInputStream bitInputStream;
 
 
+       BitInputStream bitInputStream;
        ColorStats colorStats = new ColorStats();
        ColorStats colorStats = new ColorStats();
+
        OperatingContext context = new OperatingContext();
 
        public ImageDecoder(final Image image, final BitInputStream bitInputStream) {
        OperatingContext context = new OperatingContext();
 
        public ImageDecoder(final Image image, final BitInputStream bitInputStream) {
@@ -91,15 +101,13 @@ public class ImageDecoder {
                // detect initial step
                int largestDimension;
                int initialStep = 2;
                // detect initial step
                int largestDimension;
                int initialStep = 2;
-               if (width > height) {
+               if (width > height)
                        largestDimension = width;
                        largestDimension = width;
-               } else {
+               else
                        largestDimension = height;
                        largestDimension = height;
-               }
 
 
-               while (initialStep < largestDimension) {
+               while (initialStep < largestDimension)
                        initialStep = initialStep * 2;
                        initialStep = initialStep * 2;
-               }
 
                grid(initialStep, pixels);
        }
 
                grid(initialStep, pixels);
        }
@@ -110,15 +118,14 @@ public class ImageDecoder {
                gridSquare(step / 2, 0, step, pixels);
                gridSquare(0, step / 2, step, pixels);
 
                gridSquare(step / 2, 0, step, pixels);
                gridSquare(0, step / 2, step, pixels);
 
-               if (step > 2) {
+               if (step > 2)
                        grid(step / 2, pixels);
                        grid(step / 2, pixels);
-               }
        }
 
        public void gridDiagonal(final int offsetX, final int offsetY,
                        final int step, final byte[] pixels) throws IOException {
 
        }
 
        public void gridDiagonal(final int offsetX, final int offsetY,
                        final int step, final byte[] pixels) throws IOException {
 
-               for (int y = offsetY; y < height; y = y + step) {
+               for (int y = offsetY; y < height; y = y + step)
                        for (int x = offsetX; x < width; x = x + step) {
 
                                final int halfStep = step / 2;
                        for (int x = offsetX; x < width; x = x + step) {
 
                                final int halfStep = step / 2;
@@ -135,13 +142,12 @@ public class ImageDecoder {
                                                context.colorStats.getAverageV());
 
                        }
                                                context.colorStats.getAverageV());
 
                        }
-               }
        }
 
        public void gridSquare(final int offsetX, final int offsetY,
                        final int step, final byte[] pixels) throws IOException {
 
        }
 
        public void gridSquare(final int offsetX, final int offsetY,
                        final int step, final byte[] pixels) throws IOException {
 
-               for (int y = offsetY; y < height; y = y + step) {
+               for (int y = offsetY; y < height; y = y + step)
                        for (int x = offsetX; x < width; x = x + step) {
 
                                final int halfStep = step / 2;
                        for (int x = offsetX; x < width; x = x + step) {
 
                                final int halfStep = step / 2;
@@ -158,7 +164,6 @@ public class ImageDecoder {
                                                context.colorStats.getAverageV());
 
                        }
                                                context.colorStats.getAverageV());
 
                        }
-               }
        }
 
        private int loadChannel(final byte[] decodedRangeMap,
        }
 
        private int loadChannel(final byte[] decodedRangeMap,
@@ -176,9 +181,8 @@ public class ImageDecoder {
                if (bitCount > 0) {
 
                        final int rangeDecreases = bitInputStream.readBits(1);
                if (bitCount > 0) {
 
                        final int rangeDecreases = bitInputStream.readBits(1);
-                       if (rangeDecreases != 0) {
+                       if (rangeDecreases != 0)
                                computedRange = table.proposeDecreasedRange(inheritedRange);
                                computedRange = table.proposeDecreasedRange(inheritedRange);
-                       }
 
                        decodedRangeMap[index] = (byte) computedRange;
                        computedRangeBitCount = table
 
                        decodedRangeMap[index] = (byte) computedRange;
                        computedRangeBitCount = table
@@ -194,16 +198,13 @@ public class ImageDecoder {
                                                                computedRange, computedRangeBitCount);
 
                                decodedValue = averageDecodedValue - decodedDifference;
                                                                computedRange, computedRangeBitCount);
 
                                decodedValue = averageDecodedValue - decodedDifference;
-                               if (decodedValue > 255) {
+                               if (decodedValue > 255)
                                        decodedValue = 255;
                                        decodedValue = 255;
-                               }
-                               if (decodedValue < 0) {
+                               if (decodedValue < 0)
                                        decodedValue = 0;
                                        decodedValue = 0;
-                               }
                        }
                        }
-               } else {
+               } else
                        decodedRangeMap[index] = (byte) inheritedRange;
                        decodedRangeMap[index] = (byte) inheritedRange;
-               }
                decodedMap[index] = (byte) decodedValue;
                return decodedValue;
        }
                decodedMap[index] = (byte) decodedValue;
                return decodedValue;
        }
@@ -219,17 +220,15 @@ public class ImageDecoder {
 
                int parentIndex;
                if (offsetX > 0) {
 
                int parentIndex;
                if (offsetX > 0) {
-                       if (offsetY > 0) {
+                       if (offsetY > 0)
                                // diagonal approach
                                parentIndex = ((y - halfStep) * width) + (x - halfStep);
                                // diagonal approach
                                parentIndex = ((y - halfStep) * width) + (x - halfStep);
-                       } else {
+                       else
                                // take left pixel
                                parentIndex = (y * width) + (x - halfStep);
                                // take left pixel
                                parentIndex = (y * width) + (x - halfStep);
-                       }
-               } else {
+               } else
                        // take upper pixel
                        parentIndex = ((y - halfStep) * width) + x;
                        // take upper pixel
                        parentIndex = ((y - halfStep) * width) + x;
-               }
 
                final int colorBufferIndex = index * 3;
 
 
                final int colorBufferIndex = index * 3;