From bb5db0835d585f885aaca733b5f57da55cc3389d Mon Sep 17 00:00:00 2001 From: MacDue Date: Sun, 2 Jul 2023 22:36:35 +0100 Subject: [PATCH] LibGfx: Allow loading a Bitmap (from bytes) with an ideal size --- Userland/Libraries/LibGfx/Bitmap.cpp | 21 +++++++++++++-------- Userland/Libraries/LibGfx/Bitmap.h | 5 +++-- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index 30d1fa5ce8..33ef24c2b6 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -107,10 +107,10 @@ ErrorOr> Bitmap::create_wrapper(BitmapFormat format, IntSi return adopt_ref(*new Bitmap(format, size, scale_factor, pitch, data)); } -ErrorOr> Bitmap::load_from_file(StringView path, int scale_factor) +ErrorOr> Bitmap::load_from_file(StringView path, int scale_factor, Optional ideal_size) { if (scale_factor > 1 && path.starts_with("/res/"sv)) { - auto load_scaled_bitmap = [](StringView path, int scale_factor) -> ErrorOr> { + auto load_scaled_bitmap = [](StringView path, int scale_factor, Optional ideal_size) -> ErrorOr> { LexicalPath lexical_path { path }; StringBuilder highdpi_icon_path; TRY(highdpi_icon_path.try_appendff("{}/{}-{}x.{}", lexical_path.dirname(), lexical_path.title(), scale_factor, lexical_path.extension())); @@ -118,7 +118,7 @@ ErrorOr> Bitmap::load_from_file(StringView path, int scale auto highdpi_icon_string = highdpi_icon_path.string_view(); auto file = TRY(Core::File::open(highdpi_icon_string, Core::File::OpenMode::Read)); - auto bitmap = TRY(load_from_file(move(file), highdpi_icon_string)); + auto bitmap = TRY(load_from_file(move(file), highdpi_icon_string, ideal_size)); if (bitmap->width() % scale_factor != 0 || bitmap->height() % scale_factor != 0) return Error::from_string_literal("Bitmap::load_from_file: HighDPI image size should be divisible by scale factor"); bitmap->m_size.set_width(bitmap->width() / scale_factor); @@ -127,7 +127,7 @@ ErrorOr> Bitmap::load_from_file(StringView path, int scale return bitmap; }; - auto scaled_bitmap_or_error = load_scaled_bitmap(path, scale_factor); + auto scaled_bitmap_or_error = load_scaled_bitmap(path, scale_factor, ideal_size); if (!scaled_bitmap_or_error.is_error()) return scaled_bitmap_or_error.release_value(); @@ -139,15 +139,20 @@ ErrorOr> Bitmap::load_from_file(StringView path, int scale } auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read)); - return load_from_file(move(file), path); + return load_from_file(move(file), path, ideal_size); } -ErrorOr> Bitmap::load_from_file(NonnullOwnPtr file, StringView path) +ErrorOr> Bitmap::load_from_file(NonnullOwnPtr file, StringView path, Optional ideal_size) { auto mapped_file = TRY(Core::MappedFile::map_from_file(move(file), path)); auto mime_type = Core::guess_mime_type_based_on_filename(path); - if (auto decoder = ImageDecoder::try_create_for_raw_bytes(mapped_file->bytes(), mime_type)) { - auto frame = TRY(decoder->frame(0)); + return load_from_bytes(mapped_file->bytes(), ideal_size, mime_type); +} + +ErrorOr> Bitmap::load_from_bytes(ReadonlyBytes bytes, Optional ideal_size, Optional mine_type) +{ + if (auto decoder = ImageDecoder::try_create_for_raw_bytes(bytes, mine_type)) { + auto frame = TRY(decoder->frame(0, ideal_size)); if (auto& bitmap = frame.image) return bitmap.release_nonnull(); } diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h index 44f7cdbf55..da26d0f377 100644 --- a/Userland/Libraries/LibGfx/Bitmap.h +++ b/Userland/Libraries/LibGfx/Bitmap.h @@ -98,8 +98,9 @@ public: [[nodiscard]] static ErrorOr> create(BitmapFormat, IntSize, int intrinsic_scale = 1); [[nodiscard]] static ErrorOr> create_shareable(BitmapFormat, IntSize, int intrinsic_scale = 1); [[nodiscard]] static ErrorOr> create_wrapper(BitmapFormat, IntSize, int intrinsic_scale, size_t pitch, void*); - [[nodiscard]] static ErrorOr> load_from_file(StringView path, int scale_factor = 1); - [[nodiscard]] static ErrorOr> load_from_file(NonnullOwnPtr, StringView path); + [[nodiscard]] static ErrorOr> load_from_file(StringView path, int scale_factor = 1, Optional ideal_size = {}); + [[nodiscard]] static ErrorOr> load_from_file(NonnullOwnPtr, StringView path, Optional ideal_size = {}); + [[nodiscard]] static ErrorOr> load_from_bytes(ReadonlyBytes, Optional ideal_size = {}, Optional mine_type = {}); [[nodiscard]] static ErrorOr> create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize, int intrinsic_scale, Vector const& palette); static ErrorOr> create_from_serialized_bytes(ReadonlyBytes); static ErrorOr> create_from_serialized_byte_buffer(ByteBuffer&&);