diff options
author | Andreas Kling <kling@serenityos.org> | 2021-11-06 19:53:32 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-11-08 00:35:27 +0100 |
commit | 6e255b262f26e6815d3651b1ae8b9f5d73fc3ccd (patch) | |
tree | 0231e297c0affbb13b587c0d9d19ed5b9d45e963 /Userland/Libraries | |
parent | 0de33b3d6c3b61a960a8e0aca3a851b8896b7491 (diff) | |
download | serenity-6e255b262f26e6815d3651b1ae8b9f5d73fc3ccd.zip |
LibGfx: Use ErrorOr<T> for try_create_from_serialized_byte_buffer()
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibGUI/DragOperation.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Bitmap.cpp | 18 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Bitmap.h | 5 |
3 files changed, 10 insertions, 15 deletions
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<NonnullRefPtr<Bitmap>> Bitmap::try_create_with_anonymous_buffer(BitmapFo /// - palette count /// - palette data (= palette count * BGRA8888) /// - image data (= actual size * u8) -RefPtr<Bitmap> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffer) +ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffer) { InputMemoryStream stream { buffer }; size_t actual_size; @@ -229,35 +229,31 @@ RefPtr<Bitmap> 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 <kling@serenityos.org> + * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -8,7 +8,6 @@ #include <AK/Forward.h> #include <AK/RefCounted.h> -#include <AK/RefPtr.h> #include <LibCore/AnonymousBuffer.h> #include <LibGfx/Color.h> #include <LibGfx/Forward.h> @@ -96,7 +95,7 @@ public: [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_load_from_file(String const& path, int scale_factor = 1); [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_load_from_fd_and_close(int fd, String const& path, int scale_factor = 1); [[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize const&, int intrinsic_scale, Vector<RGBA32> const& palette); - [[nodiscard]] static RefPtr<Bitmap> try_create_from_serialized_byte_buffer(ByteBuffer&& buffer); + static ErrorOr<NonnullRefPtr<Bitmap>> try_create_from_serialized_byte_buffer(ByteBuffer&&); static bool is_path_a_supported_image_format(StringView const& path) { |