首页 > 解决方案 > libpng 在什么颜色空间中传递 png_read_image() 中的像素?

问题描述

我的目标是加载 PNG 文件以供查看。我做了以下仪式:

      png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
                   &color_type, &interlace_type, NULL, NULL);

      if (bit_depth == 16)
        {
          /* strip it down to 8-bit */
          png_set_strip_16(png_ptr);
        }

      /* we do not want interlaced image data */
      png_set_interlace_handling(png_ptr);

      if (color_type == PNG_COLOR_TYPE_PALETTE)
        {
          /* we need raw color values, no palette */
          png_set_palette_to_rgb(png_ptr);
        }

      if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
        {
          /* always expand 1, 2 or 4 bit grey maps to full 8-bits */
          png_set_expand_gray_1_2_4_to_8(png_ptr);
        }

      if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
        {
          /* realize transparency in the alpha */
          png_set_tRNS_to_alpha(png_ptr);
        }

      if (color_type == PNG_COLOR_TYPE_RGB
          || color_type == PNG_COLOR_TYPE_GRAY
          || color_type == PNG_COLOR_TYPE_PALETTE)
        {
          /* if the color type does not have an alpha, make it have an alpha of 0xFF */
          png_set_add_alpha(png_ptr, 0xFF, PNG_FILLER_AFTER);
        }

      if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
        {
          /* expand grey to RGB */
          png_set_gray_to_rgb(png_ptr);
        }

我猜测 usingpng_read_image()将提供 sRGB 中的像素,但我不知道。我也看到了函数png_set_sRGB(),但我那是写PNG文件的时候。

标签: libpng

解决方案


推荐阅读