Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm a little confused after running png gauntlet on some png texture assets. Previously all of them were either PNG_COLOR_TYPE_RGB or PNG_COLOR_TYPE_RGBA and therefore mapping them to the appropriate GL color format was simple enough. Full disclosure, I'm not a graphics master.

After running png gauntlet to optimize the textures, calling png_get_IHDR() returns color type PNG_COLOR_MASK_ALPHA and I'm a little confused as to whether or not this can map directly to a color format that I use when passing to glTexImage2D() .

The particular texture image that behaves this way is a sprite mostly composed of whites and grays with varying alpha values. My best guess here is that the optimization determined it was some kind of gray scale with alpha and therefore I need to detect this in my texture loading code.

I would think that perhaps the appropriate color type would be GL_LUMINANCE_ALPHA , since it's the one that uses the luminance value as the R,G, and B components, which would essentially be gray scale with an alpha channel.

I believe GL_LUMINANCE_ALPHA would work, however, it has been deprecated on OpenGL version 3 and above. In such case, you can probably replace it by GL_RG . Which version are you targeting? glampert Aug 18, 2014 at 21:43 Thanks, this code isn't changing from ES 2 any time soon, so I think GL_LUMINANCE_ALPHA should do the trick, but I'll need to give it a shot. Thanks for the heads up on the deprecation. Joey Carson Aug 19, 2014 at 4:36
/* color type masks */
#define PNG_COLOR_MASK_PALETTE    1
#define PNG_COLOR_MASK_COLOR      2
#define PNG_COLOR_MASK_ALPHA      4
/* color types.  Note that not all combinations are legal */
#define PNG_COLOR_TYPE_GRAY 0
#define PNG_COLOR_TYPE_PALETTE  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_PALETTE)
#define PNG_COLOR_TYPE_RGB        (PNG_COLOR_MASK_COLOR)
#define PNG_COLOR_TYPE_RGB_ALPHA  (PNG_COLOR_MASK_COLOR | PNG_COLOR_MASK_ALPHA)
#define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA) /* aliases */
#define PNG_COLOR_TYPE_RGBA  PNG_COLOR_TYPE_RGB_ALPHA

These relate to the color types defined in the PNG specification, which are

0: Gray (1 channel)
2: RGB  (3 channels)
3: color palette (1 channel)
4: Gray-alpha (2 channels)
6: RGBA (4 channels)

In glTeximage2D version 1.1, PNG colortype 0 is equivalent to GL_LUMINANCE_ALPHA. In glTexImage2D version 3 PNG colortype 0 does not have a direct equivalence. The one-channel GL format is GL_RED, but that means the samples are only put in the red channel while the green and blue channels are set to 0.

The safest way to handle the conversion, I think, is to use (after png_get_IHDR(...))

png_set_gray_to_rgb(png_ptr);
png_set_palette_to_rgb(png_ptr);
png_read_update_info(png_ptr);

This will guarantee that you only receive colortype 2 (RGB) or 6 (RGBA), both of which have direct equivalence to GL formats GL_RGB or GL_RGBA in either glTexImage2D version 1.1 or version 3. In particular, png_set_gray_to_rgb() simply copies the gray channel into the green and blue channels, and uses the gray channel as the red channel.

Thanks for your answer. Yes I realize what they mean. The question was more related to how it equates to the color format used with glTexImage2D(). The documentation for glTexImage2D() sounds like GL_LUMINANCE_ALPHA is the associated color, but I wanted to ask around in case anyone knew for sure. – Joey Carson Aug 19, 2014 at 4:37

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.