防止GDI+ PNG 编码器将 Gamma 信息添加到 1 位 PNG

发布于 2024-09-28 17:09:47 字数 1163 浏览 7 评论 0原文

我想知道是否可以指示成像 PNG 编码器不向 1 位 PNG 添加任何伽玛和色度信息。

我正在为图像创建一个 2 调色板

ColorPalette* pal = (ColorPalette*)CoTaskMemAlloc(sizeof(ColorPalette) + 2 * sizeof(ARGB));
pal->Count = 2;
pal->Flags = 0;
pal->Entries[0] = MAKEARGB(0,0,0,0);
pal->Entries[1] = MAKEARGB(0,255,255,255);
if (FAILED(res = sink->SetPalette(pal))) {
  return res;
}
CoTaskMemFree(pal);

,然后

BitmapData bmData;
bmData.Height = bm.bmHeight;
bmData.Width = bm.bmWidth;
bmData.Scan0 = bm.bmBits;
bmData.PixelFormat = PixelFormat1bppIndexed;

UINT bitsPerLine = imageInfo.Width * bm.bmBitsPixel;
UINT bitAlignment = sizeof(LONG) * 8;
UINT bitStride = bitAlignment * (bitsPerLine / bitAlignment);    // The image buffer is always padded to LONG boundaries
if ((bitsPerLine % bitAlignment) != 0) bitStride += bitAlignment; // Add a bit more for the leftover values
  bmData.Stride = bitStride / 8;

if (FAILED(res = sink->PushPixelData(&rect, &bmData, TRUE))) {
  return res;
}

生成的 PNG 图像太大并且包含以下无用的标题:

sRGB、gAMA、cHRM

我实际上只期待 PLTE 而不是 sRGB。我如何设置编码器以跳过伽玛和色度计算?

I wonder if its possible to instruct the Imaging PNG Encoder not to add any gamma and chroma informations to a 1-bit PNG.

I am creating a 2 color palette for the image

ColorPalette* pal = (ColorPalette*)CoTaskMemAlloc(sizeof(ColorPalette) + 2 * sizeof(ARGB));
pal->Count = 2;
pal->Flags = 0;
pal->Entries[0] = MAKEARGB(0,0,0,0);
pal->Entries[1] = MAKEARGB(0,255,255,255);
if (FAILED(res = sink->SetPalette(pal))) {
  return res;
}
CoTaskMemFree(pal);

and then just

BitmapData bmData;
bmData.Height = bm.bmHeight;
bmData.Width = bm.bmWidth;
bmData.Scan0 = bm.bmBits;
bmData.PixelFormat = PixelFormat1bppIndexed;

UINT bitsPerLine = imageInfo.Width * bm.bmBitsPixel;
UINT bitAlignment = sizeof(LONG) * 8;
UINT bitStride = bitAlignment * (bitsPerLine / bitAlignment);    // The image buffer is always padded to LONG boundaries
if ((bitsPerLine % bitAlignment) != 0) bitStride += bitAlignment; // Add a bit more for the leftover values
  bmData.Stride = bitStride / 8;

if (FAILED(res = sink->PushPixelData(&rect, &bmData, TRUE))) {
  return res;
}

The resulting PNG image is way to large and contains the following useless headers:

sRGB,gAMA,cHRM

I was actually only expecting PLTE not sRGB. How do I have to setup the encoder to skip gamma and chroma calculations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

内心激荡 2024-10-05 17:09:47

我也有兴趣知道是否可能。我在 c++ 程序中使用 gdi+ 为网站生成 png,尽管我输入了完全相同的值,但 png 的颜色与 css 不同。通过删除 sRGB 内容,可以解决大多数浏览器中的伽玛问题。

我希望有一个解决方案!

I'm also interested to know if it's possible. I use gdi+ in a c++ program to generate png's for a website and the png's have different colors than the css although I put in the exact same values. By removing the sRGB stuff, that could solve the gamma problem in most browsers.

I hope there is a solution for this!

半枫 2024-10-05 17:09:47

我通过实现 FreeImage 库 (http://freeimage.sourceforge.net/) 解决了这个问题。

我使用 GDI+ 创建位图,锁定其像素数据,创建自由图像位图,也锁定它并复制像素。

然后我让 freeimage 将其保存为 PNG 格式,瞧...正确的伽马信息,在每个浏览器中都很好。

它的开销有点大(尽管我感觉 FreeImage 保存图像的速度比 GDI+ 快得多,从而使整体过程更快)。当然,您的项目还需要一个带有 dll 的额外库。

I have resolved this by implementing the FreeImage library (http://freeimage.sourceforge.net/).

I create the bitmap with GDI+, I lock its pixeldata, create a freeimage bitmap, lock it too and copy the pixels.

Then I make freeimage save it to a PNG and voila... correct gamma information, good in every browser.

It's a little bit more overhead (although I have a feeling that FreeImage saves the images much faster than GDI+, making the overal process even faster). But of course, you will need an extra library with dll with your project.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文