summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-11-06 11:52:35 +0100
committerAndreas Kling <kling@serenityos.org>2021-11-08 00:35:27 +0100
commit2da4cfcc80b5fcd322c5194a829b76571616e5b4 (patch)
treebe401c53feed6f52e74033c02879543ef1f9fd12
parentc417820bff1b24843359800b41bcec8c8fbd5788 (diff)
downloadserenity-2da4cfcc80b5fcd322c5194a829b76571616e5b4.zip
LibGfx: Use ErrorOr<T> for Bitmap::clone()
-rw-r--r--Userland/Applications/PixelPaint/Layer.cpp6
-rw-r--r--Userland/Libraries/LibCards/Card.cpp3
-rw-r--r--Userland/Libraries/LibGUI/FileIconProvider.cpp5
-rw-r--r--Userland/Libraries/LibGfx/Bitmap.cpp10
-rw-r--r--Userland/Libraries/LibGfx/Bitmap.h2
-rw-r--r--Userland/Libraries/LibGfx/GIFLoader.cpp8
6 files changed, 23 insertions, 11 deletions
diff --git a/Userland/Applications/PixelPaint/Layer.cpp b/Userland/Applications/PixelPaint/Layer.cpp
index c0be453314..e0ca6ff023 100644
--- a/Userland/Applications/PixelPaint/Layer.cpp
+++ b/Userland/Applications/PixelPaint/Layer.cpp
@@ -39,7 +39,11 @@ RefPtr<Layer> Layer::try_create_with_bitmap(Image& image, NonnullRefPtr<Gfx::Bit
RefPtr<Layer> Layer::try_create_snapshot(Image& image, Layer const& layer)
{
- auto snapshot = try_create_with_bitmap(image, *layer.bitmap().clone(), layer.name());
+ auto new_bitmap_or_error = layer.bitmap().clone();
+ if (new_bitmap_or_error.is_error())
+ return nullptr;
+
+ auto snapshot = try_create_with_bitmap(image, new_bitmap_or_error.release_value_but_fixme_should_propagate_errors(), layer.name());
/*
We set these properties directly because calling the setters might
notify the image of an update on the newly created layer, but this
diff --git a/Userland/Libraries/LibCards/Card.cpp b/Userland/Libraries/LibCards/Card.cpp
index 2c27912d86..78b53a5361 100644
--- a/Userland/Libraries/LibCards/Card.cpp
+++ b/Userland/Libraries/LibCards/Card.cpp
@@ -172,8 +172,7 @@ void Card::clear_and_draw(GUI::Painter& painter, const Color& background_color)
NonnullRefPtr<Gfx::Bitmap> Card::invert_bitmap(Gfx::Bitmap& bitmap)
{
- auto inverted_bitmap = bitmap.clone();
- VERIFY(inverted_bitmap);
+ auto inverted_bitmap = bitmap.clone().release_value_but_fixme_should_propagate_errors();
for (int y = 0; y < inverted_bitmap->height(); y++) {
for (int x = 0; x < inverted_bitmap->width(); x++) {
inverted_bitmap->set_pixel(x, y, inverted_bitmap->get_pixel(x, y).inverted());
diff --git a/Userland/Libraries/LibGUI/FileIconProvider.cpp b/Userland/Libraries/LibGUI/FileIconProvider.cpp
index 4345ece13b..302b0f2590 100644
--- a/Userland/Libraries/LibGUI/FileIconProvider.cpp
+++ b/Userland/Libraries/LibGUI/FileIconProvider.cpp
@@ -239,11 +239,12 @@ Icon FileIconProvider::icon_for_path(const String& path, mode_t mode)
auto& emblem = size < 32 ? *s_symlink_emblem_small : *s_symlink_emblem;
auto original_bitmap = target_icon.bitmap_for_size(size);
VERIFY(original_bitmap);
- auto generated_bitmap = original_bitmap->clone();
- if (!generated_bitmap) {
+ auto generated_bitmap_or_error = original_bitmap->clone();
+ if (generated_bitmap_or_error.is_error()) {
dbgln("Failed to clone {}x{} icon for symlink variant", size, size);
return s_symlink_icon;
}
+ auto generated_bitmap = generated_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
GUI::Painter painter(*generated_bitmap);
painter.blit({ size - emblem.width(), size - emblem.height() }, emblem, emblem.rect());
diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp
index 52fde5c20c..5090434917 100644
--- a/Userland/Libraries/LibGfx/Bitmap.cpp
+++ b/Userland/Libraries/LibGfx/Bitmap.cpp
@@ -332,17 +332,19 @@ Bitmap::Bitmap(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize&
allocate_palette_from_format(m_format, palette);
}
-RefPtr<Gfx::Bitmap> Bitmap::clone() const
+ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Bitmap::clone() const
{
auto new_bitmap = Bitmap::try_create(format(), size(), scale());
- if (!new_bitmap)
- return nullptr;
+ if (!new_bitmap) {
+ // FIXME: Propagate the *real* error, once we have it.
+ return Error::from_errno(ENOMEM);
+ }
VERIFY(size_in_bytes() == new_bitmap->size_in_bytes());
memcpy(new_bitmap->scanline(0), scanline(0), size_in_bytes());
- return new_bitmap;
+ return new_bitmap.release_nonnull();
}
RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const
diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h
index 223bd3d5d5..b7c2f25e31 100644
--- a/Userland/Libraries/LibGfx/Bitmap.h
+++ b/Userland/Libraries/LibGfx/Bitmap.h
@@ -109,7 +109,7 @@ public:
return false;
}
- [[nodiscard]] RefPtr<Gfx::Bitmap> clone() const;
+ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> clone() const;
[[nodiscard]] RefPtr<Gfx::Bitmap> rotated(Gfx::RotationDirection) const;
[[nodiscard]] RefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const;
diff --git a/Userland/Libraries/LibGfx/GIFLoader.cpp b/Userland/Libraries/LibGfx/GIFLoader.cpp
index d17202ee71..f13c25b53e 100644
--- a/Userland/Libraries/LibGfx/GIFLoader.cpp
+++ b/Userland/Libraries/LibGfx/GIFLoader.cpp
@@ -733,8 +733,14 @@ ImageFrameDescriptor GIFImageDecoderPlugin::frame(size_t i)
m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAllFrames;
}
+ auto image_or_error = m_context->frame_buffer->clone();
+ if (image_or_error.is_error()) {
+ m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAllFrames;
+ return {};
+ }
+
ImageFrameDescriptor frame {};
- frame.image = m_context->frame_buffer->clone();
+ frame.image = image_or_error.release_value_but_fixme_should_propagate_errors();
frame.duration = m_context->images.at(i).duration * 10;
if (frame.duration <= 10) {