Skip to content

Commit

Permalink
LibGfx+icc: Print pcs illuminant
Browse files Browse the repository at this point in the history
  • Loading branch information
nico authored and awesomekling committed Jan 2, 2023
1 parent 3df2eb6 commit 79badfd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
33 changes: 30 additions & 3 deletions Userland/Libraries/LibGfx/ICCProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions Userland/Libraries/LibGfx/ICCProfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ class Flags {
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);
Expand All @@ -129,6 +135,7 @@ class Profile : public RefCounted<Profile> {
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;
Expand All @@ -138,6 +145,7 @@ class Profile : public RefCounted<Profile> {
time_t m_creation_timestamp;
Flags m_flags;
RenderingIntent m_rendering_intent;
XYZ m_pcs_illuminant;
};

}
Expand All @@ -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);
}
};
}
1 change: 1 addition & 0 deletions Userland/Utilities/icc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 79badfd

Please sign in to comment.