How to write PNG files in java using RGB pixel values ​​from 0 to 1?

I am writing a ray tracer in java and I am trying to understand how to write the generated image to a PNG file. So far, all the examples I found demonstrate using BufferedImage to create PNGs, but they all use RGB values ​​from 0 to 255. In my code, I represent each pixel color value from 0 to 1, so for example, magenta (1) , 0, 1). How can I write PNG with such values?

thank

+3
source share
2 answers

If you multiply your value between 0 and 1 by 255, you will get a number from 0 to 255.

. BufferedImage PNG API ImageIO, :

import javax.imageio.ImageIO;

// ...

BufferedImage image = ...;

ImageIO.write(image, "png", new File("output.png"));
+4

BufferedImage, a float[].

, API , (, sRGB.)

:

ColorModel cm =
    new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                            false, false, Transparency.OPAQUE,
                            DataBuffer.TYPE_FLOAT);
WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
BufferedImage img = new BufferedImage(cm, raster, false, null);
0

Source: https://habr.com/ru/post/1788861/


All Articles