GDI+:灰度 PNG 加载为 PixelFormat32bppARGB

GDI+: Grayscale PNG loaded as PixelFormat32bppARGB

我正在尝试在 Windows 7/64bits Pro 上使用 GDI+ 加载灰度 PNG 文件。但是,如果我使用从 Image::GetPixelFormat() 返回的值,它会告诉我像素格式是 PixelFormat32bppARGB.

如果我将此 PNG 转换为 JPG and/or TIFF,它现在告诉我像素格式现在是 PixelFormat8bppIndexed

查看这些值 here, does not tell much. However looking them here 的定义,表明 PixelFormat32bppARGB 实际上是 (10 | (32 << 8) | PixelFormatAlpha | PixelFormatGDI | PixelFormatCanonical).

的结果

我的问题是:

  1. 以下是什么意思:PixelFormatCanonical: Is in canonical format ?
  2. 我知道 GDI+ 可以自由地将单分量灰度图像表示为 32bpp ARGB、非预乘 alpha,但是我如何检查底层表示实际上是灰度图像?这将很有用,因为我只想从 ARGB 缓冲区复制一个组件。

以下是 PNG 文件中的一些信息,使用来自 UNIX shell 的 pnginfo 命令:

$ pnginfo input.png
input.png...
  Image Width: 2492 Image Length: 3508
  Bitdepth (Bits/Sample): 8
  Channels (Samples/Pixel): 1
  Pixel depth (Pixel Depth): 8
  Colour Type (Photometric Interpretation): GRAYSCALE 
  Image filter: Single row per byte filter 
  Interlacing: No interlacing 
  Compression Scheme: Deflate method 8, 32k window
  Resolution: 11811, 11811 (pixels per meter)
  FillOrder: msb-to-lsb
  Byte Order: Network (Big Endian)
  Number of text strings: 0 of 0

这甚至是 wine GDI+ 实施的问题。当前wine 1.6.2还认为我输入的PNG真的是:PixelFormat8bppIndexed.

1:您为标志链接的页面是来自 Windows CE 的页面,您是否确认 Windows SDK 中的标志实际上是相同的?

2:Image::GetPixelFormat告诉你Image对象的像素格式,不一定要和PNG文件一样。

页底describing the pixel formats有备注:

PixelFormat48bppRGB, PixelFormat64bppARGB, and PixelFormat64bppPARGB use 16 bits per color component (channel). Windows GDI+ version 1.0 can read 16-bits-per-channel images, but such images are converted to an 8-bits-per-channel format for processing, displaying, and saving.

在尝试了 GDI+ Image class 中所有可能的功能之后,我认为找到了解决方案。需要检索 flags from the Image and test against ImageFlagsColorSpaceGRAY

对于灰度 PNG:

  • 像素格式为:PixelFormat32bppARGB
  • ImageFlagsColorSpaceGRAY 已设置(ImageFlagsColorSpaceRGB 未设置

对于 RGB PNG:

  • 像素格式为:PixelFormat24bppRGB
  • ImageFlagsColorSpaceRGB 已设置(ImageFlagsColorSpaceGRAY 未设置

对于 RGBA PNG:

  • 像素格式为:PixelFormat32bppARGB
  • ImageFlagsColorSpaceRGB 已设置(ImageFlagsColorSpaceGRAY 未设置