mirror of
https://github.com/RGBCube/serenity
synced 2025-05-15 13:45:01 +00:00
LibGfx: Deduplicate code in Bitmap::try_load_from_file()
This can share logic with try_load_from_fd_and_close(), we just need to open the file first. :^)
This commit is contained in:
parent
09cba7c780
commit
d85d741c59
1 changed files with 5 additions and 32 deletions
|
@ -24,6 +24,7 @@
|
||||||
#include <LibGfx/PPMLoader.h>
|
#include <LibGfx/PPMLoader.h>
|
||||||
#include <LibGfx/ShareableBitmap.h>
|
#include <LibGfx/ShareableBitmap.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
@ -111,38 +112,10 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::try_create_wrapper(BitmapFormat format, I
|
||||||
|
|
||||||
RefPtr<Bitmap> Bitmap::try_load_from_file(String const& path, int scale_factor)
|
RefPtr<Bitmap> Bitmap::try_load_from_file(String const& path, int scale_factor)
|
||||||
{
|
{
|
||||||
if (scale_factor > 1 && path.starts_with("/res/")) {
|
int fd = open(path.characters(), O_RDONLY);
|
||||||
LexicalPath lexical_path { path };
|
if (fd < 0)
|
||||||
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)) \
|
|
||||||
bmp = load_##Name(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)) \
|
|
||||||
return load_##Name(path);
|
|
||||||
ENUMERATE_IMAGE_FORMATS
|
|
||||||
#undef __ENUMERATE_IMAGE_FORMAT
|
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
return try_load_from_fd_and_close(fd, path, scale_factor);
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<Bitmap> Bitmap::try_load_from_fd_and_close(int fd, String const& path, int scale_factor)
|
RefPtr<Bitmap> Bitmap::try_load_from_fd_and_close(int fd, String const& path, int scale_factor)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue