Search code examples
javargba

Convert any file to PNG in Java


I am wanting to convert any file to a PNG and also reverse the process, all in Java.

I want to use an int-RGB form for the image, and have bytes from the file be a byte in the RGB integer. This should produce an image.

I've gotten this to work by only storing the bytes in the red color, but I can't figure out how to also use green and blue.

This is the code I use at the moment, which only uses red, and it works fine:

public static void fileToImage(String sourceFile, String imageFile) throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream(sourceFile));
    int size = ((int) Math.sqrt(dis.available())) + 2;
    BufferedImage image = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
    for (int y = 0; y < size; y++) {
        for (int x = 0; x < size; x++) {
            int red = dis.read(); // I'm using only red
            int green = 0; // default
            int blue = 0; // default
            int rgb = (0xFF << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
            image.setRGB(x, y, rgb);
        }
    }
    dis.close();
    ImageIO.write(image, "png", new File(imageFile));
}

public static void imageToFile(String imageFile, String outputFile) throws IOException {
    BufferedImage image = ImageIO.read(new File(imageFile));
    DataOutputStream dos = new DataOutputStream(new FileOutputStream(outputFile));
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            int rgb = image.getRGB(x, y);
            int red = (rgb >> 16) & 0xFF;
            int green = (rgb >> 8) & 0xFF;
            int blue = rgb & 0xFF;
            dos.write(red); // I'm using only red
        }
    }
    dos.close();
}

EDIT: Okay, so I have modified the code, here it is:

public static void fileToImage(String sourceFile, String imageFile) throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream(sourceFile));
    int size = ((int) Math.sqrt(dis.available())) + 2;
    BufferedImage image = new BufferedImage(size,size, BufferedImage.TYPE_INT_RGB);
    for (int y = 0; y < size; y++) {
        for (int x = 0; x < size; x++) {
            int red = dis.read();
            int green = dis.read();
            int blue = dis.read();
            int rgb = (0xFF << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
            image.setRGB(x, y, rgb);
        }
    }
    dis.close();
    ImageIO.write(image, "png", new File(imageFile));
}

public static void imageToFile(String imageFile, String outputFile) throws IOException {
    BufferedImage image = ImageIO.read(new File(imageFile));
    DataOutputStream dos = new DataOutputStream(new FileOutputStream(outputFile));
    for (int y = 0; y < image.getHeight(); y++) {
        for (int x = 0; x < image.getWidth(); x++) {
            int rgb = image.getRGB(x, y);
            int red = (rgb >> 16) & 0xFF;
            int green = (rgb >> 8) & 0xFF;
            int blue = rgb & 0xFF;
            dos.write(red);
            dos.write(green);
            dos.write(blue);
        }
    }
    dos.close();
}

This does "work", but not exactly as expected. There's a whole lot of black space in the produced PNG, because I believe the "size" of the image is wrong. Because of this, when translating the PNG back into the original file, it becomes much larger than originally.

EDIT: The issue I am now having is this: For example, if I turn a text file with the following content to a PNG using the fileToImage method: hello world! Then I use imageToFile to convert it back, the output is: hello world!SSSSSSSSSSSSSSS (S stands for "space", there are 15)

EDIT: Still can't figure this out. Here's what I'm using:

private static final int NAN = -1;

private static int readByte(DataInputStream dis) throws IOException {
    int b;
    try {
        b = dis.readByte();
    } catch (EOFException e) {
        b = NAN;
    }
    return b;
}

public static void fileToImage(String sourceFile, String imageFile) throws IOException {
    DataInputStream dis = new DataInputStream(new FileInputStream(sourceFile));
    int size = ((int) Math.sqrt(dis.available())) + 2;
    BufferedImage image = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
    for (int y = 0; y < size; y++) {
        boolean finished = false;
        for (int x = 0; x < size; x++) {
            int alpha = 3;
            int red = readByte(dis);
            int green = readByte(dis);
            int blue = readByte(dis);
            if (red == NAN) {
                alpha--;
                red = 0;
            }
            if (green == NAN) {
                alpha--;
                green = 0;
            }
            if (blue == NAN) {
                alpha--;
                blue = 0;
            }
            int rgb = ((alpha & 0xFF) << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
            image.setRGB(x, y, rgb);
            if (alpha < 3) {
                finished = true;
                break;
            }
        }
        if (finished) break;
    }
    dis.close();
    ImageIO.write(image, "png", new File(imageFile));
}

public static void imageToFile(String imageFile, String outputFile) throws IOException {
    BufferedImage image = ImageIO.read(new File(imageFile));
    DataOutputStream dos = new DataOutputStream(new FileOutputStream(outputFile));
    for (int y = 0; y < image.getHeight(); y++) {
        boolean finished = false;
        for (int x = 0; x < image.getWidth(); x++) {
            int rgb = image.getRGB(x, y);
            int alpha = (rgb >> 24) & 0xFF;
            int red = (rgb >> 16) & 0xFF;
            int green = (rgb >> 8) & 0xFF;
            int blue = rgb & 0xFF;
            if (alpha == 0) {
                finished = true;
                break;
            }
            if (alpha >= 1) dos.write(red);
            if (alpha >= 2) dos.write(green);
            if (alpha == 3) dos.write(blue);
        }
        if (finished) break;
    }
    dos.close();
}

Solution

  • I think you just need to adjust the inner loops slightly. A small helper method will make this thing easier to use, thought I'm sure my sketch is a bit ugly:

    int myReadByte(DataInputStream dis) {
        int b;
        try {
            b = dis.readByte():
        } catch (EOFException e) {
            b = 0;
        }
        return b;
    }
    

    Now with this helper...

    for (int x = 0; x < size; x++) {
            int red = myReadByte(dis);
            int green = myReadByte(dis);
            int blue = myReadByte(dis);
            int rgb = (0xFF << 24) | ((red & 0xFF) << 16) | ((green & 0xFF) << 8) | (blue & 0xFF);
            image.setRGB(x, y, rgb);
    

    and

    for (int x = 0; x < image.getWidth(); x++) {
            int rgb = image.getRGB(x, y);
            int red = (rgb >> 16) & 0xFF;
            int green = (rgb >> 8) & 0xFF;
            int blue = rgb & 0xFF;
            dos.write(red);
            dos.write(green);
            dos.write(blue);
        }