I created a map editor in Java. The problem is, I have steps for every byte value, so the map isn't smooth. Is it possible to change the BufferedImage raster data to float data and draw in float precision on it?
Asked
Active
Viewed 2,791 times
3

Eric Leschinski
- 146,994
- 96
- 417
- 335

bitQUAKE
- 473
- 1
- 8
- 19
-
1It's unclear what you mean by "i have steps for every byte value". I don't understand what you're trying to achieve here... – Jon Skeet Nov 05 '14 at 20:47
-
Conversion from float to RGBA should be relatively simple since you only need to parse (portions of) the binary-representation of the float to an int. But you would not get any smoother, since you have only so many bits to define a color. – Turing85 Nov 05 '14 at 20:53
-
Sorry, i mean when I draw 1 layer of the texture that sets the height, the height is 5cm for example, not 0.5cm. So the step for each number(byte) is visible. – bitQUAKE Nov 05 '14 at 21:07
1 Answers
4
To answer your question, yes, you can create a BufferedImage
with float precision. It is however a little unclear if this will help you solve your problem.
In any case, here's working example code for creating a BufferedImage
with float
precision:
public class FloatImage {
public static void main(String[] args) {
// Define dimensions and layout of the image
int w = 300;
int h = 200;
int bands = 4; // 4 bands for ARGB, 3 for RGB etc
int[] bandOffsets = {0, 1, 2, 3}; // length == bands, 0 == R, 1 == G, 2 == B and 3 == A
// Create a TYPE_FLOAT sample model (specifying how the pixels are stored)
SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, w, h, bands, w * bands, bandOffsets);
// ...and data buffer (where the pixels are stored)
DataBuffer buffer = new DataBufferFloat(w * h * bands);
// 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 image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
System.out.println("image = " + image);
}
}
For map elevation data, using a single band (bands = 1; bandOffsets = {0};
) and a grayscale color space (ColorSpace.CS_GRAY
) and no transparency may make more sense.

Harald K
- 26,314
- 7
- 65
- 111
-
Can you also tell me how to draw floats directly to it ? While i was writing this sentence i remembered that Color provides float values. I will mark this question as answered, if it works :) – bitQUAKE Nov 06 '14 at 15:13
-
I'm uncertain if the Java2D drawing operations supports precision higher than 8 bits/pixel component, but at least worth a try. If not, you can always access the samples in their native format, using one of the `raster.setDataElements()` methods. – Harald K Nov 06 '14 at 18:34
-
@haraldK Which `ColorSpace` should be used for RGB images without alpha channel ? Also, how `ComponentColorModel` should be changed for it ? – Melike Jan 11 '21 at 16:13
-
1@Melike The color space would be the same (ie. sRGB), there's no alpha channel in the color space. To create a `ComponentColorModel` without alpha, you would simply change the second argument (`hasAlpha`) to `false`, and the second last (`transparency`) to `OPAQUE`. – Harald K Jan 11 '21 at 19:42