Prechádzať zdrojové kódy

LibGfx: Move ICC::Profile::read_header() out of class

This one is slightly more involved: To make it nice, extract
all the Profile fields that belong to the header into a separate
struct.
Nico Weber 2 rokov pred
rodič
commit
db614b47dd

+ 25 - 24
Userland/Libraries/LibGfx/ICC/Profile.cpp

@@ -574,33 +574,34 @@ DeviceAttributes::DeviceAttributes(u64 bits)
 {
 }
 
-ErrorOr<void> Profile::read_header(ReadonlyBytes bytes)
+static ErrorOr<ProfileHeader> read_header(ReadonlyBytes bytes)
 {
     if (bytes.size() < sizeof(ICCHeader))
         return Error::from_string_literal("ICC::Profile: Not enough data for header");
 
-    auto header = *bit_cast<ICCHeader const*>(bytes.data());
-
-    TRY(parse_file_signature(header));
-    m_on_disk_size = TRY(parse_size(header, bytes));
-    m_preferred_cmm_type = parse_preferred_cmm_type(header);
-    m_version = TRY(parse_version(header));
-    m_device_class = TRY(parse_device_class(header));
-    m_data_color_space = TRY(parse_data_color_space(header));
-    m_connection_space = TRY(parse_connection_space(header));
-    m_creation_timestamp = TRY(parse_creation_date_time(header));
-    m_primary_platform = TRY(parse_primary_platform(header));
-    m_flags = Flags { header.profile_flags };
-    m_device_manufacturer = parse_device_manufacturer(header);
-    m_device_model = parse_device_model(header);
-    m_device_attributes = TRY(parse_device_attributes(header));
-    m_rendering_intent = TRY(parse_rendering_intent(header));
-    m_pcs_illuminant = TRY(parse_pcs_illuminant(header));
-    m_creator = parse_profile_creator(header);
-    m_id = TRY(parse_profile_id(header, bytes));
-    TRY(parse_reserved(header));
-
-    return {};
+    ProfileHeader header;
+    auto raw_header = *bit_cast<ICCHeader const*>(bytes.data());
+
+    TRY(parse_file_signature(raw_header));
+    header.on_disk_size = TRY(parse_size(raw_header, bytes));
+    header.preferred_cmm_type = parse_preferred_cmm_type(raw_header);
+    header.version = TRY(parse_version(raw_header));
+    header.device_class = TRY(parse_device_class(raw_header));
+    header.data_color_space = TRY(parse_data_color_space(raw_header));
+    header.connection_space = TRY(parse_connection_space(raw_header));
+    header.creation_timestamp = TRY(parse_creation_date_time(raw_header));
+    header.primary_platform = TRY(parse_primary_platform(raw_header));
+    header.flags = Flags { raw_header.profile_flags };
+    header.device_manufacturer = parse_device_manufacturer(raw_header);
+    header.device_model = parse_device_model(raw_header);
+    header.device_attributes = TRY(parse_device_attributes(raw_header));
+    header.rendering_intent = TRY(parse_rendering_intent(raw_header));
+    header.pcs_illuminant = TRY(parse_pcs_illuminant(raw_header));
+    header.creator = parse_profile_creator(raw_header);
+    header.id = TRY(parse_profile_id(raw_header, bytes));
+    TRY(parse_reserved(raw_header));
+
+    return header;
 }
 
 static ErrorOr<NonnullRefPtr<TagData>> read_tag(ReadonlyBytes bytes, u32 offset_to_beginning_of_tag_data_element, u32 size_of_tag_data_element)
@@ -1377,7 +1378,7 @@ ErrorOr<void> Profile::check_tag_types()
 ErrorOr<NonnullRefPtr<Profile>> Profile::try_load_from_externally_owned_memory(ReadonlyBytes bytes)
 {
     auto profile = TRY(try_make_ref_counted<Profile>());
-    TRY(profile->read_header(bytes));
+    profile->m_header = TRY(read_header(bytes));
     bytes = bytes.trim(profile->on_disk_size());
     profile->m_tag_table = TRY(read_tag_table(bytes));
 

+ 37 - 35
Userland/Libraries/LibGfx/ICC/Profile.h

@@ -187,29 +187,48 @@ private:
     u64 m_bits = 0;
 };
 
+struct ProfileHeader {
+    u32 on_disk_size { 0 };
+    Optional<PreferredCMMType> preferred_cmm_type;
+    Version version;
+    DeviceClass device_class {};
+    ColorSpace data_color_space {};
+    ColorSpace connection_space {};
+    time_t creation_timestamp { 0 };
+    Optional<PrimaryPlatform> primary_platform {};
+    Flags flags;
+    Optional<DeviceManufacturer> device_manufacturer;
+    Optional<DeviceModel> device_model;
+    DeviceAttributes device_attributes;
+    RenderingIntent rendering_intent {};
+    XYZ pcs_illuminant;
+    Optional<Creator> creator;
+    Optional<Crypto::Hash::MD5::DigestType> id;
+};
+
 class Profile : public RefCounted<Profile> {
 public:
     static ErrorOr<NonnullRefPtr<Profile>> try_load_from_externally_owned_memory(ReadonlyBytes);
 
-    Optional<PreferredCMMType> preferred_cmm_type() const { return m_preferred_cmm_type; }
-    Version version() const { return m_version; }
-    DeviceClass device_class() const { return m_device_class; }
-    ColorSpace data_color_space() const { return m_data_color_space; }
+    Optional<PreferredCMMType> preferred_cmm_type() const { return m_header.preferred_cmm_type; }
+    Version version() const { return m_header.version; }
+    DeviceClass device_class() const { return m_header.device_class; }
+    ColorSpace data_color_space() const { return m_header.data_color_space; }
 
     // For non-DeviceLink profiles, always PCSXYZ or PCSLAB.
-    ColorSpace connection_space() const { return m_connection_space; }
-
-    u32 on_disk_size() const { return m_on_disk_size; }
-    time_t creation_timestamp() const { return m_creation_timestamp; }
-    Optional<PrimaryPlatform> primary_platform() const { return m_primary_platform; }
-    Flags flags() const { return m_flags; }
-    Optional<DeviceManufacturer> device_manufacturer() const { return m_device_manufacturer; }
-    Optional<DeviceModel> device_model() const { return m_device_model; }
-    DeviceAttributes device_attributes() const { return m_device_attributes; }
-    RenderingIntent rendering_intent() const { return m_rendering_intent; }
-    XYZ const& pcs_illuminant() const { return m_pcs_illuminant; }
-    Optional<Creator> creator() const { return m_creator; }
-    Optional<Crypto::Hash::MD5::DigestType> const& id() const { return m_id; }
+    ColorSpace connection_space() const { return m_header.connection_space; }
+
+    u32 on_disk_size() const { return m_header.on_disk_size; }
+    time_t creation_timestamp() const { return m_header.creation_timestamp; }
+    Optional<PrimaryPlatform> primary_platform() const { return m_header.primary_platform; }
+    Flags flags() const { return m_header.flags; }
+    Optional<DeviceManufacturer> device_manufacturer() const { return m_header.device_manufacturer; }
+    Optional<DeviceModel> device_model() const { return m_header.device_model; }
+    DeviceAttributes device_attributes() const { return m_header.device_attributes; }
+    RenderingIntent rendering_intent() const { return m_header.rendering_intent; }
+    XYZ const& pcs_illuminant() const { return m_header.pcs_illuminant; }
+    Optional<Creator> creator() const { return m_header.creator; }
+    Optional<Crypto::Hash::MD5::DigestType> const& id() const { return m_header.id; }
 
     static Crypto::Hash::MD5::DigestType compute_id(ReadonlyBytes);
 
@@ -225,27 +244,10 @@ public:
     bool is_v4() const { return version().major_version() == 4; }
 
 private:
-    ErrorOr<void> read_header(ReadonlyBytes);
     ErrorOr<void> check_required_tags();
     ErrorOr<void> check_tag_types();
 
-    u32 m_on_disk_size { 0 };
-    Optional<PreferredCMMType> m_preferred_cmm_type;
-    Version m_version;
-    DeviceClass m_device_class {};
-    ColorSpace m_data_color_space {};
-    ColorSpace m_connection_space {};
-    time_t m_creation_timestamp { 0 };
-    Optional<PrimaryPlatform> m_primary_platform {};
-    Flags m_flags;
-    Optional<DeviceManufacturer> m_device_manufacturer;
-    Optional<DeviceModel> m_device_model;
-    DeviceAttributes m_device_attributes;
-    RenderingIntent m_rendering_intent {};
-    XYZ m_pcs_illuminant;
-    Optional<Creator> m_creator;
-    Optional<Crypto::Hash::MD5::DigestType> m_id;
-
+    ProfileHeader m_header;
     OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> m_tag_table;
 };