3

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();
    }
user1088777
  • 31
  • 1
  • 2

2 Answers2

1

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.

James
  • 9,064
  • 3
  • 31
  • 49
0

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();
}
srkavin
  • 1,152
  • 7
  • 17
  • sorry im not by my computer but could you tell me whether that creates a 8 or 16 bit png? thanks! – user1088777 Dec 09 '11 at 06:50
  • @user1088777 I've no idea. [Here](http://stackoverflow.com/q/4116858/862441) is a related SO question that might help. – srkavin Dec 09 '11 at 18:11