# win32画线考虑去锯齿

这几天一直在研究win32 SDk下画线去锯齿，之前一直用的QT的画线接口函数，里面有去锯齿的效果，可是突然项目要求不能用QT的只能用win32 SDK下的GDI画线接口函数，可是显示的效果有锯齿，怎么办？只能研究下怎么去锯齿影响，因为GDI下没有去锯齿的处理，所以只能自己找算法处理。在网上找了一下

void RVS_drawLine_width(HDC hDC, int x1, int y1, int x2, int y2, COLORREF color)
{
// Calculate line params
int dx = (x2 - x1);
int dy = (y2 - y1);
COLORREF bgColor;
int temp;
float k;

// X-dominant line
if (abs(dx) > abs(dy))
{
// Ex-change line end points
if (dx < 0)
{
temp = x1;
x1 = x2;
x2 = temp;

temp = y1;
y1 = y2;
y2 = temp;
}
k = (float)dy / (float)dx;

int xs;
float yt = (float)y1;
float distance;
UCHAR red, green, blue;

for (xs=x1; xs<=x2; xs++)
{
distance = (float)(yt - (int)(yt));

bgColor = ::GetPixel(hDC, xs, (int)yt-1);
red = (UCHAR)(distance*GetRValue(bgColor)) + (UCHAR)((1.0f-distance)*GetRValue(color));
green = (UCHAR)(distance*GetGValue(bgColor)) + (UCHAR)((1.0f-distance)*GetGValue(color));
blue = (UCHAR)(distance*GetBValue(bgColor)) + (UCHAR)((1.0f-distance)*GetBValue(color));
::SetPixel(hDC, xs, (int)yt-1, RGB(red,green,blue));

//::SetPixel(hDC, xs, (int)yt-1, color);
::SetPixel(hDC, xs, (int)yt, color);

bgColor = ::GetPixel(hDC, xs, (int)yt+1);
red = (UCHAR)((1.0f-distance)*GetRValue(bgColor)) + (UCHAR)(distance*GetRValue(color));
green = (UCHAR)((1.0f-distance)*GetGValue(bgColor)) + (UCHAR)(distance*GetGValue(color));
blue = (UCHAR)((1.0f-distance)*GetBValue(bgColor)) + (UCHAR)(distance*GetBValue(color));
::SetPixel(hDC, xs, (int)yt+1, RGB(red,green,blue));

yt += k;
}

}
// Y-dominant line
else
{
// Ex-change line end points
if (dy < 0)
{
temp = x1;
x1 = x2;
x2 = temp;

temp = y1;
y1 = y2;
y2 = temp;
}
k = (float)dx / (float)dy;

int ys;
float xt = (float)x1;
float distance;
UCHAR red, green, blue;
for (ys=y1; ys<=y2; ys++)
{
distance = (float)(xt - (int)(xt));

bgColor = ::GetPixel(hDC, (int)xt-1, ys);
red = (UCHAR)(distance*GetRValue(bgColor)) + (UCHAR)((1.0f-distance)*GetRValue(color));
green = (UCHAR)(distance*GetGValue(bgColor)) + (UCHAR)((1.0f-distance)*GetGValue(color));
blue = (UCHAR)(distance*GetBValue(bgColor)) + (UCHAR)((1.0f-distance)*GetBValue(color));
::SetPixel(hDC, (int)xt-1, ys, RGB(red,green,blue));

//::SetPixel(hDC, (int)xt-1, ys, color);
::SetPixel(hDC, (int)xt, ys, color);

bgColor = ::GetPixel(hDC, (int)xt+1, ys);
red = (UCHAR)((1.0f-distance)*GetRValue(bgColor)) + (UCHAR)(distance*GetRValue(color));
green = (UCHAR)((1.0f-distance)*GetGValue(bgColor)) + (UCHAR)(distance*GetGValue(color));
blue = (UCHAR)((1.0f-distance)*GetBValue(bgColor)) + (UCHAR)(distance*GetBValue(color));
::SetPixel(hDC, (int)xt+1, ys, RGB(red,green,blue));

xt += k;
}
}

}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

geminimuchachas

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

01-25 977

06-18 3592
01-10 7594
04-16 60
06-26 2750
03-14 4568
09-11 3751
12-21 162
05-28 3296
12-26 630