Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flip an image stored as a byte array

I have an image which is stored as a byte[] array, and I want to flip the image vertically before writing the bytes to disk elsewhere.

The image bytes come from a compressed jp2 image file. I've looked into implementing something like Flip image stored as a byte[] array, but I'm not working in android and don't have access to BitmapFactory. I've also looked into converting the byte array to a BufferedImage first, then flipping it, but the height and width of the image isn't known in the current context (EDIT: I've modified the code so the height and width are now known).

Is there a way to do this just with strict array manipulation?

EDIT: Attempted flip code

 public static byte[] flip(byte[] imageBytes) {
    //separate out the sub arrays
    byte[] holder = new byte[imageBytes.length];
    byte[] subArray = new byte[dimWidth];//dimWidth is the image width, or number of matrix columns
    int subCount = 0;
    for (int i = 0; i < imageBytes.length; i++) {
        subArray[subCount] = imageBytes[i];
        subCount++;
        if (i% dimWidth == 0) {
            subArray = reverse(subArray);
            if (i == (dimWidth)) {
                holder = subArray;
            } else {
                holder = concat(holder, subArray);
            }
            subCount = 0;
            subArray = new byte[dimWidth];
        }
    }
    subArray = new byte[dimWidth];
    System.arraycopy(imageBytes, imageBytes.length - dimWidth, subArray, 0, subArray.length);
    holder = concat(holder, subArray);
    imageBytes = holder;
    return imageBytes;
}
like image 731
Sarah Avatar asked Nov 08 '22 03:11

Sarah


1 Answers

Nevermind guys, I got it working. I just had to flip the high and low bytes BEFORE interleaving them into the final array. To any with the same problem as me, use the above flip function on your high and low byte arrays separately before interleaving them.

Thank you for your help!

like image 68
Sarah Avatar answered Nov 14 '22 21:11

Sarah