From b13695c9f481cbb60742154d4ad31496f0d92fba Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 14 Jan 2023 19:30:07 -0500 Subject: [PATCH] LibGfx: Add a stream compatible overload of `Bitmap::load_from_file()` --- Userland/Libraries/LibGfx/Bitmap.cpp | 14 ++++++++++++++ Userland/Libraries/LibGfx/Bitmap.h | 2 ++ 2 files changed, 16 insertions(+) diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index 6180457e28..f9c777e1e7 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -141,6 +142,19 @@ ErrorOr> Bitmap::load_from_file(StringView path, int scale return load_from_fd_and_close(fd, path); } +ErrorOr> Bitmap::load_from_file(NonnullOwnPtr file, StringView path) +{ + 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)); + if (auto& bitmap = frame.image) + return bitmap.release_nonnull(); + } + + return Error::from_string_literal("Gfx::Bitmap unable to load from file"); +} + ErrorOr> Bitmap::load_from_fd_and_close(int fd, StringView path) { auto file = TRY(Core::MappedFile::map_from_fd_and_close(fd, path)); diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h index 68d04f95b5..0734b8a2dc 100644 --- a/Userland/Libraries/LibGfx/Bitmap.h +++ b/Userland/Libraries/LibGfx/Bitmap.h @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -97,6 +98,7 @@ public: [[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_fd_and_close(int fd, StringView path); [[nodiscard]] static ErrorOr> create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize, int intrinsic_scale, Vector const& palette); static ErrorOr> create_from_serialized_bytes(ReadonlyBytes);