From 81a3ec0692ebab6eb378e6eedae84d71d0d6c29b Mon Sep 17 00:00:00 2001 From: MacDue Date: Wed, 15 Jun 2022 21:05:58 +0100 Subject: [PATCH] LibGfx: Add Painter::get_pixel() This is required to do some painting tricks in LibWeb, this has to be added to the painter so it can apply the paint translation first. --- Userland/Libraries/LibGfx/Painter.cpp | 29 ++++++++++++++++++--------- Userland/Libraries/LibGfx/Painter.h | 1 + 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibGfx/Painter.cpp b/Userland/Libraries/LibGfx/Painter.cpp index 73a56467dc9..418f216dbbf 100644 --- a/Userland/Libraries/LibGfx/Painter.cpp +++ b/Userland/Libraries/LibGfx/Painter.cpp @@ -1205,37 +1205,37 @@ void Painter::draw_scaled_bitmap(IntRect const& a_dst_rect, Gfx::Bitmap const& s if (source.has_alpha_channel() || opacity != 1.0f) { switch (source.format()) { case BitmapFormat::BGRx8888: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; case BitmapFormat::BGRA8888: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; case BitmapFormat::Indexed8: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; case BitmapFormat::Indexed4: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; case BitmapFormat::Indexed2: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; case BitmapFormat::Indexed1: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; default: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; } } else { switch (source.format()) { case BitmapFormat::BGRx8888: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; case BitmapFormat::Indexed8: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; default: - do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, get_pixel, opacity, scaling_mode); + do_draw_scaled_bitmap(*m_target, dst_rect, clipped_rect, source, src_rect, Gfx::get_pixel, opacity, scaling_mode); break; } } @@ -1725,6 +1725,15 @@ void Painter::set_pixel(IntPoint const& p, Color color, bool blend) } } +Optional Painter::get_pixel(IntPoint const& p) +{ + auto point = p; + point.translate_by(state().translation); + if (!clip_rect().contains(point / scale())) + return {}; + return Color::from_argb(m_target->scanline(point.y())[point.x()]); +} + ALWAYS_INLINE void Painter::set_physical_pixel_with_draw_op(u32& pixel, Color const& color) { // This always sets a single physical pixel, independent of scale(). diff --git a/Userland/Libraries/LibGfx/Painter.h b/Userland/Libraries/LibGfx/Painter.h index dcad9b10ca0..9b5f0012dde 100644 --- a/Userland/Libraries/LibGfx/Painter.h +++ b/Userland/Libraries/LibGfx/Painter.h @@ -59,6 +59,7 @@ public: void draw_ellipse_intersecting(IntRect const&, Color, int thickness = 1); void set_pixel(IntPoint const&, Color, bool blend = false); void set_pixel(int x, int y, Color color, bool blend = false) { set_pixel({ x, y }, color, blend); } + Optional get_pixel(IntPoint const&); void draw_line(IntPoint const&, IntPoint const&, Color, int thickness = 1, LineStyle style = LineStyle::Solid, Color alternate_color = Color::Transparent); void draw_triangle_wave(IntPoint const&, IntPoint const&, Color color, int amplitude, int thickness = 1); void draw_quadratic_bezier_curve(IntPoint const& control_point, IntPoint const&, IntPoint const&, Color, int thickness = 1, LineStyle style = LineStyle::Solid);