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.
–
–
/* 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 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.