使用GDI +绘制PixelFormat32bppPARGB图像时,使用的是常规公式,而不是预乘的公式 [英] Drawing PixelFormat32bppPARGB images with GDI+ uses conventional formula instead of premultiplied one

查看:208
本文介绍了使用GDI +绘制PixelFormat32bppPARGB图像时,使用的是常规公式,而不是预乘的公式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是显示问题的最少代码:

Here is some minimal code to show an issue:

static const int MAX_WIDTH = 320;
static const int MAX_HEIGHT = 320;

Gdiplus::Bitmap foregroundImg(MAX_WIDTH,MAX_HEIGHT,PixelFormat32bppPARGB);
{
    Gdiplus::Graphics g(&foregroundImg);
    g.Clear(Gdiplus::Color(10,255,255,255));
}

Gdiplus::Bitmap softwareBitmap(MAX_WIDTH,MAX_HEIGHT,PixelFormat32bppPARGB);
Gdiplus::Graphics g(&softwareBitmap);
g.SetCompositingMode(Gdiplus::CompositingModeSourceOver);
g.SetCompositingQuality(Gdiplus::CompositingQualityDefault);

g.Clear(Gdiplus::Color(255,0,0,0));

g.DrawImage(foregroundImg,0,0);

CLSID encoder;
GetEncoderClsid(L"image/png",&encoder);
softwareBitmap.Save(L"d:\\image.png",&encoder);

结果是我正在用等于10的RGB值填充图像.看来GDI +使用的是常规算法:

As result I'm getting image filled by RGB values equals to 10. It seems GDI+ uses the conventional algorithm:

255 *(10/255)+ 0 *(1-10/255)== 10.

255*(10/255) + 0*(1-10/255) == 10.

但是我期望将使用预乘算法(因为前景图像具有预乘PixelFormat32bppPARGB格式):

But I'm expecting that premultiplied algorithm will be used (because foreground image has the premultiplied PixelFormat32bppPARGB format):

255 + 0 *(1-10/255)== 255

255 + 0*(1-10/255) == 255

所以我的问题是,当图像为预乘alpha格式时,为什么GDI +使用常规公式?是否有任何解决方法可以使GDI +使用预乘alpha算法?

So my question, why GDI+ uses conventional formula when image is in premultiplied alpha format? And is there any workaround to make GDI+ to use the premultiplied alpha algorithm?

推荐答案

前景图像的格式无关紧要(假设它具有alpha值),因为您将其设置为Gdiplus :: Color.颜色值定义为未预乘,因此gdiplus在清除前景图像时会将分量乘以alpha值.另一种选择是使Color值具有不同的含义,具体取决于渲染目标的格式,而这就是疯狂.

The format of your foreground image doesn't matter (given that it has alpha) because you're setting it to a Gdiplus::Color. Color values are defined as non-premultiplied, so gdiplus multiplies the components by the alpha value when it clears the foreground image. The alternative would be for Color values to have different meaning depending on the format of the render target, and that way lies madness.

您可能可以通过直接设置源图像位来完成您打算做的事情,或者您可能做不到.值大于100%的组件在gdiplus的渲染模型中并不是真正有效的,因此如果在渲染过程中将它们加帽,我也不会感到惊讶.如果您确实希望对渲染进行这种级别的控制,则必须锁定位图位并自己完成.

You might be able to do what you intend by setting the source image bits directly, or you might not. Components with values greater than 100% aren't really valid in gdiplus's rendering model, so I'd not be surprised if it caps them during rendering. If you really want this level of control over the rendering, you'll have to lock the bitmap bits and do it yourself.

这篇关于使用GDI +绘制PixelFormat32bppPARGB图像时,使用的是常规公式,而不是预乘的公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆