From 880fafd2c59c61b3a86cee4f507c800fd8bce185 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Thu, 11 Nov 2021 11:43:08 +0100 Subject: [PATCH] LibGfx: Use ImageDecoder in Bitmap::try_load_from_fd_and_close() Before this patch, both Bitmap and ImageDecoder had logic for guessing which image codec to use for a chunk of data. Bitmap now defers to ImageDecoder so that we only have to do this in one place. There's room for improvement in the ImageDecoder heuristic, but that's outside the scope of this change. --- Userland/Libraries/LibGfx/Bitmap.cpp | 48 ++++++++-------------------- 1 file changed, 14 insertions(+), 34 deletions(-) diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index f8db7fa4b1..592f0a03c5 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -13,16 +13,8 @@ #include #include #include -#include #include -#include -#include -#include -#include -#include -#include -#include -#include +#include #include #include #include @@ -128,35 +120,23 @@ ErrorOr> Bitmap::try_load_from_fd_and_close(int fd, String highdpi_icon_path.appendff("-{}x.", scale_factor); highdpi_icon_path.append(lexical_path.extension()); - RefPtr bmp; -#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \ - if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) { \ - auto file = MappedFile::map_from_fd_and_close(fd, highdpi_icon_path.to_string()); \ - if (!file.is_error()) \ - bmp = load_##Name##_from_memory((u8 const*)file.value()->data(), file.value()->size(), highdpi_icon_path.to_string()); \ - } - ENUMERATE_IMAGE_FORMATS -#undef __ENUMERATE_IMAGE_FORMAT - if (bmp) { - VERIFY(bmp->width() % scale_factor == 0); - VERIFY(bmp->height() % scale_factor == 0); - bmp->m_size.set_width(bmp->width() / scale_factor); - bmp->m_size.set_height(bmp->height() / scale_factor); - bmp->m_scale = scale_factor; - return bmp.release_nonnull(); + auto file = TRY(MappedFile::map_from_fd_and_close(fd, highdpi_icon_path.to_string())); + if (auto decoder = ImageDecoder::try_create(file->bytes())) { + auto bitmap = decoder->frame(0).image; + VERIFY(bitmap->width() % scale_factor == 0); + VERIFY(bitmap->height() % scale_factor == 0); + bitmap->m_size.set_width(bitmap->width() / scale_factor); + bitmap->m_size.set_height(bitmap->height() / scale_factor); + bitmap->m_scale = scale_factor; + return bitmap.release_nonnull(); } } -#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \ - if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) { \ - auto file = MappedFile::map_from_fd_and_close(fd, path); \ - if (!file.is_error()) { \ - if (auto bitmap = load_##Name##_from_memory((u8 const*)file.value()->data(), file.value()->size(), path)) \ - return bitmap.release_nonnull(); \ - } \ + auto file = TRY(MappedFile::map_from_fd_and_close(fd, path)); + if (auto decoder = ImageDecoder::try_create(file->bytes())) { + if (auto bitmap = decoder->frame(0).image) + return bitmap.release_nonnull(); } - ENUMERATE_IMAGE_FORMATS -#undef __ENUMERATE_IMAGE_FORMAT return Error::from_string_literal("Gfx::Bitmap unable to load from fd"sv); }