From e818c955b34f456d4bf0940137f7157d1c36442c Mon Sep 17 00:00:00 2001 From: Rodrigo Tobar Date: Fri, 25 Nov 2022 10:56:48 +0800 Subject: [PATCH] LibGfx: Allow creating Bitmaps from ReadonlyBytes objects The existing try_create_from_serialized_byte_buffer function accepts a ByteBuffer, but in reality it requires only a ReadonlyBytes, since internally the only thing it does is calling buffer.bytes(). There is thus no reason to have a function that simply accepts ReadonlyBytes, and implement the former in terms of the newer. --- Userland/Libraries/LibGfx/Bitmap.cpp | 9 +++++++-- Userland/Libraries/LibGfx/Bitmap.h | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index 6f5b3e8cd6..cd91702f1a 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -194,6 +194,11 @@ ErrorOr> Bitmap::try_create_with_anonymous_buffer(BitmapFo return adopt_nonnull_ref_or_enomem(new (nothrow) Bitmap(format, move(buffer), size, scale_factor, palette)); } +ErrorOr> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffer) +{ + return try_create_from_serialized_bytes(buffer.bytes()); +} + /// Read a bitmap as described by: /// - actual size /// - width @@ -203,9 +208,9 @@ ErrorOr> Bitmap::try_create_with_anonymous_buffer(BitmapFo /// - palette count /// - palette data (= palette count * BGRA8888) /// - image data (= actual size * u8) -ErrorOr> Bitmap::try_create_from_serialized_byte_buffer(ByteBuffer&& buffer) +ErrorOr> Bitmap::try_create_from_serialized_bytes(ReadonlyBytes bytes) { - InputMemoryStream stream { buffer }; + InputMemoryStream stream { bytes }; size_t actual_size; unsigned width; unsigned height; diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h index c694634ce7..b1dcf9bb65 100644 --- a/Userland/Libraries/LibGfx/Bitmap.h +++ b/Userland/Libraries/LibGfx/Bitmap.h @@ -98,6 +98,7 @@ public: [[nodiscard]] static ErrorOr> try_load_from_file(StringView path, int scale_factor = 1); [[nodiscard]] static ErrorOr> try_load_from_fd_and_close(int fd, StringView path); [[nodiscard]] static ErrorOr> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize, int intrinsic_scale, Vector const& palette); + static ErrorOr> try_create_from_serialized_bytes(ReadonlyBytes); static ErrorOr> try_create_from_serialized_byte_buffer(ByteBuffer&&); static bool is_path_a_supported_image_format(StringView path)