LWJGL Forum

Please login or register.

Login with username, password and session length

Author Topic: BufferedImage to ByteBuffer  (Read 83 times)

matan45

  • Newbie
  • *
  • Offline Offline
  • Posts: 14
BufferedImage to ByteBuffer
« on: October 28, 2017, 07:36:13 »

there is any faster way to convert BufferedImage to ByteBuffer
then this:
Code: [Select]
public ByteBuffer convertImageData(BufferedImage image) {

int[] pixels = new int[image.getWidth() * image.getHeight()];
image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 3);

for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
int pixel = pixels[y * image.getWidth() + x];
buffer.put((byte) ((pixel >> 16) & 0xFF)); // Red component
buffer.put((byte) ((pixel >> 8) & 0xFF)); // Green component
buffer.put((byte) (pixel & 0xFF)); // Blue component
}
}

buffer.flip();
return buffer;

}
Logged