libpng读取PNG8和PNG24的区别

来源:互联网 发布:网络兼职可靠吗 编辑:程序博客网 时间:2024/05/16 18:32

PNG8和PNG24最大的不同就在于透明度,PNG8只有一位存储透明度,PNG24有8位。这也就影响了PNG数据块的数据格式大小。在libpng中提供了检测设置的方法。

// expand any tRNS chunk data into a full alpha channelif (png_get_valid(pngPtr, infoPtr, PNG_INFO_tRNS)) {    png_set_tRNS_to_alpha(pngPtr);    LOGD("png_set_tRNS_to_alpha");}

这个检测来自于libpng官方教程,检测透明度如果不够,那么会补齐数据格式。另外libpng还提供了额外的一些检测和设置。

// force palette images to be expanded to 24-bit RGB// it may include alpha channelif (colorType == PNG_COLOR_TYPE_PALETTE) {    png_set_palette_to_rgb(pngPtr);    LOGD("png_set_palette_to_rgb");} // low-bit-depth grayscale images are to be expanded to 8 bitsif (colorType == PNG_COLOR_TYPE_GRAY && bitDepth < 8) {    png_set_expand_gray_1_2_4_to_8(pngPtr);    LOGD("png_set_expand_gray_1_2_4_to_8");} // expand any tRNS chunk data into a full alpha channelif (png_get_valid(pngPtr, infoPtr, PNG_INFO_tRNS)) {    png_set_tRNS_to_alpha(pngPtr);    LOGD("png_set_tRNS_to_alpha");} // reduce images with 16-bit samples to 8 bitsif (bitDepth == 16) {    png_set_strip_16(pngPtr);} // expand grayscale images to RGBif (colorType == PNG_COLOR_TYPE_GRAY || colorType == PNG_COLOR_TYPE_GRAY_ALPHA) {    png_set_gray_to_rgb(pngPtr);    LOGD("png_set_gray_to_rgb");}

所有的设置,都必须在png_read_update_info函数调用之前,才能起作用。

最后,关于png的一些信息获取,png_get_IHDR是针对32位图片的,那么16位图片就会有问题。我们需要用别的方法调用确保正确性。

/* Note that png_get_IHDR() returns 32-bit data into * the application's width and height variables. * This is an unsafe situation if these are 16-bit * variables */width     = png_get_image_width(pngPtr, infoPtr);height    = png_get_image_height(pngPtr, infoPtr);bitDepth  = png_get_bit_depth(pngPtr, infoPtr);colorType = png_get_color_type(pngPtr, infoPtr);


0 0