views:

657

answers:

3

I'm creating an openGL game and so far I have been using .pngs in the RGBA8888 format as texture sheets, but those are too memory hungry, and my app crashes frequently. I read in Apple's site that such format such be used just when too much quality is needed, and recommends to use RGBA4444 and RGBA5551 instead ( I already converted my textures to PVR but the quality loss is too great in most of the sprite sheets).

I only need to use GL_UNSIGNED_SHORT_5_5_5_1 or GL_UNSIGNED_SHORT_4_4_4_4 in my glTexImage2D call inside my texture loader class in order to load my textures, but I need to convert my texture sheets to RGBA4444 and RGBA5551, and I'm clueless about how could I achieve this.

+2  A: 

Using ImageMagick you can create RGBA4444 PNG files by running:

convert source.png -depth 4 destination.png

You can get ImageMagick from MacPorts.

pgb
That will truncate the bottom bits of pixel values, but the output from libpng (or whatever loader is in use) will still be 8 bit components. The question is about how to bit-pack the pixel component values into the OpenGL formats. There's plenty of sample code on the web to do that, it's just a bunch of bit-twiddling.
Andy Ross
@Andy: Can you point me to one or more of those examples ?
José Joel.
+2  A: 

You may consider using Imagination's PVRTexTool for Windows. It's specifically for creating PVR textures in every supported color format. It can create both PVRTC compressed textures (what you call "PVR") as well as uncompressed textures in 8888, 5551, 4444, etc.

However, it doesn't output PNGs (only PVRs) so your loading code would have change. Also, sometimes PVRs are much larger than PNGs because the pixels in PNGs are compressed with deflate compression.

Since you're most likely running OS X, you can use Darwine (now WineBottler) to run it (and other windows programs) on OS X.

You'll need to register as an Imagination developer before you can download PVRTexTool. Registration and the tool are both free.

Once you set it up, it's pretty painless and it gives you a decent GUI for working with PVRs.

Jon-Eric
A: 

Seriously? There are libraries to do this kind of conversion. But frankly, this is a bit of bit twiddling. There are libraries that use asm, or specialized SSE commands to accellerate this which will be fast, but its pretty easy to roll your own format converter in C/C++. Your basic process would be:

  • Given a buffer of RGBA8888 encoded values
  • Create a buffer big enough to hold the RGBA4444 or RGBA5551 values. In this case, its simple - half the size.
  • Loop over the source buffer, unpacking each component, and repacking into the destination format, and write it into the destination buffer.

    void* rgba8888_to_rgba4444(
      void* src, // IN, pointer to source buffer
      int cb)    // IN size of source buffer, in bytes
    {
      // this code assumes that a long is 4 bytes and short is 2.
      //on some compilers this isnt true
      int i;
      // compute the actual number of pixel elements in the buffer.
      int cpel = cb/4;
      unsigned long* psrc = (unsigned long*)src;
      // create the RGBA4444 buffer
      unsigned short* pdst = (unsigned short*)malloc(cpel*2);
      // convert every pixel
      for(i=0;i<cpel; i++)
      {
        // read a source pixel
        unsigned pel = psrc[i];
        // unpack the source data as 8 bit values
        unsigned r = p & 0xff;
        unsigned g = (pel >> 8) & 0xff;
        unsigned b = (pel >> 16) & 0xff; 
        unsigned a = (pel >> 24) & 0xff;
        //convert to 4 bit vales
        r >>= 4;
        g >>= 4;
        b >>= 4;
        a >>= 4;
        // and store
        pdst[i] = r | g >> 4  | b >> 8 | a >> 12;
      }
      return pdst;
    } 
    

The actual conversion loop I did very wastefully, the components can be extracted, converted and repacked in a single pass, making for far faster code. I did it this way to make the conversion explicit, and easy to change. Also, im not sure that I got the component order the right way around. So it might be b, r, g, a, but it shouldn't effect the result of the function as it repackes in the same order into the dest buffer.

Chris Becke