mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00: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
2 changed files with 21 additions and 0 deletions
|
@ -498,6 +498,26 @@ RefPtr<Gfx::Bitmap> Bitmap::scaled(float sx, float sy) const
|
||||||
return new_bitmap;
|
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__
|
#ifdef __serenity__
|
||||||
RefPtr<Bitmap> Bitmap::to_bitmap_backed_by_anon_fd() const
|
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> flipped(Gfx::Orientation) const;
|
||||||
RefPtr<Gfx::Bitmap> scaled(int sx, int sy) const;
|
RefPtr<Gfx::Bitmap> scaled(int sx, int sy) const;
|
||||||
RefPtr<Gfx::Bitmap> scaled(float sx, float 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;
|
RefPtr<Bitmap> to_bitmap_backed_by_anon_fd() const;
|
||||||
ByteBuffer serialize_to_byte_buffer() const;
|
ByteBuffer serialize_to_byte_buffer() const;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue