summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/DDSLoader.cpp
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2023-01-20 10:13:14 +0200
committerLinus Groh <mail@linusgroh.de>2023-01-20 15:13:31 +0000
commit57e19a7e56a7684ce78e7660bda9fc1f5d9d16f3 (patch)
treed8e4846109693dadc43db559ecba2e8f0cf9ffa8 /Userland/Libraries/LibGfx/DDSLoader.cpp
parent6e6999ce5767121a6904b6c36454723e39b9bf24 (diff)
downloadserenity-57e19a7e56a7684ce78e7660bda9fc1f5d9d16f3.zip
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.
Diffstat (limited to 'Userland/Libraries/LibGfx/DDSLoader.cpp')
-rw-r--r--Userland/Libraries/LibGfx/DDSLoader.cpp17
1 files changed, 16 insertions, 1 deletions
diff --git a/Userland/Libraries/LibGfx/DDSLoader.cpp b/Userland/Libraries/LibGfx/DDSLoader.cpp
index a9ed4641b2..f69f7fccac 100644
--- a/Userland/Libraries/LibGfx/DDSLoader.cpp
+++ b/Userland/Libraries/LibGfx/DDSLoader.cpp
@@ -968,7 +968,7 @@ bool DDSImageDecoderPlugin::set_nonvolatile(bool& was_purged)
return m_context->bitmap->set_nonvolatile(was_purged);
}
-bool DDSImageDecoderPlugin::sniff()
+bool DDSImageDecoderPlugin::initialize()
{
// The header is always at least 128 bytes, so if the file is smaller, it can't be a DDS.
return m_context->data_size > 128
@@ -978,6 +978,21 @@ bool DDSImageDecoderPlugin::sniff()
&& m_context->data[3] == 0x20;
}
+ErrorOr<bool> DDSImageDecoderPlugin::sniff(ReadonlyBytes data)
+{
+ // The header is always at least 128 bytes, so if the file is smaller, it can't be a DDS.
+ return data.size() > 128
+ && data.data()[0] == 0x44
+ && data.data()[1] == 0x44
+ && data.data()[2] == 0x53
+ && data.data()[3] == 0x20;
+}
+
+ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> DDSImageDecoderPlugin::create(ReadonlyBytes data)
+{
+ return adopt_nonnull_own_or_enomem(new (nothrow) DDSImageDecoderPlugin(data.data(), data.size()));
+}
+
bool DDSImageDecoderPlugin::is_animated()
{
return false;