2022-09-16 16:15:14 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2023-08-02 21:01:17 +03:00
|
|
|
#include "ImageCodecPlugin.h"
|
2022-09-16 16:15:14 +03:00
|
|
|
#include <LibGfx/Bitmap.h>
|
2023-03-21 21:58:06 +03:00
|
|
|
#include <LibGfx/ImageFormats/ImageDecoder.h>
|
2022-09-16 16:15:14 +03:00
|
|
|
|
|
|
|
namespace Ladybird {
|
|
|
|
|
2023-08-02 21:01:17 +03:00
|
|
|
ImageCodecPlugin::~ImageCodecPlugin() = default;
|
2022-09-16 16:15:14 +03:00
|
|
|
|
2023-08-02 21:01:17 +03:00
|
|
|
Optional<Web::Platform::DecodedImage> ImageCodecPlugin::decode_image(ReadonlyBytes data)
|
2022-09-16 16:15:14 +03:00
|
|
|
{
|
2023-01-15 22:50:32 +03:00
|
|
|
auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(data);
|
2022-09-16 16:15:14 +03:00
|
|
|
|
|
|
|
if (!decoder || !decoder->frame_count()) {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Web::Platform::Frame> frames;
|
|
|
|
for (size_t i = 0; i < decoder->frame_count(); ++i) {
|
|
|
|
auto frame_or_error = decoder->frame(i);
|
2023-06-02 04:07:55 +03:00
|
|
|
if (frame_or_error.is_error())
|
|
|
|
return {};
|
|
|
|
auto frame = frame_or_error.release_value();
|
|
|
|
frames.append({ move(frame.image), static_cast<size_t>(frame.duration) });
|
2022-09-16 16:15:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return Web::Platform::DecodedImage {
|
|
|
|
decoder->is_animated(),
|
|
|
|
static_cast<u32>(decoder->loop_count()),
|
|
|
|
move(frames),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|