From 8e9d031cb3327619982297fa878265692c23edb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20ASLIT=C3=9CRK?= Date: Sun, 12 Apr 2020 13:19:18 +0300 Subject: [PATCH] LibGfx: Add Bitmap::rotated and Bitmap::flipped --- Libraries/LibGfx/Bitmap.cpp | 42 +++++++++++++++++++++++++++++++++++++ Libraries/LibGfx/Bitmap.h | 7 +++++++ 2 files changed, 49 insertions(+) diff --git a/Libraries/LibGfx/Bitmap.cpp b/Libraries/LibGfx/Bitmap.cpp index 962ea59ba9d..3cbf6b2001b 100644 --- a/Libraries/LibGfx/Bitmap.cpp +++ b/Libraries/LibGfx/Bitmap.cpp @@ -98,6 +98,48 @@ Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr&& shared_buffer, ASSERT(format != BitmapFormat::Indexed8); } +NonnullRefPtr 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 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::to_bitmap_backed_by_shared_buffer() const { if (m_shared_buffer) diff --git a/Libraries/LibGfx/Bitmap.h b/Libraries/LibGfx/Bitmap.h index 7fdec97e65b..3340521a4b6 100644 --- a/Libraries/LibGfx/Bitmap.h +++ b/Libraries/LibGfx/Bitmap.h @@ -42,6 +42,11 @@ enum class BitmapFormat { Indexed8 }; +enum RotationDirection { + Left, + Right +}; + class Bitmap : public RefCounted { public: static NonnullRefPtr create(BitmapFormat, const Size&); @@ -50,6 +55,8 @@ public: static RefPtr load_from_file(const StringView& path); static NonnullRefPtr create_with_shared_buffer(BitmapFormat, NonnullRefPtr&&, const Size&); + NonnullRefPtr rotated(Gfx::RotationDirection) const; + NonnullRefPtr flipped(Gfx::Orientation) const; NonnullRefPtr to_bitmap_backed_by_shared_buffer() const; ShareableBitmap to_shareable_bitmap(pid_t peer_pid = -1) const;