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

LibGfx: Re-work the abstractions of sending image for decoding over IPC

Originally I simply thought that passing file paths is quite OK, but as
Linus pointed to, it turned out that passing file paths to ensure some
files are able to be decoded is awkward because it does not work with
images being served over HTTP.

Therefore, ideally we should just use the MIME type as an optional
argument  to ensure that we can always fallback to use that in case
sniffing for the correct image type has failed so we can still detect
files like with the TGA format, which has no magic bytes.
This commit is contained in:
Liav A 2023-01-20 03:27:10 +02:00 committed by Linus Groh
parent fedd18eb89
commit 6e6999ce57
11 changed files with 34 additions and 91 deletions

View file

@ -20,7 +20,7 @@ void Client::die()
on_death();
}
Optional<DecodedImage> Client::decode_image_with_known_path(DeprecatedString const& path, ReadonlyBytes encoded_data)
Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data, Optional<DeprecatedString> mime_type)
{
if (encoded_data.is_empty())
return {};
@ -33,44 +33,7 @@ Optional<DecodedImage> Client::decode_image_with_known_path(DeprecatedString con
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())
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(move(encoded_buffer));
auto response_or_error = try_decode_image(move(encoded_buffer), mime_type);
if (response_or_error.is_error()) {
dbgln("ImageDecoder died heroically");