From 16ebbde26fd66da1e5a78ddd3a8b71d7df14eec7 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 12 Sep 2020 12:20:34 +0100 Subject: [PATCH] LibGfx: add Bitmap::clone() method --- Libraries/LibGfx/Bitmap.cpp | 19 +++++++++++++++++++ Libraries/LibGfx/Bitmap.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/Libraries/LibGfx/Bitmap.cpp b/Libraries/LibGfx/Bitmap.cpp index e7fcfbbaf6..b5e8a8164c 100644 --- a/Libraries/LibGfx/Bitmap.cpp +++ b/Libraries/LibGfx/Bitmap.cpp @@ -182,6 +182,25 @@ Bitmap::Bitmap(BitmapFormat format, NonnullRefPtr&& shared_buffer, allocate_palette_from_format(m_format, palette); } +RefPtr Bitmap::clone() const +{ + RefPtr new_bitmap {}; + if (m_purgeable) { + new_bitmap = Bitmap::create_purgeable(format(), size()); + } else { + new_bitmap = Bitmap::create(format(), size()); + } + + if (!new_bitmap) { + return nullptr; + } + + ASSERT(size_in_bytes() == new_bitmap->size_in_bytes()); + memcpy(new_bitmap->scanline(0), scanline(0), size_in_bytes()); + + return new_bitmap; +} + RefPtr Bitmap::rotated(Gfx::RotationDirection rotation_direction) const { auto w = this->width(); diff --git a/Libraries/LibGfx/Bitmap.h b/Libraries/LibGfx/Bitmap.h index 022c841b80..3a514543f5 100644 --- a/Libraries/LibGfx/Bitmap.h +++ b/Libraries/LibGfx/Bitmap.h @@ -103,6 +103,8 @@ public: return false; } + RefPtr clone() const; + RefPtr rotated(Gfx::RotationDirection) const; RefPtr flipped(Gfx::Orientation) const; RefPtr to_bitmap_backed_by_shared_buffer() const;