Creating and loading .pngs in RGBA4444 RGBA5551 for openGL

蹲街弑〆低调 提交于 2020-01-02 09:11:00

问题


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.


回答1:


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.




回答2:


Using ImageMagick you can create RGBA4444 PNG files by running:

convert source.png -depth 4 destination.png

You can get ImageMagick from MacPorts.




回答3:


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.




回答4:


You might also want to look how to optimize RGBA8888 for conversion to RGBA4444 using floyd-steinberg dithering in GIMP: http://www.youtube.com/watch?v=v1xGYecsnX0




回答5:


You could also use http://www.texturepacker.com for conversion.




回答6:


Here is an optimized in-place conversion of Chris' code which should run 2x as fast but is not as strait forward. The in-place conversion helps to avoid crashes by lowering the memory spike. Just thought I'd share in case anyone was planning on using this code. I've tested it and it works great:

void* rgba8888_to_rgba4444( void* src, // IN, pointer to source buffer
                           int cb)    // IN size of source buffer, in bytes
{
    int i;
    // compute the actual number of pixel elements in the buffer.
    int cpel = cb/4;
    unsigned long* psrc = (unsigned long*)src;
    unsigned short* pdst = (unsigned short*)src;

    // 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 = (pel << 8)  & 0xf000;
        unsigned g = (pel >> 4) & 0x0f00;
        unsigned b = (pel >> 16) & 0x00f0;
        unsigned a = (pel >> 28) & 0x000f;

        // and store
        pdst[i] = r | g | b | a;
    }
    return pdst;
}


来源:https://stackoverflow.com/questions/2008842/creating-and-loading-pngs-in-rgba4444-rgba5551-for-opengl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!