From 1457e36b791a3a537454ac52f60291d32757c0ab Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 17 Feb 2023 15:59:48 -0500 Subject: [PATCH] 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. --- .../Libraries/LibGfx/ICC/BinaryWriter.cpp | 25 +++++++++++++++++-- Userland/Libraries/LibGfx/ICC/Profile.h | 2 ++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/Userland/Libraries/LibGfx/ICC/BinaryWriter.cpp b/Userland/Libraries/LibGfx/ICC/BinaryWriter.cpp index 170e637f9f..17f7aa8def 100644 --- a/Userland/Libraries/LibGfx/ICC/BinaryWriter.cpp +++ b/Userland/Libraries/LibGfx/ICC/BinaryWriter.cpp @@ -9,8 +9,27 @@ #include #include +#pragma GCC diagnostic ignored "-Warray-bounds" + namespace Gfx::ICC { +static ErrorOr encode_tag_table(ByteBuffer& bytes, Profile const& profile) +{ + VERIFY(bytes.size() >= sizeof(ICCHeader) + sizeof(u32) + profile.tag_count() * sizeof(TagTableEntry)); + + *bit_cast*>(bytes.data() + sizeof(ICCHeader)) = profile.tag_count(); + + TagTableEntry* tag_table_entry = bit_cast(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 encode_header(ByteBuffer& bytes, Profile const& profile) { VERIFY(bytes.size() >= sizeof(ICCHeader)); @@ -63,9 +82,11 @@ static ErrorOr encode_header(ByteBuffer& bytes, Profile const& profile) ErrorOr 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; diff --git a/Userland/Libraries/LibGfx/ICC/Profile.h b/Userland/Libraries/LibGfx/ICC/Profile.h index 79e477353a..8059c83d70 100644 --- a/Userland/Libraries/LibGfx/ICC/Profile.h +++ b/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; }