0
votes

I have PNG files and they all have RGBA format. The difference is that some of them are RGBA8888 and some of them are RGBA4444. After some digging, it seems that if pixel format in the PNG is RGBA4444, libpng automatically converts it to RGBA8888. The manual says that if the pixel format is RGBA, the depth will only be 8 bits.

 PNG_COLOR_TYPE_GRAY
    (bit depths 1, 2, 4, 8, 16)
 PNG_COLOR_TYPE_GRAY_ALPHA
    (bit depths 8, 16)
 PNG_COLOR_TYPE_PALETTE
    (bit depths 1, 2, 4, 8)
 PNG_COLOR_TYPE_RGB
    (bit_depths 8, 16)
 PNG_COLOR_TYPE_RGB_ALPHA
    (bit_depths 8, 16)

Is there a way to retrieve the original bit depth that is defined in the file? I need to be able to tell which image has which channel depth so that I can load them using different format into OpenGL.

1
considered something simple as econding that information in file name? another alternative is using a color istogram do decide real color depth of the image. If you ask png to decode into 8888 it will do that regardless of original colordepth, since you need that for GPU, then you need to decode already in the most suitable format, - CoffeDeveloper
UPDATE: Seems I got a little bit confused. The image I have is not 4 bit PNG image, but 8bit/channel. The reason I thought it was 4bit png was that the file became much smaller when I changed export options from RGBA8888 to RGBA4444. - Kristupas A.

1 Answers

0
votes

Use libpng's png_get_IHDR() or png_get_bit_depth() to find out the image's depth (bits/sample) and png_get_color_type() to find out the color type. For an RGBA8888 image, these would return bit_depth = 8 and color_type = 6 (RGBA).

There's no RGBA4444 PNG format. PNG can, however, store truecolor-with-alpha images in PALETTE form, with 4-bit indices into a palette of RGB colors, with the alpha channel carried in the tRNS chunk, or, as you observed, in RGBA8888 form. Use the libpng functions mentioned above, or use any tool to look at the contents of the IHDR chunk near the beginning of the PNG file to find out the bit depth and color type.