X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=imagesqueeze.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fimagesqueeze%2Fcodec%2FImageDecoder.java;h=53f2d9125be148544ad2dcf95fe5873065dc324c;hp=78bf71515c3447dd2b77165cef6117802dc259fc;hb=a4acb4f31760b0c654f9a041eef9797782d6043a;hpb=84d3759e6f959c1f9f8b29bec5303d57f1c5c098;ds=sidebyside diff --git a/src/main/java/eu/svjatoslav/imagesqueeze/codec/ImageDecoder.java b/src/main/java/eu/svjatoslav/imagesqueeze/codec/ImageDecoder.java index 78bf715..53f2d91 100755 --- a/src/main/java/eu/svjatoslav/imagesqueeze/codec/ImageDecoder.java +++ b/src/main/java/eu/svjatoslav/imagesqueeze/codec/ImageDecoder.java @@ -1,7 +1,7 @@ /* * 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. @@ -21,24 +21,34 @@ import eu.svjatoslav.commons.data.BitInputStream; 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; - Image image; + Image image; byte[] decodedYRangeMap; - byte[] decodedYMap; + byte[] decodedYMap; byte[] decodedURangeMap; - byte[] decodedUMap; + byte[] decodedUMap; byte[] decodedVRangeMap; + byte[] decodedVMap; Color tmpColor = new Color(); - Approximator approximator; - BitInputStream bitInputStream; + BitInputStream bitInputStream; ColorStats colorStats = new ColorStats(); + 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; - if (width > height) { + if (width > height) largestDimension = width; - } else { + else largestDimension = height; - } - while (initialStep < largestDimension) { + while (initialStep < largestDimension) initialStep = initialStep * 2; - } grid(initialStep, pixels); } @@ -110,15 +118,14 @@ public class ImageDecoder { gridSquare(step / 2, 0, step, pixels); gridSquare(0, step / 2, step, pixels); - if (step > 2) { + if (step > 2) grid(step / 2, pixels); - } } 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; @@ -135,13 +142,12 @@ public class ImageDecoder { context.colorStats.getAverageV()); } - } } 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; @@ -158,7 +164,6 @@ public class ImageDecoder { context.colorStats.getAverageV()); } - } } private int loadChannel(final byte[] decodedRangeMap, @@ -176,9 +181,8 @@ public class ImageDecoder { if (bitCount > 0) { final int rangeDecreases = bitInputStream.readBits(1); - if (rangeDecreases != 0) { + if (rangeDecreases != 0) computedRange = table.proposeDecreasedRange(inheritedRange); - } decodedRangeMap[index] = (byte) computedRange; computedRangeBitCount = table @@ -194,16 +198,13 @@ public class ImageDecoder { computedRange, computedRangeBitCount); decodedValue = averageDecodedValue - decodedDifference; - if (decodedValue > 255) { + if (decodedValue > 255) decodedValue = 255; - } - if (decodedValue < 0) { + if (decodedValue < 0) decodedValue = 0; - } } - } else { + } else decodedRangeMap[index] = (byte) inheritedRange; - } decodedMap[index] = (byte) decodedValue; return decodedValue; } @@ -219,17 +220,15 @@ public class ImageDecoder { int parentIndex; if (offsetX > 0) { - if (offsetY > 0) { + if (offsetY > 0) // diagonal approach parentIndex = ((y - halfStep) * width) + (x - halfStep); - } else { + else // take left pixel parentIndex = (y * width) + (x - halfStep); - } - } else { + } else // take upper pixel parentIndex = ((y - halfStep) * width) + x; - } final int colorBufferIndex = index * 3;