Buffered image using float data

P

Peter Ashford

Does anyone know whether there's anwyay to use floating point data to
make a greyscale BufferedImage?

I'd like this because I want an image I can edit and get the results
directly in floating point format. There's a unsigned 16bit format
which is better than 8bit, but I'm using the data for heightfield info
which doesn't need to be restricted to 16 bit int accuracy.

Ideas?

TIA.
 
B

Betty

Peter Ashford said:
Does anyone know whether there's anwyay to use floating point data to
make a greyscale BufferedImage?

I'd like this because I want an image I can edit and get the results
directly in floating point format. There's a unsigned 16bit format
which is better than 8bit, but I'm using the data for heightfield info
which doesn't need to be restricted to 16 bit int accuracy.
You could keep your data in floating point separate from the BufferedImage
and convert it when you want to put it on the screen.
 
P

Peter Ashford

Betty said:
You could keep your data in floating point separate from the BufferedImage
and convert it when you want to put it on the screen.

True. However, I will be updating a 3d image on the basis on the data
in the bitmap and editing the bitmap, converting the results and
updating the 3d image will be much slower than getting the data directly
from the bitmap in the correct format.

More significantly, as the edits to the buffered image are being used to
alter the heightmap, if the image uses 16bit int accuracy, it will apply
that limitation to the float buffer. This is the problem I'm trying to
avoid in the first place. If it weren't for this problem I could just
use a 16bit greyscale image for the data and live with the resolution
restraints.
 
B

Boudewijn Dijkstra

Peter Ashford said:
Does anyone know whether there's anwyay to use floating point data to make a
greyscale BufferedImage?

I'd like this because I want an image I can edit and get the results
directly in floating point format. There's a unsigned 16bit format which is
better than 8bit, but I'm using the data for heightfield info which doesn't
need to be restricted to 16 bit int accuracy.

Ideas?

SampleModel sm = new ComponentSampleModel(DataBuffer.TYPE_FLOAT, w, h, 1, w,
new int[] {0});
DataBuffer db = new DataBufferFloat(w * h);
WritableRaster wr = Raster.createRaster(sm, db, null);
ColorSpace cs = ColorSpace.getInstance(CS_GRAY);
ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE,
DataBuffer.TYPE_FLOAT);
BufferedImage bi = new BufferedImage(cm, wr, true, null);
 
B

Boudewijn Dijkstra

Errors corrected from last try; test suite added. ;)


public static void main(String[] args)
{
final int w = 320;
final int h = 240;

SampleModel sm = new ComponentSampleModel(
DataBuffer.TYPE_FLOAT, w, h, 1, w, new int[] {0});
DataBuffer db = new DataBufferFloat(w * h);
WritableRaster wr = Raster.createWritableRaster(sm, db, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel cm = new ComponentColorModel(
cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
final BufferedImage bi = new BufferedImage(cm, wr, true, null);

Graphics2D g2 = bi.createGraphics();
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, w, h);
g2.setColor(Color.WHITE);
g2.drawLine(0, 0, w, h);
g2.drawLine(w, 0, 0, h);
g2.drawOval(w/4, h/4, w/2, h/2);
g2.dispose();

Frame f = new Frame("Float Graphics");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
f.setLayout(new BorderLayout());
Canvas c = new Canvas() {
public void paint(Graphics g) {
g.drawImage(bi, 0, 0, null);
}
};
c.setSize(w, h);
f.add(c, BorderLayout.CENTER);
f.pack();
f.show();
}
 
P

Peter Ashford

Boudewijn said:
Errors corrected from last try; test suite added. ;)

<code snipped>

Awesome! Great! Thanks very much - looks like exactly what I needed.
Thanks for the effort :eek:)

Peter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top