<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://jogamp.org/bugzilla/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.2"
          urlbase="https://jogamp.org/bugzilla/"
          
          maintainer="sgothel@jausoft.com"
>

    <bug>
          <bug_id>584</bug_id>
          
          <creation_ts>2012-05-23 18:47:16 +0200</creation_ts>
          <short_desc>Texture binding fails if image width &lt; alignment</short_desc>
          <delta_ts>2012-05-24 12:30:26 +0200</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>3</classification_id>
          <classification>JogAmp</classification>
          <product>Jogl</product>
          <component>util</component>
          <version>2</version>
          <rep_platform>All</rep_platform>
          <op_sys>all</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>INVALID</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>---</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>0</everconfirmed>
          <reporter name="Fabien Mars">fabmars</reporter>
          <assigned_to name="Sven Gothel">sgothel</assigned_to>
          
          
          <cf_type>DEFECT</cf_type>
          <cf_scm_refs></cf_scm_refs>
          <cf_workaround>---</cf_workaround>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1593</commentid>
    <comment_count>0</comment_count>
    <who name="Fabien Mars">fabmars</who>
    <bug_when>2012-05-23 18:47:16 +0200</bug_when>
    <thetext>I&apos;m trying to bind a .gif 2x2 texture in 4 colors, JOGL won&apos;t let me.

First loading it with BufferedImage sourceImage = ImageIO.read(url);
Then, using an appropriate Color Model: new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 0 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
I&apos;m creating a raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, 2, 2, 3, null) to hold the data
Prior to filling a new proper BufferedImage destImage = new BufferedImage(colorModel, raster, false, new Hashtable&lt;Object, Object&gt;());
Graphics g = destImage.getGraphics();
g.drawImage(sourceImage, 0, 0, null);
And getting the databuffer with raster.getDataBuffer().
I do the above with all my textures, no matter what they are, they will load properly. Except here:

This method is called:
      gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 2, 2, 0, GL.GL_RGB, GL.GL_UNSIGNED_BYTE, textureBuffer);

And I get:
java.lang.IndexOutOfBoundsException: Required 14 remaining bytes in buffer, only had 12
	at com.jogamp.common.nio.Buffers.rangeCheckBytes(Buffers.java:853)
	at jogamp.opengl.gl4.GL4bcImpl.glTexImage2D(GL4bcImpl.java:25283)


That&apos;s because of what&apos;s in GLBuffers line 321: remainder won&apos;t be good if image width &lt; alignment.

This works in C, should also in Java.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1594</commentid>
    <comment_count>1</comment_count>
    <who name="Fabien Mars">fabmars</who>
    <bug_when>2012-05-24 07:33:07 +0200</bug_when>
    <thetext>Actually this very texture used to load in C and also with JOGL in 2005. I discovered this whilist dusting my code with the latest libs of everything.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1595</commentid>
    <comment_count>2</comment_count>
    <who name="Sven Gothel">sgothel</who>
    <bug_when>2012-05-24 09:21:28 +0200</bug_when>
    <thetext>(In reply to comment #0)
&gt; I&apos;m trying to bind a .gif 2x2 texture in 4 colors, JOGL won&apos;t let me.

&gt; 
&gt; This method is called:
&gt;       gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGB, 2, 2, 0, GL.GL_RGB,
&gt; GL.GL_UNSIGNED_BYTE, textureBuffer);
&gt; 
&gt; And I get:
&gt; java.lang.IndexOutOfBoundsException: Required 14 remaining bytes in buffer,
&gt; only had 12
&gt;     at com.jogamp.common.nio.Buffers.rangeCheckBytes(Buffers.java:853)
&gt;     at jogamp.opengl.gl4.GL4bcImpl.glTexImage2D(GL4bcImpl.java:25283)
&gt; 
&gt; 
&gt; That&apos;s because of what&apos;s in GLBuffers line 321: remainder won&apos;t be good if
&gt; image width &lt; alignment.
&gt; 

Alignment should be 1 or 2 in your case 
(not 4, which is the default).
It&apos;s up to you to properly set it up.

In your example, you have probably kept alignment at 4,
hence the 1st row-length is assumed to be 8:
  2 * 3 =6, aligned -&gt; 8
the 2nd row-length is not required to be aligned,
since only the beginning of a row requires alignment:
  8 + 6 = 14

So you are missing the 2 bytes of alignment.
Either add it to the buffer, or set alignment to 1.

&gt; This works in C, should also in Java.

Well, if the native OpenGL driver allows you to ignore alignment
and does not post an error - this is one thing.
Please notice that in such case, the OpenGL driver may
copy more bytes than you offer from host memory to GPU memory.

JOGL likes to fail-fast and  does not allow to offer less bytes
than required.

(In reply to comment #1)
&gt; Actually this very texture used to load in C and also with JOGL in 2005. I
&gt; discovered this whilist dusting my code with the latest libs of everything.
In 2005 we might not had this pre-validation in place
or my latest update of this code refined the restrictions.

If my calculations and/or assumptions above are wrong,
please reopen the bug and clarify.

Thank you.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1596</commentid>
    <comment_count>3</comment_count>
    <who name="Fabien Mars">fabmars</who>
    <bug_when>2012-05-24 12:30:26 +0200</bug_when>
    <thetext>Indeed, I&apos;m blatantly stupid.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>