From c966c325719673a411b793250b6247862e3b89de Mon Sep 17 00:00:00 2001 From: Timothy Date: Tue, 7 Sep 2021 21:13:29 +1000 Subject: [PATCH] LibGfx: Move common loader functionality to load from memory functions This will share functionality between the load from path and load from memory functions. --- Userland/Libraries/LibGfx/BMPLoader.cpp | 9 +++------ Userland/Libraries/LibGfx/BMPLoader.h | 3 ++- Userland/Libraries/LibGfx/DDSLoader.cpp | 9 +++------ Userland/Libraries/LibGfx/DDSLoader.h | 3 ++- Userland/Libraries/LibGfx/GIFLoader.cpp | 10 +++------- Userland/Libraries/LibGfx/GIFLoader.h | 3 ++- Userland/Libraries/LibGfx/ICOLoader.cpp | 10 +++------- Userland/Libraries/LibGfx/ICOLoader.h | 3 ++- Userland/Libraries/LibGfx/JPGLoader.cpp | 10 +++------- Userland/Libraries/LibGfx/JPGLoader.h | 4 +++- Userland/Libraries/LibGfx/PBMLoader.cpp | 7 ++----- Userland/Libraries/LibGfx/PBMLoader.h | 3 ++- Userland/Libraries/LibGfx/PGMLoader.cpp | 7 ++----- Userland/Libraries/LibGfx/PGMLoader.h | 3 ++- Userland/Libraries/LibGfx/PNGLoader.cpp | 9 +++------ Userland/Libraries/LibGfx/PNGLoader.h | 3 ++- Userland/Libraries/LibGfx/PPMLoader.cpp | 7 ++----- Userland/Libraries/LibGfx/PPMLoader.h | 3 ++- .../LibGfx/PortableImageLoaderCommon.h | 18 +++++++++++------- 19 files changed, 54 insertions(+), 70 deletions(-) diff --git a/Userland/Libraries/LibGfx/BMPLoader.cpp b/Userland/Libraries/LibGfx/BMPLoader.cpp index 17c690629d..4973622a0c 100644 --- a/Userland/Libraries/LibGfx/BMPLoader.cpp +++ b/Userland/Libraries/LibGfx/BMPLoader.cpp @@ -171,17 +171,14 @@ RefPtr load_bmp(String const& path) auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) return nullptr; - auto bitmap = load_bmp_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded BMP: {}", bitmap->size(), LexicalPath::canonicalized_path(path))); - return bitmap; + return load_bmp_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path)); } -RefPtr load_bmp_from_memory(const u8* data, size_t length) +RefPtr load_bmp_from_memory(u8 const* data, size_t length, String const& mmap_name) { auto bitmap = load_bmp_impl(data, length); if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded BMP: ", bitmap->size())); + bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded BMP: {}", bitmap->size(), mmap_name)); return bitmap; } diff --git a/Userland/Libraries/LibGfx/BMPLoader.h b/Userland/Libraries/LibGfx/BMPLoader.h index c73e94857e..1503bc3680 100644 --- a/Userland/Libraries/LibGfx/BMPLoader.h +++ b/Userland/Libraries/LibGfx/BMPLoader.h @@ -6,13 +6,14 @@ #pragma once +#include #include #include namespace Gfx { RefPtr load_bmp(String const& path); -RefPtr load_bmp_from_memory(const u8*, size_t); +RefPtr load_bmp_from_memory(u8 const*, size_t, String const& mmap_name = ""); struct BMPLoadingContext; diff --git a/Userland/Libraries/LibGfx/DDSLoader.cpp b/Userland/Libraries/LibGfx/DDSLoader.cpp index 5bf294919e..430bcdeab3 100644 --- a/Userland/Libraries/LibGfx/DDSLoader.cpp +++ b/Userland/Libraries/LibGfx/DDSLoader.cpp @@ -957,17 +957,14 @@ RefPtr load_dds(String const& path) auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) return nullptr; - auto bitmap = load_dds_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded DDS: {}", bitmap->size(), LexicalPath::canonicalized_path(path))); - return bitmap; + return load_dds_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path)); } -RefPtr load_dds_from_memory(const u8* data, size_t length) +RefPtr load_dds_from_memory(u8 const* data, size_t length, String const& mmap_name) { auto bitmap = load_dds_impl(data, length); if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded DDS: ", bitmap->size())); + bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded DDS: {}", bitmap->size(), mmap_name)); return bitmap; } diff --git a/Userland/Libraries/LibGfx/DDSLoader.h b/Userland/Libraries/LibGfx/DDSLoader.h index 4179a956d1..852f410473 100644 --- a/Userland/Libraries/LibGfx/DDSLoader.h +++ b/Userland/Libraries/LibGfx/DDSLoader.h @@ -6,6 +6,7 @@ #pragma once +#include #include #include @@ -233,7 +234,7 @@ struct DDSHeaderDXT10 { }; RefPtr load_dds(String const& path); -RefPtr load_dds_from_memory(const u8*, size_t); +RefPtr load_dds_from_memory(u8 const*, size_t, String const& mmap_name = ""); struct DDSLoadingContext; diff --git a/Userland/Libraries/LibGfx/GIFLoader.cpp b/Userland/Libraries/LibGfx/GIFLoader.cpp index 679cc8f461..53d0772f6d 100644 --- a/Userland/Libraries/LibGfx/GIFLoader.cpp +++ b/Userland/Libraries/LibGfx/GIFLoader.cpp @@ -87,19 +87,15 @@ RefPtr load_gif(String const& path) auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) return nullptr; - GIFImageDecoderPlugin gif_decoder((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); - auto bitmap = gif_decoder.bitmap(); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded GIF: {}", bitmap->size(), LexicalPath::canonicalized_path(path))); - return bitmap; + return load_gif_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path)); } -RefPtr load_gif_from_memory(const u8* data, size_t length) +RefPtr load_gif_from_memory(u8 const* data, size_t length, String const& mmap_name) { GIFImageDecoderPlugin gif_decoder(data, length); auto bitmap = gif_decoder.bitmap(); if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded GIF: ", bitmap->size())); + bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded GIF: {}", bitmap->size(), mmap_name)); return bitmap; } diff --git a/Userland/Libraries/LibGfx/GIFLoader.h b/Userland/Libraries/LibGfx/GIFLoader.h index 2227294e46..07eb4d6286 100644 --- a/Userland/Libraries/LibGfx/GIFLoader.h +++ b/Userland/Libraries/LibGfx/GIFLoader.h @@ -6,13 +6,14 @@ #pragma once +#include #include #include namespace Gfx { RefPtr load_gif(String const& path); -RefPtr load_gif_from_memory(const u8*, size_t); +RefPtr load_gif_from_memory(u8 const*, size_t, String const& mmap_name = ""); struct GIFLoadingContext; diff --git a/Userland/Libraries/LibGfx/ICOLoader.cpp b/Userland/Libraries/LibGfx/ICOLoader.cpp index cc0ee6dc05..6102ea5898 100644 --- a/Userland/Libraries/LibGfx/ICOLoader.cpp +++ b/Userland/Libraries/LibGfx/ICOLoader.cpp @@ -96,19 +96,15 @@ RefPtr load_ico(const StringView& path) auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) return nullptr; - ICOImageDecoderPlugin decoder((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); - auto bitmap = decoder.bitmap(); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded ICO: {}", bitmap->size(), LexicalPath::canonicalized_path(path))); - return bitmap; + return load_ico_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path)); } -RefPtr load_ico_from_memory(const u8* data, size_t length) +RefPtr load_ico_from_memory(u8 const* data, size_t length, String const& mmap_name) { ICOImageDecoderPlugin decoder(data, length); auto bitmap = decoder.bitmap(); if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded ICO: ", bitmap->size())); + bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded ICO: {}", bitmap->size(), mmap_name)); return bitmap; } diff --git a/Userland/Libraries/LibGfx/ICOLoader.h b/Userland/Libraries/LibGfx/ICOLoader.h index f7fa88502a..3c0fd8ba3f 100644 --- a/Userland/Libraries/LibGfx/ICOLoader.h +++ b/Userland/Libraries/LibGfx/ICOLoader.h @@ -6,13 +6,14 @@ #pragma once +#include #include #include namespace Gfx { RefPtr load_ico(const StringView& path); -RefPtr load_ico_from_memory(const u8*, size_t); +RefPtr load_ico_from_memory(u8 const*, size_t, String const& mmap_name = ""); struct ICOLoadingContext; diff --git a/Userland/Libraries/LibGfx/JPGLoader.cpp b/Userland/Libraries/LibGfx/JPGLoader.cpp index 0169e03316..0784044bbe 100644 --- a/Userland/Libraries/LibGfx/JPGLoader.cpp +++ b/Userland/Libraries/LibGfx/JPGLoader.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include #include @@ -1238,17 +1237,14 @@ RefPtr load_jpg(String const& path) auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) return nullptr; - auto bitmap = load_jpg_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded JPG: {}", bitmap->size(), LexicalPath::canonicalized_path(path))); - return bitmap; + return load_jpg_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), path); } -RefPtr load_jpg_from_memory(const u8* data, size_t length) +RefPtr load_jpg_from_memory(u8 const* data, size_t length, String const& mmap_name) { auto bitmap = load_jpg_impl(data, length); if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded jpg: ", bitmap->size())); + bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded jpg: {}", bitmap->size(), mmap_name)); return bitmap; } diff --git a/Userland/Libraries/LibGfx/JPGLoader.h b/Userland/Libraries/LibGfx/JPGLoader.h index 549eeee019..04741fa978 100644 --- a/Userland/Libraries/LibGfx/JPGLoader.h +++ b/Userland/Libraries/LibGfx/JPGLoader.h @@ -6,6 +6,8 @@ #pragma once +#include +#include #include #include #include @@ -14,7 +16,7 @@ namespace Gfx { RefPtr load_jpg(String const& path); -RefPtr load_jpg_from_memory(const u8* data, size_t length); +RefPtr load_jpg_from_memory(u8 const* data, size_t length, String const& mmap_name = ""); struct JPGLoadingContext; diff --git a/Userland/Libraries/LibGfx/PBMLoader.cpp b/Userland/Libraries/LibGfx/PBMLoader.cpp index 573c174fc4..a90cd05c80 100644 --- a/Userland/Libraries/LibGfx/PBMLoader.cpp +++ b/Userland/Libraries/LibGfx/PBMLoader.cpp @@ -102,12 +102,9 @@ RefPtr load_pbm(const StringView& path) return load(path); } -RefPtr load_pbm_from_memory(const u8* data, size_t length) +RefPtr load_pbm_from_memory(u8 const* data, size_t length, String const& mmap_name) { - auto bitmap = load_impl(data, length); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PBM: ", bitmap->size())); - return bitmap; + return load_from_memory(data, length, mmap_name); } PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size) diff --git a/Userland/Libraries/LibGfx/PBMLoader.h b/Userland/Libraries/LibGfx/PBMLoader.h index 06fd5ecb75..1c022f9e45 100644 --- a/Userland/Libraries/LibGfx/PBMLoader.h +++ b/Userland/Libraries/LibGfx/PBMLoader.h @@ -6,13 +6,14 @@ #pragma once +#include #include #include namespace Gfx { RefPtr load_pbm(const StringView& path); -RefPtr load_pbm_from_memory(const u8*, size_t); +RefPtr load_pbm_from_memory(u8 const*, size_t, String const& mmap_name = ""); struct PBMLoadingContext; diff --git a/Userland/Libraries/LibGfx/PGMLoader.cpp b/Userland/Libraries/LibGfx/PGMLoader.cpp index 79323d0981..be2aad4821 100644 --- a/Userland/Libraries/LibGfx/PGMLoader.cpp +++ b/Userland/Libraries/LibGfx/PGMLoader.cpp @@ -104,12 +104,9 @@ RefPtr load_pgm(const StringView& path) return load(path); } -RefPtr load_pgm_from_memory(const u8* data, size_t length) +RefPtr load_pgm_from_memory(u8 const* data, size_t length, String const& mmap_name) { - auto bitmap = load_impl(data, length); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PGM: ", bitmap->size())); - return bitmap; + return load_from_memory(data, length, mmap_name); } PGMImageDecoderPlugin::PGMImageDecoderPlugin(const u8* data, size_t size) diff --git a/Userland/Libraries/LibGfx/PGMLoader.h b/Userland/Libraries/LibGfx/PGMLoader.h index bb2ab9c15b..dab535e3be 100644 --- a/Userland/Libraries/LibGfx/PGMLoader.h +++ b/Userland/Libraries/LibGfx/PGMLoader.h @@ -6,13 +6,14 @@ #pragma once +#include #include #include namespace Gfx { RefPtr load_pgm(const StringView& path); -RefPtr load_pgm_from_memory(const u8*, size_t); +RefPtr load_pgm_from_memory(u8 const*, size_t, String const& mmap_name = ""); struct PGMLoadingContext; diff --git a/Userland/Libraries/LibGfx/PNGLoader.cpp b/Userland/Libraries/LibGfx/PNGLoader.cpp index 2f3512a831..70f458a211 100644 --- a/Userland/Libraries/LibGfx/PNGLoader.cpp +++ b/Userland/Libraries/LibGfx/PNGLoader.cpp @@ -174,17 +174,14 @@ RefPtr load_png(String const& path) auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) return nullptr; - auto bitmap = load_png_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PNG: {}", bitmap->size(), LexicalPath::canonicalized_path(path))); - return bitmap; + return load_png_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path)); } -RefPtr load_png_from_memory(const u8* data, size_t length) +RefPtr load_png_from_memory(u8 const* data, size_t length, String const& mmap_name) { auto bitmap = load_png_impl(data, length); if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PNG: ", bitmap->size())); + bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PNG: {}", bitmap->size(), mmap_name)); return bitmap; } diff --git a/Userland/Libraries/LibGfx/PNGLoader.h b/Userland/Libraries/LibGfx/PNGLoader.h index 49f5c17906..56b0860c3b 100644 --- a/Userland/Libraries/LibGfx/PNGLoader.h +++ b/Userland/Libraries/LibGfx/PNGLoader.h @@ -6,13 +6,14 @@ #pragma once +#include #include #include namespace Gfx { RefPtr load_png(String const& path); -RefPtr load_png_from_memory(const u8*, size_t); +RefPtr load_png_from_memory(u8 const*, size_t, String const& mmap_name = ""); struct PNGLoadingContext; diff --git a/Userland/Libraries/LibGfx/PPMLoader.cpp b/Userland/Libraries/LibGfx/PPMLoader.cpp index 41d9587941..0d8112a0ae 100644 --- a/Userland/Libraries/LibGfx/PPMLoader.cpp +++ b/Userland/Libraries/LibGfx/PPMLoader.cpp @@ -106,12 +106,9 @@ RefPtr load_ppm(const StringView& path) return load(path); } -RefPtr load_ppm_from_memory(const u8* data, size_t length) +RefPtr load_ppm_from_memory(u8 const* data, size_t length, String const& mmap_name) { - auto bitmap = load_impl(data, length); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PPM: ", bitmap->size())); - return bitmap; + return load_from_memory(data, length, mmap_name); } PPMImageDecoderPlugin::PPMImageDecoderPlugin(const u8* data, size_t size) diff --git a/Userland/Libraries/LibGfx/PPMLoader.h b/Userland/Libraries/LibGfx/PPMLoader.h index 0e20adf3ab..5668cfc8f1 100644 --- a/Userland/Libraries/LibGfx/PPMLoader.h +++ b/Userland/Libraries/LibGfx/PPMLoader.h @@ -6,13 +6,14 @@ #pragma once +#include #include #include namespace Gfx { RefPtr load_ppm(const StringView& path); -RefPtr load_ppm_from_memory(const u8*, size_t); +RefPtr load_ppm_from_memory(u8 const*, size_t, String const& mmap_name = ""); struct PPMLoadingContext; diff --git a/Userland/Libraries/LibGfx/PortableImageLoaderCommon.h b/Userland/Libraries/LibGfx/PortableImageLoaderCommon.h index 0130f07654..569694fbc3 100644 --- a/Userland/Libraries/LibGfx/PortableImageLoaderCommon.h +++ b/Userland/Libraries/LibGfx/PortableImageLoaderCommon.h @@ -261,19 +261,23 @@ static RefPtr load_impl(const u8* data, size_t data_size) } return context.bitmap; } + +template +static RefPtr load_from_memory(u8 const* data, size_t length, String const& mmap_name) +{ + auto bitmap = load_impl(data, length); + if (bitmap) + bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded {}: {}", bitmap->size(), TContext::image_type, mmap_name)); + return bitmap; +} + template static RefPtr load(const StringView& path) { auto file_or_error = MappedFile::map(path); if (file_or_error.is_error()) return nullptr; - auto bitmap = load_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); - if (bitmap) - bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded {}: {}", - bitmap->size(), - TContext::image_type, - LexicalPath::canonicalized_path(path))); - return bitmap; + return load_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path)); } }