mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 21:57:35 +00:00
LibGfx: Move common loader functionality to load from memory functions
This will share functionality between the load from path and load from memory functions.
This commit is contained in:
parent
eb5320023a
commit
c966c32571
19 changed files with 54 additions and 70 deletions
|
@ -171,17 +171,14 @@ RefPtr<Gfx::Bitmap> 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<Gfx::Bitmap> load_bmp_from_memory(const u8* data, size_t length)
|
||||
RefPtr<Gfx::Bitmap> 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: <memory>", bitmap->size()));
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded BMP: {}", bitmap->size(), mmap_name));
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,13 +6,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ImageDecoder.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_bmp(String const& path);
|
||||
RefPtr<Gfx::Bitmap> load_bmp_from_memory(const u8*, size_t);
|
||||
RefPtr<Gfx::Bitmap> load_bmp_from_memory(u8 const*, size_t, String const& mmap_name = "<memory>");
|
||||
|
||||
struct BMPLoadingContext;
|
||||
|
||||
|
|
|
@ -957,17 +957,14 @@ RefPtr<Gfx::Bitmap> 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<Gfx::Bitmap> load_dds_from_memory(const u8* data, size_t length)
|
||||
RefPtr<Gfx::Bitmap> 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: <memory>", bitmap->size()));
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded DDS: {}", bitmap->size(), mmap_name));
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ImageDecoder.h>
|
||||
|
||||
|
@ -233,7 +234,7 @@ struct DDSHeaderDXT10 {
|
|||
};
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_dds(String const& path);
|
||||
RefPtr<Gfx::Bitmap> load_dds_from_memory(const u8*, size_t);
|
||||
RefPtr<Gfx::Bitmap> load_dds_from_memory(u8 const*, size_t, String const& mmap_name = "<memory>");
|
||||
|
||||
struct DDSLoadingContext;
|
||||
|
||||
|
|
|
@ -87,19 +87,15 @@ RefPtr<Gfx::Bitmap> 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<Gfx::Bitmap> load_gif_from_memory(const u8* data, size_t length)
|
||||
RefPtr<Gfx::Bitmap> 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: <memory>", bitmap->size()));
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded GIF: {}", bitmap->size(), mmap_name));
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,13 +6,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ImageDecoder.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_gif(String const& path);
|
||||
RefPtr<Gfx::Bitmap> load_gif_from_memory(const u8*, size_t);
|
||||
RefPtr<Gfx::Bitmap> load_gif_from_memory(u8 const*, size_t, String const& mmap_name = "<memory>");
|
||||
|
||||
struct GIFLoadingContext;
|
||||
|
||||
|
|
|
@ -96,19 +96,15 @@ RefPtr<Gfx::Bitmap> 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<Gfx::Bitmap> load_ico_from_memory(const u8* data, size_t length)
|
||||
RefPtr<Gfx::Bitmap> 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: <memory>", bitmap->size()));
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded ICO: {}", bitmap->size(), mmap_name));
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,13 +6,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ImageDecoder.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_ico(const StringView& path);
|
||||
RefPtr<Gfx::Bitmap> load_ico_from_memory(const u8*, size_t);
|
||||
RefPtr<Gfx::Bitmap> load_ico_from_memory(u8 const*, size_t, String const& mmap_name = "<memory>");
|
||||
|
||||
struct ICOLoadingContext;
|
||||
|
||||
|
|
|
@ -9,7 +9,6 @@
|
|||
#include <AK/Debug.h>
|
||||
#include <AK/HashMap.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/MappedFile.h>
|
||||
#include <AK/Math.h>
|
||||
#include <AK/MemoryStream.h>
|
||||
#include <AK/String.h>
|
||||
|
@ -1238,17 +1237,14 @@ RefPtr<Gfx::Bitmap> 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<Gfx::Bitmap> load_jpg_from_memory(const u8* data, size_t length)
|
||||
RefPtr<Gfx::Bitmap> 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: <memory>", bitmap->size()));
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded jpg: {}", bitmap->size(), mmap_name));
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/MappedFile.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ImageDecoder.h>
|
||||
|
@ -14,7 +16,7 @@
|
|||
namespace Gfx {
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_jpg(String const& path);
|
||||
RefPtr<Gfx::Bitmap> load_jpg_from_memory(const u8* data, size_t length);
|
||||
RefPtr<Gfx::Bitmap> load_jpg_from_memory(u8 const* data, size_t length, String const& mmap_name = "<memory>");
|
||||
|
||||
struct JPGLoadingContext;
|
||||
|
||||
|
|
|
@ -102,12 +102,9 @@ RefPtr<Gfx::Bitmap> load_pbm(const StringView& path)
|
|||
return load<PBMLoadingContext>(path);
|
||||
}
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_pbm_from_memory(const u8* data, size_t length)
|
||||
RefPtr<Gfx::Bitmap> load_pbm_from_memory(u8 const* data, size_t length, String const& mmap_name)
|
||||
{
|
||||
auto bitmap = load_impl<PBMLoadingContext>(data, length);
|
||||
if (bitmap)
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PBM: <memory>", bitmap->size()));
|
||||
return bitmap;
|
||||
return load_from_memory<PBMLoadingContext>(data, length, mmap_name);
|
||||
}
|
||||
|
||||
PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size)
|
||||
|
|
|
@ -6,13 +6,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ImageDecoder.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_pbm(const StringView& path);
|
||||
RefPtr<Gfx::Bitmap> load_pbm_from_memory(const u8*, size_t);
|
||||
RefPtr<Gfx::Bitmap> load_pbm_from_memory(u8 const*, size_t, String const& mmap_name = "<memory>");
|
||||
|
||||
struct PBMLoadingContext;
|
||||
|
||||
|
|
|
@ -104,12 +104,9 @@ RefPtr<Gfx::Bitmap> load_pgm(const StringView& path)
|
|||
return load<PGMLoadingContext>(path);
|
||||
}
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_pgm_from_memory(const u8* data, size_t length)
|
||||
RefPtr<Gfx::Bitmap> load_pgm_from_memory(u8 const* data, size_t length, String const& mmap_name)
|
||||
{
|
||||
auto bitmap = load_impl<PGMLoadingContext>(data, length);
|
||||
if (bitmap)
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PGM: <memory>", bitmap->size()));
|
||||
return bitmap;
|
||||
return load_from_memory<PGMLoadingContext>(data, length, mmap_name);
|
||||
}
|
||||
|
||||
PGMImageDecoderPlugin::PGMImageDecoderPlugin(const u8* data, size_t size)
|
||||
|
|
|
@ -6,13 +6,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ImageDecoder.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_pgm(const StringView& path);
|
||||
RefPtr<Gfx::Bitmap> load_pgm_from_memory(const u8*, size_t);
|
||||
RefPtr<Gfx::Bitmap> load_pgm_from_memory(u8 const*, size_t, String const& mmap_name = "<memory>");
|
||||
|
||||
struct PGMLoadingContext;
|
||||
|
||||
|
|
|
@ -174,17 +174,14 @@ RefPtr<Gfx::Bitmap> 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<Gfx::Bitmap> load_png_from_memory(const u8* data, size_t length)
|
||||
RefPtr<Gfx::Bitmap> 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: <memory>", bitmap->size()));
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PNG: {}", bitmap->size(), mmap_name));
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
|
|
|
@ -6,13 +6,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ImageDecoder.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_png(String const& path);
|
||||
RefPtr<Gfx::Bitmap> load_png_from_memory(const u8*, size_t);
|
||||
RefPtr<Gfx::Bitmap> load_png_from_memory(u8 const*, size_t, String const& mmap_name = "<memory>");
|
||||
|
||||
struct PNGLoadingContext;
|
||||
|
||||
|
|
|
@ -106,12 +106,9 @@ RefPtr<Gfx::Bitmap> load_ppm(const StringView& path)
|
|||
return load<PPMLoadingContext>(path);
|
||||
}
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_ppm_from_memory(const u8* data, size_t length)
|
||||
RefPtr<Gfx::Bitmap> load_ppm_from_memory(u8 const* data, size_t length, String const& mmap_name)
|
||||
{
|
||||
auto bitmap = load_impl<PPMLoadingContext>(data, length);
|
||||
if (bitmap)
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PPM: <memory>", bitmap->size()));
|
||||
return bitmap;
|
||||
return load_from_memory<PPMLoadingContext>(data, length, mmap_name);
|
||||
}
|
||||
|
||||
PPMImageDecoderPlugin::PPMImageDecoderPlugin(const u8* data, size_t size)
|
||||
|
|
|
@ -6,13 +6,14 @@
|
|||
|
||||
#pragma once
|
||||
|
||||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ImageDecoder.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
RefPtr<Gfx::Bitmap> load_ppm(const StringView& path);
|
||||
RefPtr<Gfx::Bitmap> load_ppm_from_memory(const u8*, size_t);
|
||||
RefPtr<Gfx::Bitmap> load_ppm_from_memory(u8 const*, size_t, String const& mmap_name = "<memory>");
|
||||
|
||||
struct PPMLoadingContext;
|
||||
|
||||
|
|
|
@ -261,19 +261,23 @@ static RefPtr<Gfx::Bitmap> load_impl(const u8* data, size_t data_size)
|
|||
}
|
||||
return context.bitmap;
|
||||
}
|
||||
|
||||
template<typename TContext>
|
||||
static RefPtr<Gfx::Bitmap> load_from_memory(u8 const* data, size_t length, String const& mmap_name)
|
||||
{
|
||||
auto bitmap = load_impl<TContext>(data, length);
|
||||
if (bitmap)
|
||||
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded {}: {}", bitmap->size(), TContext::image_type, mmap_name));
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
template<typename TContext>
|
||||
static RefPtr<Gfx::Bitmap> load(const StringView& path)
|
||||
{
|
||||
auto file_or_error = MappedFile::map(path);
|
||||
if (file_or_error.is_error())
|
||||
return nullptr;
|
||||
auto bitmap = load_impl<TContext>((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<TContext>((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue