Browse Source

LibGfx: Split ICC/Profile.{h,cpp} into several files

s15Fixed16Number and XYZNumber are somewhat awkwardly duplicated
in both Profile.cpp and TagTypes.cpp. Other than that, this is a
pure code move.

No behavior change.
Nico Weber 2 years ago
parent
commit
b5deccf859

+ 2 - 0
Userland/Libraries/LibGfx/CMakeLists.txt

@@ -29,6 +29,8 @@ set(SOURCES
     GradientPainting.cpp
     GradientPainting.cpp
     GIFLoader.cpp
     GIFLoader.cpp
     ICC/Profile.cpp
     ICC/Profile.cpp
+    ICC/Tags.cpp
+    ICC/TagTypes.cpp
     ICOLoader.cpp
     ICOLoader.cpp
     ImageDecoder.cpp
     ImageDecoder.cpp
     JPGLoader.cpp
     JPGLoader.cpp

+ 78 - 0
Userland/Libraries/LibGfx/ICC/DistinctFourCC.h

@@ -0,0 +1,78 @@
+/*
+ * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Format.h>
+#include <AK/Types.h>
+
+namespace Gfx::ICC {
+
+// The ICC spec uses FourCCs for many different things.
+// This is used to give FourCCs for different roles distinct types, so that they can only be compared to the correct constants.
+// (FourCCs that have only a small and fixed set of values should use an enum class instead, see e.g. DeviceClass and ColorSpace in Profile.h.)
+enum class FourCCType {
+    PreferredCMMType,
+    DeviceManufacturer,
+    DeviceModel,
+    Creator,
+    TagSignature,
+    TagTypeSignature,
+};
+
+template<FourCCType type>
+struct [[gnu::packed]] DistinctFourCC {
+    constexpr explicit DistinctFourCC(u32 value)
+        : value(value)
+    {
+    }
+    constexpr operator u32() const { return value; }
+
+    char c0() const { return value >> 24; }
+    char c1() const { return (value >> 16) & 0xff; }
+    char c2() const { return (value >> 8) & 0xff; }
+    char c3() const { return value & 0xff; }
+
+    bool operator==(DistinctFourCC b) const { return value == b.value; }
+
+    u32 value { 0 };
+};
+
+using PreferredCMMType = DistinctFourCC<FourCCType::PreferredCMMType>;     // ICC v4, "7.2.3 Preferred CMM type field"
+using DeviceManufacturer = DistinctFourCC<FourCCType::DeviceManufacturer>; // ICC v4, "7.2.12 Device manufacturer field"
+using DeviceModel = DistinctFourCC<FourCCType::DeviceModel>;               // ICC v4, "7.2.13 Device model field"
+using Creator = DistinctFourCC<FourCCType::Creator>;                       // ICC v4, "7.2.17 Profile creator field"
+using TagSignature = DistinctFourCC<FourCCType::TagSignature>;             // ICC v4, "9.2 Tag listing"
+using TagTypeSignature = DistinctFourCC<FourCCType::TagTypeSignature>;     // ICC v4, "10 Tag type definitions"
+
+}
+
+template<Gfx::ICC::FourCCType Type>
+struct AK::Formatter<Gfx::ICC::DistinctFourCC<Type>> : StandardFormatter {
+    ErrorOr<void> format(FormatBuilder& builder, Gfx::ICC::DistinctFourCC<Type> const& four_cc)
+    {
+        TRY(builder.put_padding('\'', 1));
+        TRY(builder.put_padding(four_cc.c0(), 1));
+        TRY(builder.put_padding(four_cc.c1(), 1));
+        TRY(builder.put_padding(four_cc.c2(), 1));
+        TRY(builder.put_padding(four_cc.c3(), 1));
+        TRY(builder.put_padding('\'', 1));
+        return {};
+    }
+};
+
+template<Gfx::ICC::FourCCType Type>
+struct AK::Traits<Gfx::ICC::DistinctFourCC<Type>> : public GenericTraits<Gfx::ICC::DistinctFourCC<Type>> {
+    static unsigned hash(Gfx::ICC::DistinctFourCC<Type> const& key)
+    {
+        return int_hash(key.value);
+    }
+
+    static bool equals(Gfx::ICC::DistinctFourCC<Type> const& a, Gfx::ICC::DistinctFourCC<Type> const& b)
+    {
+        return a == b;
+    }
+};

+ 1 - 370
Userland/Libraries/LibGfx/ICC/Profile.cpp

@@ -6,7 +6,7 @@
 
 
 #include <AK/Endian.h>
 #include <AK/Endian.h>
 #include <LibGfx/ICC/Profile.h>
 #include <LibGfx/ICC/Profile.h>
-#include <LibTextCodec/Decoder.h>
+#include <LibGfx/ICC/Tags.h>
 #include <math.h>
 #include <math.h>
 #include <time.h>
 #include <time.h>
 
 
@@ -395,18 +395,6 @@ URL device_model_url(DeviceModel device_model)
         device_model.c0(), device_model.c1(), device_model.c2(), device_model.c3(), device_model.value));
         device_model.c0(), device_model.c1(), device_model.c2(), device_model.c3(), device_model.value));
 }
 }
 
 
-Optional<StringView> tag_signature_spec_name(TagSignature tag_signature)
-{
-    switch (tag_signature) {
-#define TAG(name, id) \
-    case name:        \
-        return #name##sv;
-        ENUMERATE_TAG_SIGNATURES(TAG)
-#undef TAG
-    }
-    return {};
-}
-
 StringView device_class_name(DeviceClass device_class)
 StringView device_class_name(DeviceClass device_class)
 {
 {
     switch (device_class) {
     switch (device_class) {
@@ -539,363 +527,6 @@ DeviceAttributes::DeviceAttributes(u64 bits)
 {
 {
 }
 }
 
 
-static TagTypeSignature tag_type(ReadonlyBytes tag_bytes)
-{
-    VERIFY(tag_bytes.size() >= sizeof(u32));
-    return *bit_cast<BigEndian<TagTypeSignature> const*>(tag_bytes.data());
-}
-
-static ErrorOr<void> check_reserved(ReadonlyBytes tag_bytes)
-{
-    if (tag_bytes.size() < 2 * sizeof(u32))
-        return Error::from_string_literal("ICC::Profile: Not enough data for tag reserved field");
-
-    if (*bit_cast<BigEndian<u32> const*>(tag_bytes.data() + sizeof(u32)) != 0)
-        return Error::from_string_literal("ICC::Profile: tag reserved field not 0");
-
-    return {};
-}
-
-ErrorOr<NonnullRefPtr<CurveTagData>> CurveTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
-{
-    // ICC v4, 10.6 curveType
-    VERIFY(tag_type(bytes) == Type);
-    TRY(check_reserved(bytes));
-
-    if (bytes.size() < 3 * sizeof(u32))
-        return Error::from_string_literal("ICC::Profile: curveType has not enough data for count");
-    u32 count = *bit_cast<BigEndian<u32> const*>(bytes.data() + 8);
-
-    if (bytes.size() < 3 * sizeof(u32) + count * sizeof(u16))
-        return Error::from_string_literal("ICC::Profile: curveType has not enough data for curve points");
-
-    BigEndian<u16> const* raw_values = bit_cast<BigEndian<u16> const*>(bytes.data() + 12);
-    Vector<u16> values;
-    TRY(values.try_resize(count));
-
-    for (u32 i = 0; i < count; ++i)
-        values[i] = raw_values[i];
-
-    return adopt_ref(*new CurveTagData(offset, size, move(values)));
-}
-
-ErrorOr<NonnullRefPtr<MultiLocalizedUnicodeTagData>> MultiLocalizedUnicodeTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
-{
-    // ICC v4, 10.15 multiLocalizedUnicodeType
-    VERIFY(tag_type(bytes) == Type);
-    TRY(check_reserved(bytes));
-
-    // "Multiple strings within this tag may share storage locations. For example, en/US and en/UK can refer to the
-    //  same string data."
-    // This implementation makes redudant string copies in that case.
-    // Most of the time, this costs just a few bytes, so that seems ok.
-
-    if (bytes.size() < 4 * sizeof(u32))
-        return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType has not enough data");
-
-    // Table 54 — multiLocalizedUnicodeType
-    u32 number_of_records = *bit_cast<BigEndian<u32> const*>(bytes.data() + 8);
-    u32 record_size = *bit_cast<BigEndian<u32> const*>(bytes.data() + 12);
-
-    // "The fourth field of this tag, the record size, should contain the value 12, which corresponds to the size in bytes
-    // of each record. Any code that needs to access the nth record should determine the record’s offset by multiplying
-    // n by the contents of this size field and adding 16. This minor extra effort allows for future expansion of the record
-    // encoding, should the need arise, without having to define a new tag type."
-    if (record_size < 12)
-        return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType record size too small");
-    if (bytes.size() < 16 + number_of_records * record_size)
-        return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType not enough data for records");
-
-    Vector<Record> records;
-    TRY(records.try_resize(number_of_records));
-
-    // "For the definition of language codes and country codes, see respectively
-    //  ISO 639-1 and ISO 3166-1. The Unicode strings in storage should be encoded as 16-bit big-endian, UTF-16BE,
-    //  and should not be NULL terminated."
-    auto& utf_16be_decoder = *TextCodec::decoder_for("utf-16be");
-
-    struct RawRecord {
-        BigEndian<u16> language_code;
-        BigEndian<u16> country_code;
-        BigEndian<u32> string_length_in_bytes;
-        BigEndian<u32> string_offset_in_bytes;
-    };
-
-    for (u32 i = 0; i < number_of_records; ++i) {
-        size_t offset = 16 + i * record_size;
-        RawRecord record = *bit_cast<RawRecord const*>(bytes.data() + offset);
-
-        records[i].iso_639_1_language_code = record.language_code;
-        records[i].iso_3166_1_country_code = record.country_code;
-
-        if (record.string_length_in_bytes % 2 != 0)
-            return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType odd UTF-16 byte length");
-
-        if (record.string_offset_in_bytes + record.string_length_in_bytes > bytes.size())
-            return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType string offset out of bounds");
-
-        StringView utf_16be_data { bytes.data() + record.string_offset_in_bytes, record.string_length_in_bytes };
-        records[i].text = TRY(String::from_deprecated_string(utf_16be_decoder.to_utf8(utf_16be_data)));
-    }
-
-    return adopt_ref(*new MultiLocalizedUnicodeTagData(offset, size, move(records)));
-}
-
-unsigned ParametricCurveTagData::parameter_count(FunctionType function_type)
-{
-    switch (function_type) {
-    case FunctionType::Type0:
-        return 1;
-    case FunctionType::Type1:
-        return 3;
-    case FunctionType::Type2:
-        return 4;
-    case FunctionType::Type3:
-        return 5;
-    case FunctionType::Type4:
-        return 7;
-    }
-    VERIFY_NOT_REACHED();
-}
-
-ErrorOr<NonnullRefPtr<ParametricCurveTagData>> ParametricCurveTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
-{
-    // ICC v4, 10.18 parametricCurveType
-    VERIFY(tag_type(bytes) == Type);
-    TRY(check_reserved(bytes));
-
-    // "The parametricCurveType describes a one-dimensional curve by specifying one of a predefined set of functions
-    //  using the parameters."
-
-    if (bytes.size() < 2 * sizeof(u32) + 2 * sizeof(u16))
-        return Error::from_string_literal("ICC::Profile: parametricCurveType has not enough data");
-
-    u16 raw_function_type = *bit_cast<BigEndian<u16> const*>(bytes.data() + 8);
-    u16 reserved = *bit_cast<BigEndian<u16> const*>(bytes.data() + 10);
-    if (reserved != 0)
-        return Error::from_string_literal("ICC::Profile: parametricCurveType reserved u16 after function type not 0");
-
-    if (raw_function_type > 4)
-        return Error::from_string_literal("ICC::Profile: parametricCurveType unknown function type");
-
-    FunctionType function_type = (FunctionType)raw_function_type;
-    unsigned count = parameter_count(function_type);
-
-    if (bytes.size() < 2 * sizeof(u32) + 2 * sizeof(u16) + count * sizeof(s15Fixed16Number))
-        return Error::from_string_literal("ICC::Profile: parametricCurveType has not enough data for parameters");
-
-    BigEndian<s15Fixed16Number> const* raw_parameters = bit_cast<BigEndian<s15Fixed16Number> const*>(bytes.data() + 12);
-    Array<S15Fixed16, 7> parameters;
-    parameters.fill(0);
-    for (unsigned i = 0; i < count; ++i)
-        parameters[i] = S15Fixed16::create_raw(raw_parameters[i]);
-
-    return adopt_ref(*new ParametricCurveTagData(offset, size, function_type, move(parameters)));
-}
-
-ErrorOr<NonnullRefPtr<S15Fixed16ArrayTagData>> S15Fixed16ArrayTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
-{
-    // ICC v4, 10.22 s15Fixed16ArrayType
-    VERIFY(tag_type(bytes) == Type);
-    TRY(check_reserved(bytes));
-
-    // "This type represents an array of generic 4-byte (32-bit) fixed point quantity. The number of values is determined
-    //  from the size of the tag."
-    size_t byte_size = bytes.size() - 8;
-    if (byte_size % sizeof(s15Fixed16Number) != 0)
-        return Error::from_string_literal("ICC::Profile: s15Fixed16ArrayType has wrong size");
-
-    size_t count = byte_size / sizeof(s15Fixed16Number);
-    BigEndian<s15Fixed16Number> const* raw_values = bit_cast<BigEndian<s15Fixed16Number> const*>(bytes.data() + 8);
-    Vector<S15Fixed16, 9> values;
-    TRY(values.try_resize(count));
-    for (size_t i = 0; i < count; ++i)
-        values[i] = S15Fixed16::create_raw(raw_values[i]);
-
-    return adopt_ref(*new S15Fixed16ArrayTagData(offset, size, move(values)));
-}
-
-ErrorOr<NonnullRefPtr<TextDescriptionTagData>> TextDescriptionTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
-{
-    // ICC v2, 6.5.17 textDescriptionType
-    // textDescriptionType is no longer in the V4 spec.
-    // In both the V2 and V4 specs, 'desc' is a required tag. In V4, it has type multiLocalizedUnicodeType,
-    // but in V2 it has type textDescriptionType. Since 'desc' is required, this type is present in every
-    // V2 icc file, and there are still many V2 files in use. So textDescriptionType is here to stay for now.
-    // It's a very 90s type, preceding universal adoption of Unicode.
-
-    // "The textDescriptionType is a complex structure that contains three types of text description structures:
-    //  7-bit ASCII, Unicode and ScriptCode. Since no single standard method for specifying localizable character
-    //  sets exists across the major platform vendors, including all three provides access for the major operating
-    //  systems. The 7-bit ASCII description is to be an invariant, nonlocalizable name for consistent reference.
-    //  It is preferred that both the Unicode and ScriptCode structures be properly localized."
-
-    VERIFY(tag_type(bytes) == Type);
-    TRY(check_reserved(bytes));
-
-    // 7-bit ASCII
-
-    // "ASCII: The count is the length of the string in bytes including the null terminator."
-    if (bytes.size() < 3 * sizeof(u32))
-        return Error::from_string_literal("ICC::Profile: textDescriptionType has not enough data for ASCII size");
-    u32 ascii_description_length = *bit_cast<BigEndian<u32> const*>(bytes.data() + 8);
-
-    if (bytes.size() < 3 * sizeof(u32) + ascii_description_length)
-        return Error::from_string_literal("ICC::Profile: textDescriptionType has not enough data for ASCII description");
-
-    u8 const* ascii_description_data = bytes.data() + 3 * sizeof(u32);
-    for (u32 i = 0; i < ascii_description_length; ++i) {
-        if (ascii_description_data[i] >= 128)
-            return Error::from_string_literal("ICC::Profile: textDescriptionType ASCII description not 7-bit ASCII");
-    }
-
-    if (ascii_description_length == 0)
-        return Error::from_string_literal("ICC::Profile: textDescriptionType ASCII description length does not include trailing \\0");
-
-    if (ascii_description_data[ascii_description_length - 1] != '\0')
-        return Error::from_string_literal("ICC::Profile: textDescriptionType ASCII description not \\0-terminated");
-
-    StringView ascii_description { ascii_description_data, ascii_description_length - 1 };
-
-    // Unicode
-
-    if (bytes.size() < 3 * sizeof(u32) + ascii_description_length + 2 * sizeof(u32))
-        return Error::from_string_literal("ICC::Profile: textDescriptionType has not enough data for Unicode metadata");
-
-    // "Because the Unicode language code and Unicode count immediately follow the ASCII description,
-    //  their alignment is not correct when the ASCII count is not a multiple of four"
-    // So we can't use BigEndian<u32> here.
-    u8 const* cursor = ascii_description_data + ascii_description_length;
-    u32 unicode_language_code = (u32)(cursor[0] << 24) | (u32)(cursor[1] << 16) | (u32)(cursor[2] << 8) | (u32)cursor[3];
-    cursor += 4;
-
-    // "Unicode: The count is the number of characters including a Unicode null where a character is always two bytes."
-    // This implies UCS-2.
-    u32 unicode_description_length = (u32)(cursor[0] << 24) | (u32)(cursor[1] << 16) | (u32)(cursor[2] << 8) | (u32)cursor[3];
-    cursor += 4;
-
-    if (bytes.size() < 3 * sizeof(u32) + ascii_description_length + 2 * sizeof(u32) + 2 * unicode_description_length)
-        return Error::from_string_literal("ICC::Profile: textDescriptionType has not enough data for Unicode description");
-
-    u8 const* unicode_description_data = cursor;
-    cursor += 2 * unicode_description_length;
-    for (u32 i = 0; i < unicode_description_length; ++i) {
-        u16 code_point = (u16)(unicode_description_data[2 * i] << 8) | (u16)unicode_description_data[2 * i + 1];
-        if (is_unicode_surrogate(code_point))
-            return Error::from_string_literal("ICC::Profile: textDescriptionType Unicode description is not valid UCS-2");
-    }
-
-    // If Unicode is not native on the platform, then the Unicode language code and Unicode count should be
-    // filled in as 0, with no data placed in the Unicode localizable profile description area.
-    Optional<String> unicode_description;
-    if (unicode_description_length > 0) {
-        u16 last_code_point = (u16)(unicode_description_data[2 * (unicode_description_length - 1)] << 8) | (u16)unicode_description_data[2 * (unicode_description_length - 1) + 1];
-        if (last_code_point != 0)
-            return Error::from_string_literal("ICC::Profile: textDescriptionType Unicode description not \\0-terminated");
-
-        StringView utf_16be_data { unicode_description_data, 2 * (unicode_description_length - 1) };
-        unicode_description = TRY(String::from_deprecated_string(TextCodec::decoder_for("utf-16be")->to_utf8(utf_16be_data)));
-    }
-
-    // ScriptCode
-
-    // What is a script code? It's an old, obsolete mac thing. It looks like it's documented in
-    // https://developer.apple.com/library/archive/documentation/mac/pdf/Text.pdf
-    // "Script Codes, Language Codes, and Region Codes 1", PDF page 82.
-    // I haven't found a complete explanation though. PDF page 84 suggests that:
-    // - There are 16 script codes
-    // - 0 is Roman, 1 is Japanese, 2 is Chinese, 3 is Korean, 9 is Devanagari
-    // Roman uses https://en.wikipedia.org/wiki/Mac_OS_Roman as encoding (also on page 89),
-    // and "All non-Roman script systems include Roman as a subscript" (page 87).
-
-    // Aha, "Script Codes 6" on page 676 has the complete list! There are 32 of them.
-    // The document mentions that each script code possibly has its own encoding, but I haven't found
-    // details on the encodings for script codes other than 0 (which uses Mac OS Roman).
-    // http://www.kreativekorp.com/charset/encoding/ has an unofficial list of old Mac OS encodings,
-    // but it's not clear to me which script codes map to which encoding.
-
-    // From here on, quotes are from the ICC spec on textDescriptionType again.
-
-    // "The ScriptCode code is misaligned when the ASCII count is odd."
-    // So don't use BigEndian<u16> here.
-    u16 scriptcode_code = (u16)(cursor[0] << 8) | (u32)cursor[1];
-    cursor += 2;
-
-    // "ScriptCode: The count is the length of the string in bytes including the terminating null."
-    u8 macintosh_description_length = *cursor;
-    cursor += 1;
-
-    if (macintosh_description_length > 67)
-        return Error::from_string_literal("ICC::Profile: textDescriptionType ScriptCode description too long");
-
-    u8 const* macintosh_description_data = cursor;
-
-    // "If Scriptcode is not native on the platform, then the ScriptCode code and ScriptCode count should be filled
-    // in as 0. The 67-byte localizable Macintosh profile description should be filled with 0’s."
-    Optional<String> macintosh_description;
-    if (macintosh_description_length > 0) {
-        // ScriptCode is old-timey and a complicated to fully support. Lightroom Classic does write the ScriptCode section of textDescriptionType.
-        // But supporting only ASCII MacRoman is good enough for those files, and easy to implement, so let's do only that for now.
-        if (scriptcode_code == 0) { // MacRoman
-            if (macintosh_description_data[macintosh_description_length - 1] != '\0')
-                return Error::from_string_literal("ICC::Profile: textDescriptionType ScriptCode not \\0-terminated");
-
-            macintosh_description = TRY(String::from_deprecated_string(TextCodec::decoder_for("x-mac-roman")->to_utf8({ macintosh_description_data, (size_t)macintosh_description_length - 1 })));
-        } else {
-            dbgln("TODO: ICCProfile textDescriptionType ScriptCode {}, length {}", scriptcode_code, macintosh_description_length);
-        }
-    }
-
-    return adopt_ref(*new TextDescriptionTagData(offset, size, TRY(String::from_utf8(ascii_description)), unicode_language_code, move(unicode_description), move(macintosh_description)));
-}
-
-ErrorOr<NonnullRefPtr<TextTagData>> TextTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
-{
-    // ICC v4, 10.24 textType
-    VERIFY(tag_type(bytes) == Type);
-    TRY(check_reserved(bytes));
-
-    // "The textType is a simple text structure that contains a 7-bit ASCII text string. The length of the string is obtained
-    //  by subtracting 8 from the element size portion of the tag itself. This string shall be terminated with a 00h byte."
-    u32 length = bytes.size() - 8;
-
-    u8 const* text_data = bytes.data() + 8;
-    for (u32 i = 0; i < length; ++i) {
-        if (text_data[i] >= 128)
-            return Error::from_string_literal("ICC::Profile: textType data not 7-bit ASCII");
-    }
-
-    if (length == 0)
-        return Error::from_string_literal("ICC::Profile: textType too short for \\0 byte");
-
-    if (text_data[length - 1] != '\0')
-        return Error::from_string_literal("ICC::Profile: textType data not \\0-terminated");
-
-    return adopt_ref(*new TextTagData(offset, size, TRY(String::from_utf8(StringView(text_data, length - 1)))));
-}
-
-ErrorOr<NonnullRefPtr<XYZTagData>> XYZTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
-{
-    // ICC v4, 10.31 XYZType
-    VERIFY(tag_type(bytes) == Type);
-    TRY(check_reserved(bytes));
-
-    // "The XYZType contains an array of three encoded values for PCSXYZ, CIEXYZ, or nCIEXYZ values. The
-    //  number of sets of values is determined from the size of the tag."
-    size_t byte_size = bytes.size() - 8;
-    if (byte_size % sizeof(XYZNumber) != 0)
-        return Error::from_string_literal("ICC::Profile: XYZType has wrong size");
-
-    size_t xyz_count = byte_size / sizeof(XYZNumber);
-    XYZNumber const* raw_xyzs = bit_cast<XYZNumber const*>(bytes.data() + 8);
-    Vector<XYZ, 1> xyzs;
-    TRY(xyzs.try_resize(xyz_count));
-    for (size_t i = 0; i < xyz_count; ++i)
-        xyzs[i] = (XYZ)raw_xyzs[i];
-
-    return adopt_ref(*new XYZTagData(offset, size, move(xyzs)));
-}
-
 ErrorOr<void> Profile::read_header(ReadonlyBytes bytes)
 ErrorOr<void> Profile::read_header(ReadonlyBytes bytes)
 {
 {
     if (bytes.size() < sizeof(ICCHeader))
     if (bytes.size() < sizeof(ICCHeader))

+ 3 - 411
Userland/Libraries/LibGfx/ICC/Profile.h

@@ -7,119 +7,21 @@
 #pragma once
 #pragma once
 
 
 #include <AK/Error.h>
 #include <AK/Error.h>
-#include <AK/FixedPoint.h>
 #include <AK/Format.h>
 #include <AK/Format.h>
 #include <AK/HashMap.h>
 #include <AK/HashMap.h>
 #include <AK/NonnullRefPtr.h>
 #include <AK/NonnullRefPtr.h>
 #include <AK/RefCounted.h>
 #include <AK/RefCounted.h>
 #include <AK/Span.h>
 #include <AK/Span.h>
-#include <AK/String.h>
 #include <AK/URL.h>
 #include <AK/URL.h>
 #include <LibCrypto/Hash/MD5.h>
 #include <LibCrypto/Hash/MD5.h>
+#include <LibGfx/ICC/DistinctFourCC.h>
+#include <LibGfx/ICC/TagTypes.h>
 
 
 namespace Gfx::ICC {
 namespace Gfx::ICC {
 
 
-// The ICC spec uses FourCCs for many different things.
-// This is used to give FourCCs for different roles distinct types, so that they can only be compared to the correct constants.
-// (FourCCs that have only a small and fixed set of values should use an enum class instead, see e.g. DeviceClass and ColorSpace below.)
-enum class FourCCType {
-    PreferredCMMType,
-    DeviceManufacturer,
-    DeviceModel,
-    Creator,
-    TagSignature,
-    TagTypeSignature,
-};
-
-template<FourCCType type>
-struct [[gnu::packed]] DistinctFourCC {
-    constexpr explicit DistinctFourCC(u32 value)
-        : value(value)
-    {
-    }
-    constexpr operator u32() const { return value; }
-
-    char c0() const { return value >> 24; }
-    char c1() const { return (value >> 16) & 0xff; }
-    char c2() const { return (value >> 8) & 0xff; }
-    char c3() const { return value & 0xff; }
-
-    bool operator==(DistinctFourCC b) const { return value == b.value; }
-
-    u32 value { 0 };
-};
-
-using PreferredCMMType = DistinctFourCC<FourCCType::PreferredCMMType>;     // ICC v4, "7.2.3 Preferred CMM type field"
-using DeviceManufacturer = DistinctFourCC<FourCCType::DeviceManufacturer>; // ICC v4, "7.2.12 Device manufacturer field"
-using DeviceModel = DistinctFourCC<FourCCType::DeviceModel>;               // ICC v4, "7.2.13 Device model field"
-using Creator = DistinctFourCC<FourCCType::Creator>;                       // ICC v4, "7.2.17 Profile creator field"
-using TagSignature = DistinctFourCC<FourCCType::TagSignature>;             // ICC v4, "9.2 Tag listing"
-using TagTypeSignature = DistinctFourCC<FourCCType::TagTypeSignature>;     // ICC v4, "10 Tag type definitions"
-
 URL device_manufacturer_url(DeviceManufacturer);
 URL device_manufacturer_url(DeviceManufacturer);
 URL device_model_url(DeviceModel);
 URL device_model_url(DeviceModel);
 
 
-// ICC v4, 9.2 Tag listing
-// FIXME: Add v2-only tags too.
-#define ENUMERATE_TAG_SIGNATURES(TAG)                               \
-    TAG(AToB0Tag, 0x41324230 /* 'A2B0' */)                          \
-    TAG(AToB1Tag, 0x41324231 /* 'A2B1' */)                          \
-    TAG(AToB2Tag, 0x41324232 /* 'A2B2' */)                          \
-    TAG(blueMatrixColumnTag, 0x6258595A /* 'bXYZ' */)               \
-    TAG(blueTRCTag, 0x62545243 /* 'bTRC' */)                        \
-    TAG(BToA0Tag, 0x42324130 /* 'B2A0' */)                          \
-    TAG(BToA1Tag, 0x42324131 /* 'B2A1' */)                          \
-    TAG(BToA2Tag, 0x42324132 /* 'B2A2' */)                          \
-    TAG(BToD0Tag, 0x42324430 /* 'B2D0' */)                          \
-    TAG(BToD1Tag, 0x42324431 /* 'B2D1' */)                          \
-    TAG(BToD2Tag, 0x42324432 /* 'B2D2' */)                          \
-    TAG(BToD3Tag, 0x42324433 /* 'B2D3' */)                          \
-    TAG(calibrationDateTimeTag, 0x63616C74 /* 'calt' */)            \
-    TAG(charTargetTag, 0x74617267 /* 'targ' */)                     \
-    TAG(chromaticAdaptationTag, 0x63686164 /* 'chad' */)            \
-    TAG(chromaticityTag, 0x6368726D /* 'chrm' */)                   \
-    TAG(cicpTag, 0x63696370 /* 'cicp' */)                           \
-    TAG(colorantOrderTag, 0x636C726F /* 'clro' */)                  \
-    TAG(colorantTableTag, 0x636C7274 /* 'clrt' */)                  \
-    TAG(colorantTableOutTag, 0x636C6F74 /* 'clot' */)               \
-    TAG(colorimetricIntentImageStateTag, 0x63696973 /* 'ciis' */)   \
-    TAG(copyrightTag, 0x63707274 /* 'cprt' */)                      \
-    TAG(deviceMfgDescTag, 0x646D6E64 /* 'dmnd' */)                  \
-    TAG(deviceModelDescTag, 0x646D6464 /* 'dmdd' */)                \
-    TAG(DToB0Tag, 0x44324230 /* 'D2B0' */)                          \
-    TAG(DToB1Tag, 0x44324231 /* 'D2B1' */)                          \
-    TAG(DToB2Tag, 0x44324232 /* 'D2B2' */)                          \
-    TAG(DToB3Tag, 0x44324233 /* 'D2B3' */)                          \
-    TAG(gamutTag, 0x67616D74 /* 'gamt' */)                          \
-    TAG(grayTRCTag, 0x6B545243 /* 'kTRC' */)                        \
-    TAG(greenMatrixColumnTag, 0x6758595A /* 'gXYZ' */)              \
-    TAG(greenTRCTag, 0x67545243 /* 'gTRC' */)                       \
-    TAG(luminanceTag, 0x6C756D69 /* 'lumi' */)                      \
-    TAG(measurementTag, 0x6D656173 /* 'meas' */)                    \
-    TAG(metadataTag, 0x6D657461 /* 'meta' */)                       \
-    TAG(mediaWhitePointTag, 0x77747074 /* 'wtpt' */)                \
-    TAG(namedColor2Tag, 0x6E636C32 /* 'ncl2' */)                    \
-    TAG(outputResponseTag, 0x72657370 /* 'resp' */)                 \
-    TAG(perceptualRenderingIntentGamutTag, 0x72696730 /* 'rig0' */) \
-    TAG(preview0Tag, 0x70726530 /* 'pre0' */)                       \
-    TAG(preview1Tag, 0x70726531 /* 'pre1' */)                       \
-    TAG(preview2Tag, 0x70726532 /* 'pre2' */)                       \
-    TAG(profileDescriptionTag, 0x64657363 /* 'desc' */)             \
-    TAG(profileSequenceDescTag, 0x70736571 /* 'pseq' */)            \
-    TAG(profileSequenceIdentifierTag, 0x70736964 /* 'psid' */)      \
-    TAG(redMatrixColumnTag, 0x7258595A /* 'rXYZ' */)                \
-    TAG(redTRCTag, 0x72545243 /* 'rTRC' */)                         \
-    TAG(saturationRenderingIntentGamutTag, 0x72696732 /* 'rig2' */) \
-    TAG(technologyTag, 0x74656368 /* 'tech' */)                     \
-    TAG(viewingCondDescTag, 0x76756564 /* 'vued' */)                \
-    TAG(viewingConditionsTag, 0x76696577 /* 'view' */)
-
-#define TAG(name, id) constexpr inline TagSignature name { id };
-ENUMERATE_TAG_SIGNATURES(TAG)
-#undef TAG
-
-Optional<StringView> tag_signature_spec_name(TagSignature);
-
 // ICC v4, 7.2.4 Profile version field
 // ICC v4, 7.2.4 Profile version field
 class Version {
 class Version {
 public:
 public:
@@ -285,279 +187,6 @@ private:
     u64 m_bits = 0;
     u64 m_bits = 0;
 };
 };
 
 
-using S15Fixed16 = FixedPoint<16, i32>;
-
-struct XYZ {
-    double x { 0 };
-    double y { 0 };
-    double z { 0 };
-
-    bool operator==(const XYZ&) const = default;
-};
-
-class TagData : public RefCounted<TagData> {
-public:
-    u32 offset() const { return m_offset; }
-    u32 size() const { return m_size; }
-    TagTypeSignature type() const { return m_type; }
-
-protected:
-    TagData(u32 offset, u32 size, TagTypeSignature type)
-        : m_offset(offset)
-        , m_size(size)
-        , m_type(type)
-    {
-    }
-
-private:
-    u32 m_offset;
-    u32 m_size;
-    TagTypeSignature m_type;
-};
-
-class UnknownTagData : public TagData {
-public:
-    UnknownTagData(u32 offset, u32 size, TagTypeSignature type)
-        : TagData(offset, size, type)
-    {
-    }
-};
-
-// ICC v4, 10.6 curveType
-class CurveTagData : public TagData {
-public:
-    static constexpr TagTypeSignature Type { 0x63757276 }; // 'curv'
-
-    static ErrorOr<NonnullRefPtr<CurveTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
-
-    CurveTagData(u32 offset, u32 size, Vector<u16> values)
-        : TagData(offset, size, Type)
-        , m_values(move(values))
-    {
-    }
-
-    // "The curveType embodies a one-dimensional function which maps an input value in the domain of the function
-    //  to an output value in the range of the function. The domain and range values are in the range of 0,0 to 1,0.
-    //  - When n is equal to 0, an identity response is assumed.
-    //  - When n is equal to 1, then the curve value shall be interpreted as a gamma value, encoded as a
-    //    u8Fixed8Number. Gamma shall be interpreted as the exponent in the equation y = pow(x,γ) and not as an inverse.
-    //  - When n is greater than 1, the curve values (which embody a sampled one-dimensional function) shall be
-    //    defined as follows:
-    //    - The first entry represents the input value 0,0, the last entry represents the input value 1,0, and intermediate
-    //      entries are uniformly spaced using an increment of 1,0/(n-1). These entries are encoded as uInt16Numbers
-    //      (i.e. the values represented by the entries, which are in the range 0,0 to 1,0 are encoded in the range 0 to
-    //      65 535). Function values between the entries shall be obtained through linear interpolation."
-    Vector<u16> const& values() const { return m_values; }
-
-private:
-    Vector<u16> m_values;
-};
-
-// ICC v4, 10.15 multiLocalizedUnicodeType
-class MultiLocalizedUnicodeTagData : public TagData {
-public:
-    static constexpr TagTypeSignature Type { 0x6D6C7563 }; // 'mluc'
-
-    static ErrorOr<NonnullRefPtr<MultiLocalizedUnicodeTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
-
-    struct Record {
-        u16 iso_639_1_language_code;
-        u16 iso_3166_1_country_code;
-        String text;
-    };
-
-    MultiLocalizedUnicodeTagData(u32 offset, u32 size, Vector<Record> records)
-        : TagData(offset, size, Type)
-        , m_records(move(records))
-    {
-    }
-
-    Vector<Record> const& records() const { return m_records; }
-
-private:
-    Vector<Record> m_records;
-};
-
-// ICC v4, 10.18 parametricCurveType
-class ParametricCurveTagData : public TagData {
-public:
-    // Table 68 — parametricCurveType function type encoding
-    enum class FunctionType {
-        // Y = X**g
-        Type0,
-
-        // Y = (a*X + b)**g       if X >= -b/a
-        //   = 0                  else
-        Type1,
-        CIE_122_1966 = Type1,
-
-        // Y = (a*X + b)**g + c   if X >= -b/a
-        //   = c                  else
-        Type2,
-        IEC_61966_1 = Type2,
-
-        // Y = (a*X + b)**g       if X >= d
-        //   =  c*X               else
-        Type3,
-        IEC_61966_2_1 = Type3,
-        sRGB = Type3,
-
-        // Y = (a*X + b)**g + e   if X >= d
-        //   =  c*X + f           else
-        Type4,
-    };
-
-    // "The domain and range of each function shall be [0,0 1,0]. Any function value outside the range shall be clipped
-    //  to the range of the function."
-    // "NOTE 1 The parameters selected for a parametric curve can result in complex or undefined values for the input range
-    //  used. This can occur, for example, if d < -b/a. In such cases the behaviour of the curve is undefined."
-
-    static constexpr TagTypeSignature Type { 0x70617261 }; // 'para'
-
-    static ErrorOr<NonnullRefPtr<ParametricCurveTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
-
-    ParametricCurveTagData(u32 offset, u32 size, FunctionType function_type, Array<S15Fixed16, 7> parameters)
-        : TagData(offset, size, Type)
-        , m_function_type(function_type)
-        , m_parameters(move(parameters))
-    {
-    }
-
-    FunctionType function_type() const { return m_function_type; }
-
-    static unsigned parameter_count(FunctionType);
-
-    S15Fixed16 g() const { return m_parameters[0]; }
-    S15Fixed16 a() const
-    {
-        VERIFY(function_type() >= FunctionType::Type1);
-        return m_parameters[1];
-    }
-    S15Fixed16 b() const
-    {
-        VERIFY(function_type() >= FunctionType::Type1);
-        return m_parameters[2];
-    }
-    S15Fixed16 c() const
-    {
-        VERIFY(function_type() >= FunctionType::Type2);
-        return m_parameters[3];
-    }
-    S15Fixed16 d() const
-    {
-        VERIFY(function_type() >= FunctionType::Type3);
-        return m_parameters[4];
-    }
-    S15Fixed16 e() const
-    {
-        VERIFY(function_type() >= FunctionType::Type4);
-        return m_parameters[5];
-    }
-    S15Fixed16 f() const
-    {
-        VERIFY(function_type() >= FunctionType::Type4);
-        return m_parameters[6];
-    }
-
-private:
-    FunctionType m_function_type;
-
-    // Contains, in this order, g a b c d e f.
-    // Not all FunctionTypes use all parameters.
-    Array<S15Fixed16, 7> m_parameters;
-};
-
-// ICC v4, 10.22 s15Fixed16ArrayType
-class S15Fixed16ArrayTagData : public TagData {
-public:
-    static constexpr TagTypeSignature Type { 0x73663332 }; // 'sf32'
-
-    static ErrorOr<NonnullRefPtr<S15Fixed16ArrayTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
-
-    S15Fixed16ArrayTagData(u32 offset, u32 size, Vector<S15Fixed16, 9> values)
-        : TagData(offset, size, Type)
-        , m_values(move(values))
-    {
-    }
-
-    Vector<S15Fixed16, 9> const& values() const { return m_values; }
-
-private:
-    Vector<S15Fixed16, 9> m_values;
-};
-
-// ICC v2, 6.5.17 textDescriptionType
-class TextDescriptionTagData : public TagData {
-public:
-    static constexpr TagTypeSignature Type { 0x64657363 }; // 'desc'
-
-    static ErrorOr<NonnullRefPtr<TextDescriptionTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
-
-    TextDescriptionTagData(u32 offset, u32 size, String ascii_description, u32 unicode_language_code, Optional<String> unicode_description, Optional<String> macintosh_description)
-        : TagData(offset, size, Type)
-        , m_ascii_description(move(ascii_description))
-        , m_unicode_language_code(unicode_language_code)
-        , m_unicode_description(move(unicode_description))
-        , m_macintosh_description(move(macintosh_description))
-    {
-    }
-
-    // Guaranteed to be 7-bit ASCII.
-    String const& ascii_description() const { return m_ascii_description; }
-
-    u32 unicode_language_code() const { return m_unicode_language_code; }
-    Optional<String> const& unicode_description() const { return m_unicode_description; }
-
-    Optional<String> const& macintosh_description() const { return m_macintosh_description; }
-
-private:
-    String m_ascii_description;
-
-    u32 m_unicode_language_code { 0 };
-    Optional<String> m_unicode_description;
-
-    Optional<String> m_macintosh_description;
-};
-
-// ICC v4, 10.24 textType
-class TextTagData : public TagData {
-public:
-    static constexpr TagTypeSignature Type { 0x74657874 }; // 'text'
-
-    static ErrorOr<NonnullRefPtr<TextTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
-
-    TextTagData(u32 offset, u32 size, String text)
-        : TagData(offset, size, Type)
-        , m_text(move(text))
-    {
-    }
-
-    // Guaranteed to be 7-bit ASCII.
-    String const& text() const { return m_text; }
-
-private:
-    String m_text;
-};
-
-// ICC v4, 10.31 XYZType
-class XYZTagData : public TagData {
-public:
-    static constexpr TagTypeSignature Type { 0x58595A20 }; // 'XYZ '
-
-    static ErrorOr<NonnullRefPtr<XYZTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
-
-    XYZTagData(u32 offset, u32 size, Vector<XYZ, 1> xyzs)
-        : TagData(offset, size, Type)
-        , m_xyzs(move(xyzs))
-    {
-    }
-
-    Vector<XYZ, 1> const& xyzs() const { return m_xyzs; }
-
-private:
-    Vector<XYZ, 1> m_xyzs;
-};
-
 class Profile : public RefCounted<Profile> {
 class Profile : public RefCounted<Profile> {
 public:
 public:
     static ErrorOr<NonnullRefPtr<Profile>> try_load_from_externally_owned_memory(ReadonlyBytes);
     static ErrorOr<NonnullRefPtr<Profile>> try_load_from_externally_owned_memory(ReadonlyBytes);
@@ -624,47 +253,10 @@ private:
 
 
 }
 }
 
 
-namespace AK {
-template<Gfx::ICC::FourCCType Type>
-struct Formatter<Gfx::ICC::DistinctFourCC<Type>> : StandardFormatter {
-    ErrorOr<void> format(FormatBuilder& builder, Gfx::ICC::DistinctFourCC<Type> const& four_cc)
-    {
-        TRY(builder.put_padding('\'', 1));
-        TRY(builder.put_padding(four_cc.c0(), 1));
-        TRY(builder.put_padding(four_cc.c1(), 1));
-        TRY(builder.put_padding(four_cc.c2(), 1));
-        TRY(builder.put_padding(four_cc.c3(), 1));
-        TRY(builder.put_padding('\'', 1));
-        return {};
-    }
-};
-
 template<>
 template<>
-struct Formatter<Gfx::ICC::Version> : Formatter<FormatString> {
+struct AK::Formatter<Gfx::ICC::Version> : Formatter<FormatString> {
     ErrorOr<void> format(FormatBuilder& builder, Gfx::ICC::Version const& version)
     ErrorOr<void> format(FormatBuilder& builder, Gfx::ICC::Version const& version)
     {
     {
         return Formatter<FormatString>::format(builder, "{}.{}.{}"sv, version.major_version(), version.minor_version(), version.bugfix_version());
         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);
-    }
-};
-
-template<Gfx::ICC::FourCCType Type>
-struct Traits<Gfx::ICC::DistinctFourCC<Type>> : public GenericTraits<Gfx::ICC::DistinctFourCC<Type>> {
-    static unsigned hash(Gfx::ICC::DistinctFourCC<Type> const& key)
-    {
-        return int_hash(key.value);
-    }
-
-    static bool equals(Gfx::ICC::DistinctFourCC<Type> const& a, Gfx::ICC::DistinctFourCC<Type> const& b)
-    {
-        return a == b;
-    }
-};
-}

+ 390 - 0
Userland/Libraries/LibGfx/ICC/TagTypes.cpp

@@ -0,0 +1,390 @@
+/*
+ * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/DeprecatedString.h>
+#include <AK/Endian.h>
+#include <LibGfx/ICC/TagTypes.h>
+#include <LibTextCodec/Decoder.h>
+
+namespace Gfx::ICC {
+
+namespace {
+
+// 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<void> check_reserved(ReadonlyBytes tag_bytes)
+{
+    if (tag_bytes.size() < 2 * sizeof(u32))
+        return Error::from_string_literal("ICC::Profile: Not enough data for tag reserved field");
+
+    if (*bit_cast<BigEndian<u32> const*>(tag_bytes.data() + sizeof(u32)) != 0)
+        return Error::from_string_literal("ICC::Profile: tag reserved field not 0");
+
+    return {};
+}
+
+}
+
+TagTypeSignature tag_type(ReadonlyBytes tag_bytes)
+{
+    VERIFY(tag_bytes.size() >= sizeof(u32));
+    return *bit_cast<BigEndian<TagTypeSignature> const*>(tag_bytes.data());
+}
+
+ErrorOr<NonnullRefPtr<CurveTagData>> CurveTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
+{
+    // ICC v4, 10.6 curveType
+    VERIFY(tag_type(bytes) == Type);
+    TRY(check_reserved(bytes));
+
+    if (bytes.size() < 3 * sizeof(u32))
+        return Error::from_string_literal("ICC::Profile: curveType has not enough data for count");
+    u32 count = *bit_cast<BigEndian<u32> const*>(bytes.data() + 8);
+
+    if (bytes.size() < 3 * sizeof(u32) + count * sizeof(u16))
+        return Error::from_string_literal("ICC::Profile: curveType has not enough data for curve points");
+
+    BigEndian<u16> const* raw_values = bit_cast<BigEndian<u16> const*>(bytes.data() + 12);
+    Vector<u16> values;
+    TRY(values.try_resize(count));
+
+    for (u32 i = 0; i < count; ++i)
+        values[i] = raw_values[i];
+
+    return adopt_ref(*new CurveTagData(offset, size, move(values)));
+}
+
+ErrorOr<NonnullRefPtr<MultiLocalizedUnicodeTagData>> MultiLocalizedUnicodeTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
+{
+    // ICC v4, 10.15 multiLocalizedUnicodeType
+    VERIFY(tag_type(bytes) == Type);
+    TRY(check_reserved(bytes));
+
+    // "Multiple strings within this tag may share storage locations. For example, en/US and en/UK can refer to the
+    //  same string data."
+    // This implementation makes redudant string copies in that case.
+    // Most of the time, this costs just a few bytes, so that seems ok.
+
+    if (bytes.size() < 4 * sizeof(u32))
+        return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType has not enough data");
+
+    // Table 54 — multiLocalizedUnicodeType
+    u32 number_of_records = *bit_cast<BigEndian<u32> const*>(bytes.data() + 8);
+    u32 record_size = *bit_cast<BigEndian<u32> const*>(bytes.data() + 12);
+
+    // "The fourth field of this tag, the record size, should contain the value 12, which corresponds to the size in bytes
+    // of each record. Any code that needs to access the nth record should determine the record’s offset by multiplying
+    // n by the contents of this size field and adding 16. This minor extra effort allows for future expansion of the record
+    // encoding, should the need arise, without having to define a new tag type."
+    if (record_size < 12)
+        return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType record size too small");
+    if (bytes.size() < 16 + number_of_records * record_size)
+        return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType not enough data for records");
+
+    Vector<Record> records;
+    TRY(records.try_resize(number_of_records));
+
+    // "For the definition of language codes and country codes, see respectively
+    //  ISO 639-1 and ISO 3166-1. The Unicode strings in storage should be encoded as 16-bit big-endian, UTF-16BE,
+    //  and should not be NULL terminated."
+    auto& utf_16be_decoder = *TextCodec::decoder_for("utf-16be");
+
+    struct RawRecord {
+        BigEndian<u16> language_code;
+        BigEndian<u16> country_code;
+        BigEndian<u32> string_length_in_bytes;
+        BigEndian<u32> string_offset_in_bytes;
+    };
+
+    for (u32 i = 0; i < number_of_records; ++i) {
+        size_t offset = 16 + i * record_size;
+        RawRecord record = *bit_cast<RawRecord const*>(bytes.data() + offset);
+
+        records[i].iso_639_1_language_code = record.language_code;
+        records[i].iso_3166_1_country_code = record.country_code;
+
+        if (record.string_length_in_bytes % 2 != 0)
+            return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType odd UTF-16 byte length");
+
+        if (record.string_offset_in_bytes + record.string_length_in_bytes > bytes.size())
+            return Error::from_string_literal("ICC::Profile: multiLocalizedUnicodeType string offset out of bounds");
+
+        StringView utf_16be_data { bytes.data() + record.string_offset_in_bytes, record.string_length_in_bytes };
+        records[i].text = TRY(String::from_deprecated_string(utf_16be_decoder.to_utf8(utf_16be_data)));
+    }
+
+    return adopt_ref(*new MultiLocalizedUnicodeTagData(offset, size, move(records)));
+}
+
+unsigned ParametricCurveTagData::parameter_count(FunctionType function_type)
+{
+    switch (function_type) {
+    case FunctionType::Type0:
+        return 1;
+    case FunctionType::Type1:
+        return 3;
+    case FunctionType::Type2:
+        return 4;
+    case FunctionType::Type3:
+        return 5;
+    case FunctionType::Type4:
+        return 7;
+    }
+    VERIFY_NOT_REACHED();
+}
+
+ErrorOr<NonnullRefPtr<ParametricCurveTagData>> ParametricCurveTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
+{
+    // ICC v4, 10.18 parametricCurveType
+    VERIFY(tag_type(bytes) == Type);
+    TRY(check_reserved(bytes));
+
+    // "The parametricCurveType describes a one-dimensional curve by specifying one of a predefined set of functions
+    //  using the parameters."
+
+    if (bytes.size() < 2 * sizeof(u32) + 2 * sizeof(u16))
+        return Error::from_string_literal("ICC::Profile: parametricCurveType has not enough data");
+
+    u16 raw_function_type = *bit_cast<BigEndian<u16> const*>(bytes.data() + 8);
+    u16 reserved = *bit_cast<BigEndian<u16> const*>(bytes.data() + 10);
+    if (reserved != 0)
+        return Error::from_string_literal("ICC::Profile: parametricCurveType reserved u16 after function type not 0");
+
+    if (raw_function_type > 4)
+        return Error::from_string_literal("ICC::Profile: parametricCurveType unknown function type");
+
+    FunctionType function_type = (FunctionType)raw_function_type;
+    unsigned count = parameter_count(function_type);
+
+    if (bytes.size() < 2 * sizeof(u32) + 2 * sizeof(u16) + count * sizeof(s15Fixed16Number))
+        return Error::from_string_literal("ICC::Profile: parametricCurveType has not enough data for parameters");
+
+    BigEndian<s15Fixed16Number> const* raw_parameters = bit_cast<BigEndian<s15Fixed16Number> const*>(bytes.data() + 12);
+    Array<S15Fixed16, 7> parameters;
+    parameters.fill(0);
+    for (unsigned i = 0; i < count; ++i)
+        parameters[i] = S15Fixed16::create_raw(raw_parameters[i]);
+
+    return adopt_ref(*new ParametricCurveTagData(offset, size, function_type, move(parameters)));
+}
+
+ErrorOr<NonnullRefPtr<S15Fixed16ArrayTagData>> S15Fixed16ArrayTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
+{
+    // ICC v4, 10.22 s15Fixed16ArrayType
+    VERIFY(tag_type(bytes) == Type);
+    TRY(check_reserved(bytes));
+
+    // "This type represents an array of generic 4-byte (32-bit) fixed point quantity. The number of values is determined
+    //  from the size of the tag."
+    size_t byte_size = bytes.size() - 8;
+    if (byte_size % sizeof(s15Fixed16Number) != 0)
+        return Error::from_string_literal("ICC::Profile: s15Fixed16ArrayType has wrong size");
+
+    size_t count = byte_size / sizeof(s15Fixed16Number);
+    BigEndian<s15Fixed16Number> const* raw_values = bit_cast<BigEndian<s15Fixed16Number> const*>(bytes.data() + 8);
+    Vector<S15Fixed16, 9> values;
+    TRY(values.try_resize(count));
+    for (size_t i = 0; i < count; ++i)
+        values[i] = S15Fixed16::create_raw(raw_values[i]);
+
+    return adopt_ref(*new S15Fixed16ArrayTagData(offset, size, move(values)));
+}
+
+ErrorOr<NonnullRefPtr<TextDescriptionTagData>> TextDescriptionTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
+{
+    // ICC v2, 6.5.17 textDescriptionType
+    // textDescriptionType is no longer in the V4 spec.
+    // In both the V2 and V4 specs, 'desc' is a required tag. In V4, it has type multiLocalizedUnicodeType,
+    // but in V2 it has type textDescriptionType. Since 'desc' is required, this type is present in every
+    // V2 icc file, and there are still many V2 files in use. So textDescriptionType is here to stay for now.
+    // It's a very 90s type, preceding universal adoption of Unicode.
+
+    // "The textDescriptionType is a complex structure that contains three types of text description structures:
+    //  7-bit ASCII, Unicode and ScriptCode. Since no single standard method for specifying localizable character
+    //  sets exists across the major platform vendors, including all three provides access for the major operating
+    //  systems. The 7-bit ASCII description is to be an invariant, nonlocalizable name for consistent reference.
+    //  It is preferred that both the Unicode and ScriptCode structures be properly localized."
+
+    VERIFY(tag_type(bytes) == Type);
+    TRY(check_reserved(bytes));
+
+    // 7-bit ASCII
+
+    // "ASCII: The count is the length of the string in bytes including the null terminator."
+    if (bytes.size() < 3 * sizeof(u32))
+        return Error::from_string_literal("ICC::Profile: textDescriptionType has not enough data for ASCII size");
+    u32 ascii_description_length = *bit_cast<BigEndian<u32> const*>(bytes.data() + 8);
+
+    if (bytes.size() < 3 * sizeof(u32) + ascii_description_length)
+        return Error::from_string_literal("ICC::Profile: textDescriptionType has not enough data for ASCII description");
+
+    u8 const* ascii_description_data = bytes.data() + 3 * sizeof(u32);
+    for (u32 i = 0; i < ascii_description_length; ++i) {
+        if (ascii_description_data[i] >= 128)
+            return Error::from_string_literal("ICC::Profile: textDescriptionType ASCII description not 7-bit ASCII");
+    }
+
+    if (ascii_description_length == 0)
+        return Error::from_string_literal("ICC::Profile: textDescriptionType ASCII description length does not include trailing \\0");
+
+    if (ascii_description_data[ascii_description_length - 1] != '\0')
+        return Error::from_string_literal("ICC::Profile: textDescriptionType ASCII description not \\0-terminated");
+
+    StringView ascii_description { ascii_description_data, ascii_description_length - 1 };
+
+    // Unicode
+
+    if (bytes.size() < 3 * sizeof(u32) + ascii_description_length + 2 * sizeof(u32))
+        return Error::from_string_literal("ICC::Profile: textDescriptionType has not enough data for Unicode metadata");
+
+    // "Because the Unicode language code and Unicode count immediately follow the ASCII description,
+    //  their alignment is not correct when the ASCII count is not a multiple of four"
+    // So we can't use BigEndian<u32> here.
+    u8 const* cursor = ascii_description_data + ascii_description_length;
+    u32 unicode_language_code = (u32)(cursor[0] << 24) | (u32)(cursor[1] << 16) | (u32)(cursor[2] << 8) | (u32)cursor[3];
+    cursor += 4;
+
+    // "Unicode: The count is the number of characters including a Unicode null where a character is always two bytes."
+    // This implies UCS-2.
+    u32 unicode_description_length = (u32)(cursor[0] << 24) | (u32)(cursor[1] << 16) | (u32)(cursor[2] << 8) | (u32)cursor[3];
+    cursor += 4;
+
+    if (bytes.size() < 3 * sizeof(u32) + ascii_description_length + 2 * sizeof(u32) + 2 * unicode_description_length)
+        return Error::from_string_literal("ICC::Profile: textDescriptionType has not enough data for Unicode description");
+
+    u8 const* unicode_description_data = cursor;
+    cursor += 2 * unicode_description_length;
+    for (u32 i = 0; i < unicode_description_length; ++i) {
+        u16 code_point = (u16)(unicode_description_data[2 * i] << 8) | (u16)unicode_description_data[2 * i + 1];
+        if (is_unicode_surrogate(code_point))
+            return Error::from_string_literal("ICC::Profile: textDescriptionType Unicode description is not valid UCS-2");
+    }
+
+    // If Unicode is not native on the platform, then the Unicode language code and Unicode count should be
+    // filled in as 0, with no data placed in the Unicode localizable profile description area.
+    Optional<String> unicode_description;
+    if (unicode_description_length > 0) {
+        u16 last_code_point = (u16)(unicode_description_data[2 * (unicode_description_length - 1)] << 8) | (u16)unicode_description_data[2 * (unicode_description_length - 1) + 1];
+        if (last_code_point != 0)
+            return Error::from_string_literal("ICC::Profile: textDescriptionType Unicode description not \\0-terminated");
+
+        StringView utf_16be_data { unicode_description_data, 2 * (unicode_description_length - 1) };
+        unicode_description = TRY(String::from_deprecated_string(TextCodec::decoder_for("utf-16be")->to_utf8(utf_16be_data)));
+    }
+
+    // ScriptCode
+
+    // What is a script code? It's an old, obsolete mac thing. It looks like it's documented in
+    // https://developer.apple.com/library/archive/documentation/mac/pdf/Text.pdf
+    // "Script Codes, Language Codes, and Region Codes 1", PDF page 82.
+    // I haven't found a complete explanation though. PDF page 84 suggests that:
+    // - There are 16 script codes
+    // - 0 is Roman, 1 is Japanese, 2 is Chinese, 3 is Korean, 9 is Devanagari
+    // Roman uses https://en.wikipedia.org/wiki/Mac_OS_Roman as encoding (also on page 89),
+    // and "All non-Roman script systems include Roman as a subscript" (page 87).
+
+    // Aha, "Script Codes 6" on page 676 has the complete list! There are 32 of them.
+    // The document mentions that each script code possibly has its own encoding, but I haven't found
+    // details on the encodings for script codes other than 0 (which uses Mac OS Roman).
+    // http://www.kreativekorp.com/charset/encoding/ has an unofficial list of old Mac OS encodings,
+    // but it's not clear to me which script codes map to which encoding.
+
+    // From here on, quotes are from the ICC spec on textDescriptionType again.
+
+    // "The ScriptCode code is misaligned when the ASCII count is odd."
+    // So don't use BigEndian<u16> here.
+    u16 scriptcode_code = (u16)(cursor[0] << 8) | (u32)cursor[1];
+    cursor += 2;
+
+    // "ScriptCode: The count is the length of the string in bytes including the terminating null."
+    u8 macintosh_description_length = *cursor;
+    cursor += 1;
+
+    if (macintosh_description_length > 67)
+        return Error::from_string_literal("ICC::Profile: textDescriptionType ScriptCode description too long");
+
+    u8 const* macintosh_description_data = cursor;
+
+    // "If Scriptcode is not native on the platform, then the ScriptCode code and ScriptCode count should be filled
+    // in as 0. The 67-byte localizable Macintosh profile description should be filled with 0’s."
+    Optional<String> macintosh_description;
+    if (macintosh_description_length > 0) {
+        // ScriptCode is old-timey and a complicated to fully support. Lightroom Classic does write the ScriptCode section of textDescriptionType.
+        // But supporting only ASCII MacRoman is good enough for those files, and easy to implement, so let's do only that for now.
+        if (scriptcode_code == 0) { // MacRoman
+            if (macintosh_description_data[macintosh_description_length - 1] != '\0')
+                return Error::from_string_literal("ICC::Profile: textDescriptionType ScriptCode not \\0-terminated");
+
+            macintosh_description = TRY(String::from_deprecated_string(TextCodec::decoder_for("x-mac-roman")->to_utf8({ macintosh_description_data, (size_t)macintosh_description_length - 1 })));
+        } else {
+            dbgln("TODO: ICCProfile textDescriptionType ScriptCode {}, length {}", scriptcode_code, macintosh_description_length);
+        }
+    }
+
+    return adopt_ref(*new TextDescriptionTagData(offset, size, TRY(String::from_utf8(ascii_description)), unicode_language_code, move(unicode_description), move(macintosh_description)));
+}
+
+ErrorOr<NonnullRefPtr<TextTagData>> TextTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
+{
+    // ICC v4, 10.24 textType
+    VERIFY(tag_type(bytes) == Type);
+    TRY(check_reserved(bytes));
+
+    // "The textType is a simple text structure that contains a 7-bit ASCII text string. The length of the string is obtained
+    //  by subtracting 8 from the element size portion of the tag itself. This string shall be terminated with a 00h byte."
+    u32 length = bytes.size() - 8;
+
+    u8 const* text_data = bytes.data() + 8;
+    for (u32 i = 0; i < length; ++i) {
+        if (text_data[i] >= 128)
+            return Error::from_string_literal("ICC::Profile: textType data not 7-bit ASCII");
+    }
+
+    if (length == 0)
+        return Error::from_string_literal("ICC::Profile: textType too short for \\0 byte");
+
+    if (text_data[length - 1] != '\0')
+        return Error::from_string_literal("ICC::Profile: textType data not \\0-terminated");
+
+    return adopt_ref(*new TextTagData(offset, size, TRY(String::from_utf8(StringView(text_data, length - 1)))));
+}
+
+ErrorOr<NonnullRefPtr<XYZTagData>> XYZTagData::from_bytes(ReadonlyBytes bytes, u32 offset, u32 size)
+{
+    // ICC v4, 10.31 XYZType
+    VERIFY(tag_type(bytes) == Type);
+    TRY(check_reserved(bytes));
+
+    // "The XYZType contains an array of three encoded values for PCSXYZ, CIEXYZ, or nCIEXYZ values. The
+    //  number of sets of values is determined from the size of the tag."
+    size_t byte_size = bytes.size() - 8;
+    if (byte_size % sizeof(XYZNumber) != 0)
+        return Error::from_string_literal("ICC::Profile: XYZType has wrong size");
+
+    size_t xyz_count = byte_size / sizeof(XYZNumber);
+    XYZNumber const* raw_xyzs = bit_cast<XYZNumber const*>(bytes.data() + 8);
+    Vector<XYZ, 1> xyzs;
+    TRY(xyzs.try_resize(xyz_count));
+    for (size_t i = 0; i < xyz_count; ++i)
+        xyzs[i] = (XYZ)raw_xyzs[i];
+
+    return adopt_ref(*new XYZTagData(offset, size, move(xyzs)));
+}
+
+}

+ 302 - 0
Userland/Libraries/LibGfx/ICC/TagTypes.h

@@ -0,0 +1,302 @@
+/*
+ * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/FixedPoint.h>
+#include <AK/NonnullRefPtr.h>
+#include <AK/RefCounted.h>
+#include <AK/Span.h>
+#include <AK/String.h>
+#include <AK/Vector.h>
+#include <LibGfx/ICC/DistinctFourCC.h>
+
+namespace Gfx::ICC {
+
+using S15Fixed16 = FixedPoint<16, i32>;
+
+struct XYZ {
+    double x { 0 };
+    double y { 0 };
+    double z { 0 };
+
+    bool operator==(const XYZ&) const = default;
+};
+
+TagTypeSignature tag_type(ReadonlyBytes tag_bytes);
+
+class TagData : public RefCounted<TagData> {
+public:
+    u32 offset() const { return m_offset; }
+    u32 size() const { return m_size; }
+    TagTypeSignature type() const { return m_type; }
+
+protected:
+    TagData(u32 offset, u32 size, TagTypeSignature type)
+        : m_offset(offset)
+        , m_size(size)
+        , m_type(type)
+    {
+    }
+
+private:
+    u32 m_offset;
+    u32 m_size;
+    TagTypeSignature m_type;
+};
+
+class UnknownTagData : public TagData {
+public:
+    UnknownTagData(u32 offset, u32 size, TagTypeSignature type)
+        : TagData(offset, size, type)
+    {
+    }
+};
+
+// ICC v4, 10.6 curveType
+class CurveTagData : public TagData {
+public:
+    static constexpr TagTypeSignature Type { 0x63757276 }; // 'curv'
+
+    static ErrorOr<NonnullRefPtr<CurveTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
+
+    CurveTagData(u32 offset, u32 size, Vector<u16> values)
+        : TagData(offset, size, Type)
+        , m_values(move(values))
+    {
+    }
+
+    // "The curveType embodies a one-dimensional function which maps an input value in the domain of the function
+    //  to an output value in the range of the function. The domain and range values are in the range of 0,0 to 1,0.
+    //  - When n is equal to 0, an identity response is assumed.
+    //  - When n is equal to 1, then the curve value shall be interpreted as a gamma value, encoded as a
+    //    u8Fixed8Number. Gamma shall be interpreted as the exponent in the equation y = pow(x,γ) and not as an inverse.
+    //  - When n is greater than 1, the curve values (which embody a sampled one-dimensional function) shall be
+    //    defined as follows:
+    //    - The first entry represents the input value 0,0, the last entry represents the input value 1,0, and intermediate
+    //      entries are uniformly spaced using an increment of 1,0/(n-1). These entries are encoded as uInt16Numbers
+    //      (i.e. the values represented by the entries, which are in the range 0,0 to 1,0 are encoded in the range 0 to
+    //      65 535). Function values between the entries shall be obtained through linear interpolation."
+    Vector<u16> const& values() const { return m_values; }
+
+private:
+    Vector<u16> m_values;
+};
+
+// ICC v4, 10.15 multiLocalizedUnicodeType
+class MultiLocalizedUnicodeTagData : public TagData {
+public:
+    static constexpr TagTypeSignature Type { 0x6D6C7563 }; // 'mluc'
+
+    static ErrorOr<NonnullRefPtr<MultiLocalizedUnicodeTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
+
+    struct Record {
+        u16 iso_639_1_language_code;
+        u16 iso_3166_1_country_code;
+        String text;
+    };
+
+    MultiLocalizedUnicodeTagData(u32 offset, u32 size, Vector<Record> records)
+        : TagData(offset, size, Type)
+        , m_records(move(records))
+    {
+    }
+
+    Vector<Record> const& records() const { return m_records; }
+
+private:
+    Vector<Record> m_records;
+};
+
+// ICC v4, 10.18 parametricCurveType
+class ParametricCurveTagData : public TagData {
+public:
+    // Table 68 — parametricCurveType function type encoding
+    enum class FunctionType {
+        // Y = X**g
+        Type0,
+
+        // Y = (a*X + b)**g       if X >= -b/a
+        //   = 0                  else
+        Type1,
+        CIE_122_1966 = Type1,
+
+        // Y = (a*X + b)**g + c   if X >= -b/a
+        //   = c                  else
+        Type2,
+        IEC_61966_1 = Type2,
+
+        // Y = (a*X + b)**g       if X >= d
+        //   =  c*X               else
+        Type3,
+        IEC_61966_2_1 = Type3,
+        sRGB = Type3,
+
+        // Y = (a*X + b)**g + e   if X >= d
+        //   =  c*X + f           else
+        Type4,
+    };
+
+    // "The domain and range of each function shall be [0,0 1,0]. Any function value outside the range shall be clipped
+    //  to the range of the function."
+    // "NOTE 1 The parameters selected for a parametric curve can result in complex or undefined values for the input range
+    //  used. This can occur, for example, if d < -b/a. In such cases the behaviour of the curve is undefined."
+
+    static constexpr TagTypeSignature Type { 0x70617261 }; // 'para'
+
+    static ErrorOr<NonnullRefPtr<ParametricCurveTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
+
+    ParametricCurveTagData(u32 offset, u32 size, FunctionType function_type, Array<S15Fixed16, 7> parameters)
+        : TagData(offset, size, Type)
+        , m_function_type(function_type)
+        , m_parameters(move(parameters))
+    {
+    }
+
+    FunctionType function_type() const { return m_function_type; }
+
+    static unsigned parameter_count(FunctionType);
+
+    S15Fixed16 g() const { return m_parameters[0]; }
+    S15Fixed16 a() const
+    {
+        VERIFY(function_type() >= FunctionType::Type1);
+        return m_parameters[1];
+    }
+    S15Fixed16 b() const
+    {
+        VERIFY(function_type() >= FunctionType::Type1);
+        return m_parameters[2];
+    }
+    S15Fixed16 c() const
+    {
+        VERIFY(function_type() >= FunctionType::Type2);
+        return m_parameters[3];
+    }
+    S15Fixed16 d() const
+    {
+        VERIFY(function_type() >= FunctionType::Type3);
+        return m_parameters[4];
+    }
+    S15Fixed16 e() const
+    {
+        VERIFY(function_type() >= FunctionType::Type4);
+        return m_parameters[5];
+    }
+    S15Fixed16 f() const
+    {
+        VERIFY(function_type() >= FunctionType::Type4);
+        return m_parameters[6];
+    }
+
+private:
+    FunctionType m_function_type;
+
+    // Contains, in this order, g a b c d e f.
+    // Not all FunctionTypes use all parameters.
+    Array<S15Fixed16, 7> m_parameters;
+};
+
+// ICC v4, 10.22 s15Fixed16ArrayType
+class S15Fixed16ArrayTagData : public TagData {
+public:
+    static constexpr TagTypeSignature Type { 0x73663332 }; // 'sf32'
+
+    static ErrorOr<NonnullRefPtr<S15Fixed16ArrayTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
+
+    S15Fixed16ArrayTagData(u32 offset, u32 size, Vector<S15Fixed16, 9> values)
+        : TagData(offset, size, Type)
+        , m_values(move(values))
+    {
+    }
+
+    Vector<S15Fixed16, 9> const& values() const { return m_values; }
+
+private:
+    Vector<S15Fixed16, 9> m_values;
+};
+
+// ICC v2, 6.5.17 textDescriptionType
+class TextDescriptionTagData : public TagData {
+public:
+    static constexpr TagTypeSignature Type { 0x64657363 }; // 'desc'
+
+    static ErrorOr<NonnullRefPtr<TextDescriptionTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
+
+    TextDescriptionTagData(u32 offset, u32 size, String ascii_description, u32 unicode_language_code, Optional<String> unicode_description, Optional<String> macintosh_description)
+        : TagData(offset, size, Type)
+        , m_ascii_description(move(ascii_description))
+        , m_unicode_language_code(unicode_language_code)
+        , m_unicode_description(move(unicode_description))
+        , m_macintosh_description(move(macintosh_description))
+    {
+    }
+
+    // Guaranteed to be 7-bit ASCII.
+    String const& ascii_description() const { return m_ascii_description; }
+
+    u32 unicode_language_code() const { return m_unicode_language_code; }
+    Optional<String> const& unicode_description() const { return m_unicode_description; }
+
+    Optional<String> const& macintosh_description() const { return m_macintosh_description; }
+
+private:
+    String m_ascii_description;
+
+    u32 m_unicode_language_code { 0 };
+    Optional<String> m_unicode_description;
+
+    Optional<String> m_macintosh_description;
+};
+
+// ICC v4, 10.24 textType
+class TextTagData : public TagData {
+public:
+    static constexpr TagTypeSignature Type { 0x74657874 }; // 'text'
+
+    static ErrorOr<NonnullRefPtr<TextTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
+
+    TextTagData(u32 offset, u32 size, String text)
+        : TagData(offset, size, Type)
+        , m_text(move(text))
+    {
+    }
+
+    // Guaranteed to be 7-bit ASCII.
+    String const& text() const { return m_text; }
+
+private:
+    String m_text;
+};
+
+// ICC v4, 10.31 XYZType
+class XYZTagData : public TagData {
+public:
+    static constexpr TagTypeSignature Type { 0x58595A20 }; // 'XYZ '
+
+    static ErrorOr<NonnullRefPtr<XYZTagData>> from_bytes(ReadonlyBytes, u32 offset, u32 size);
+
+    XYZTagData(u32 offset, u32 size, Vector<XYZ, 1> xyzs)
+        : TagData(offset, size, Type)
+        , m_xyzs(move(xyzs))
+    {
+    }
+
+    Vector<XYZ, 1> const& xyzs() const { return m_xyzs; }
+
+private:
+    Vector<XYZ, 1> m_xyzs;
+};
+
+}
+
+template<>
+struct AK::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);
+    }
+};

+ 23 - 0
Userland/Libraries/LibGfx/ICC/Tags.cpp

@@ -0,0 +1,23 @@
+/*
+ * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <LibGfx/ICC/Tags.h>
+
+namespace Gfx::ICC {
+
+Optional<StringView> tag_signature_spec_name(TagSignature tag_signature)
+{
+    switch (tag_signature) {
+#define TAG(name, id) \
+    case name:        \
+        return #name##sv;
+        ENUMERATE_TAG_SIGNATURES(TAG)
+#undef TAG
+    }
+    return {};
+}
+
+}

+ 76 - 0
Userland/Libraries/LibGfx/ICC/Tags.h

@@ -0,0 +1,76 @@
+/*
+ * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <AK/Optional.h>
+#include <AK/StringView.h>
+#include <LibGfx/ICC/DistinctFourCC.h>
+
+namespace Gfx::ICC {
+
+// ICC v4, 9.2 Tag listing
+// FIXME: Add v2-only tags too.
+#define ENUMERATE_TAG_SIGNATURES(TAG)                               \
+    TAG(AToB0Tag, 0x41324230 /* 'A2B0' */)                          \
+    TAG(AToB1Tag, 0x41324231 /* 'A2B1' */)                          \
+    TAG(AToB2Tag, 0x41324232 /* 'A2B2' */)                          \
+    TAG(blueMatrixColumnTag, 0x6258595A /* 'bXYZ' */)               \
+    TAG(blueTRCTag, 0x62545243 /* 'bTRC' */)                        \
+    TAG(BToA0Tag, 0x42324130 /* 'B2A0' */)                          \
+    TAG(BToA1Tag, 0x42324131 /* 'B2A1' */)                          \
+    TAG(BToA2Tag, 0x42324132 /* 'B2A2' */)                          \
+    TAG(BToD0Tag, 0x42324430 /* 'B2D0' */)                          \
+    TAG(BToD1Tag, 0x42324431 /* 'B2D1' */)                          \
+    TAG(BToD2Tag, 0x42324432 /* 'B2D2' */)                          \
+    TAG(BToD3Tag, 0x42324433 /* 'B2D3' */)                          \
+    TAG(calibrationDateTimeTag, 0x63616C74 /* 'calt' */)            \
+    TAG(charTargetTag, 0x74617267 /* 'targ' */)                     \
+    TAG(chromaticAdaptationTag, 0x63686164 /* 'chad' */)            \
+    TAG(chromaticityTag, 0x6368726D /* 'chrm' */)                   \
+    TAG(cicpTag, 0x63696370 /* 'cicp' */)                           \
+    TAG(colorantOrderTag, 0x636C726F /* 'clro' */)                  \
+    TAG(colorantTableTag, 0x636C7274 /* 'clrt' */)                  \
+    TAG(colorantTableOutTag, 0x636C6F74 /* 'clot' */)               \
+    TAG(colorimetricIntentImageStateTag, 0x63696973 /* 'ciis' */)   \
+    TAG(copyrightTag, 0x63707274 /* 'cprt' */)                      \
+    TAG(deviceMfgDescTag, 0x646D6E64 /* 'dmnd' */)                  \
+    TAG(deviceModelDescTag, 0x646D6464 /* 'dmdd' */)                \
+    TAG(DToB0Tag, 0x44324230 /* 'D2B0' */)                          \
+    TAG(DToB1Tag, 0x44324231 /* 'D2B1' */)                          \
+    TAG(DToB2Tag, 0x44324232 /* 'D2B2' */)                          \
+    TAG(DToB3Tag, 0x44324233 /* 'D2B3' */)                          \
+    TAG(gamutTag, 0x67616D74 /* 'gamt' */)                          \
+    TAG(grayTRCTag, 0x6B545243 /* 'kTRC' */)                        \
+    TAG(greenMatrixColumnTag, 0x6758595A /* 'gXYZ' */)              \
+    TAG(greenTRCTag, 0x67545243 /* 'gTRC' */)                       \
+    TAG(luminanceTag, 0x6C756D69 /* 'lumi' */)                      \
+    TAG(measurementTag, 0x6D656173 /* 'meas' */)                    \
+    TAG(metadataTag, 0x6D657461 /* 'meta' */)                       \
+    TAG(mediaWhitePointTag, 0x77747074 /* 'wtpt' */)                \
+    TAG(namedColor2Tag, 0x6E636C32 /* 'ncl2' */)                    \
+    TAG(outputResponseTag, 0x72657370 /* 'resp' */)                 \
+    TAG(perceptualRenderingIntentGamutTag, 0x72696730 /* 'rig0' */) \
+    TAG(preview0Tag, 0x70726530 /* 'pre0' */)                       \
+    TAG(preview1Tag, 0x70726531 /* 'pre1' */)                       \
+    TAG(preview2Tag, 0x70726532 /* 'pre2' */)                       \
+    TAG(profileDescriptionTag, 0x64657363 /* 'desc' */)             \
+    TAG(profileSequenceDescTag, 0x70736571 /* 'pseq' */)            \
+    TAG(profileSequenceIdentifierTag, 0x70736964 /* 'psid' */)      \
+    TAG(redMatrixColumnTag, 0x7258595A /* 'rXYZ' */)                \
+    TAG(redTRCTag, 0x72545243 /* 'rTRC' */)                         \
+    TAG(saturationRenderingIntentGamutTag, 0x72696732 /* 'rig2' */) \
+    TAG(technologyTag, 0x74656368 /* 'tech' */)                     \
+    TAG(viewingCondDescTag, 0x76756564 /* 'vued' */)                \
+    TAG(viewingConditionsTag, 0x76696577 /* 'view' */)
+
+#define TAG(name, id) constexpr inline TagSignature name { id };
+ENUMERATE_TAG_SIGNATURES(TAG)
+#undef TAG
+
+Optional<StringView> tag_signature_spec_name(TagSignature);
+
+}

+ 1 - 0
Userland/Utilities/icc.cpp

@@ -10,6 +10,7 @@
 #include <LibCore/DateTime.h>
 #include <LibCore/DateTime.h>
 #include <LibCore/MappedFile.h>
 #include <LibCore/MappedFile.h>
 #include <LibGfx/ICC/Profile.h>
 #include <LibGfx/ICC/Profile.h>
+#include <LibGfx/ICC/Tags.h>
 #include <LibGfx/ImageDecoder.h>
 #include <LibGfx/ImageDecoder.h>
 
 
 template<class T>
 template<class T>