LibGfx: Add method to load bitmap from fd

This commit is contained in:
Timothy 2021-09-07 21:22:50 +10:00 committed by Andreas Kling
parent c966c32571
commit b953588e0e
Notes: sideshowbarker 2024-07-19 01:59:31 +09:00
2 changed files with 43 additions and 0 deletions

View file

@ -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)

View file

@ -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);