From 7e769c721758b66f4664cb889d09da95707e34db Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sat, 18 Feb 2023 21:56:07 -0500 Subject: [PATCH] LibGfx: Partially implement serialization of TextDescriptionTagData It only implements serialization of the 7-bit ASCII string, not yet serialization of the UCS-2 and Macintosh ScriptCode strings. With this, matrix-based v2 profiles can be reencoded :^) --- .../Libraries/LibGfx/ICC/BinaryWriter.cpp | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/Userland/Libraries/LibGfx/ICC/BinaryWriter.cpp b/Userland/Libraries/LibGfx/ICC/BinaryWriter.cpp index 6b43ce2a87..2361dc47e2 100644 --- a/Userland/Libraries/LibGfx/ICC/BinaryWriter.cpp +++ b/Userland/Libraries/LibGfx/ICC/BinaryWriter.cpp @@ -166,6 +166,62 @@ static ErrorOr encode_signature(SignatureTagData const& tag_data) return bytes; } +static ErrorOr encode_text_description(TextDescriptionTagData const& tag_data) +{ + // ICC v2, 6.5.17 textDescriptionType + // All lengths include room for a trailing nul character. + // See also the many comments in TextDescriptionTagData::from_bytes(). + u32 ascii_size = sizeof(u32) + tag_data.ascii_description().bytes().size() + 1; + + // FIXME: Include tag_data.unicode_description() if it's set. + u32 unicode_size = 2 * sizeof(u32); + + // FIXME: Include tag_data.macintosh_description() if it's set. + u32 macintosh_size = sizeof(u16) + sizeof(u8) + 67; + + auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + ascii_size + unicode_size + macintosh_size)); + *bit_cast*>(bytes.data()) = static_cast(TextDescriptionTagData::Type); + *bit_cast*>(bytes.data() + 4) = 0; + + // ASCII + *bit_cast*>(bytes.data() + 8) = tag_data.ascii_description().bytes().size() + 1; + memcpy(bytes.data() + 12, tag_data.ascii_description().bytes().data(), tag_data.ascii_description().bytes().size()); + bytes.data()[12 + tag_data.ascii_description().bytes().size()] = '\0'; + + // Unicode + // "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 here. + u8* cursor = bytes.data() + 8 + ascii_size; + u32 unicode_language_code = 0; // FIXME: Set to tag_data.unicode_language_code() once this writes unicode data. + cursor[0] = unicode_language_code >> 24; + cursor[1] = (unicode_language_code >> 16) & 0xff; + cursor[2] = (unicode_language_code >> 8) & 0xff; + cursor[3] = unicode_language_code & 0xff; + cursor += 4; + + // FIXME: Include tag_data.unicode_description() if it's set. + u32 ucs2_count = 0; // FIXME: If tag_data.unicode_description() is set, set this to its length plus room for one nul character. + cursor[0] = ucs2_count >> 24; + cursor[1] = (ucs2_count >> 16) & 0xff; + cursor[2] = (ucs2_count >> 8) & 0xff; + cursor[3] = ucs2_count & 0xff; + cursor += 4; + + // Macintosh scriptcode + u16 scriptcode_code = 0; // MacRoman + cursor[0] = (scriptcode_code >> 8) & 0xff; + cursor[1] = scriptcode_code & 0xff; + cursor += 2; + + u8 macintosh_description_length = 0; // FIXME: If tag_data.macintosh_description() is set, set this to tis length plus room for one nul character. + cursor[0] = macintosh_description_length; + cursor += 1; + memset(cursor, 0, 67); + + return bytes; +} + static ErrorOr encode_text(TextTagData const& tag_data) { // ICC v4, 10.24 textType @@ -228,6 +284,8 @@ static ErrorOr encode_tag_data(TagData const& tag_data) return encode_s15_fixed_array(static_cast(tag_data)); case SignatureTagData::Type: return encode_signature(static_cast(tag_data)); + case TextDescriptionTagData::Type: + return encode_text_description(static_cast(tag_data)); case TextTagData::Type: return encode_text(static_cast(tag_data)); case ViewingConditionsTagData::Type: