LibGfx: Allow specifying outside color in Bitmap::cropped()

This is required for LibWeb bitmap cropping.
This commit is contained in:
Idan Horowitz 2025-08-04 14:33:30 +03:00 committed by Jelle Raaijmakers
commit 3c3da1ce8c
Notes: github-actions[bot] 2025-08-04 21:40:34 +00:00
2 changed files with 3 additions and 3 deletions

View file

@ -158,7 +158,7 @@ void Bitmap::apply_mask(Gfx::Bitmap const& mask, MaskKind mask_kind)
} }
} }
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::cropped(Gfx::IntRect crop) const ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::cropped(Gfx::IntRect crop, Gfx::Color outside_color) const
{ {
auto new_bitmap = TRY(Gfx::Bitmap::create(format(), alpha_type(), { crop.width(), crop.height() })); auto new_bitmap = TRY(Gfx::Bitmap::create(format(), alpha_type(), { crop.width(), crop.height() }));
@ -167,7 +167,7 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::cropped(Gfx::IntRect crop) const
int global_x = x + crop.left(); int global_x = x + crop.left();
int global_y = y + crop.top(); int global_y = y + crop.top();
if (global_x >= width() || global_y >= height() || global_x < 0 || global_y < 0) { if (global_x >= width() || global_y >= height() || global_x < 0 || global_y < 0) {
new_bitmap->set_pixel(x, y, Gfx::Color::Black); new_bitmap->set_pixel(x, y, outside_color);
} else { } else {
new_bitmap->set_pixel(x, y, get_pixel(global_x, global_y)); new_bitmap->set_pixel(x, y, get_pixel(global_x, global_y));
} }

View file

@ -73,7 +73,7 @@ public:
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> clone() const; ErrorOr<NonnullRefPtr<Gfx::Bitmap>> clone() const;
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect) const; ErrorOr<NonnullRefPtr<Gfx::Bitmap>> cropped(Gfx::IntRect, Gfx::Color outside_color = Gfx::Color::Black) const;
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_backed_by_anonymous_buffer() const; ErrorOr<NonnullRefPtr<Gfx::Bitmap>> to_bitmap_backed_by_anonymous_buffer() const;
[[nodiscard]] ShareableBitmap to_shareable_bitmap() const; [[nodiscard]] ShareableBitmap to_shareable_bitmap() const;