1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:27:44 +00:00

LibGfx: Use ErrorOr<T> for Bitmap::clone()

This commit is contained in:
Andreas Kling 2021-11-06 11:52:35 +01:00
parent c417820bff
commit 2da4cfcc80
6 changed files with 23 additions and 11 deletions

View file

@ -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) 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 We set these properties directly because calling the setters might
notify the image of an update on the newly created layer, but this notify the image of an update on the newly created layer, but this

View file

@ -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) NonnullRefPtr<Gfx::Bitmap> Card::invert_bitmap(Gfx::Bitmap& bitmap)
{ {
auto inverted_bitmap = bitmap.clone(); auto inverted_bitmap = bitmap.clone().release_value_but_fixme_should_propagate_errors();
VERIFY(inverted_bitmap);
for (int y = 0; y < inverted_bitmap->height(); y++) { for (int y = 0; y < inverted_bitmap->height(); y++) {
for (int x = 0; x < inverted_bitmap->width(); x++) { for (int x = 0; x < inverted_bitmap->width(); x++) {
inverted_bitmap->set_pixel(x, y, inverted_bitmap->get_pixel(x, y).inverted()); inverted_bitmap->set_pixel(x, y, inverted_bitmap->get_pixel(x, y).inverted());

View file

@ -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& emblem = size < 32 ? *s_symlink_emblem_small : *s_symlink_emblem;
auto original_bitmap = target_icon.bitmap_for_size(size); auto original_bitmap = target_icon.bitmap_for_size(size);
VERIFY(original_bitmap); VERIFY(original_bitmap);
auto generated_bitmap = original_bitmap->clone(); auto generated_bitmap_or_error = original_bitmap->clone();
if (!generated_bitmap) { if (generated_bitmap_or_error.is_error()) {
dbgln("Failed to clone {}x{} icon for symlink variant", size, size); dbgln("Failed to clone {}x{} icon for symlink variant", size, size);
return s_symlink_icon; return s_symlink_icon;
} }
auto generated_bitmap = generated_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
GUI::Painter painter(*generated_bitmap); GUI::Painter painter(*generated_bitmap);
painter.blit({ size - emblem.width(), size - emblem.height() }, emblem, emblem.rect()); painter.blit({ size - emblem.width(), size - emblem.height() }, emblem, emblem.rect());

View file

@ -332,17 +332,19 @@ Bitmap::Bitmap(BitmapFormat format, Core::AnonymousBuffer buffer, const IntSize&
allocate_palette_from_format(m_format, palette); 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()); auto new_bitmap = Bitmap::try_create(format(), size(), scale());
if (!new_bitmap) if (!new_bitmap) {
return nullptr; // FIXME: Propagate the *real* error, once we have it.
return Error::from_errno(ENOMEM);
}
VERIFY(size_in_bytes() == new_bitmap->size_in_bytes()); VERIFY(size_in_bytes() == new_bitmap->size_in_bytes());
memcpy(new_bitmap->scanline(0), scanline(0), 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 RefPtr<Gfx::Bitmap> Bitmap::rotated(Gfx::RotationDirection rotation_direction) const

View file

@ -109,7 +109,7 @@ public:
return false; 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> rotated(Gfx::RotationDirection) const;
[[nodiscard]] RefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const; [[nodiscard]] RefPtr<Gfx::Bitmap> flipped(Gfx::Orientation) const;

View file

@ -733,8 +733,14 @@ ImageFrameDescriptor GIFImageDecoderPlugin::frame(size_t i)
m_context->error_state = GIFLoadingContext::ErrorState::FailedToDecodeAllFrames; 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 {}; 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; frame.duration = m_context->images.at(i).duration * 10;
if (frame.duration <= 10) { if (frame.duration <= 10) {