mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-13 11:42:38 +03:00
LibGfx: Implement Bitmap::cropped()
This cuts a region of a bitmap specified by the provided Gfx::IntRect and returns it. Areas outside of the bounds of the original bitmap are filled in with black.
This commit is contained in:
parent
e60c0d675e
commit
cb74f7992a
Notes:
sideshowbarker
2024-07-18 18:21:06 +09:00
Author: https://github.com/vkoskiv Commit: https://github.com/SerenityOS/serenity/commit/cb74f7992af Pull-request: https://github.com/SerenityOS/serenity/pull/6994 Reviewed-by: https://github.com/linusg
@ -498,6 +498,26 @@ RefPtr<Gfx::Bitmap> Bitmap::scaled(float sx, float sy) const
|
||||
return new_bitmap;
|
||||
}
|
||||
|
||||
RefPtr<Gfx::Bitmap> Bitmap::cropped(Gfx::IntRect crop) const
|
||||
{
|
||||
auto new_bitmap = Gfx::Bitmap::create(format(), { crop.width(), crop.height() }, 1);
|
||||
if (!new_bitmap)
|
||||
return nullptr;
|
||||
|
||||
for (int y = 0; y < crop.height(); ++y) {
|
||||
for (int x = 0; x < crop.width(); ++x) {
|
||||
int global_x = x + crop.left();
|
||||
int global_y = y + crop.top();
|
||||
if (global_x >= physical_width() || global_y >= physical_height() || global_x < 0 || global_y < 0) {
|
||||
new_bitmap->set_pixel(x, y, Gfx::Color::Black);
|
||||
} else {
|
||||
new_bitmap->set_pixel(x, y, get_pixel(global_x, global_y));
|
||||
}
|
||||
}
|
||||
}
|
||||
return new_bitmap;
|
||||
}
|
||||
|
||||
#ifdef __serenity__
|
||||
RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anon_fd() const
|
||||
{
|
||||
|
@ -117,6 +117,7 @@ public:
|
||||
RefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const;
|
||||
RefPtr<Gfx::Bitmap> scaled(int sx, int sy) const;
|
||||
RefPtr<Gfx::Bitmap> scaled(float sx, float sy) const;
|
||||
RefPtr<Gfx::Bitmap> cropped(Gfx::IntRect) const;
|
||||
RefPtr<Bitmap> to_bitmap_backed_by_anon_fd() const;
|
||||
ByteBuffer serialize_to_byte_buffer() const;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user