summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibPDF
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/LibPDF
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/LibPDF')
-rw-r--r--Userland/Libraries/LibPDF/Filter.cpp11
1 files changed, 8 insertions, 3 deletions
diff --git a/Userland/Libraries/LibPDF/Filter.cpp b/Userland/Libraries/LibPDF/Filter.cpp
index 8898e010eb..976c3be9de 100644
--- a/Userland/Libraries/LibPDF/Filter.cpp
+++ b/Userland/Libraries/LibPDF/Filter.cpp
@@ -269,9 +269,14 @@ ErrorOr<ByteBuffer> Filter::decode_jbig2(ReadonlyBytes)
ErrorOr<ByteBuffer> Filter::decode_dct(ReadonlyBytes bytes)
{
- Gfx::JPGImageDecoderPlugin decoder(bytes.data(), bytes.size());
- auto frame = TRY(decoder.frame(0));
- return frame.image->serialize_to_byte_buffer();
+ if (Gfx::JPGImageDecoderPlugin::sniff({ bytes.data(), bytes.size() }).release_value_but_fixme_should_propagate_errors()) {
+ auto decoder = Gfx::JPGImageDecoderPlugin::create({ bytes.data(), bytes.size() }).release_value_but_fixme_should_propagate_errors();
+ if (decoder->initialize()) {
+ auto frame = TRY(decoder->frame(0));
+ return frame.image->serialize_to_byte_buffer();
+ }
+ }
+ return AK::Error::from_string_literal("Not a JPG image!");
};
ErrorOr<ByteBuffer> Filter::decode_jpx(ReadonlyBytes)