title: Loading texture doesn't work with the correct texture width/height
body: <p>I am trying to load a texture for a cube and I have trouble with the dimensions I use. The texture has the power of two (256x256). When it should use 256 as width and height it throws an exception:</p>
  
  <pre><code>java.lang.IndexOutOfBoundsException: Required 262144 remaining bytes in buffer, only had 68998
      at com.jogamp.common.nio.Buffers.rangeCheckBytes(Buffers.java:828)
  </code></pre>
  
  <p>The code:</p>
  
  <pre><code>private void initTexture(GL2ES2 gl) {
  try {
      BufferedImage bufferedImage = ImageIO.read(new URI("http://192.168.0.39/images/box.gif").toURL());
      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
      ImageIO.write(bufferedImage, "gif", byteArrayOutputStream);
      byte[] imageData = byteArrayOutputStream.toByteArray();
      imageBuffer = ByteBuffer.wrap(imageData);
  } catch (Exception e) {
      e.printStackTrace();
  }
  imageBuffer.rewind();
  gl.glGenTextures(1, textureIds, 0);
  gl.glBindTexture(GL2ES2.GL_TEXTURE_2D, textureIds[0]);
  gl.glTexImage2D(GL2ES2.GL_TEXTURE_2D, 0, GL2ES2.GL_RGBA, 256, 256, 0, GL2ES2.GL_RGBA, GL2ES2.GL_UNSIGNED_BYTE, imageBuffer);
  gl.glTexParameteri(GL2ES2.GL_TEXTURE_2D, GL2ES2.GL_TEXTURE_MAG_FILTER, GL2ES2.GL_LINEAR);
  gl.glTexParameteri(GL2ES2.GL_TEXTURE_2D, GL2ES2.GL_TEXTURE_MIN_FILTER, GL2ES2.GL_LINEAR_MIPMAP_NEAREST);
  gl.glGenerateMipmap(GL2ES2.GL_TEXTURE_2D);
  gl.glBindTexture(GL2ES2.GL_TEXTURE_2D, 0);
  }
  </code></pre>
  
  <p>When I change the parameter width/height to 128 the exception disappears but the cubes show wrong colors:</p>
  
  <p><img src="https://i.stack.imgur.com/roVnC.png" alt="enter image description here"></p>
  
  <p>As bestsss mentioned, the reason might be some raw format. The problem: I can't fix this. I tried multiple images and formats. Created them with gimp (working on ubuntu) but the exception is always the same. So I guess the reason for that is that I read the image in a wrong way. Some ideas?</p>
  
  <p><strong>Update</strong></p>
  
  <p>My solution (which uses JOGL classes TextureIO and Texture):</p>
  
  <pre><code>Texture texture;
  
  private void initTexture(GL2ES2 gl) {
      try {
          texture = TextureIO.newTexture(new URI("http://192.168.0.39/images/box.gif").toURL(),true,null);
          texture.setTexParameterf(GL2ES2.GL_TEXTURE_MIN_FILTER, GL2ES2.GL_LINEAR_MIPMAP_LINEAR);
          texture.setTexParameterf(GL2ES2.GL_TEXTURE_MAG_FILTER, GL2ES2.GL_LINEAR);
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  
  public void display(GL2ES2 gl) {
      // code snipped
      if (texture != null) {
          texture.enable();
          texture.bind();
      }
      // code snipped
  }
  </code></pre>