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

PixelPaint: Use ErrorOr<T> for Image and Layer creation helpers

This commit is contained in:
Andreas Kling 2021-11-07 11:22:35 +01:00
parent 9268ed9605
commit 801d46d02c
7 changed files with 76 additions and 113 deletions

View file

@ -7,46 +7,41 @@
#include "Layer.h"
#include "Image.h"
#include "Selection.h"
#include <AK/RefPtr.h>
#include <AK/Try.h>
#include <LibGfx/Bitmap.h>
namespace PixelPaint {
RefPtr<Layer> Layer::try_create_with_size(Image& image, Gfx::IntSize const& size, String name)
ErrorOr<NonnullRefPtr<Layer>> Layer::try_create_with_size(Image& image, Gfx::IntSize const& size, String name)
{
if (size.is_empty())
return nullptr;
VERIFY(!size.is_empty());
if (size.width() > 16384 || size.height() > 16384)
return nullptr;
return Error::from_string_literal("Layer size too large"sv);
auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, size);
if (bitmap_or_error.is_error())
return nullptr;
return adopt_ref(*new Layer(image, bitmap_or_error.release_value_but_fixme_should_propagate_errors(), move(name)));
auto bitmap = TRY(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, size));
return adopt_nonnull_ref_or_enomem(new (nothrow) Layer(image, move(bitmap), move(name)));
}
RefPtr<Layer> Layer::try_create_with_bitmap(Image& image, NonnullRefPtr<Gfx::Bitmap> bitmap, String name)
ErrorOr<NonnullRefPtr<Layer>> Layer::try_create_with_bitmap(Image& image, NonnullRefPtr<Gfx::Bitmap> bitmap, String name)
{
if (bitmap->size().is_empty())
return nullptr;
VERIFY(!bitmap->size().is_empty());
if (bitmap->size().width() > 16384 || bitmap->size().height() > 16384)
return nullptr;
return Error::from_string_literal("Layer size too large"sv);
return adopt_ref(*new Layer(image, bitmap, move(name)));
return adopt_nonnull_ref_or_enomem(new (nothrow) Layer(image, bitmap, move(name)));
}
RefPtr<Layer> Layer::try_create_snapshot(Image& image, Layer const& layer)
ErrorOr<NonnullRefPtr<Layer>> Layer::try_create_snapshot(Image& image, Layer const& layer)
{
auto new_bitmap_or_error = layer.bitmap().clone();
if (new_bitmap_or_error.is_error())
return nullptr;
auto bitmap = TRY(layer.bitmap().clone());
auto snapshot = TRY(try_create_with_bitmap(image, move(bitmap), layer.name()));
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
We set these properties directly because calling the setters might
notify the image of an update on the newly created layer, but this
layer has not yet been added to the image.
*/
snapshot->m_opacity_percent = layer.opacity_percent();