Преглед изворни кода

LibGfx: Write ICC tag table

All offsets and sizes are set to 0 for now, so this still doesn't
produce a valid icc file. It gets closer, though.
Nico Weber пре 2 година
родитељ
комит
1457e36b79

+ 23 - 2
Userland/Libraries/LibGfx/ICC/BinaryWriter.cpp

@@ -9,8 +9,27 @@
 #include <LibGfx/ICC/Profile.h>
 #include <time.h>
 
+#pragma GCC diagnostic ignored "-Warray-bounds"
+
 namespace Gfx::ICC {
 
+static ErrorOr<void> encode_tag_table(ByteBuffer& bytes, Profile const& profile)
+{
+    VERIFY(bytes.size() >= sizeof(ICCHeader) + sizeof(u32) + profile.tag_count() * sizeof(TagTableEntry));
+
+    *bit_cast<BigEndian<u32>*>(bytes.data() + sizeof(ICCHeader)) = profile.tag_count();
+
+    TagTableEntry* tag_table_entry = bit_cast<TagTableEntry*>(bytes.data() + sizeof(ICCHeader) + sizeof(u32));
+    profile.for_each_tag([&tag_table_entry](auto tag_signature, auto) {
+        tag_table_entry->tag_signature = tag_signature;
+        tag_table_entry->offset_to_beginning_of_tag_data_element = 0; // FIXME
+        tag_table_entry->size_of_tag_data_element = 0; // FIXME
+        ++tag_table_entry;
+    });
+
+    return {};
+}
+
 static ErrorOr<void> encode_header(ByteBuffer& bytes, Profile const& profile)
 {
     VERIFY(bytes.size() >= sizeof(ICCHeader));
@@ -63,9 +82,11 @@ static ErrorOr<void> encode_header(ByteBuffer& bytes, Profile const& profile)
 ErrorOr<ByteBuffer> encode(Profile const& profile)
 {
     // Leaves enough room for the profile header and the tag table count.
-    // FIXME: Serialize tag data and write tag table and tag data too.
-    auto bytes = TRY(ByteBuffer::create_zeroed(sizeof(ICCHeader) + sizeof(u32)));
+    // FIXME: Serialize tag data and write tag data too.
+    size_t tag_table_size = sizeof(u32) + profile.tag_count() * sizeof(TagTableEntry);
+    auto bytes = TRY(ByteBuffer::create_zeroed(sizeof(ICCHeader) + tag_table_size));
 
+    TRY(encode_tag_table(bytes, profile));
     TRY(encode_header(bytes, profile));
 
     return bytes;

+ 2 - 0
Userland/Libraries/LibGfx/ICC/Profile.h

@@ -247,6 +247,8 @@ public:
             callback(tag.key, tag.value);
     }
 
+    size_t tag_count() const { return m_tag_table.size(); }
+
     // Only versions 2 and 4 are in use.
     bool is_v2() const { return version().major_version() == 2; }
     bool is_v4() const { return version().major_version() == 4; }