title: android bitmap pixel format for glTexImage2D
body: <p>I'm trying to load textures to use with NDK OpenGL from Java with the Bitmap class. It works, but I'm having problems with the pixel format.</p>
  
  <p>First, in Java, I load a bitmap from the assets folder like this:</p>
  
  <pre><code>Bitmap bitmap = BitmapFactory.decodeStream(amgr.open(path));
  return bitmap.copy(Bitmap.Config.ARGB_8888, false);
  </code></pre>
  
  <p>the bitmap config does not have an option for RGBA channel order.</p>
  
  <p>[JNI things happen here]</p>
  
  <p>Using GLES 1, I then buffer the texture like so:</p>
  
  <pre><code>glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
  // `pixels` is the pixel buffer I produced earlier.
  </code></pre>
  
  <p>As you can see, there is a problem with the pixel format. <code>glTexImage2D</code> does not have an option for ARGB, but the Java Bitmap class does not have an option to create a buffer in RGBA. So I end up with messed up color channels. I do need the alpha channel by the way.</p>
  
  <p>The question is: how do I most efficiently produce a pixel buffer in RGBA8888 format from the Java bitmap class, or, how do I load a GL texture in ARGB8888 format?</p>
  
  <p>Surely there is a way other than manually swapping bytes pixel-by-pixel?</p>
  
  <p>I am currently doing this:</p>
  
  <pre><code>void pxl::swap_channels_ARGB_to_RGBA(void *pixBuf, const int len)
  {
      jint *pixels = (jint *)pixBuf;
  
      for(int i = 0; i &lt; len; i++)
      {
          jint pixel = pixels[i];
  
          jint a = (pixel &gt;&gt; 24) &amp; 0xFF;
          jint r = (pixel &gt;&gt; 16) &amp; 0xFF;
          jint g = (pixel &gt;&gt;  8) &amp; 0xFF;
          jint b = (pixel &gt;&gt;  0) &amp; 0xFF;
  
          pixels[i] = (jint)(a | (r &lt;&lt; 24 ) | (g &lt;&lt; 16) | (b &lt;&lt; 8));
      }
  }
  </code></pre>
  
  <p>Or maybe there is another error? Not exactly sure about the <code>glTexImage2D</code> options to be honest.</p>
  
  <p>Thanks!</p>