Serenity Operating System
1/*
2 * Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
3 * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include "ImageCodecPluginSerenity.h"
9#include <LibImageDecoderClient/Client.h>
10
11namespace WebContent {
12
13ImageCodecPluginSerenity::ImageCodecPluginSerenity() = default;
14ImageCodecPluginSerenity::~ImageCodecPluginSerenity() = default;
15
16Optional<Web::Platform::DecodedImage> ImageCodecPluginSerenity::decode_image(ReadonlyBytes bytes)
17{
18 if (!m_client) {
19 m_client = ImageDecoderClient::Client::try_create().release_value_but_fixme_should_propagate_errors();
20 m_client->on_death = [&] {
21 m_client = nullptr;
22 };
23 }
24
25 auto result_or_empty = m_client->decode_image(bytes);
26 if (!result_or_empty.has_value())
27 return {};
28 auto result = result_or_empty.release_value();
29
30 Web::Platform::DecodedImage decoded_image;
31 decoded_image.is_animated = result.is_animated;
32 decoded_image.loop_count = result.loop_count;
33 for (auto const& frame : result.frames) {
34 decoded_image.frames.empend(move(frame.bitmap), frame.duration);
35 }
36
37 return decoded_image;
38}
39
40}