Reputation: 2731
I have a PNG image of 128x128 dimension. When I read its IHDR chunk with libpng it shows that the image has color type 3. The problem is I cant find anywhere what should be the texture format for this color type. I want to draw this image with OpenGL. But without correct texture format the image color is not what it should be. And also If any reference could be provided where I can read detail about this matter will be greatly appreciated.
I use this method to set texture format for other color types
inline void GetPNGtextureInfo (int color_type,GLuint *format)
{
switch (color_type)
{
case PNG_COLOR_TYPE_GRAY:
*format = GL_LUMINANCE;
break;
case PNG_COLOR_TYPE_GRAY_ALPHA:
*format = GL_LUMINANCE_ALPHA;
break;
case PNG_COLOR_TYPE_RGB:
*format = GL_RGB;
break;
case PNG_COLOR_TYPE_RGB_ALPHA:
*format = GL_RGBA;
break;
default:
break;
}
}
Upvotes: 0
Views: 1827
Reputation: 16582
The palette is just an array of colours, and the image data is just indices into that array.
So if you want to convert your image to an RGB image, allocate a new buffer large enough for your image in that format, and fill it by taking the index for the pixel from the source image, index the palette with that value, and store the RGB value you get into the target image.
You may find a flavour of GL which has support for paletted textures, and can load them directly, but hardware support is less common these days, and the chances are all you're doing is offloading work to the driver, which will convert your texture to 24-bit.
For example, OpenGL ES supports some types of palettised texture through the glCompressedTexImage2D()
function, but it's entirely possible that this will simply place a burden on the implementation to convert those textures to something the hardware can handle.
Unless you have storage space concerns, I'd favour offline conversion (i.e. save your images out as 24-bit in the first place), but it's not technically difficult either way.
Upvotes: 1