-import eu.svjatoslav.commons.data.BitOutputStream;
-
-public class ImageEncoder {
-
- public static int byteToInt(final byte input) {
- int result = input;
- if (result < 0)
- result = result + 256;
- return result;
- }
-
- public static int decodeValueFromGivenBits(final int encodedBits,
- final int range, final int bitCount) {
- final int negativeBit = encodedBits & 1;
-
- final int remainingBitCount = bitCount - 1;
-
- if (remainingBitCount == 0) {
- // no more bits remaining to encode actual value
-
- if (negativeBit == 0)
- return range;
- else
- return -range;
-
- } else {
- // still one or more bits left, encode value as precisely as
- // possible
-
- final int encodedValue = (encodedBits >>> 1) + 1;
-
- final int realvalueForThisBitcount = 1 << remainingBitCount;
-
- // int valueMultiplier = range / realvalueForThisBitcount;
- int decodedValue = (range * encodedValue)
- / realvalueForThisBitcount;
-
- if (decodedValue > range)
- decodedValue = range;
-
- if (negativeBit == 0)
- return decodedValue;
- else
- return -decodedValue;