- // detect initial step
- int largestDimension;
- int initialStep = 2;
- if (width > height) {
- largestDimension = width;
- } else {
- largestDimension = height;
- }
-
- while (initialStep < largestDimension) {
- initialStep = initialStep * 2;
- }
-
- rangeGrid(initialStep);
- rangeRoundGrid(2);
- saveGrid(initialStep);
- }
-
- private void encodeChannel(final Table table, final Channel channel,
- final int averageDecodedValue, final int index, final int value,
- final int range, final int parentIndex) throws IOException {
-
- final byte[] decodedRangeMap = channel.decodedRangeMap;
- final byte[] decodedMap = channel.decodedMap;
-
- final int inheritedRange = byteToInt(decodedRangeMap[parentIndex]);
-
- final int inheritedBitCount = table
- .proposeBitcountForRange(inheritedRange);
-
- if (inheritedBitCount > 0) {
- int computedRange;
- computedRange = table.proposeRangeForRange(range, inheritedRange);
- decodedRangeMap[index] = (byte) computedRange;
-
- channel.bitCount++;
- if (computedRange != inheritedRange) {
- // brightness range shrinked
- bitOutputStream.storeBits(1, 1);
- } else {
- // brightness range stayed the same
- bitOutputStream.storeBits(0, 1);
- }
-
- // encode brightness into available amount of bits
- final int computedBitCount = table
- .proposeBitcountForRange(computedRange);
-
- if (computedBitCount > 0) {
-
- final int differenceToEncode = -(value - averageDecodedValue);
- final int bitEncodedDifference = encodeValueIntoGivenBits(
- differenceToEncode, computedRange, computedBitCount);
-
- channel.bitCount = channel.bitCount + computedBitCount;
- bitOutputStream.storeBits(bitEncodedDifference,
- computedBitCount);
-
- final int decodedDifference = decodeValueFromGivenBits(
- bitEncodedDifference, computedRange, computedBitCount);
- int decodedValue = averageDecodedValue - decodedDifference;
- if (decodedValue > 255) {
- decodedValue = 255;
- }
- if (decodedValue < 0) {
- decodedValue = 0;
- }
-
- decodedMap[index] = (byte) decodedValue;
- } else {
- decodedMap[index] = (byte) averageDecodedValue;
- }
-
- } else {
- decodedRangeMap[index] = (byte) inheritedRange;
- decodedMap[index] = (byte) averageDecodedValue;
- }
- }
-
- public void printStatistics() {
- System.out.println("Y channel:");
- yChannel.printStatistics();
-
- System.out.println("U channel:");
- uChannel.printStatistics();
-
- System.out.println("V channel:");
- vChannel.printStatistics();
- }
-
- public void rangeGrid(final int step) {
-
- // gridSquare(step / 2, step / 2, step, pixels);
-
- rangeGridDiagonal(step / 2, step / 2, step);
- rangeGridSquare(step / 2, 0, step);
- rangeGridSquare(0, step / 2, step);
-
- if (step > 2) {
- rangeGrid(step / 2);
- }
- }
-
- public void rangeGridDiagonal(final int offsetX, final int offsetY,
- final int step) {
- for (int y = offsetY; y < height; y = y + step) {
- for (int x = offsetX; x < width; x = x + step) {
-
- final int index = (y * width) + x;
- final int halfStep = step / 2;
-
- context.initialize(image, yChannel.map, uChannel.map,
- vChannel.map);
-
- context.measureNeighborEncode(x - halfStep, y - halfStep);
- context.measureNeighborEncode(x + halfStep, y - halfStep);
- context.measureNeighborEncode(x - halfStep, y + halfStep);
- context.measureNeighborEncode(x + halfStep, y + halfStep);
-
- yChannel.rangeMap[index] = (byte) context.getYRange(index);
- uChannel.rangeMap[index] = (byte) context.getURange(index);
- vChannel.rangeMap[index] = (byte) context.getVRange(index);
- }
- }
- }
-
- public void rangeGridSquare(final int offsetX, final int offsetY,
- final int step) {
- for (int y = offsetY; y < height; y = y + step) {
- for (int x = offsetX; x < width; x = x + step) {
-
- final int index = (y * width) + x;
- final int halfStep = step / 2;
-
- context.initialize(image, yChannel.map, uChannel.map,
- vChannel.map);
-
- context.measureNeighborEncode(x - halfStep, y);
- context.measureNeighborEncode(x + halfStep, y);
- context.measureNeighborEncode(x, y - halfStep);
- context.measureNeighborEncode(x, y + halfStep);