Using the code found here, I've written code that stores image data where each color component is a floating point value.
// Create a TYPE_FLOAT sample model (specifying how the pixels are stored)
SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, options.width, options.height, 4, options.width * 4, new int[]{0,1,2,3});
// ...and data buffer (where the pixels are stored)
DataBufferFloat buffer = new DataBufferFloat(options.width * options.height * 4);
// Wrap it in a writable raster
WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null);
// Create a color model compatible with this sample model/raster (TYPE_FLOAT)
// Note that the number of bands must equal the number of color components in the
// color space (3 for RGB) + 1 extra band if the color model contains alpha
ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel colorModel = new ComponentColorModel(colorSpace, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_FLOAT);
// And finally create an image with this raster
BufferedImage out = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
float[] backingImageData = buffer.getData();
FloatBuffer data = /*External Image Data Source...*/;
data.get(backingImageData); //Place data in image
boolean writerFound = ImageIO.write(out, "png", new File("C:\\out.png"));
However, this code is failing because ImageIO
is failing to find an appropriate writer for this custom image configuration (as seen when debugging, where writerFound
is false
). How do I get ImageIO
to successfully write data with this image?