I am using PNGDecoder to decode a png that is a height map. Howerver, when I am just printing the values returned in console I see few of the rgb values are returned as negative
-128, -128, -128, -1
-128, -128, -128, -1
-128, -128, -128, -1
-124, -124, -124, -1
-119, -119, -119, -1
-118, -118, -118, -1
The code that I’m using to decode and then read is below
public static ByteBuffer decodeImage(String path) throws IOException {
InputStream stream = new FileInputStream(path);
PNGDecoder decoder = new PNGDecoder(stream);
ByteBuffer decodedImageData = ByteBuffer.allocateDirect(4 * decoder.getWidth() * decoder.getHeight());
decoder.decodeFlipped(decodedImageData, decoder.getWidth() * 4, PNGDecoder.Format.RGBA);
decodedImageData.flip();
return decodedImageData;
}
private void applyHeightMap (String heightMapPath) throws IOException {
float vertices[] = plane.getVertices();
ByteBuffer buffer = Texture.decodeImage(heightMapPath);
for (int i = 0; i < 2624 * 1756; i++) {
byte r = buffer.get();
byte g = buffer.get();
byte b = buffer.get();
byte a = buffer.get();
if(r < 0 || b < 0 || g < 0) {
System.out.println(r + ", " + g + ", " + b+", "+a);
}
}
}
Not sure why there are few negative values and also why the alpha channel is read as -1
The image I’m using is https://learnopengl.com/img/guest/2021/tessellation/height_map/iceland_heightmap.png