1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:57:45 +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:
Timothy 2021-09-07 21:13:29 +10:00 committed by Andreas Kling
parent eb5320023a
commit c966c32571
19 changed files with 54 additions and 70 deletions

View file

@ -171,17 +171,14 @@ RefPtr<Gfx::Bitmap> load_bmp(String const& path)
auto file_or_error = MappedFile::map(path); auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error()) if (file_or_error.is_error())
return nullptr; return nullptr;
auto bitmap = load_bmp_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); return load_bmp_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path));
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded BMP: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;
} }
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); auto bitmap = load_bmp_impl(data, length);
if (bitmap) 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; return bitmap;
} }

View file

@ -6,13 +6,14 @@
#pragma once #pragma once
#include <AK/String.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
namespace Gfx { namespace Gfx {
RefPtr<Gfx::Bitmap> load_bmp(String const& path); 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; struct BMPLoadingContext;

View file

@ -957,17 +957,14 @@ RefPtr<Gfx::Bitmap> load_dds(String const& path)
auto file_or_error = MappedFile::map(path); auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error()) if (file_or_error.is_error())
return nullptr; return nullptr;
auto bitmap = load_dds_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); return load_dds_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path));
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded DDS: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;
} }
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); auto bitmap = load_dds_impl(data, length);
if (bitmap) 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; return bitmap;
} }

View file

@ -6,6 +6,7 @@
#pragma once #pragma once
#include <AK/String.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
@ -233,7 +234,7 @@ struct DDSHeaderDXT10 {
}; };
RefPtr<Gfx::Bitmap> load_dds(String const& path); 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; struct DDSLoadingContext;

View file

@ -87,19 +87,15 @@ RefPtr<Gfx::Bitmap> load_gif(String const& path)
auto file_or_error = MappedFile::map(path); auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error()) if (file_or_error.is_error())
return nullptr; return nullptr;
GIFImageDecoderPlugin gif_decoder((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); return load_gif_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path));
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;
} }
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); GIFImageDecoderPlugin gif_decoder(data, length);
auto bitmap = gif_decoder.bitmap(); auto bitmap = gif_decoder.bitmap();
if (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; return bitmap;
} }

View file

@ -6,13 +6,14 @@
#pragma once #pragma once
#include <AK/String.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
namespace Gfx { namespace Gfx {
RefPtr<Gfx::Bitmap> load_gif(String const& path); 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; struct GIFLoadingContext;

View file

@ -96,19 +96,15 @@ RefPtr<Gfx::Bitmap> load_ico(const StringView& path)
auto file_or_error = MappedFile::map(path); auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error()) if (file_or_error.is_error())
return nullptr; return nullptr;
ICOImageDecoderPlugin decoder((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); return load_ico_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path));
auto bitmap = decoder.bitmap();
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded ICO: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;
} }
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); ICOImageDecoderPlugin decoder(data, length);
auto bitmap = decoder.bitmap(); auto bitmap = decoder.bitmap();
if (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; return bitmap;
} }

View file

@ -6,13 +6,14 @@
#pragma once #pragma once
#include <AK/String.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
namespace Gfx { namespace Gfx {
RefPtr<Gfx::Bitmap> load_ico(const StringView& path); 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; struct ICOLoadingContext;

View file

@ -9,7 +9,6 @@
#include <AK/Debug.h> #include <AK/Debug.h>
#include <AK/HashMap.h> #include <AK/HashMap.h>
#include <AK/LexicalPath.h> #include <AK/LexicalPath.h>
#include <AK/MappedFile.h>
#include <AK/Math.h> #include <AK/Math.h>
#include <AK/MemoryStream.h> #include <AK/MemoryStream.h>
#include <AK/String.h> #include <AK/String.h>
@ -1238,17 +1237,14 @@ RefPtr<Gfx::Bitmap> load_jpg(String const& path)
auto file_or_error = MappedFile::map(path); auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error()) if (file_or_error.is_error())
return nullptr; return nullptr;
auto bitmap = load_jpg_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); return load_jpg_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), path);
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded JPG: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;
} }
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); auto bitmap = load_jpg_impl(data, length);
if (bitmap) 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; return bitmap;
} }

View file

@ -6,6 +6,8 @@
#pragma once #pragma once
#include <AK/MappedFile.h>
#include <AK/String.h>
#include <AK/Vector.h> #include <AK/Vector.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
@ -14,7 +16,7 @@
namespace Gfx { namespace Gfx {
RefPtr<Gfx::Bitmap> load_jpg(String const& path); 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; struct JPGLoadingContext;

View file

@ -102,12 +102,9 @@ RefPtr<Gfx::Bitmap> load_pbm(const StringView& path)
return load<PBMLoadingContext>(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); return load_from_memory<PBMLoadingContext>(data, length, mmap_name);
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PBM: <memory>", bitmap->size()));
return bitmap;
} }
PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size) PBMImageDecoderPlugin::PBMImageDecoderPlugin(const u8* data, size_t size)

View file

@ -6,13 +6,14 @@
#pragma once #pragma once
#include <AK/String.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
namespace Gfx { namespace Gfx {
RefPtr<Gfx::Bitmap> load_pbm(const StringView& path); 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; struct PBMLoadingContext;

View file

@ -104,12 +104,9 @@ RefPtr<Gfx::Bitmap> load_pgm(const StringView& path)
return load<PGMLoadingContext>(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); return load_from_memory<PGMLoadingContext>(data, length, mmap_name);
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PGM: <memory>", bitmap->size()));
return bitmap;
} }
PGMImageDecoderPlugin::PGMImageDecoderPlugin(const u8* data, size_t size) PGMImageDecoderPlugin::PGMImageDecoderPlugin(const u8* data, size_t size)

View file

@ -6,13 +6,14 @@
#pragma once #pragma once
#include <AK/String.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
namespace Gfx { namespace Gfx {
RefPtr<Gfx::Bitmap> load_pgm(const StringView& path); 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; struct PGMLoadingContext;

View file

@ -174,17 +174,14 @@ RefPtr<Gfx::Bitmap> load_png(String const& path)
auto file_or_error = MappedFile::map(path); auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error()) if (file_or_error.is_error())
return nullptr; return nullptr;
auto bitmap = load_png_impl((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); return load_png_from_memory((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path));
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PNG: {}", bitmap->size(), LexicalPath::canonicalized_path(path)));
return bitmap;
} }
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); auto bitmap = load_png_impl(data, length);
if (bitmap) 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; return bitmap;
} }

View file

@ -6,13 +6,14 @@
#pragma once #pragma once
#include <AK/String.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
namespace Gfx { namespace Gfx {
RefPtr<Gfx::Bitmap> load_png(String const& path); 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; struct PNGLoadingContext;

View file

@ -106,12 +106,9 @@ RefPtr<Gfx::Bitmap> load_ppm(const StringView& path)
return load<PPMLoadingContext>(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); return load_from_memory<PPMLoadingContext>(data, length, mmap_name);
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded PPM: <memory>", bitmap->size()));
return bitmap;
} }
PPMImageDecoderPlugin::PPMImageDecoderPlugin(const u8* data, size_t size) PPMImageDecoderPlugin::PPMImageDecoderPlugin(const u8* data, size_t size)

View file

@ -6,13 +6,14 @@
#pragma once #pragma once
#include <AK/String.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/ImageDecoder.h> #include <LibGfx/ImageDecoder.h>
namespace Gfx { namespace Gfx {
RefPtr<Gfx::Bitmap> load_ppm(const StringView& path); 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; struct PPMLoadingContext;

View file

@ -261,19 +261,23 @@ static RefPtr<Gfx::Bitmap> load_impl(const u8* data, size_t data_size)
} }
return context.bitmap; 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> template<typename TContext>
static RefPtr<Gfx::Bitmap> load(const StringView& path) static RefPtr<Gfx::Bitmap> load(const StringView& path)
{ {
auto file_or_error = MappedFile::map(path); auto file_or_error = MappedFile::map(path);
if (file_or_error.is_error()) if (file_or_error.is_error())
return nullptr; return nullptr;
auto bitmap = load_impl<TContext>((const u8*)file_or_error.value()->data(), file_or_error.value()->size()); return load_from_memory<TContext>((u8 const*)file_or_error.value()->data(), file_or_error.value()->size(), LexicalPath::canonicalized_path(path));
if (bitmap)
bitmap->set_mmap_name(String::formatted("Gfx::Bitmap [{}] - Decoded {}: {}",
bitmap->size(),
TContext::image_type,
LexicalPath::canonicalized_path(path)));
return bitmap;
} }
} }