1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 14:38:11 +00:00

LibGfx: Re-structure the whole initialization pattern for image decoders

When trying to figure out the correct implementation, we now have a very
strong distinction on plugins that are well suited for sniffing, and
plugins that need a MIME type to be chosen.

Instead of having multiple calls to non-static virtual sniff methods for
each Image decoding plugin, we have 2 static methods for each
implementation:
1. The sniff method, which in contrast to the old method, gets a
    ReadonlyBytes parameter and ensures we can figure out the result
    with zero heap allocations for most implementations.
2. The create method, which just creates a new instance so we don't
    expose the constructor to everyone anymore.

In addition to that, we have a new virtual method called initialize,
which has a per-implementation initialization pattern to actually ensure
each implementation can construct a decoder object, and then have a
correct context being applied to it for the actual decoding.
This commit is contained in:
Liav A 2023-01-20 10:13:14 +02:00 committed by Linus Groh
parent 6e6999ce57
commit 57e19a7e56
33 changed files with 493 additions and 206 deletions

View file

@ -20,64 +20,58 @@
namespace Gfx {
struct ImagePluginInitializer {
ErrorOr<bool> (*sniff)(ReadonlyBytes) = nullptr;
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
};
static constexpr ImagePluginInitializer s_initializers[] = {
{ PNGImageDecoderPlugin::sniff, PNGImageDecoderPlugin::create },
{ GIFImageDecoderPlugin::sniff, GIFImageDecoderPlugin::create },
{ BMPImageDecoderPlugin::sniff, BMPImageDecoderPlugin::create },
{ PBMImageDecoderPlugin::sniff, PBMImageDecoderPlugin::create },
{ PGMImageDecoderPlugin::sniff, PGMImageDecoderPlugin::create },
{ PPMImageDecoderPlugin::sniff, PPMImageDecoderPlugin::create },
{ ICOImageDecoderPlugin::sniff, ICOImageDecoderPlugin::create },
{ JPGImageDecoderPlugin::sniff, JPGImageDecoderPlugin::create },
{ DDSImageDecoderPlugin::sniff, DDSImageDecoderPlugin::create },
{ QOIImageDecoderPlugin::sniff, QOIImageDecoderPlugin::create },
};
struct ImagePluginWithMIMETypeInitializer {
ErrorOr<bool> (*validate_before_create)(ReadonlyBytes) = nullptr;
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> (*create)(ReadonlyBytes) = nullptr;
StringView mime_type;
};
static constexpr ImagePluginWithMIMETypeInitializer s_initializers_with_mime_type[] = {
{ TGAImageDecoderPlugin::validate_before_create, TGAImageDecoderPlugin::create, "image/x-targa"sv },
};
static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin(ReadonlyBytes bytes)
{
auto* data = bytes.data();
auto size = bytes.size();
OwnPtr<ImageDecoderPlugin> plugin;
plugin = make<PNGImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
plugin = make<GIFImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
plugin = make<BMPImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
plugin = make<PBMImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
plugin = make<PGMImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
plugin = make<PPMImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
plugin = make<ICOImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
plugin = make<JPGImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
plugin = make<DDSImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
plugin = make<QOIImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
for (auto& plugin : s_initializers) {
auto sniff_result = plugin.sniff(bytes).release_value_but_fixme_should_propagate_errors();
if (!sniff_result)
continue;
auto plugin_decoder = plugin.create(bytes).release_value_but_fixme_should_propagate_errors();
if (plugin_decoder->initialize())
return plugin_decoder;
}
return {};
}
static OwnPtr<ImageDecoderPlugin> probe_and_sniff_for_appropriate_plugin_with_known_mime_type(StringView mime_type, ReadonlyBytes bytes)
{
auto* data = bytes.data();
auto size = bytes.size();
OwnPtr<ImageDecoderPlugin> plugin;
if (mime_type == "image/x-targa"sv) {
plugin = make<TGAImageDecoderPlugin>(data, size);
if (plugin->sniff())
return plugin;
for (auto& plugin : s_initializers_with_mime_type) {
if (plugin.mime_type != mime_type)
continue;
auto validation_result = plugin.validate_before_create(bytes).release_value_but_fixme_should_propagate_errors();
if (!validation_result)
continue;
auto plugin_decoder = plugin.create(bytes).release_value_but_fixme_should_propagate_errors();
if (plugin_decoder->initialize())
return plugin_decoder;
}
return {};
}