diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index cd4598e8c6..0d9cb01d19 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -498,6 +498,26 @@ RefPtr Bitmap::scaled(float sx, float sy) const return new_bitmap; } +RefPtr 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::to_bitmap_backed_by_anon_fd() const { diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h index be920a64ee..9a6cfee8a3 100644 --- a/Userland/Libraries/LibGfx/Bitmap.h +++ b/Userland/Libraries/LibGfx/Bitmap.h @@ -117,6 +117,7 @@ public: RefPtr flipped(Gfx::Orientation) const; RefPtr scaled(int sx, int sy) const; RefPtr scaled(float sx, float sy) const; + RefPtr cropped(Gfx::IntRect) const; RefPtr to_bitmap_backed_by_anon_fd() const; ByteBuffer serialize_to_byte_buffer() const;