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:
Valtteri Koskivuori 2021-05-09 22:19:04 +03:00 committed by Linus Groh
parent e60c0d675e
commit cb74f7992a
Notes: sideshowbarker 2024-07-18 18:21:06 +09:00
2 changed files with 21 additions and 0 deletions

View File

@ -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
{

View File

@ -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;