Menu ▾ ▴

#102 Fixed PNG plugin handling of 16-bit grayscale + 16-bit alpha images

None
closed
None
5
2015-03-15
2014-12-16
No

Hi Hervé,

This is a very small fix to PluginPNG.cpp, to improve handling for 16-bit grayscale + 16-bit alpha PNG files. At present, FreeImage downsamples these images to 32-bit FIT_BITMAP format. I believe the proper handling is to expand the data to 64-bit RGBA16. LibPNG can do this internally, using the png_set_gray_to_rgb() function, which supports 8 or 16-bit grayscale sources.

Here is the fixed code, encompassing lines 341-363 of PluginPNG.cpp. The new lines are the final else if {} section, covering type PNG_COLOR_TYPE_GRAY_ALPHA:

        FREE_IMAGE_TYPE image_type = FIT_BITMAP;
        if (bit_depth == 16) {
            if ((pixel_depth == 16) && (color_type == PNG_COLOR_TYPE_GRAY)) {
                image_type = FIT_UINT16;
            } 
            else if ((pixel_depth == 48) && (color_type == PNG_COLOR_TYPE_RGB)) {
                image_type = FIT_RGB16;
            } 
            else if ((pixel_depth == 64) && (color_type == PNG_COLOR_TYPE_RGB_ALPHA)) {
                image_type = FIT_RGBA16;
            } 
            else if ((pixel_depth == 32) && (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)) {
                // tell libpng to expand 16 bit grayscale + 16 bit alpha to 64 bit RGBA
                png_set_gray_to_rgb(png_ptr);
                image_type = FIT_RGBA16;
                pixel_depth = 64;
            }
            else {
                // tell libpng to strip 16 bit/color files down to 8 bits/color
                png_set_strip_16(png_ptr);
                bit_depth = 8;
            }
        }

I tested this fix on the 32-bpp grayscale+alpha test images from this link, without trouble:

http://entropymine.com/jason/testbed/pngtrans/

Thank you!

Discussion

  • Hervé Drolon

    Hervé Drolon - 2015-02-19
    • status: open --> pending
    • assigned_to: Hervé Drolon
    • Group: -->
     
  • Hervé Drolon

    Hervé Drolon - 2015-02-19

    Hi Tanner,

    Thanks for the patch, it is now available in the CVS.

    Hervé

     

    Last edit: Hervé Drolon 2015-02-19
  • Hervé Drolon

    Hervé Drolon - 2015-03-15
    • status: pending --> closed
     
  • Hervé Drolon

    Hervé Drolon - 2015-03-15

    fixed in release 3.17.0

     

Anonymous
Anonymous

Add attachments
Cancel