1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 21:27:34 +00:00

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.
This commit is contained in:
Andreas Kling 2021-11-11 11:43:08 +01:00
parent 10add3f4c2
commit 880fafd2c5

View file

@ -13,16 +13,8 @@
#include <AK/ScopeGuard.h> #include <AK/ScopeGuard.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/Try.h> #include <AK/Try.h>
#include <LibGfx/BMPLoader.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/DDSLoader.h> #include <LibGfx/ImageDecoder.h>
#include <LibGfx/GIFLoader.h>
#include <LibGfx/ICOLoader.h>
#include <LibGfx/JPGLoader.h>
#include <LibGfx/PBMLoader.h>
#include <LibGfx/PGMLoader.h>
#include <LibGfx/PNGLoader.h>
#include <LibGfx/PPMLoader.h>
#include <LibGfx/ShareableBitmap.h> #include <LibGfx/ShareableBitmap.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
@ -128,35 +120,23 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_load_from_fd_and_close(int fd, String
highdpi_icon_path.appendff("-{}x.", scale_factor); highdpi_icon_path.appendff("-{}x.", scale_factor);
highdpi_icon_path.append(lexical_path.extension()); highdpi_icon_path.append(lexical_path.extension());
RefPtr<Bitmap> bmp; auto file = TRY(MappedFile::map_from_fd_and_close(fd, highdpi_icon_path.to_string()));
#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \ if (auto decoder = ImageDecoder::try_create(file->bytes())) {
if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) { \ auto bitmap = decoder->frame(0).image;
auto file = MappedFile::map_from_fd_and_close(fd, highdpi_icon_path.to_string()); \ VERIFY(bitmap->width() % scale_factor == 0);
if (!file.is_error()) \ VERIFY(bitmap->height() % scale_factor == 0);
bmp = load_##Name##_from_memory((u8 const*)file.value()->data(), file.value()->size(), highdpi_icon_path.to_string()); \ bitmap->m_size.set_width(bitmap->width() / scale_factor);
} bitmap->m_size.set_height(bitmap->height() / scale_factor);
ENUMERATE_IMAGE_FORMATS bitmap->m_scale = scale_factor;
#undef __ENUMERATE_IMAGE_FORMAT return bitmap.release_nonnull();
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();
} }
} }
#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \ auto file = TRY(MappedFile::map_from_fd_and_close(fd, path));
if (path.ends_with(Ext, CaseSensitivity::CaseInsensitive)) { \ if (auto decoder = ImageDecoder::try_create(file->bytes())) {
auto file = MappedFile::map_from_fd_and_close(fd, path); \ if (auto bitmap = decoder->frame(0).image)
if (!file.is_error()) { \ return bitmap.release_nonnull();
if (auto bitmap = load_##Name##_from_memory((u8 const*)file.value()->data(), file.value()->size(), path)) \
return bitmap.release_nonnull(); \
} \
} }
ENUMERATE_IMAGE_FORMATS
#undef __ENUMERATE_IMAGE_FORMAT
return Error::from_string_literal("Gfx::Bitmap unable to load from fd"sv); return Error::from_string_literal("Gfx::Bitmap unable to load from fd"sv);
} }