mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-22 09:12:13 -05:00
LibGfx+LibWeb: Do some color management on images with an ICC profile
This patch introduces the `Gfx::ColorSpace` class, this is basically a serializable wrapper for skia's SkColorSpace. Creation of the instances of this class (and thus ICC profiles parsing) is performed in the ImageDecoder process. Then the object is serialized and sent through IPC, to finally be handed to skia for rendering. However, to make sure that we're not making all LibGfx's users dependent on Skia as well, we need to ensure the `Gfx::ColorSpace` object has no dependency on objects from Skia. To that end, the only member of the `ColorSpace` class is the opaque `ColorSpaceImpl` struct. Though, there is on issue with that design, the code in `DisplayListPlayer.cpp` needs access to the underlying `sk_sp<SkColorSpace>`. It is provided by a template function, that is only specialized for this type. Doing this work allows us to pass the following WPT tests: - https://wpt.live/css/css-color/tagged-images-001.html - https://wpt.live/css/css-color/tagged-images-003.html - https://wpt.live/css/css-color/tagged-images-004.html - https://wpt.live/css/css-color/untagged-images-001.html Other test cases can also be found here: - https://github.com/svgeesus/PNG-ICC-tests Note that SkColorSpace support quite a limited amount of color spaces, so color profiles like the ones in [1] or the v4 profiles in [2] are not supported yet. In fact, SkColorSpace only accepts skcms_ICCProfile with a linear conversion to XYZ D50. [1] https://www.color.org/browsertest.xalter [2] https://www.color.org/version4html.xalter
This commit is contained in:
parent
8f8ec146a1
commit
bd93285811
Notes:
github-actions[bot]
2024-12-05 16:17:35 +00:00
Author: https://github.com/LucasChollet Commit: https://github.com/LadybirdBrowser/ladybird/commit/bd932858117 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1764 Reviewed-by: https://github.com/trflynn89
15 changed files with 192 additions and 8 deletions
|
@ -7,6 +7,7 @@ set(SOURCES
|
|||
BitmapSequence.cpp
|
||||
CMYKBitmap.cpp
|
||||
Color.cpp
|
||||
ColorSpace.cpp
|
||||
DeltaE.cpp
|
||||
FontCascadeList.cpp
|
||||
Font/Font.cpp
|
||||
|
|
97
Libraries/LibGfx/ColorSpace.cpp
Normal file
97
Libraries/LibGfx/ColorSpace.cpp
Normal file
|
@ -0,0 +1,97 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Lucas Chollet <lucas.chollet@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/ByteBuffer.h>
|
||||
#include <LibGfx/ColorSpace.h>
|
||||
#include <LibIPC/Decoder.h>
|
||||
#include <LibIPC/Encoder.h>
|
||||
|
||||
#include <core/SkColorSpace.h>
|
||||
#include <core/SkData.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
namespace Details {
|
||||
|
||||
struct ColorSpaceImpl {
|
||||
sk_sp<SkColorSpace> color_space;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
ColorSpace::ColorSpace()
|
||||
: m_color_space(make<Details::ColorSpaceImpl>())
|
||||
{
|
||||
}
|
||||
|
||||
ColorSpace::ColorSpace(ColorSpace const& other)
|
||||
: m_color_space(make<Details::ColorSpaceImpl>(other.m_color_space->color_space))
|
||||
{
|
||||
}
|
||||
|
||||
ColorSpace& ColorSpace::operator=(ColorSpace const& other)
|
||||
{
|
||||
m_color_space = make<Details::ColorSpaceImpl>(other.m_color_space->color_space);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ColorSpace::ColorSpace(ColorSpace&& other) = default;
|
||||
ColorSpace& ColorSpace::operator=(ColorSpace&&) = default;
|
||||
ColorSpace::~ColorSpace() = default;
|
||||
|
||||
ColorSpace::ColorSpace(NonnullOwnPtr<Details::ColorSpaceImpl>&& color_space)
|
||||
: m_color_space(move(color_space))
|
||||
{
|
||||
}
|
||||
|
||||
ErrorOr<ColorSpace> ColorSpace::load_from_icc_bytes(ReadonlyBytes icc_bytes)
|
||||
{
|
||||
if (icc_bytes.size() != 0) {
|
||||
skcms_ICCProfile icc_profile {};
|
||||
if (!skcms_Parse(icc_bytes.data(), icc_bytes.size(), &icc_profile))
|
||||
return Error::from_string_literal("Failed to parse the ICC profile");
|
||||
|
||||
return ColorSpace { make<Details::ColorSpaceImpl>(SkColorSpace::Make(icc_profile)) };
|
||||
}
|
||||
return ColorSpace {};
|
||||
}
|
||||
|
||||
template<>
|
||||
sk_sp<SkColorSpace>& ColorSpace::color_space()
|
||||
{
|
||||
return m_color_space->color_space;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
namespace IPC {
|
||||
template<>
|
||||
ErrorOr<void> encode(Encoder& encoder, Gfx::ColorSpace const& color_space)
|
||||
{
|
||||
if (!color_space.m_color_space->color_space) {
|
||||
TRY(encoder.encode<u64>(0));
|
||||
return {};
|
||||
}
|
||||
auto serialized = color_space.m_color_space->color_space->serialize();
|
||||
TRY(encoder.encode<u64>(serialized->size()));
|
||||
TRY(encoder.append(serialized->bytes(), serialized->size()));
|
||||
return {};
|
||||
}
|
||||
|
||||
template<>
|
||||
ErrorOr<Gfx::ColorSpace> decode(Decoder& decoder)
|
||||
{
|
||||
auto size = TRY(decoder.decode<u64>());
|
||||
if (size == 0)
|
||||
return Gfx::ColorSpace {};
|
||||
|
||||
auto buffer = TRY(ByteBuffer::create_uninitialized(size));
|
||||
TRY(decoder.decode_into(buffer.bytes()));
|
||||
|
||||
auto color_space = SkColorSpace::Deserialize(buffer.data(), buffer.size());
|
||||
return Gfx::ColorSpace { make<::Gfx::Details::ColorSpaceImpl>(move(color_space)) };
|
||||
}
|
||||
}
|
60
Libraries/LibGfx/ColorSpace.h
Normal file
60
Libraries/LibGfx/ColorSpace.h
Normal file
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* Copyright (c) 2024, Lucas Chollet <lucas.chollet@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <LibIPC/Forward.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
||||
namespace Details {
|
||||
|
||||
struct ColorSpaceImpl;
|
||||
|
||||
}
|
||||
|
||||
class ColorSpace {
|
||||
public:
|
||||
ColorSpace();
|
||||
ColorSpace(ColorSpace const&);
|
||||
ColorSpace(ColorSpace&&);
|
||||
ColorSpace& operator=(ColorSpace const&);
|
||||
ColorSpace& operator=(ColorSpace&&);
|
||||
~ColorSpace();
|
||||
|
||||
static ErrorOr<ColorSpace> load_from_icc_bytes(ReadonlyBytes);
|
||||
|
||||
// In order to keep this file free of Skia types, this function can't return
|
||||
// a sk_sp<ColorSpace>. To work around that issue, we define a template here
|
||||
// and only provide a specialization for sk_sp<SkColorSpace>.
|
||||
template<typename T>
|
||||
T& color_space();
|
||||
|
||||
private:
|
||||
template<typename T>
|
||||
friend ErrorOr<void> IPC::encode(IPC::Encoder&, T const&);
|
||||
template<typename T>
|
||||
friend ErrorOr<T> IPC::decode(IPC::Decoder&);
|
||||
|
||||
explicit ColorSpace(NonnullOwnPtr<Details::ColorSpaceImpl>&& color_pace);
|
||||
|
||||
NonnullOwnPtr<Details::ColorSpaceImpl> m_color_space;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace IPC {
|
||||
|
||||
template<>
|
||||
ErrorOr<void> encode(Encoder&, Gfx::ColorSpace const&);
|
||||
|
||||
template<>
|
||||
ErrorOr<Gfx::ColorSpace> decode(Decoder&);
|
||||
|
||||
}
|
|
@ -47,6 +47,14 @@ static ErrorOr<OwnPtr<ImageDecoderPlugin>> probe_and_sniff_for_appropriate_plugi
|
|||
return OwnPtr<ImageDecoderPlugin> {};
|
||||
}
|
||||
|
||||
ErrorOr<ColorSpace> ImageDecoder::color_space()
|
||||
{
|
||||
auto maybe_icc_data = TRY(icc_data());
|
||||
if (!maybe_icc_data.has_value())
|
||||
return ColorSpace {};
|
||||
return ColorSpace::load_from_icc_bytes(maybe_icc_data.value());
|
||||
}
|
||||
|
||||
ErrorOr<RefPtr<ImageDecoder>> ImageDecoder::try_create_for_raw_bytes(ReadonlyBytes bytes, [[maybe_unused]] Optional<ByteString> mime_type)
|
||||
{
|
||||
if (auto plugin = TRY(probe_and_sniff_for_appropriate_plugin(bytes)); plugin)
|
||||
|
|
|
@ -13,6 +13,7 @@
|
|||
#include <AK/String.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/CMYKBitmap.h>
|
||||
#include <LibGfx/ColorSpace.h>
|
||||
#include <LibGfx/Size.h>
|
||||
#include <LibGfx/VectorGraphic.h>
|
||||
|
||||
|
@ -111,6 +112,7 @@ public:
|
|||
ErrorOr<ImageFrameDescriptor> frame(size_t index, Optional<IntSize> ideal_size = {}) const { return m_plugin->frame(index, ideal_size); }
|
||||
|
||||
Optional<Metadata const&> metadata() const { return m_plugin->metadata(); }
|
||||
ErrorOr<ColorSpace> color_space();
|
||||
ErrorOr<Optional<ReadonlyBytes>> icc_data() const { return m_plugin->icc_data(); }
|
||||
|
||||
NaturalFrameFormat natural_frame_format() { return m_plugin->natural_frame_format(); }
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <LibGfx/SkiaUtils.h>
|
||||
|
||||
#include <core/SkBitmap.h>
|
||||
#include <core/SkColorSpace.h>
|
||||
#include <core/SkImage.h>
|
||||
|
||||
namespace Gfx {
|
||||
|
@ -17,6 +18,7 @@ struct ImmutableBitmapImpl {
|
|||
sk_sp<SkImage> sk_image;
|
||||
SkBitmap sk_bitmap;
|
||||
Variant<NonnullRefPtr<Gfx::Bitmap>, NonnullRefPtr<Gfx::PaintingSurface>, Empty> source;
|
||||
ColorSpace color_space;
|
||||
};
|
||||
|
||||
int ImmutableBitmap::width() const
|
||||
|
@ -73,14 +75,15 @@ static SkAlphaType to_skia_alpha_type(Gfx::AlphaType alpha_type)
|
|||
}
|
||||
}
|
||||
|
||||
NonnullRefPtr<ImmutableBitmap> ImmutableBitmap::create(NonnullRefPtr<Bitmap> bitmap)
|
||||
NonnullRefPtr<ImmutableBitmap> ImmutableBitmap::create(NonnullRefPtr<Bitmap> bitmap, ColorSpace color_space)
|
||||
{
|
||||
ImmutableBitmapImpl impl;
|
||||
auto info = SkImageInfo::Make(bitmap->width(), bitmap->height(), to_skia_color_type(bitmap->format()), to_skia_alpha_type(bitmap->alpha_type()));
|
||||
auto info = SkImageInfo::Make(bitmap->width(), bitmap->height(), to_skia_color_type(bitmap->format()), to_skia_alpha_type(bitmap->alpha_type()), color_space.color_space<sk_sp<SkColorSpace>>());
|
||||
impl.sk_bitmap.installPixels(info, const_cast<void*>(static_cast<void const*>(bitmap->scanline(0))), bitmap->pitch());
|
||||
impl.sk_bitmap.setImmutable();
|
||||
impl.sk_image = impl.sk_bitmap.asImage();
|
||||
impl.source = bitmap;
|
||||
impl.color_space = move(color_space);
|
||||
return adopt_ref(*new ImmutableBitmap(make<ImmutableBitmapImpl>(impl)));
|
||||
}
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <AK/NonnullOwnPtr.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <LibGfx/Bitmap.h>
|
||||
#include <LibGfx/ColorSpace.h>
|
||||
#include <LibGfx/Forward.h>
|
||||
#include <LibGfx/Rect.h>
|
||||
|
||||
|
@ -21,7 +22,7 @@ struct ImmutableBitmapImpl;
|
|||
|
||||
class ImmutableBitmap final : public RefCounted<ImmutableBitmap> {
|
||||
public:
|
||||
static NonnullRefPtr<ImmutableBitmap> create(NonnullRefPtr<Bitmap> bitmap);
|
||||
static NonnullRefPtr<ImmutableBitmap> create(NonnullRefPtr<Bitmap> bitmap, ColorSpace color_space = {});
|
||||
static NonnullRefPtr<ImmutableBitmap> create_snapshot_from_painting_surface(NonnullRefPtr<PaintingSurface>);
|
||||
|
||||
~ImmutableBitmap();
|
||||
|
|
|
@ -57,7 +57,7 @@ NonnullRefPtr<Core::Promise<DecodedImage>> Client::decode_image(ReadonlyBytes en
|
|||
return promise;
|
||||
}
|
||||
|
||||
void Client::did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence const& bitmap_sequence, Vector<u32> const& durations, Gfx::FloatPoint scale)
|
||||
void Client::did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence const& bitmap_sequence, Vector<u32> const& durations, Gfx::FloatPoint scale, Gfx::ColorSpace const& color_space)
|
||||
{
|
||||
auto const& bitmaps = bitmap_sequence.bitmaps;
|
||||
VERIFY(!bitmaps.is_empty());
|
||||
|
@ -74,6 +74,7 @@ void Client::did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gf
|
|||
image.loop_count = loop_count;
|
||||
image.scale = scale;
|
||||
image.frames.ensure_capacity(bitmaps.size());
|
||||
image.color_space = move(color_space);
|
||||
for (size_t i = 0; i < bitmaps.size(); ++i) {
|
||||
if (!bitmaps[i].has_value()) {
|
||||
dbgln("ImageDecoderClient: Invalid bitmap for request {} at index {}", image_id, i);
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <ImageDecoder/ImageDecoderClientEndpoint.h>
|
||||
#include <ImageDecoder/ImageDecoderServerEndpoint.h>
|
||||
#include <LibCore/Promise.h>
|
||||
#include <LibGfx/ColorSpace.h>
|
||||
#include <LibIPC/ConnectionToServer.h>
|
||||
|
||||
namespace ImageDecoderClient {
|
||||
|
@ -24,6 +25,7 @@ struct DecodedImage {
|
|||
Gfx::FloatPoint scale { 1, 1 };
|
||||
u32 loop_count { 0 };
|
||||
Vector<Frame> frames;
|
||||
Gfx::ColorSpace color_space;
|
||||
};
|
||||
|
||||
class Client final
|
||||
|
@ -41,7 +43,7 @@ public:
|
|||
private:
|
||||
virtual void die() override;
|
||||
|
||||
virtual void did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence const& bitmap_sequence, Vector<u32> const& durations, Gfx::FloatPoint scale) override;
|
||||
virtual void did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence const& bitmap_sequence, Vector<u32> const& durations, Gfx::FloatPoint scale, Gfx::ColorSpace const& color_profile) override;
|
||||
virtual void did_fail_to_decode_image(i64 image_id, String const& error_message) override;
|
||||
|
||||
HashMap<i64, NonnullRefPtr<Core::Promise<DecodedImage>>> m_pending_decoded_images;
|
||||
|
|
|
@ -161,7 +161,7 @@ void SharedResourceRequest::handle_successful_fetch(URL::URL const& url_string,
|
|||
Vector<AnimatedBitmapDecodedImageData::Frame> frames;
|
||||
for (auto& frame : result.frames) {
|
||||
frames.append(AnimatedBitmapDecodedImageData::Frame {
|
||||
.bitmap = Gfx::ImmutableBitmap::create(*frame.bitmap),
|
||||
.bitmap = Gfx::ImmutableBitmap::create(*frame.bitmap, move(result.color_space)),
|
||||
.duration = static_cast<int>(frame.duration),
|
||||
});
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <AK/RefPtr.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <LibCore/Promise.h>
|
||||
#include <LibGfx/ColorSpace.h>
|
||||
#include <LibGfx/Forward.h>
|
||||
|
||||
namespace Web::Platform {
|
||||
|
@ -23,6 +24,7 @@ struct DecodedImage {
|
|||
bool is_animated { false };
|
||||
u32 loop_count { 0 };
|
||||
Vector<Frame> frames;
|
||||
Gfx::ColorSpace color_space;
|
||||
};
|
||||
|
||||
class ImageCodecPlugin {
|
||||
|
|
|
@ -54,6 +54,7 @@ NonnullRefPtr<Core::Promise<Web::Platform::DecodedImage>> ImageCodecPlugin::deco
|
|||
for (auto& frame : result.frames) {
|
||||
decoded_image.frames.empend(move(frame.bitmap), frame.duration);
|
||||
}
|
||||
decoded_image.color_space = move(result.color_space);
|
||||
promise->resolve(move(decoded_image));
|
||||
return {};
|
||||
},
|
||||
|
|
|
@ -104,6 +104,9 @@ static ErrorOr<ConnectionFromClient::DecodeResult> decode_image_to_details(Core:
|
|||
result.is_animated = decoder->is_animated();
|
||||
result.loop_count = decoder->loop_count();
|
||||
|
||||
if (auto maybe_icc_data = decoder->color_space(); !maybe_icc_data.is_error())
|
||||
result.color_profile = maybe_icc_data.value();
|
||||
|
||||
Vector<Optional<NonnullRefPtr<Gfx::Bitmap>>> bitmaps;
|
||||
|
||||
if (auto maybe_metadata = decoder->metadata(); maybe_metadata.has_value() && is<Gfx::ExifMetadata>(*maybe_metadata)) {
|
||||
|
@ -135,7 +138,7 @@ NonnullRefPtr<ConnectionFromClient::Job> ConnectionFromClient::make_decode_image
|
|||
return TRY(decode_image_to_details(encoded_buffer, ideal_size, mime_type));
|
||||
},
|
||||
[strong_this = NonnullRefPtr(*this), image_id](DecodeResult result) -> ErrorOr<void> {
|
||||
strong_this->async_did_decode_image(image_id, result.is_animated, result.loop_count, result.bitmaps, result.durations, result.scale);
|
||||
strong_this->async_did_decode_image(image_id, result.is_animated, result.loop_count, result.bitmaps, result.durations, result.scale, result.color_profile);
|
||||
strong_this->m_pending_jobs.remove(image_id);
|
||||
return {};
|
||||
},
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <ImageDecoder/ImageDecoderClientEndpoint.h>
|
||||
#include <ImageDecoder/ImageDecoderServerEndpoint.h>
|
||||
#include <LibGfx/BitmapSequence.h>
|
||||
#include <LibGfx/ColorSpace.h>
|
||||
#include <LibIPC/ConnectionFromClient.h>
|
||||
#include <LibThreading/BackgroundAction.h>
|
||||
|
||||
|
@ -31,6 +32,7 @@ public:
|
|||
Gfx::FloatPoint scale { 1, 1 };
|
||||
Gfx::BitmapSequence bitmaps;
|
||||
Vector<u32> durations;
|
||||
Gfx::ColorSpace color_profile;
|
||||
};
|
||||
|
||||
private:
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
#include <LibGfx/BitmapSequence.h>
|
||||
#include <LibGfx/ColorSpace.h>
|
||||
|
||||
endpoint ImageDecoderClient
|
||||
{
|
||||
did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmaps, Vector<u32> durations, Gfx::FloatPoint scale) =|
|
||||
did_decode_image(i64 image_id, bool is_animated, u32 loop_count, Gfx::BitmapSequence bitmaps, Vector<u32> durations, Gfx::FloatPoint scale, Gfx::ColorSpace color_profile) =|
|
||||
did_fail_to_decode_image(i64 image_id, String error_message) =|
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue