1

I was having a problem due to not flipping the buffer previously, but now I can't get the buffer to add anything with the .put() or .putInt() it constantly throws the BufferOverflowException at the first attempt of:

buffer.put((byte) c.getRed());

The relevant code below:

BufferedImage image = loadImage(".\\res\\" + fileName);
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() * 4);
buffer.flip();
Color c;

for (int y = 0; y < image.getHeight(); y++) {
    for (int x = 0; x < image.getWidth(); x++) {
        c = new Color(image.getRGB(x, y));
        buffer.put((byte) c.getRed());     // Red component
        buffer.put((byte) c.getGreen());      // Green component
        buffer.put((byte) c.getBlue());               // Blue component
        buffer.put((byte) c.getAlpha());    // Alpha component. Only for RGBA
    }
}
jrgilman
  • 460
  • 1
  • 4
  • 15
  • 2
    Let's see what `BufferUtils#createByteBuffer` does. – Sotirios Delimanolis Jan 27 '15 at 02:24
  • 1
    Unrelated to you `ByteBuffer` question, why are you filling an `int[]` with the rgb values, then looping through the x, y coordinates and getting each rgb value individually? – Brett Okken Jan 27 '15 at 02:29
  • Javadoc page for BufferUtils: http://javadoc.lwjgl.org/org/lwjgl/BufferUtils.html Also to the above comment, I do that because the int[] holds a value that I would need to use bitmasks with to get the red, green and blue components, thus I thought it would be easier for me to simply do the method above rather than something like the second comment in this question explains: http://stackoverflow.com/questions/25761438/understanding-bufferedimage-getrgb-output-values. – jrgilman Jan 27 '15 at 02:47

1 Answers1

2

Your call to buffer.flip() is in the wrong place. From the documentation:

Flips this buffer. The limit is set to the current position and then the position is set to zero.

Where limit is defined as:

A buffer's limit is the index of the first element that should not be read or written. A buffer's limit is never negative and is never greater than its capacity.

Since you call flip() immediately after allocating the buffer, where the current position is 0, the flip() call sets the limit to 0. Which means that nothing can get written at index 0, or after it. Which in turn means that nothing can be written at all.

To fix this, you need to move the buffer.flip() call after the loop that fills the buffer with values using buffer.put().

The main point that your original code was missing is that the buffer position needs to be set to 0 after you write the data to it. Otherwise, future operations will start reading at the current position, which is the end of the buffer after you completed all the buffer.put() operations.

There are various ways to reset the position to 0 after filling the buffer with data. Any one of these should do the job:

buffer.flip();
buffer.position(0);
buffer.rewind();
Reto Koradi
  • 53,228
  • 8
  • 93
  • 133