user1088777
user1088777

Reputation: 31

how do i create a 16-bit grayscale image with java

Im trying to create a 16-bit PNG but cant get it keeps writing just black. Also how can i convert an 8-bit color defined as 255,255,255/r,g,b to a 16-bit color?

    BufferedImage bi = new BufferedImage(256, 256,
            BufferedImage.TYPE_USHORT_GRAY);

    // 65536
    for (int i = 0; i < 256; i++)
        for (int j = 0; j < 256; j++) {
            int mask = 0xf0
            int value = 255 & mask; // zero other bits
            value >>= 16;
            bi.setRGB(i, j, value);
            // bi.setRGB(i, j, 65536);
        }

    File f = new File("gray.png");

    try {
        ImageIO.write(bi, "png", f);
    } catch (IOException e) {
        e.printStackTrace();
    }

Upvotes: 3

Views: 3055

Answers (2)

srkavin
srkavin

Reputation: 1180

Not a solution to this problem, but another way of accomplishing the requirement.

BufferedImage bi = new BufferedImage(256, 256, BufferedImage.TYPE_USHORT_GRAY);

Graphics2D g = bi.createGraphics();
g.setColor(Color.GRAY);

g.fillRect(0,0,256,256);
File f = new File("C:/gray.png");

try {
    ImageIO.write(bi, "png", f);
} catch (IOException e) {
    e.printStackTrace();
}

Upvotes: 0

James
James

Reputation: 9278

The line value >>= 16 is setting it to zero.

As for converting from 24-bit RGB to 16-bit colours there are usually two ways... RGB565 and RGB555. The digits denote how many bits is given to each colour component.

Upvotes: 1

Related Questions