mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 05:27:43 +00:00
LibGfx: Clean up PathRasterizer
No functional changes; mainly cleaning up the code style and simplifying the code so I could understand it better.
This commit is contained in:
parent
8f736be711
commit
8dc55f5fda
1 changed files with 32 additions and 52 deletions
|
@ -1,5 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com>
|
* Copyright (c) 2020, Srimanta Barua <srimanta.barua1@gmail.com>
|
||||||
|
* Copyright (c) 2023, Jelle Raaijmakers <jelle@gmta.nl>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -11,10 +12,9 @@ namespace Gfx {
|
||||||
PathRasterizer::PathRasterizer(Gfx::IntSize size)
|
PathRasterizer::PathRasterizer(Gfx::IntSize size)
|
||||||
: m_size(size)
|
: m_size(size)
|
||||||
{
|
{
|
||||||
m_data.resize(m_size.width() * m_size.height());
|
m_data.resize(m_size.area());
|
||||||
for (int i = 0; i < m_size.width() * m_size.height(); i++) {
|
for (int i = 0; i < m_size.area(); i++)
|
||||||
m_data[i] = 0.0f;
|
m_data[i] = 0.f;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PathRasterizer::draw_path(Gfx::Path& path)
|
void PathRasterizer::draw_path(Gfx::Path& path)
|
||||||
|
@ -31,17 +31,11 @@ RefPtr<Gfx::Bitmap> PathRasterizer::accumulate()
|
||||||
auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
|
auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
|
||||||
Color base_color = Color::from_rgb(0xffffff);
|
Color base_color = Color::from_rgb(0xffffff);
|
||||||
for (int y = 0; y < m_size.height(); y++) {
|
for (int y = 0; y < m_size.height(); y++) {
|
||||||
float accumulator = 0.0;
|
float accumulator = 0.f;
|
||||||
for (int x = 0; x < m_size.width(); x++) {
|
for (int x = 0; x < m_size.width(); x++) {
|
||||||
accumulator += m_data[y * m_size.width() + x];
|
accumulator += m_data[y * m_size.width() + x];
|
||||||
float value = accumulator;
|
float value = AK::min(AK::abs(accumulator), 1.f);
|
||||||
if (value < 0.0f) {
|
u8 alpha = value * 255.f;
|
||||||
value = -value;
|
|
||||||
}
|
|
||||||
if (value > 1.0f) {
|
|
||||||
value = 1.0;
|
|
||||||
}
|
|
||||||
u8 alpha = value * 255.0f;
|
|
||||||
bitmap->set_pixel(x, y, base_color.with_alpha(alpha));
|
bitmap->set_pixel(x, y, base_color.with_alpha(alpha));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -51,46 +45,38 @@ RefPtr<Gfx::Bitmap> PathRasterizer::accumulate()
|
||||||
void PathRasterizer::draw_line(Gfx::FloatPoint p0, Gfx::FloatPoint p1)
|
void PathRasterizer::draw_line(Gfx::FloatPoint p0, Gfx::FloatPoint p1)
|
||||||
{
|
{
|
||||||
// FIXME: Shift x and y according to dy/dx
|
// FIXME: Shift x and y according to dy/dx
|
||||||
if (p0.x() < 0.0f) {
|
if (p0.x() < 0.f)
|
||||||
p0.set_x(roundf(p0.x()));
|
p0.set_x(roundf(p0.x()));
|
||||||
}
|
if (p0.y() < 0.f)
|
||||||
if (p0.y() < 0.0f) {
|
|
||||||
p0.set_y(roundf(p0.y()));
|
p0.set_y(roundf(p0.y()));
|
||||||
}
|
if (p1.x() < 0.f)
|
||||||
if (p1.x() < 0.0f) {
|
|
||||||
p1.set_x(roundf(p1.x()));
|
p1.set_x(roundf(p1.x()));
|
||||||
}
|
if (p1.y() < 0.f)
|
||||||
if (p1.y() < 0.0f) {
|
|
||||||
p1.set_y(roundf(p1.y()));
|
p1.set_y(roundf(p1.y()));
|
||||||
}
|
|
||||||
|
|
||||||
if (!(p0.x() >= 0.0f && p0.y() >= 0.0f && p0.x() <= m_size.width() && p0.y() <= m_size.height())) {
|
if (p0.x() < 0.f || p0.y() < 0.f || p0.x() > m_size.width() || p0.y() > m_size.height()) {
|
||||||
dbgln("!P0({},{})", p0.x(), p0.y());
|
dbgln("!P0({},{})", p0.x(), p0.y());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(p1.x() >= 0.0f && p1.y() >= 0.0f && p1.x() <= m_size.width() && p1.y() <= m_size.height())) {
|
if (p1.x() < 0.f || p1.y() < 0.f || p1.x() > m_size.width() || p1.y() > m_size.height()) {
|
||||||
dbgln("!P1({},{})", p1.x(), p1.y());
|
dbgln("!P1({},{})", p1.x(), p1.y());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
VERIFY(p0.x() >= 0.0f && p0.y() >= 0.0f && p0.x() <= m_size.width() && p0.y() <= m_size.height());
|
|
||||||
VERIFY(p1.x() >= 0.0f && p1.y() >= 0.0f && p1.x() <= m_size.width() && p1.y() <= m_size.height());
|
|
||||||
|
|
||||||
// If we're on the same Y, there's no need to draw
|
// If we're on the same Y, there's no need to draw
|
||||||
if (p0.y() == p1.y()) {
|
if (p0.y() == p1.y())
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
float direction = -1.0;
|
float direction = -1.f;
|
||||||
if (p1.y() < p0.y()) {
|
if (p1.y() < p0.y()) {
|
||||||
direction = 1.0;
|
direction = 1.f;
|
||||||
auto tmp = p0;
|
AK::swap(p0, p1);
|
||||||
p0 = p1;
|
|
||||||
p1 = tmp;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
float dxdy = (p1.x() - p0.x()) / (p1.y() - p0.y());
|
float const dxdy = (p1.x() - p0.x()) / (p1.y() - p0.y());
|
||||||
|
float const dydx = AK::abs(1.f / dxdy);
|
||||||
|
|
||||||
u32 y0 = floorf(p0.y());
|
u32 y0 = floorf(p0.y());
|
||||||
u32 y1 = ceilf(p1.y());
|
u32 y1 = ceilf(p1.y());
|
||||||
float x_cur = p0.x();
|
float x_cur = p0.x();
|
||||||
|
@ -98,12 +84,10 @@ void PathRasterizer::draw_line(Gfx::FloatPoint p0, Gfx::FloatPoint p1)
|
||||||
for (u32 y = y0; y < y1; y++) {
|
for (u32 y = y0; y < y1; y++) {
|
||||||
u32 line_offset = m_size.width() * y;
|
u32 line_offset = m_size.width() * y;
|
||||||
|
|
||||||
float dy = min(y + 1.0f, p1.y()) - max((float)y, p0.y());
|
float dy = AK::min(y + 1.f, p1.y()) - AK::max(static_cast<float>(y), p0.y());
|
||||||
float directed_dy = dy * direction;
|
float directed_dy = dy * direction;
|
||||||
float x_next = x_cur + dy * dxdy;
|
float x_next = x_cur + dy * dxdy;
|
||||||
if (x_next < 0.0f) {
|
x_next = AK::max(x_next, 0.f);
|
||||||
x_next = 0.0f;
|
|
||||||
}
|
|
||||||
float x0 = x_cur;
|
float x0 = x_cur;
|
||||||
float x1 = x_next;
|
float x1 = x_next;
|
||||||
if (x1 < x0) {
|
if (x1 < x0) {
|
||||||
|
@ -112,27 +96,23 @@ void PathRasterizer::draw_line(Gfx::FloatPoint p0, Gfx::FloatPoint p1)
|
||||||
}
|
}
|
||||||
float x0_floor = floorf(x0);
|
float x0_floor = floorf(x0);
|
||||||
float x1_ceil = ceilf(x1);
|
float x1_ceil = ceilf(x1);
|
||||||
u32 x0i = x0_floor;
|
u32 x0_floor_i = x0_floor;
|
||||||
|
|
||||||
if (x1_ceil <= x0_floor + 1.0f) {
|
if (x1_ceil <= x0_floor + 1.f) {
|
||||||
// If x0 and x1 are within the same pixel, then area to the right is (1 - (mid(x0, x1) - x0_floor)) * dy
|
// If x0 and x1 are within the same pixel, then area to the right is (1 - (mid(x0, x1) - x0_floor)) * dy
|
||||||
float area = ((x0 + x1) * 0.5f) - x0_floor;
|
float area = .5f * (x0 + x1) - x0_floor;
|
||||||
m_data[line_offset + x0i] += directed_dy * (1.0f - area);
|
m_data[line_offset + x0_floor_i] += directed_dy * (1.f - area);
|
||||||
m_data[line_offset + x0i + 1] += directed_dy * area;
|
m_data[line_offset + x0_floor_i + 1] += directed_dy * area;
|
||||||
} else {
|
} else {
|
||||||
float dydx = 1.0f / dxdy;
|
float x0_right = 1.f - (x0 - x0_floor);
|
||||||
if (dydx < 0)
|
|
||||||
dydx = -dydx;
|
|
||||||
|
|
||||||
float x0_right = 1.0f - (x0 - x0_floor);
|
|
||||||
u32 x1_floor_i = floorf(x1);
|
u32 x1_floor_i = floorf(x1);
|
||||||
float area_upto_here = 0.5f * x0_right * x0_right * dydx;
|
float area_upto_here = .5f * x0_right * x0_right * dydx;
|
||||||
m_data[line_offset + x0i] += direction * area_upto_here;
|
m_data[line_offset + x0_floor_i] += direction * area_upto_here;
|
||||||
for (u32 x = x0i + 1; x < x1_floor_i; x++) {
|
for (u32 x = x0_floor_i + 1; x < x1_floor_i; x++) {
|
||||||
m_data[line_offset + x] += direction * dydx;
|
m_data[line_offset + x] += direction * dydx;
|
||||||
area_upto_here += dydx;
|
area_upto_here += dydx;
|
||||||
}
|
}
|
||||||
float remaining_area = (dy - area_upto_here);
|
float remaining_area = dy - area_upto_here;
|
||||||
m_data[line_offset + x1_floor_i] += direction * remaining_area;
|
m_data[line_offset + x1_floor_i] += direction * remaining_area;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue