mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 17:40:27 +00:00
LibPDF: Use proper ICC profiles for ICCBasedColorSpace
This commit is contained in:
parent
fa1b1f8244
commit
e989008471
Notes:
sideshowbarker
2024-07-17 09:49:48 +09:00
Author: https://github.com/mattco98 Commit: https://github.com/SerenityOS/serenity/commit/e989008471 Pull-request: https://github.com/SerenityOS/serenity/pull/20084 Reviewed-by: https://github.com/nico ✅
2 changed files with 58 additions and 24 deletions
|
@ -4,6 +4,7 @@
|
|||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibGfx/ICC/WellKnownProfiles.h>
|
||||
#include <LibPDF/ColorSpace.h>
|
||||
#include <LibPDF/CommonNames.h>
|
||||
#include <LibPDF/Document.h>
|
||||
|
@ -11,6 +12,8 @@
|
|||
|
||||
namespace PDF {
|
||||
|
||||
RefPtr<Gfx::ICC::Profile> ICCBasedColorSpace::s_srgb_profile;
|
||||
|
||||
#define ENUMERATE(name, ever_needs_parameters) \
|
||||
ColorSpaceFamily ColorSpaceFamily::name { #name, ever_needs_parameters };
|
||||
ENUMERATE_COLOR_SPACE_FAMILIES(ENUMERATE);
|
||||
|
@ -303,39 +306,66 @@ PDFErrorOr<NonnullRefPtr<ColorSpace>> ICCBasedColorSpace::create(Document* docum
|
|||
if (!param.has<NonnullRefPtr<Object>>() || !param.get<NonnullRefPtr<Object>>()->is<StreamObject>())
|
||||
return Error { Error::Type::MalformedPDF, "ICCBased color space expects a stream parameter" };
|
||||
|
||||
auto dict = param.get<NonnullRefPtr<Object>>()->cast<StreamObject>()->dict();
|
||||
auto stream = param.get<NonnullRefPtr<Object>>()->cast<StreamObject>();
|
||||
auto dict = stream->dict();
|
||||
|
||||
DeprecatedFlyString name;
|
||||
if (!dict->contains(CommonNames::Alternate)) {
|
||||
auto number_of_components = dict->get_value(CommonNames::N).to_int();
|
||||
if (number_of_components == 1)
|
||||
name = CommonNames::DeviceGray;
|
||||
else if (number_of_components == 3)
|
||||
name = CommonNames::DeviceRGB;
|
||||
else if (number_of_components == 4)
|
||||
name = CommonNames::DeviceCMYK;
|
||||
else
|
||||
VERIFY_NOT_REACHED();
|
||||
return ColorSpace::create(name);
|
||||
auto maybe_profile = Gfx::ICC::Profile::try_load_from_externally_owned_memory(stream->bytes());
|
||||
if (!maybe_profile.is_error())
|
||||
return adopt_ref(*new ICCBasedColorSpace(maybe_profile.release_value()));
|
||||
|
||||
if (dict->contains(CommonNames::Alternate)) {
|
||||
auto alternate_color_space_object = MUST(dict->get_object(document, CommonNames::Alternate));
|
||||
if (alternate_color_space_object->is<NameObject>())
|
||||
return ColorSpace::create(alternate_color_space_object->cast<NameObject>()->name());
|
||||
|
||||
return Error { Error::Type::Internal, "Alternate color spaces in array format are not supported" };
|
||||
}
|
||||
|
||||
auto alternate_color_space_object = MUST(dict->get_object(document, CommonNames::Alternate));
|
||||
if (alternate_color_space_object->is<NameObject>()) {
|
||||
return ColorSpace::create(alternate_color_space_object->cast<NameObject>()->name());
|
||||
}
|
||||
|
||||
dbgln("Alternate color spaces in array format not supported yet ");
|
||||
TODO();
|
||||
return Error { Error::Type::MalformedPDF, "Failed to load ICC color space with malformed profile and no alternate" };
|
||||
}
|
||||
|
||||
Color ICCBasedColorSpace::color(Vector<Value> const&) const
|
||||
ICCBasedColorSpace::ICCBasedColorSpace(NonnullRefPtr<Gfx::ICC::Profile> profile)
|
||||
: m_profile(profile)
|
||||
{
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
Color ICCBasedColorSpace::color(Vector<Value> const& arguments) const
|
||||
{
|
||||
if (!s_srgb_profile)
|
||||
s_srgb_profile = MUST(Gfx::ICC::sRGB());
|
||||
|
||||
Vector<u8> bytes;
|
||||
for (auto const& arg : arguments) {
|
||||
VERIFY(arg.has_number());
|
||||
bytes.append(static_cast<u8>(arg.to_float() * 255.0f));
|
||||
}
|
||||
|
||||
auto pcs = MUST(m_profile->to_pcs(bytes));
|
||||
Array<u8, 3> output;
|
||||
MUST(s_srgb_profile->from_pcs(pcs, output.span()));
|
||||
|
||||
return Color(output[0], output[1], output[2]);
|
||||
}
|
||||
|
||||
Vector<float> ICCBasedColorSpace::default_decode() const
|
||||
{
|
||||
VERIFY_NOT_REACHED();
|
||||
auto color_space = m_profile->data_color_space();
|
||||
switch (color_space) {
|
||||
case Gfx::ICC::ColorSpace::Gray:
|
||||
return { 0.0, 1.0 };
|
||||
case Gfx::ICC::ColorSpace::RGB:
|
||||
return { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 };
|
||||
case Gfx::ICC::ColorSpace::CMYK:
|
||||
return { 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0 };
|
||||
default:
|
||||
warnln("PDF: Unknown default_decode params for color space {}", Gfx::ICC::data_color_space_name(color_space));
|
||||
Vector<float> decoding_ranges;
|
||||
for (u8 i = 0; i < Gfx::ICC::number_of_components_in_color_space(color_space); i++) {
|
||||
decoding_ranges.append(0.0);
|
||||
decoding_ranges.append(1.0);
|
||||
}
|
||||
return decoding_ranges;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include <AK/DeprecatedFlyString.h>
|
||||
#include <AK/Forward.h>
|
||||
#include <LibGfx/Color.h>
|
||||
#include <LibGfx/ICC/Profile.h>
|
||||
#include <LibPDF/Value.h>
|
||||
|
||||
#define ENUMERATE_COLOR_SPACE_FAMILIES(V) \
|
||||
|
@ -137,7 +138,10 @@ public:
|
|||
ColorSpaceFamily const& family() const override { return ColorSpaceFamily::ICCBased; }
|
||||
|
||||
private:
|
||||
ICCBasedColorSpace() = delete;
|
||||
ICCBasedColorSpace(NonnullRefPtr<Gfx::ICC::Profile>);
|
||||
|
||||
static RefPtr<Gfx::ICC::Profile> s_srgb_profile;
|
||||
NonnullRefPtr<Gfx::ICC::Profile> m_profile;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue