diff options
author | Nico Weber <thakis@chromium.org> | 2023-01-01 07:09:38 -0500 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-01-02 15:20:51 +0100 |
commit | 79badfd6500d71c003dcf3a33b880d427c22206b (patch) | |
tree | 72c4754bbc83ff0925e83b87a3df58e4dcd0d31e | |
parent | 3df2eb66be605a0c4e1a646487f7dca8735b84d8 (diff) | |
download | serenity-79badfd6500d71c003dcf3a33b880d427c22206b.zip |
LibGfx+icc: Print pcs illuminant
-rw-r--r-- | Userland/Libraries/LibGfx/ICCProfile.cpp | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibGfx/ICCProfile.h | 16 | ||||
-rw-r--r-- | Userland/Utilities/icc.cpp | 1 |
3 files changed, 47 insertions, 3 deletions
diff --git a/Userland/Libraries/LibGfx/ICCProfile.cpp b/Userland/Libraries/LibGfx/ICCProfile.cpp index fd85534790..e7d49a1e37 100644 --- a/Userland/Libraries/LibGfx/ICCProfile.cpp +++ b/Userland/Libraries/LibGfx/ICCProfile.cpp @@ -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; } diff --git a/Userland/Libraries/LibGfx/ICCProfile.h b/Userland/Libraries/LibGfx/ICCProfile.h index aa35616c0c..d60601a779 100644 --- a/Userland/Libraries/LibGfx/ICCProfile.h +++ b/Userland/Libraries/LibGfx/ICCProfile.h @@ -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); + } +}; } diff --git a/Userland/Utilities/icc.cpp b/Userland/Utilities/icc.cpp index 863c47022a..693b812253 100644 --- a/Userland/Utilities/icc.cpp +++ b/Userland/Utilities/icc.cpp @@ -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; } |