mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 19:27:45 +00:00
LibPDF: Use proper ICC profiles for ICCBasedColorSpace
This commit is contained in:
parent
fa1b1f8244
commit
e989008471
2 changed files with 58 additions and 24 deletions
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <LibGfx/ICC/WellKnownProfiles.h>
|
||||||
#include <LibPDF/ColorSpace.h>
|
#include <LibPDF/ColorSpace.h>
|
||||||
#include <LibPDF/CommonNames.h>
|
#include <LibPDF/CommonNames.h>
|
||||||
#include <LibPDF/Document.h>
|
#include <LibPDF/Document.h>
|
||||||
|
@ -11,6 +12,8 @@
|
||||||
|
|
||||||
namespace PDF {
|
namespace PDF {
|
||||||
|
|
||||||
|
RefPtr<Gfx::ICC::Profile> ICCBasedColorSpace::s_srgb_profile;
|
||||||
|
|
||||||
#define ENUMERATE(name, ever_needs_parameters) \
|
#define ENUMERATE(name, ever_needs_parameters) \
|
||||||
ColorSpaceFamily ColorSpaceFamily::name { #name, ever_needs_parameters };
|
ColorSpaceFamily ColorSpaceFamily::name { #name, ever_needs_parameters };
|
||||||
ENUMERATE_COLOR_SPACE_FAMILIES(ENUMERATE);
|
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>())
|
if (!param.has<NonnullRefPtr<Object>>() || !param.get<NonnullRefPtr<Object>>()->is<StreamObject>())
|
||||||
return Error { Error::Type::MalformedPDF, "ICCBased color space expects a stream parameter" };
|
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;
|
auto maybe_profile = Gfx::ICC::Profile::try_load_from_externally_owned_memory(stream->bytes());
|
||||||
if (!dict->contains(CommonNames::Alternate)) {
|
if (!maybe_profile.is_error())
|
||||||
auto number_of_components = dict->get_value(CommonNames::N).to_int();
|
return adopt_ref(*new ICCBasedColorSpace(maybe_profile.release_value()));
|
||||||
if (number_of_components == 1)
|
|
||||||
name = CommonNames::DeviceGray;
|
if (dict->contains(CommonNames::Alternate)) {
|
||||||
else if (number_of_components == 3)
|
auto alternate_color_space_object = MUST(dict->get_object(document, CommonNames::Alternate));
|
||||||
name = CommonNames::DeviceRGB;
|
if (alternate_color_space_object->is<NameObject>())
|
||||||
else if (number_of_components == 4)
|
return ColorSpace::create(alternate_color_space_object->cast<NameObject>()->name());
|
||||||
name = CommonNames::DeviceCMYK;
|
|
||||||
else
|
return Error { Error::Type::Internal, "Alternate color spaces in array format are not supported" };
|
||||||
VERIFY_NOT_REACHED();
|
|
||||||
return ColorSpace::create(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto alternate_color_space_object = MUST(dict->get_object(document, CommonNames::Alternate));
|
return Error { Error::Type::MalformedPDF, "Failed to load ICC color space with malformed profile and no 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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
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/DeprecatedFlyString.h>
|
||||||
#include <AK/Forward.h>
|
#include <AK/Forward.h>
|
||||||
#include <LibGfx/Color.h>
|
#include <LibGfx/Color.h>
|
||||||
|
#include <LibGfx/ICC/Profile.h>
|
||||||
#include <LibPDF/Value.h>
|
#include <LibPDF/Value.h>
|
||||||
|
|
||||||
#define ENUMERATE_COLOR_SPACE_FAMILIES(V) \
|
#define ENUMERATE_COLOR_SPACE_FAMILIES(V) \
|
||||||
|
@ -137,7 +138,10 @@ public:
|
||||||
ColorSpaceFamily const& family() const override { return ColorSpaceFamily::ICCBased; }
|
ColorSpaceFamily const& family() const override { return ColorSpaceFamily::ICCBased; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
ICCBasedColorSpace() = delete;
|
ICCBasedColorSpace(NonnullRefPtr<Gfx::ICC::Profile>);
|
||||||
|
|
||||||
|
static RefPtr<Gfx::ICC::Profile> s_srgb_profile;
|
||||||
|
NonnullRefPtr<Gfx::ICC::Profile> m_profile;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue