1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:47:35 +00:00

LibGfx: Add a stream compatible overload of Bitmap::load_from_file()

This commit is contained in:
Lucas CHOLLET 2023-01-14 19:30:07 -05:00 committed by Sam Atkins
parent 5b6e93f96a
commit b13695c9f4
2 changed files with 16 additions and 0 deletions

View file

@ -15,6 +15,7 @@
#include <AK/Queue.h>
#include <AK/ScopeGuard.h>
#include <AK/Try.h>
#include <LibCore/File.h>
#include <LibCore/MappedFile.h>
#include <LibCore/MimeData.h>
#include <LibCore/System.h>
@ -141,6 +142,19 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(StringView path, int scale
return load_from_fd_and_close(fd, path);
}
ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(NonnullOwnPtr<Core::File> 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<NonnullRefPtr<Bitmap>> Bitmap::load_from_fd_and_close(int fd, StringView path)
{
auto file = TRY(Core::MappedFile::map_from_fd_and_close(fd, path));