- // load top-, left-most pixel.
- decodedYMap[0] = (byte)bitInputStream.readBits(8);
- decodedUMap[0] = (byte)bitInputStream.readBits(8);
- decodedVMap[0] = (byte)bitInputStream.readBits(8);
-
- Color color = new Color();
- color.y = ImageEncoder.byteToInt(decodedYMap[0]);
- color.u = ImageEncoder.byteToInt(decodedUMap[0]);
- color.v = ImageEncoder.byteToInt(decodedVMap[0]);
-
- color.YUV2RGB();
-
- pixels[0] = (byte)color.r;
- pixels[0+1] = (byte)color.g;
- pixels[0+2] = (byte)color.b;
-
-
- // detect initial step
- int largestDimension;
- int initialStep = 2;
- if (width > height) {
- largestDimension = width;
- } else {
- largestDimension = height;
- }
-
- while (initialStep < largestDimension){
- initialStep = initialStep * 2;
- }
-
- grid(initialStep, pixels);
- }
-
-
- public void grid(int step, byte [] pixels) throws IOException {
-
- gridDiagonal(step / 2, step / 2, step, pixels);
- gridSquare(step / 2, 0, step, pixels);
- gridSquare(0, step / 2, step, pixels);
-
- if (step > 2) grid(step / 2, pixels);
- }
-
-
- public void gridSquare(int offsetX, int offsetY, int step, byte [] pixels) throws IOException{
-
- for (int y = offsetY; y < height; y = y + step){
- for (int x = offsetX; x < width; x = x + step){
-
-
- int halfStep = step / 2;
-
- context.initialize(image, decodedYMap, decodedUMap, decodedVMap);
- context.measureNeighborEncode(x - halfStep, y);
- context.measureNeighborEncode(x + halfStep, y);
- context.measureNeighborEncode(x, y - halfStep);
- context.measureNeighborEncode(x, y + halfStep);
-
- loadPixel(step, offsetX, offsetY, x, y, pixels,
- context.colorStats.getAverageY(),
- context.colorStats.getAverageU(),
- context.colorStats.getAverageV());
-
- }
- }
- }
-
-
- public void gridDiagonal(int offsetX, int offsetY, int step, byte [] pixels) throws IOException{
-
- for (int y = offsetY; y < height; y = y + step){
- for (int x = offsetX; x < width; x = x + step){
-
- int halfStep = step / 2;
-
- context.initialize(image, decodedYMap, decodedUMap, decodedVMap);
- context.measureNeighborEncode(x - halfStep, y - halfStep);
- context.measureNeighborEncode(x + halfStep, y - halfStep);
- context.measureNeighborEncode(x - halfStep, y + halfStep);
- context.measureNeighborEncode(x + halfStep, y + halfStep);
-
- loadPixel(step, offsetX, offsetY, x, y, pixels,
- context.colorStats.getAverageY(),
- context.colorStats.getAverageU(),
- context.colorStats.getAverageV());
-
- }
- }
- }
-
-
- public void loadPixel(int step, int offsetX, int offsetY, int x, int y, byte[] pixels,
- int averageDecodedY, int averageDecodedU, int averageDecodedV)
- throws IOException{
-
- int index = (y * width) + x;
-
- int halfStep = step / 2;
-
- int parentIndex;
- if (offsetX > 0){
- if (offsetY > 0){
- // diagonal approach
- parentIndex = ((y - halfStep) * width) + (x - halfStep);
- } else {
- // take left pixel
- parentIndex = (y * width) + (x - halfStep);
- }
- } else {
- // take upper pixel
- parentIndex = ((y - halfStep) * width) + x;
- }