1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 15:34:58 +00:00
serenity/Ladybird/ImageCodecPluginLadybird.cpp
Andreas Kling af5d892a7e Ladybird: Don't ask Qt to decode SVG images for us
While it's nice to see <img src="foo.svg"> suddenly work in Ladybird
after linking with the Qt SVG module, this is cheating.

We should implement SVG-as-image ourselves instead of relying on 3rd
party code to do it. :^)
2023-05-07 09:06:48 +02:00

84 lines
2.5 KiB
C++

/*
* Copyright (c) 2022, Dex♪ <dexes.ttp@gmail.com>
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "ImageCodecPluginLadybird.h"
#include <LibGfx/Bitmap.h>
#include <LibGfx/ImageFormats/ImageDecoder.h>
#include <QImage>
namespace Ladybird {
ImageCodecPluginLadybird::~ImageCodecPluginLadybird() = default;
static Optional<Web::Platform::DecodedImage> decode_image_with_qt(ReadonlyBytes data)
{
auto image = QImage::fromData(data.data(), static_cast<int>(data.size()));
if (image.isNull())
return {};
image = image.convertToFormat(QImage::Format::Format_ARGB32);
auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, Gfx::IntSize(image.width(), image.height())));
for (int y = 0; y < image.height(); ++y) {
memcpy(bitmap->scanline_u8(y), image.scanLine(y), image.width() * 4);
}
Vector<Web::Platform::Frame> frames;
frames.append(Web::Platform::Frame {
bitmap,
});
return Web::Platform::DecodedImage {
false,
0,
move(frames),
};
}
static Optional<Web::Platform::DecodedImage> decode_image_with_libgfx(ReadonlyBytes data)
{
auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(data);
if (!decoder || !decoder->frame_count()) {
return {};
}
bool had_errors = false;
Vector<Web::Platform::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 });
had_errors = true;
} else {
auto frame = frame_or_error.release_value();
frames.append({ move(frame.image), static_cast<size_t>(frame.duration) });
}
}
if (had_errors)
return {};
return Web::Platform::DecodedImage {
decoder->is_animated(),
static_cast<u32>(decoder->loop_count()),
move(frames),
};
}
Optional<Web::Platform::DecodedImage> ImageCodecPluginLadybird::decode_image(ReadonlyBytes data)
{
auto image = decode_image_with_libgfx(data);
if (image.has_value())
return image;
// NOTE: Even though Qt can decode SVG images for us, let's not do that.
// We should handle <img src="foo.svg"> ourselves instead of cheating by using Qt.
if (data.starts_with("<?xml"sv.bytes()) || data.starts_with("<svg"sv.bytes()))
return {};
return decode_image_with_qt(data);
}
}