diff options
-rw-r--r-- | Userland/Libraries/LibGfx/Bitmap.cpp | 42 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/Bitmap.h | 1 |
2 files changed, 43 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGfx/Bitmap.cpp b/Userland/Libraries/LibGfx/Bitmap.cpp index a6f58f730f..588bf36d19 100644 --- a/Userland/Libraries/LibGfx/Bitmap.cpp +++ b/Userland/Libraries/LibGfx/Bitmap.cpp @@ -145,6 +145,48 @@ RefPtr<Bitmap> Bitmap::try_load_from_file(String const& path, int scale_factor) return nullptr; } +RefPtr<Bitmap> Bitmap::try_load_from_fd_and_close(int fd, String const& path, int scale_factor) +{ + if (scale_factor > 1 && path.starts_with("/res/")) { + LexicalPath lexical_path { path }; + StringBuilder highdpi_icon_path; + highdpi_icon_path.append(lexical_path.dirname()); + highdpi_icon_path.append('/'); + highdpi_icon_path.append(lexical_path.title()); + highdpi_icon_path.appendff("-{}x.", scale_factor); + highdpi_icon_path.append(lexical_path.extension()); + + RefPtr<Bitmap> 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((const u8*)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; + } + } + +#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()) \ + return load_##Name##_from_memory((const u8*)file.value()->data(), file.value()->size(), path); \ + } + ENUMERATE_IMAGE_FORMATS +#undef __ENUMERATE_IMAGE_FORMAT + + return nullptr; +} + Bitmap::Bitmap(BitmapFormat format, const IntSize& size, int scale_factor, size_t pitch, void* data) : m_size(size) , m_scale(scale_factor) diff --git a/Userland/Libraries/LibGfx/Bitmap.h b/Userland/Libraries/LibGfx/Bitmap.h index 4436d56350..9cffbb5dbc 100644 --- a/Userland/Libraries/LibGfx/Bitmap.h +++ b/Userland/Libraries/LibGfx/Bitmap.h @@ -94,6 +94,7 @@ public: [[nodiscard]] static RefPtr<Bitmap> try_create_shareable(BitmapFormat, const IntSize&, int intrinsic_scale = 1); [[nodiscard]] static RefPtr<Bitmap> try_create_wrapper(BitmapFormat, const IntSize&, int intrinsic_scale, size_t pitch, void*); [[nodiscard]] static RefPtr<Bitmap> try_load_from_file(String const& path, int scale_factor = 1); + [[nodiscard]] static RefPtr<Bitmap> try_load_from_fd_and_close(int fd, String const& path, int scale_factor = 1); [[nodiscard]] static RefPtr<Bitmap> try_create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, const IntSize&, int intrinsic_scale, const Vector<RGBA32>& palette); [[nodiscard]] static RefPtr<Bitmap> try_create_from_serialized_byte_buffer(ByteBuffer&& buffer); |