1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:28:12 +00:00

PixelPaint: Remove try_ prefix from fallible Image methods

This commit is contained in:
Linus Groh 2023-01-28 20:12:17 +00:00 committed by Jelle Raaijmakers
parent 9c08bb9555
commit 39f1a6eb6f
6 changed files with 31 additions and 31 deletions

View file

@ -21,7 +21,7 @@
namespace PixelPaint {
ErrorOr<NonnullRefPtr<Image>> Image::try_create_with_size(Gfx::IntSize size)
ErrorOr<NonnullRefPtr<Image>> Image::create_with_size(Gfx::IntSize size)
{
VERIFY(!size.is_empty());
@ -51,7 +51,7 @@ void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect) con
}
}
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_decode_bitmap(ReadonlyBytes bitmap_data)
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::decode_bitmap(ReadonlyBytes bitmap_data)
{
// Spawn a new ImageDecoder service process and connect to it.
auto client = TRY(ImageDecoderClient::Client::try_create());
@ -72,18 +72,18 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_decode_bitmap(ReadonlyBytes bitma
return decoded_bitmap.release_nonnull();
}
ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const& bitmap)
ErrorOr<NonnullRefPtr<Image>> Image::create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const& bitmap)
{
auto image = TRY(try_create_with_size({ bitmap->width(), bitmap->height() }));
auto image = TRY(create_with_size({ bitmap->width(), bitmap->height() }));
auto layer = TRY(Layer::try_create_with_bitmap(*image, *bitmap, "Background"));
image->add_layer(move(layer));
return image;
}
ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_pixel_paint_json(JsonObject const& json)
ErrorOr<NonnullRefPtr<Image>> Image::create_from_pixel_paint_json(JsonObject const& json)
{
// FIXME: Handle invalid JSON data
auto image = TRY(try_create_with_size({ json.get_i32("width"sv).value_or(0), json.get_i32("height"sv).value_or(0) }));
auto image = TRY(create_with_size({ json.get_i32("width"sv).value_or(0), json.get_i32("height"sv).value_or(0) }));
auto layers_value = json.get_array("layers"sv).value();
for (auto& layer_value : layers_value.values()) {
@ -92,13 +92,13 @@ ErrorOr<NonnullRefPtr<Image>> Image::try_create_from_pixel_paint_json(JsonObject
auto bitmap_base64_encoded = layer_object.get_deprecated_string("bitmap"sv).value();
auto bitmap_data = TRY(decode_base64(bitmap_base64_encoded));
auto bitmap = TRY(try_decode_bitmap(bitmap_data));
auto bitmap = TRY(decode_bitmap(bitmap_data));
auto layer = TRY(Layer::try_create_with_bitmap(*image, move(bitmap), name));
if (auto const& mask_object = layer_object.get_deprecated_string("mask"sv); mask_object.has_value()) {
auto mask_base64_encoded = mask_object.value();
auto mask_data = TRY(decode_base64(mask_base64_encoded));
auto mask = TRY(try_decode_bitmap(mask_data));
auto mask = TRY(decode_bitmap(mask_data));
TRY(layer->try_set_bitmaps(layer->content_bitmap(), mask));
}
@ -146,7 +146,7 @@ ErrorOr<void> Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json
return {};
}
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_compose_bitmap(Gfx::BitmapFormat format) const
ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::compose_bitmap(Gfx::BitmapFormat format) const
{
auto bitmap = TRY(Gfx::Bitmap::create(format, m_size));
GUI::Painter painter(bitmap);
@ -154,14 +154,14 @@ ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::try_compose_bitmap(Gfx::BitmapFormat
return bitmap;
}
RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
RefPtr<Gfx::Bitmap> Image::copy_bitmap(Selection const& selection) const
{
if (selection.is_empty())
return {};
auto selection_rect = selection.bounding_rect();
// FIXME: Add a way to only compose a certain part of the image
auto bitmap_or_error = try_compose_bitmap(Gfx::BitmapFormat::BGRA8888);
auto bitmap_or_error = compose_bitmap(Gfx::BitmapFormat::BGRA8888);
if (bitmap_or_error.is_error())
return {};
auto full_bitmap = bitmap_or_error.release_value();
@ -175,7 +175,7 @@ RefPtr<Gfx::Bitmap> Image::try_copy_bitmap(Selection const& selection) const
ErrorOr<void> Image::export_bmp_to_file(NonnullOwnPtr<Core::Stream::Stream> stream, bool preserve_alpha_channel) const
{
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
auto bitmap = TRY(compose_bitmap(bitmap_format));
Gfx::BMPWriter dumper;
auto encoded_data = dumper.dump(bitmap);
@ -186,7 +186,7 @@ ErrorOr<void> Image::export_bmp_to_file(NonnullOwnPtr<Core::Stream::Stream> stre
ErrorOr<void> Image::export_png_to_file(NonnullOwnPtr<Core::Stream::Stream> stream, bool preserve_alpha_channel) const
{
auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
auto bitmap = TRY(try_compose_bitmap(bitmap_format));
auto bitmap = TRY(compose_bitmap(bitmap_format));
auto encoded_data = TRY(Gfx::PNGWriter::encode(*bitmap));
TRY(stream->write_entire_buffer(encoded_data));
@ -195,7 +195,7 @@ ErrorOr<void> Image::export_png_to_file(NonnullOwnPtr<Core::Stream::Stream> stre
ErrorOr<void> Image::export_qoi_to_file(NonnullOwnPtr<Core::Stream::Stream> stream) const
{
auto bitmap = TRY(try_compose_bitmap(Gfx::BitmapFormat::BGRA8888));
auto bitmap = TRY(compose_bitmap(Gfx::BitmapFormat::BGRA8888));
auto encoded_data = Gfx::QOIWriter::encode(bitmap);
TRY(stream->write_entire_buffer(encoded_data));
@ -217,7 +217,7 @@ void Image::add_layer(NonnullRefPtr<Layer> layer)
ErrorOr<NonnullRefPtr<Image>> Image::take_snapshot() const
{
auto snapshot = TRY(try_create_with_size(m_size));
auto snapshot = TRY(create_with_size(m_size));
for (auto const& layer : m_layers) {
auto layer_snapshot = TRY(Layer::try_create_snapshot(*snapshot, layer));
snapshot->add_layer(move(layer_snapshot));