win32画线考虑去锯齿

整理日: 2015年2月16日

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

在网上找了一下
http://www.codeproject.com/KB/GDI/CTGraphics.aspx
http://www.codeproject.com/KB/GDI/AntiAliasing.aspx
http://www.codeproject.com/KB/GDI-plus/AntiAliasingIssues.aspx
反走样技术之一 Wu像素
http://dev.gameres.com/Program/Visual/3D/Antialiasing.htm
http://dev.gameres.com/Program/Visual/Effects/WuLine.htm

什么是防锯齿技术?什么是亚像素? - Apple4.us

可是这些方法的画线处理不能设置线宽,我的画线处理需要线宽,按照网上找到的去锯齿的代码,我改了下代码(见后),修改的代码并不能灵活设置的线宽,两个端点处理也需要完善,最主要的是我不知道怎么去处理两个端点。搞了好几天,还是没有找到端点处理的合适方式。

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;
        }
    }

}
posted @ 2015-02-16 22:54  wanglinqiang  阅读(352)  评论(0编辑  收藏  举报