Utilities: Add image decoding to headless-browser

With this, the headless browser can now decode images on its own!
This commit is contained in:
DexesTTP 2022-04-03 18:17:42 +02:00 committed by Linus Groh
parent 355e74cf65
commit f9d4c0ecbc
Notes: sideshowbarker 2024-07-17 10:35:07 +09:00

View File

@ -223,9 +223,32 @@ public:
virtual ~HeadlessImageDecoderClient() override = default;
virtual Optional<Web::ImageDecoding::DecodedImage> decode_image(ReadonlyBytes) override
virtual Optional<Web::ImageDecoding::DecodedImage> decode_image(ReadonlyBytes data) override
{
return {};
auto decoder = Gfx::ImageDecoder::try_create(data);
if (!decoder)
return Web::ImageDecoding::DecodedImage { false, 0, Vector<Web::ImageDecoding::Frame> {} };
if (!decoder->frame_count())
return Web::ImageDecoding::DecodedImage { false, 0, Vector<Web::ImageDecoding::Frame> {} };
Vector<Web::ImageDecoding::Frame> frames;
for (size_t i = 0; i < decoder->frame_count(); ++i) {
auto frame_or_error = decoder->frame(i);
if (frame_or_error.is_error()) {
frames.append({ {}, 0 });
} else {
auto frame = frame_or_error.release_value();
frames.append({ move(frame.image), static_cast<size_t>(frame.duration) });
}
}
return Web::ImageDecoding::DecodedImage {
decoder->is_animated(),
static_cast<u32>(decoder->loop_count()),
frames,
};
}
private: