[MSPAINT] drawing.cpp: Refactor Erase, Replace and Airbrush

CORE-18867
This commit is contained in:
Katayama Hirofumi MZ 2023-06-14 16:44:54 +09:00
parent 6d9b7f77ad
commit 78c8df8393

View file

@ -116,46 +116,41 @@ Fill(HDC hdc, LONG x, LONG y, COLORREF color)
void
Erase(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2, COLORREF color, LONG radius)
{
LONG a, b;
HPEN oldPen;
HBRUSH oldBrush = (HBRUSH) SelectObject(hdc, CreateSolidBrush(color));
LONG cx = (x1 + x2) / 2, cy = (y1 + y2) / 2;
RECT rc = { cx - radius, cy - radius, cx + radius, cy + radius };
b = max(1, max(abs(x2 - x1), abs(y2 - y1)));
oldPen = (HPEN) SelectObject(hdc, CreatePen(PS_SOLID, 1, color));
for(a = 0; a <= b; a++)
Rectangle(hdc,
(x1 * (b - a) + x2 * a) / b - radius,
(y1 * (b - a) + y2 * a) / b - radius,
(x1 * (b - a) + x2 * a) / b + radius,
(y1 * (b - a) + y2 * a) / b + radius);
DeleteObject(SelectObject(hdc, oldBrush));
DeleteObject(SelectObject(hdc, oldPen));
HBRUSH hbr = ::CreateSolidBrush(color);
::FillRect(hdc, &rc, hbr);
::DeleteObject(hbr);
}
void
Replace(HDC hdc, LONG x1, LONG y1, LONG x2, LONG y2, COLORREF fg, COLORREF bg, LONG radius)
{
LONG a, b, x, y;
b = max(1, max(abs(x2 - x1), abs(y2 - y1)));
LONG cx = (x1 + x2) / 2, cy = (y1 + y2) / 2;
RECT rc = { cx - radius, cy - radius, cx + radius, cy + radius };
for(a = 0; a <= b; a++)
for(y = (y1 * (b - a) + y2 * a) / b - radius + 1;
y < (y1 * (b - a) + y2 * a) / b + radius + 1; y++)
for(x = (x1 * (b - a) + x2 * a) / b - radius + 1;
x < (x1 * (b - a) + x2 * a) / b + radius + 1; x++)
if (GetPixel(hdc, x, y) == fg)
SetPixel(hdc, x, y, bg);
for (LONG y = rc.top; y < rc.bottom; ++y)
{
for (LONG x = rc.left; x < rc.right; ++x)
{
if (::GetPixel(hdc, x, y) == fg)
::SetPixelV(hdc, x, y, bg);
}
}
}
void
Airbrush(HDC hdc, LONG x, LONG y, COLORREF color, LONG r)
{
LONG a, b;
for(b = -r; b <= r; b++)
for(a = -r; a <= r; a++)
if ((a * a + b * b <= r * r) && (rand() % 4 == 0))
SetPixel(hdc, x + a, y + b, color);
for (LONG dy = -r; dy <= r; dy++)
{
for (LONG dx = -r; dx <= r; dx++)
{
if ((dx * dx + dy * dy <= r * r) && (rand() % 4 == 0))
::SetPixelV(hdc, x + dx, y + dy, color);
}
}
}
void