title: Problems reading ATI compressed textures
body: <p>I'm trying to use compressed textures on my android application. I have a problem loading textures, textures seem to be "cut-off" on the right side of the object.</p>
  
  <p>For compressing textures I'm using ATI's "TheCompressonator".
  For testing I'm using Nexus 5.</p>
  
  <p>I'm suspecting the problem is with my "calculated" size of the texture, but can't find any refenreces / specification of this compression format. </p>
  
  <p>Does anyone know how to properly read this file format?</p>
  
  <p>Here is screenshot from nexus:</p>
  
  <p><img src="https://i.stack.imgur.com/QsfIu.png" alt="Nexus 5, blowfish right side is cut off"></p>
  
  <p>Here is how it should have looked (don't mind black object textures, the image for that was missing)
  <img src="https://i.stack.imgur.com/QSqiZ.png" alt="Samsung tab 2, this is how blowfish should look"></p>
  
  <p>Here is my code snippet. </p>
  
  <pre><code>final int[] textureObjectIds = new int[1];
  
  glGenTextures(1, textureObjectIds, 0);
  
  if(textureObjectIds[0] == 0){
      logTextureHelper(Log.WARN, "Could not generate a new OpenGL texture object");
      return 0;
  }
  
  final InputStream bitmap = context.getResources().openRawResource(resourceId);
  byte[] buffer;
  ByteBuffer bf;
  
  try {
      buffer = new byte[bitmap.available()];
      bitmap.read(buffer);
  
      int offset = 0; // 64 bit = header, 15 bit = metadata
      bf = ByteBuffer.wrap(buffer, offset, buffer.length-offset);
      bf.order(ByteOrder.LITTLE_ENDIAN);
  
      int height = bf.getInt(16);
      int width = bf.getInt(12);
  
  
      int size = ((height + 3) / 4) * ((width + 3) / 4) * 16;
      Log.d("TextureHelper","Buffer size: "+width+" "+height+" "+size);
  
      glBindTexture(GL_TEXTURE_2D, textureObjectIds[0]);
  
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
      glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
      glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
  
  
      GLES20.glCompressedTexImage2D(GL_TEXTURE_2D, 0,ATC_RGBA_EXPLICIT_ALPHA_AMD, width, height, 0, size, bf);
      Log.d("TextureHelper","Buffer size: "+bf.capacity()+"   : "+buffer.length+" error:"+GLES20.glGetError());
  
      glBindTexture(GL_TEXTURE_2D, 0); //unbind texture
  
  } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  
  return textureObjectIds[0];
  </code></pre>
  
  <p>EDIT: Solution</p>
  
  <pre><code>buffer = new byte[bitmap.available()];
              bitmap.read(buffer);
  
              int offset = 128; // 64 bit = header, 15 bit = metadata
              bf = ByteBuffer.wrap(buffer, offset, buffer.length-offset);
              bf.order(ByteOrder.LITTLE_ENDIAN);
  
              int height = bf.getInt(16);
              int width = bf.getInt(12);
  
  
              int size = ((height + 3) / 4) * ((width + 3) / 4) * 16;
              Log.d("TextureHelper","Buffer size: "+width+" "+height+" "+size);
  
  
              bf.compact();///////SOLUTION!
              bf.position(0);
  
  
              glBindTexture(GL_TEXTURE_2D, textureObjectIds[0]);
  
              GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
              GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
              glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
              glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
  
  
              GLES20.glCompressedTexImage2D(GL_TEXTURE_2D, 0,ATC_RGBA_EXPLICIT_ALPHA_AMD, width, height, 0, size, bf);
              Log.d("TextureHelper","Buffer size: "+bf.capacity()+"   : "+buffer.length+" error:"+GLES20.glGetError());
  
              glBindTexture(GL_TEXTURE_2D, 0); //unbind texture
  </code></pre>
  
  <p>Note: You need to have data at 0 position, if you just offset position(128) it will throw invalid pointer exception. </p>