X-Git-Url: http://www2.svjatoslav.eu/gitweb/?p=imagesqueeze.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fimagesqueeze%2Fcodec%2FTable.java;fp=src%2Fmain%2Fjava%2Feu%2Fsvjatoslav%2Fimagesqueeze%2Fcodec%2FTable.java;h=33637d7de36a4bd92afb313f143c8ad128d8eaa8;hp=4e714f4c8743b521caaf617058b3909d2c91e2de;hb=4bcffe8896c08c9f60b2707da71bb39a64618d93;hpb=c7d0b8e1723045c0df086d9214a35f54db47684c diff --git a/src/main/java/eu/svjatoslav/imagesqueeze/codec/Table.java b/src/main/java/eu/svjatoslav/imagesqueeze/codec/Table.java index 4e714f4..33637d7 100755 --- a/src/main/java/eu/svjatoslav/imagesqueeze/codec/Table.java +++ b/src/main/java/eu/svjatoslav/imagesqueeze/codec/Table.java @@ -1,168 +1,194 @@ +/* + * 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.io.IOException; +import eu.svjatoslav.commons.data.BitInputStream; +import eu.svjatoslav.commons.data.BitOutputStream; + /** * Quick lookup table. */ -public class Table implements Comparable{ +public class Table implements Comparable
{ - int [] range = new int[100]; - int [] switchTreshold = new int[100]; - int [] bitcount = new int[100]; + int[] range = new int[100]; + int[] switchTreshold = new int[100]; + int[] bitcount = new int[100]; + int[] bitCountForRange = new int[256]; + int[] proposedRangeForActualRange = new int[256]; + int[] proposedRangeForActualRangeLow = new int[256]; + int[] proposedRangeForActualRangeHigh = new int[256]; + byte[] proposedDecreasedRange = new byte[256]; - int [] bitCountForRange = new int[256]; - int [] proposedRangeForActualRange = new int[256]; - int [] proposedRangeForActualRangeLow = new int[256]; - int [] proposedRangeForActualRangeHigh = new int[256]; - byte [] proposedDecreasedRange = new byte[256]; + int usedEntries = 0; + /** + * @param switchTreshold + * - switch to this range when actual range in equal or below + * this treshold + */ + public void addEntry(int range, int switchTreshold, int bitcount) { + if (range < 0) + range = 0; + if (range > 255) + range = 255; + + if (switchTreshold < 0) + switchTreshold = 0; + if (switchTreshold > 255) + switchTreshold = 255; + + if (bitcount < 0) + bitcount = 0; + if (bitcount > 8) + bitcount = 8; - int usedEntries = 0; + this.range[usedEntries] = range; + this.switchTreshold[usedEntries] = switchTreshold; + this.bitcount[usedEntries] = bitcount; + usedEntries++; + } + /** + * Compares two tables. Ignores table initialization. + */ + @Override + public int compareTo(final Table o) { + if (usedEntries < o.usedEntries) + return -1; + if (usedEntries > o.usedEntries) + return 1; + + for (int i = 0; i < usedEntries; i++) { + if (range[i] < o.range[i]) + return -1; + if (range[i] > o.range[i]) + return 1; + + if (switchTreshold[i] < o.switchTreshold[i]) + return -1; + if (switchTreshold[i] > o.switchTreshold[i]) + return 1; + + if (bitcount[i] < o.bitcount[i]) + return -1; + if (bitcount[i] > o.bitcount[i]) + return 1; + } - public void computeLookupTables(){ + return 0; + } + + public void computeLookupTables() { int currentCheckPointer = 0; - for (int i=0; i<256; i++){ + for (int i = 0; i < 256; i++) { - if (range[currentCheckPointer] == i){ + if (range[currentCheckPointer] == i) { currentCheckPointer++; } - if (currentCheckPointer > 0){ - bitCountForRange[i] = bitcount[currentCheckPointer-1]; + if (currentCheckPointer > 0) { + bitCountForRange[i] = bitcount[currentCheckPointer - 1]; } else { bitCountForRange[i] = 0; } } - for (int i=0; i<256; i++){ + for (int i = 0; i < 256; i++) { int seek; - seekLoop:{ - for (seek = 0; seek < usedEntries; seek ++){ + seekLoop: { + for (seek = 0; seek < usedEntries; seek++) { - if (switchTreshold[seek] >= i) break seekLoop; + if (switchTreshold[seek] >= i) + break seekLoop; } } - proposedRangeForActualRange[i] = range[seek]; - if (seek == 0){ - proposedRangeForActualRangeLow[i] = 0; + proposedRangeForActualRange[i] = range[seek]; + if (seek == 0) { + proposedRangeForActualRangeLow[i] = 0; } else { - proposedRangeForActualRangeLow[i] = switchTreshold[seek-1]+1; + proposedRangeForActualRangeLow[i] = switchTreshold[seek - 1] + 1; } - proposedRangeForActualRangeHigh[i] = switchTreshold[seek]; + proposedRangeForActualRangeHigh[i] = switchTreshold[seek]; } - currentCheckPointer = usedEntries - 2; - for (int i=255; i >= 0; i--){ - if (range[currentCheckPointer] == i) currentCheckPointer--; + for (int i = 255; i >= 0; i--) { + if (range[currentCheckPointer] == i) + currentCheckPointer--; - if (currentCheckPointer < 0){ + if (currentCheckPointer < 0) { proposedDecreasedRange[i] = 0; } else { - proposedDecreasedRange[i] = (byte)(range[currentCheckPointer]); + proposedDecreasedRange[i] = (byte) (range[currentCheckPointer]); } } } - /** - * @param switchTreshold - switch to this range when actual range in equal or below this treshold - */ - public void addEntry(int range, int switchTreshold, int bitcount){ - if (range < 0) range = 0; - if (range > 255) range = 255; - - if (switchTreshold < 0) switchTreshold = 0; - if (switchTreshold > 255) switchTreshold = 255; - - if (bitcount < 0) bitcount = 0; - if (bitcount > 8) bitcount = 8; - - - this.range[usedEntries] = range; - this.switchTreshold[usedEntries] = switchTreshold; - this.bitcount[usedEntries] = bitcount; - usedEntries++; - } - - - - - public int proposeRangeForRange(int actualRange, int inheritedRange){ + public void load(final BitInputStream inputStream) throws IOException { + reset(); - if (inheritedRange > 255) inheritedRange = 255; - if (inheritedRange < 0) inheritedRange = 0; + final int availableEntries = inputStream.readIntegerCompressed8(); - if (proposedRangeForActualRangeLow[inheritedRange] <= actualRange){ - return inheritedRange; + for (int i = 0; i < availableEntries; i++) { + addEntry(inputStream.readBits(8), inputStream.readBits(8), + inputStream.readBits(4)); } - - return proposeDecreasedRange(inheritedRange); } - - public int proposeDecreasedRange(int range){ - if (range > 255) range = 255; - if (range < 0) range = 0; - - return ImageEncoder.byteToInt(proposedDecreasedRange[range]); + public int proposeBitcountForRange(int range) { + if (range > 255) + range = 255; + if (range < 0) + range = 0; + final int proposal = bitCountForRange[range]; + return proposal; } + public int proposeDecreasedRange(int range) { + if (range > 255) + range = 255; + if (range < 0) + range = 0; - public int proposeBitcountForRange(int range){ - if (range > 255) range = 255; - if (range < 0) range = 0; - int proposal = bitCountForRange[range]; - return proposal; + return ImageEncoder.byteToInt(proposedDecreasedRange[range]); } - /** - * Compares two tables. - * Ignores table initialization. - */ - public int compareTo(Table o) { - if (usedEntries < o.usedEntries) return -1; - if (usedEntries > o.usedEntries) return 1; - - for (int i=0; i o.range[i]) return 1; + public int proposeRangeForRange(final int actualRange, int inheritedRange) { - if (switchTreshold[i] < o.switchTreshold[i]) return -1; - if (switchTreshold[i] > o.switchTreshold[i]) return 1; + if (inheritedRange > 255) + inheritedRange = 255; + if (inheritedRange < 0) + inheritedRange = 0; - if (bitcount[i] < o.bitcount[i]) return -1; - if (bitcount[i] > o.bitcount[i]) return 1; + if (proposedRangeForActualRangeLow[inheritedRange] <= actualRange) { + return inheritedRange; } - return 0; + return proposeDecreasedRange(inheritedRange); } - public void save(BitOutputStream outputStream) throws IOException { - outputStream.storeIntegerCompressed8(usedEntries); - - for (int i=0; i < usedEntries; i++){ - outputStream.storeBits(this.range[i], 8); - outputStream.storeBits(this.switchTreshold[i], 8); - outputStream.storeBits(this.bitcount[i], 4); - } - } - - public void reset(){ - range = new int[100]; + public void reset() { + range = new int[100]; switchTreshold = new int[100]; bitcount = new int[100]; - bitCountForRange = new int[256]; proposedRangeForActualRange = new int[256]; proposedRangeForActualRangeLow = new int[256]; @@ -171,17 +197,15 @@ public class Table implements Comparable
{ usedEntries = 0; } - - public void load(BitInputStream inputStream) throws IOException { - reset(); - - int availableEntries = inputStream.readIntegerCompressed8(); - - for (int i=0; i < availableEntries; i++){ - addEntry(inputStream.readBits(8), inputStream.readBits(8), inputStream.readBits(4)); - } - } + public void save(final BitOutputStream outputStream) throws IOException { + outputStream.storeIntegerCompressed8(usedEntries); + for (int i = 0; i < usedEntries; i++) { + outputStream.storeBits(range[i], 8); + outputStream.storeBits(switchTreshold[i], 8); + outputStream.storeBits(bitcount[i], 4); + } + } }