summaryrefslogtreecommitdiff
path: root/Libraries/LibGfx/Bitmap.cpp
diff options
context:
space:
mode:
authorHüseyin ASLITÜRK <asliturk@hotmail.com>2020-04-12 13:19:18 +0300
committerAndreas Kling <kling@serenityos.org>2020-04-12 18:08:11 +0200
commit8e9d031cb3327619982297fa878265692c23edb4 (patch)
tree7770030eb6289fc85070eff27ef8a34113d4b686 /Libraries/LibGfx/Bitmap.cpp
parentc6944f8cc21c7af8e9125497deb1ec826922ff98 (diff)
downloadserenity-8e9d031cb3327619982297fa878265692c23edb4.zip
LibGfx: Add Bitmap::rotated and Bitmap::flipped
Diffstat (limited to 'Libraries/LibGfx/Bitmap.cpp')
-rw-r--r--Libraries/LibGfx/Bitmap.cpp42
1 files changed, 42 insertions, 0 deletions
diff --git a/Libraries/LibGfx/Bitmap.cpp b/Libraries/LibGfx/Bitmap.cpp
index 962ea59ba9..3cbf6b2001 100644
--- a/Libraries/LibGfx/Bitmap.cpp
+++ b/Libraries/LibGfx/Bitmap.cpp
@@ -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)