mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-04 05:19:58 +03:00
LibGfx: Allow loading a Bitmap (from bytes) with an ideal size
This commit is contained in:
parent
7a0e3474d6
commit
bb5db0835d
Notes:
sideshowbarker
2024-07-17 03:19:14 +09:00
Author: https://github.com/MacDue Commit: https://github.com/SerenityOS/serenity/commit/bb5db0835d Pull-request: https://github.com/SerenityOS/serenity/pull/19765 Reviewed-by: https://github.com/LucasChollet Reviewed-by: https://github.com/gmta ✅
@ -107,10 +107,10 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::create_wrapper(BitmapFormat format, IntSi
|
||||
return adopt_ref(*new Bitmap(format, size, scale_factor, pitch, data));
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(StringView path, int scale_factor)
|
||||
ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(StringView path, int scale_factor, Optional<IntSize> ideal_size)
|
||||
{
|
||||
if (scale_factor > 1 && path.starts_with("/res/"sv)) {
|
||||
auto load_scaled_bitmap = [](StringView path, int scale_factor) -> ErrorOr<NonnullRefPtr<Bitmap>> {
|
||||
auto load_scaled_bitmap = [](StringView path, int scale_factor, Optional<IntSize> ideal_size) -> ErrorOr<NonnullRefPtr<Bitmap>> {
|
||||
LexicalPath lexical_path { path };
|
||||
StringBuilder highdpi_icon_path;
|
||||
TRY(highdpi_icon_path.try_appendff("{}/{}-{}x.{}", lexical_path.dirname(), lexical_path.title(), scale_factor, lexical_path.extension()));
|
||||
@ -118,7 +118,7 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(StringView path, int scale
|
||||
auto highdpi_icon_string = highdpi_icon_path.string_view();
|
||||
auto file = TRY(Core::File::open(highdpi_icon_string, Core::File::OpenMode::Read));
|
||||
|
||||
auto bitmap = TRY(load_from_file(move(file), highdpi_icon_string));
|
||||
auto bitmap = TRY(load_from_file(move(file), highdpi_icon_string, ideal_size));
|
||||
if (bitmap->width() % scale_factor != 0 || bitmap->height() % scale_factor != 0)
|
||||
return Error::from_string_literal("Bitmap::load_from_file: HighDPI image size should be divisible by scale factor");
|
||||
bitmap->m_size.set_width(bitmap->width() / scale_factor);
|
||||
@ -127,7 +127,7 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(StringView path, int scale
|
||||
return bitmap;
|
||||
};
|
||||
|
||||
auto scaled_bitmap_or_error = load_scaled_bitmap(path, scale_factor);
|
||||
auto scaled_bitmap_or_error = load_scaled_bitmap(path, scale_factor, ideal_size);
|
||||
if (!scaled_bitmap_or_error.is_error())
|
||||
return scaled_bitmap_or_error.release_value();
|
||||
|
||||
@ -139,15 +139,20 @@ ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(StringView path, int scale
|
||||
}
|
||||
|
||||
auto file = TRY(Core::File::open(path, Core::File::OpenMode::Read));
|
||||
return load_from_file(move(file), path);
|
||||
return load_from_file(move(file), path, ideal_size);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(NonnullOwnPtr<Core::File> file, StringView path)
|
||||
ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_file(NonnullOwnPtr<Core::File> file, StringView path, Optional<IntSize> ideal_size)
|
||||
{
|
||||
auto mapped_file = TRY(Core::MappedFile::map_from_file(move(file), path));
|
||||
auto mime_type = Core::guess_mime_type_based_on_filename(path);
|
||||
if (auto decoder = ImageDecoder::try_create_for_raw_bytes(mapped_file->bytes(), mime_type)) {
|
||||
auto frame = TRY(decoder->frame(0));
|
||||
return load_from_bytes(mapped_file->bytes(), ideal_size, mime_type);
|
||||
}
|
||||
|
||||
ErrorOr<NonnullRefPtr<Bitmap>> Bitmap::load_from_bytes(ReadonlyBytes bytes, Optional<IntSize> ideal_size, Optional<DeprecatedString> mine_type)
|
||||
{
|
||||
if (auto decoder = ImageDecoder::try_create_for_raw_bytes(bytes, mine_type)) {
|
||||
auto frame = TRY(decoder->frame(0, ideal_size));
|
||||
if (auto& bitmap = frame.image)
|
||||
return bitmap.release_nonnull();
|
||||
}
|
||||
|
@ -98,8 +98,9 @@ public:
|
||||
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create(BitmapFormat, IntSize, int intrinsic_scale = 1);
|
||||
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_shareable(BitmapFormat, IntSize, int intrinsic_scale = 1);
|
||||
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_wrapper(BitmapFormat, IntSize, int intrinsic_scale, size_t pitch, void*);
|
||||
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(StringView path, int scale_factor = 1);
|
||||
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(NonnullOwnPtr<Core::File>, StringView path);
|
||||
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(StringView path, int scale_factor = 1, Optional<IntSize> ideal_size = {});
|
||||
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_file(NonnullOwnPtr<Core::File>, StringView path, Optional<IntSize> ideal_size = {});
|
||||
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> load_from_bytes(ReadonlyBytes, Optional<IntSize> ideal_size = {}, Optional<DeprecatedString> mine_type = {});
|
||||
[[nodiscard]] static ErrorOr<NonnullRefPtr<Bitmap>> create_with_anonymous_buffer(BitmapFormat, Core::AnonymousBuffer, IntSize, int intrinsic_scale, Vector<ARGB32> const& palette);
|
||||
static ErrorOr<NonnullRefPtr<Bitmap>> create_from_serialized_bytes(ReadonlyBytes);
|
||||
static ErrorOr<NonnullRefPtr<Bitmap>> create_from_serialized_byte_buffer(ByteBuffer&&);
|
||||
|
Loading…
Reference in New Issue
Block a user