From 6e255b262f26e6815d3651b1ae8b9f5d73fc3ccd Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 6 Nov 2021 19:53:32 +0100 Subject: LibGfx: Use ErrorOr for try_create_from_serialized_byte_buffer() --- Userland/Libraries/LibGUI/DragOperation.cpp | 2 +- Userland/Libraries/LibGfx/Bitmap.cpp | 18 +++++++----------- Userland/Libraries/LibGfx/Bitmap.h | 5 ++--- 3 files changed, 10 insertions(+), 15 deletions(-) (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibGUI/DragOperation.cpp b/Userland/Libraries/LibGUI/DragOperation.cpp index 62157f5b6c..74e8e7af7a 100644 --- a/Userland/Libraries/LibGUI/DragOperation.cpp +++ b/Userland/Libraries/LibGUI/DragOperation.cpp @@ -33,7 +33,7 @@ DragOperation::Outcome DragOperation::exec() Gfx::ShareableBitmap drag_bitmap; if (m_mime_data->has_format("image/x-raw-bitmap")) { auto data = m_mime_data->data("image/x-raw-bitmap"); - auto bitmap = Gfx::Bitmap::try_create_from_serialized_byte_buffer(move(data)); + auto bitmap = Gfx::Bitmap::try_create_from_serialized_byte_buffer(move(data)).release_value_but_fixme_should_propagate_errors(); drag_bitmap = bitmap->to_shareable_bitmap(); } diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index 3c119d1b4e..b0621577f0 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -211,7 +211,7 @@ ErrorOr> Bitmap::try_create_with_anonymous_buffer(BitmapFo /// - palette count /// - palette data (= palette count * BGRA8888) /// - image data (= actual size * u8) -RefPtr Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffer) +ErrorOr> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffer) { InputMemoryStream stream { buffer }; size_t actual_size; @@ -229,35 +229,31 @@ RefPtr Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffe }; if (!read(actual_size) || !read(width) || !read(height) || !read(scale_factor) || !read(format) || !read(palette_size)) - return nullptr; + return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv); if (format > BitmapFormat::BGRA8888 || format < BitmapFormat::Indexed1) - return nullptr; + return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv); if (!check_size({ width, height }, scale_factor, format, actual_size)) - return {}; + return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv); palette.ensure_capacity(palette_size); for (size_t i = 0; i < palette_size; ++i) { if (!read(palette[i])) - return {}; + return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv); } if (stream.remaining() < actual_size) - return {}; + return Error::from_string_literal("Gfx::Bitmap::try_create_from_serialized_byte_buffer: decode failed"sv); auto data = stream.bytes().slice(stream.offset(), actual_size); - auto bitmap_or_error = Bitmap::try_create(format, { width, height }, scale_factor); - if (bitmap_or_error.is_error()) - return {}; - auto bitmap = bitmap_or_error.release_value_but_fixme_should_propagate_errors(); + auto bitmap = TRY(Bitmap::try_create(format, { width, height }, scale_factor)); bitmap->m_palette = new RGBA32[palette_size]; memcpy(bitmap->m_palette, palette.data(), palette_size * sizeof(RGBA32)); data.copy_to({ bitmap->scanline(0), bitmap->size_in_bytes() }); - return bitmap; } diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h index 0290431738..8b02f22e8a 100644 --- a/Userland/Libraries/LibGfx/Bitmap.h +++ b/Userland/Libraries/LibGfx/Bitmap.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2018-2021, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,7 +8,6 @@ #include #include -#include #include #include #include @@ -96,7 +95,7 @@ public: [[nodiscard]] static ErrorOr> try_load_from_file(String const& path, int scale_factor = 1); [[nodiscard]] static ErrorOr> try_load_from_fd_and_close(int fd, String const& path, int scale_factor = 1); [[nodiscard]] static ErrorOr> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize const&, int intrinsic_scale, Vector const& palette); - [[nodiscard]] static RefPtr try_create_from_serialized_byte_buffer(ByteBuffer&& buffer); + static ErrorOr> try_create_from_serialized_byte_buffer(ByteBuffer&&); static bool is_path_a_supported_image_format(StringView const& path) { -- cgit v1.2.3