Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
    }
like image 964
user1088777 Avatar asked Jun 03 '26 10:06

user1088777


1 Answers

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.

like image 69
James Avatar answered Jun 06 '26 01:06

James



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!