MFC工程中GDI(非GDI+)画图反走样 抗锯齿 算法
我们先来看一下处理前后的效果图
处理前
处理后
在使用GDI画图时很容易就会出现锯齿,网上给出的方法是使用GDI+,虽然简单,但是限制了软件的移植。
在这向大家分享一套反走样的算法
代码
void DrawLine( CDC *pDC, int X0, int Y0, int X1, int Y1, COLORREF clrLine )
{
/* Make sure the line runs top to bottom */
if (Y0 > Y1)
{
int Temp = Y0; Y0 = Y1; Y1 = Temp;
Temp = X0; X0 = X1; X1 = Temp;
}
/* Draw the initial pixel, which is always exactly intersected by
the line and so needs no weighting */
pDC->SetPixel( X0, Y0, clrLine );
int XDir, DeltaX = X1 - X0;
if( DeltaX >= 0 )
{
XDir = 1;
}
else
{
XDir = -1;
DeltaX = 0 - DeltaX; /* make DeltaX positive */
}
/* Special-case horizontal, vertical, and diagonal lines, which
require no weighting because they go right through the center of
every pixel */
int DeltaY = Y1 - Y0;
if (DeltaY == 0)
{
/* Horizontal line */
while (DeltaX-- != 0)
{
X0 += XDir;
pDC->SetPixel( X0, Y0, clrLine );
}
return;
}
if (DeltaX == 0)
{
/* Vertical line */
do
{
Y0++;
pDC->SetPixel( X0, Y0, clrLine );
} while (--DeltaY != 0);
return;
}
if (DeltaX == DeltaY)
{
/* Diagonal line */
do
{
X0 += XDir;
Y0++;
pDC->SetPixel( X0, Y0, clrLine );
} while (--DeltaY != 0);
return;
}
unsigned short ErrorAdj;
unsigned short ErrorAccTemp,