Home > Enterprise >  Java Bitwise Decode Function
Java Bitwise Decode Function

Time:11-02

I've got a project right now where we basically have a tree using the Huffman Algorithm that stores ColorCodes. Then the encode method just basically iterates through the string you give it and parses it to an int and checks to see if it's equal to any of the Color Values of the ColorCodes and appends it to a new string. The EncodeGraphic method then goes through the lines of a graphic and takes in a string and then uses encode with bitwise shift operators to "encode" it. Then decode, traverses through the BinaryTree using the encoded binary string as the key for how many times it needs to traverse. Then this below is decodeGraphic which seems to not return the correct ARGB values after shifting it. Any help would be appreciated!

public void decodeGraphic(String inputFile, String outputFile) throws InvalidHuffmanCodeException, IOException {
    // Fill in this method
    // Create and output file using BufferedReader (i.e., BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) )
    // Note the height and width must be read in from the input file (first two lines)
    // The remaining lines in the input file are the pixels
    // Call the decode method for each line.
    // The returned String must be parsed on the comma to get the A, R, G and B values
    // Combine channels into one "bit" by using the left bit-shiffter, <<, and bitwise or, |
    // Call setRGB to set the pixel
    // When all the pixels are set, use ImageIO's static write method to write the graphic file (.png file)
    //*************************************

    int height, width;
    String a, r, g, b;
    int al, re, gr, bl, x = 0, y = 0;
    Scanner in = new Scanner(new File(inputFile));
    height = Integer.parseInt(in.nextLine());
    width = Integer.parseInt(in.nextLine());
    BufferedImage output = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB);
    String[] sA;

    while(in.hasNextLine()){
        String temp = decode(in.nextLine());
        sA = temp.split(",");
        a = sA[0];
        r = sA[1];
        g = sA[2];
        b = sA[3];

        al = Integer.parseUnsignedInt(a);
        re = Integer.parseUnsignedInt(r);
        gr = Integer.parseUnsignedInt(g);
        bl = Integer.parseUnsignedInt(b);

        int updatedPixel = (al << 24) | (re << 16) | (gr << 8) | (bl);

        output.setRGB(x % output.getWidth(), y % output.getHeight(), updatedPixel);

        x  ;

        if(x % output.getHeight() == 0 && x != 0) {
            y  ;
        }
    }

    ImageIO.write(output, "png", new File(outputFile));

    //************************************
}

The above code is what I tried, below is what was returned (2/5 decodeGraphic tests passed, this is an example from one of the ones that failed)

Expected:

255,37,21,19 255,45,30,27 255,43,28,26 255,45,28,24 255,43,26,21 255,43,24,17 255,49,30,22 255,48,27,24

Actual:

255,100,100,100 255,113,115,117 255,144,145,147 255,89,91,93 255,91,93,95 255,98,98,98 255,92,92,92 255,100,100,100

CodePudding user response:

Your bit shifts don't look correct. Do note that all int values in Java are always signed.

Why reimplement something that's already been done and tested by the JDK?

    final Color color = new Color(re, gr, bl, al);
    output.setRGB(x % output.getWidth(), y % output.getHeight(), color.getRGB());

Or if you really need to implement the correct bit shift manually, looking at the constructor of Color helps:

    updatedPixel = ((al & 0xFF) << 24) |
                   ((re & 0xFF) << 16) |
                   ((gr & 0xFF) << 8)  |
                   ((bl & 0xFF) << 0);

Further note that the modulo operation can be avoided and simplified, which removes one more source for potential errors:

    output.setRGB(x, y, updatedPixel);

      x;

    if (x >= output.getWidth()) {
          y;
        x = 0;
    }

NB. using good variable names can get you a long way. So either a, r, g, b or alpha, red, green, blue. Why use 2 characters?

  • Related