LibGfx: Add Bitmap::rotated and Bitmap::flipped

This commit is contained in:
Hüseyin ASLITÜRK 2020-04-12 13:19:18 +03:00 committed by Andreas Kling
parent c6944f8cc2
commit 8e9d031cb3
Notes: sideshowbarker 2024-07-19 07:40:56 +09:00
2 changed files with 49 additions and 0 deletions

View file

@ -98,6 +98,48 @@ Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr<SharedBuffer>&& shared_buffer,
ASSERT(format != BitmapFormat::Indexed8);
}
NonnullRefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
{
auto w = this->width();
auto h = this->height();
auto new_bitmap = Gfx::Bitmap::create(this->format(), { h, w });
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Color color;
if (rotation_direction == Gfx::RotationDirection::Left)
color = this->get_pixel(w - i - 1, j);
else
color = this->get_pixel(i, h - j - 1);
new_bitmap->set_pixel(j, i, color);
}
}
return new_bitmap;
}
NonnullRefPtr<Gfx::Bitmap> Bitmap::flipped(Gfx::Orientation orientation) const
{
auto w = this->width();
auto h = this->height();
auto new_bitmap = Gfx::Bitmap::create(this->format(), { w, h });
for (int i = 0; i < w; i++) {
for (int j = 0; j < h; j++) {
Color color = this->get_pixel(i, j);
if (orientation == Orientation::Vertical)
new_bitmap->set_pixel(i, h - j - 1, color);
else
new_bitmap->set_pixel(w - i - 1, j, color);
}
}
return new_bitmap;
}
NonnullRefPtr<Bitmap> Bitmap::to_bitmap_backed_by_shared_buffer() const
{
if (m_shared_buffer)

View file

@ -42,6 +42,11 @@ enum class BitmapFormat {
Indexed8
};
enum RotationDirection {
Left,
Right
};
class Bitmap : public RefCounted<Bitmap> {
public:
static NonnullRefPtr<Bitmap> create(BitmapFormat, const Size&);
@ -50,6 +55,8 @@ public:
static RefPtr<Bitmap> load_from_file(const StringView& path);
static NonnullRefPtr<Bitmap> create_with_shared_buffer(BitmapFormat, NonnullRefPtr<SharedBuffer>&&, const Size&);
NonnullRefPtr<Gfx::Bitmap> rotated(Gfx::RotationDirection) const;
NonnullRefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const;
NonnullRefPtr<Bitmap> to_bitmap_backed_by_shared_buffer() const;
ShareableBitmap to_shareable_bitmap(pid_t peer_pid = -1) const;