1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 01:51:03 +09:00

LibGfx+icc: Print pcs illuminant

This commit is contained in:
Nico Weber 2023-01-01 07:09:38 -05:00 committed by Andreas Kling
parent 3df2eb66be
commit 79badfd650
Notes: sideshowbarker 2024-07-17 02:18:36 +09:00
3 changed files with 47 additions and 3 deletions

View file

@ -6,6 +6,7 @@
#include <AK/Endian.h>
#include <LibGfx/ICCProfile.h>
#include <math.h>
#include <time.h>
// V2 spec: https://color.org/specification/ICC.1-2001-04.pdf
@ -26,6 +27,21 @@ struct DateTimeNumber {
BigEndian<u16> seconds;
};
// ICC V4, 4.6 s15Fixed16Number
using s15Fixed16Number = i32;
// ICC V4, 4.14 XYZNumber
struct XYZNumber {
BigEndian<s15Fixed16Number> x;
BigEndian<s15Fixed16Number> y;
BigEndian<s15Fixed16Number> z;
operator XYZ() const
{
return XYZ { x / (double)0x1'0000, y / (double)0x1'0000, z / (double)0x1'0000 };
}
};
ErrorOr<time_t> parse_date_time_number(DateTimeNumber const& date_time)
{
// ICC V4, 4.2 dateTimeNumber
@ -91,9 +107,7 @@ struct ICCHeader {
BigEndian<u64> device_attributes;
BigEndian<u32> rendering_intent;
BigEndian<i32> pcs_illuminant_x;
BigEndian<i32> pcs_illuminant_y;
BigEndian<i32> pcs_illuminant_z;
XYZNumber pcs_illuminant;
BigEndian<u32> profile_creator;
@ -194,6 +208,18 @@ ErrorOr<RenderingIntent> parse_rendering_intent(ICCHeader const& header)
return Error::from_string_literal("ICC::Profile: Invalid rendering intent");
}
ErrorOr<XYZ> parse_pcs_illuminant(ICCHeader const& header)
{
// ICC v4, 7.2.16 PCS illuminant field
XYZ xyz = (XYZ)header.pcs_illuminant;
/// "The value, when rounded to four decimals, shall be X = 0,9642, Y = 1,0 and Z = 0,8249."
if (round(xyz.x * 10'000) != 9'642 || round(xyz.y * 10'000) != 10'000 || round(xyz.z * 10'000) != 8'249)
return Error::from_string_literal("ICC::Profile: Invalid pcs illuminant");
return xyz;
}
ErrorOr<time_t> parse_creation_date_time(ICCHeader const& header)
{
// iCC v4, 7.2.8 Date and time field
@ -337,6 +363,7 @@ ErrorOr<NonnullRefPtr<Profile>> Profile::try_load_from_externally_owned_memory(R
profile->m_creation_timestamp = TRY(parse_creation_date_time(header));
profile->m_flags = Flags { header.profile_flags };
profile->m_rendering_intent = TRY(parse_rendering_intent(header));
profile->m_pcs_illuminant = TRY(parse_pcs_illuminant(header));
return profile;
}

View file

@ -115,6 +115,12 @@ private:
u32 m_bits = 0;
};
struct XYZ {
double x { 0 };
double y { 0 };
double z { 0 };
};
class Profile : public RefCounted<Profile> {
public:
static ErrorOr<NonnullRefPtr<Profile>> try_load_from_externally_owned_memory(ReadonlyBytes bytes);
@ -129,6 +135,7 @@ public:
time_t creation_timestamp() const { return m_creation_timestamp; }
Flags flags() const { return m_flags; }
RenderingIntent rendering_intent() const { return m_rendering_intent; }
const XYZ& pcs_illuminant() const { return m_pcs_illuminant; }
private:
Version m_version;
@ -138,6 +145,7 @@ private:
time_t m_creation_timestamp;
Flags m_flags;
RenderingIntent m_rendering_intent;
XYZ m_pcs_illuminant;
};
}
@ -150,4 +158,12 @@ struct Formatter<Gfx::ICC::Version> : Formatter<FormatString> {
return Formatter<FormatString>::format(builder, "{}.{}.{}"sv, version.major_version(), version.minor_version(), version.bugfix_version());
}
};
template<>
struct Formatter<Gfx::ICC::XYZ> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, Gfx::ICC::XYZ const& xyz)
{
return Formatter<FormatString>::format(builder, "X = {}, Y = {}, Z = {}"sv, xyz.x, xyz.y, xyz.z);
}
};
}

View file

@ -37,6 +37,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
outln(" CMM bits: 0x{:04x}", color_management_module_bits);
outln("rendering intent: {}", Gfx::ICC::rendering_intent_name(profile->rendering_intent()));
outln("pcs illuminant: {}", profile->pcs_illuminant());
return 0;
}