summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibImageDecoderClient/Client.cpp
diff options
context:
space:
mode:
authorLiav A <liavalb@gmail.com>2023-01-15 21:50:32 +0200
committerJelle Raaijmakers <jelle@gmta.nl>2023-01-18 21:48:35 +0100
commit649f78d0a4475a640ad353e1e879a7bb27db222b (patch)
tree5ef6f843a86a8c9aad5d76d180dc8c5d87ee2065 /Userland/Libraries/LibImageDecoderClient/Client.cpp
parent9f2d4d3fd590177c3b6615c18ae5ec7febdb1522 (diff)
downloadserenity-649f78d0a4475a640ad353e1e879a7bb27db222b.zip
LibGfx+Ladybird+Userland: Don't sniff for TGA images with only raw bytes
Because TGA images don't have magic bytes as a signature to be detected, instead assume a sequence of ReadonlyBytes is a possible TGA image only if we are given a path so we could check the extension of the file and see if it's a TGA image. When we know the path of the file being loaded, we will try to first check its extension, and only if there's no match to a known decoder, based on simple extension lookup, then we would probe for other formats as usual with the normal sniffing method.
Diffstat (limited to 'Userland/Libraries/LibImageDecoderClient/Client.cpp')
-rw-r--r--Userland/Libraries/LibImageDecoderClient/Client.cpp37
1 files changed, 37 insertions, 0 deletions
diff --git a/Userland/Libraries/LibImageDecoderClient/Client.cpp b/Userland/Libraries/LibImageDecoderClient/Client.cpp
index 766902bf20..abb151c279 100644
--- a/Userland/Libraries/LibImageDecoderClient/Client.cpp
+++ b/Userland/Libraries/LibImageDecoderClient/Client.cpp
@@ -20,6 +20,43 @@ void Client::die()
on_death();
}
+Optional<DecodedImage> Client::decode_image_with_known_path(DeprecatedString const& path, ReadonlyBytes encoded_data)
+{
+ if (encoded_data.is_empty())
+ return {};
+
+ auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
+ if (encoded_buffer_or_error.is_error()) {
+ dbgln("Could not allocate encoded buffer");
+ return {};
+ }
+ auto encoded_buffer = encoded_buffer_or_error.release_value();
+
+ memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
+ auto response_or_error = try_decode_image_with_known_path(path, move(encoded_buffer));
+
+ if (response_or_error.is_error()) {
+ dbgln("ImageDecoder died heroically");
+ return {};
+ }
+
+ auto& response = response_or_error.value();
+
+ if (response.bitmaps().is_empty())
+ return {};
+
+ DecodedImage image;
+ image.is_animated = response.is_animated();
+ image.loop_count = response.loop_count();
+ image.frames.resize(response.bitmaps().size());
+ for (size_t i = 0; i < image.frames.size(); ++i) {
+ auto& frame = image.frames[i];
+ frame.bitmap = response.bitmaps()[i].bitmap();
+ frame.duration = response.durations()[i];
+ }
+ return image;
+}
+
Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data)
{
if (encoded_data.is_empty())