From 2691651abfbef0f36693b2a9e77194ee6abb273d Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Sat, 2 Dec 2023 19:47:40 -0500 Subject: [PATCH] LibGfx/TIFF: Generated the code to output debug prints When the `TIFF_DEBUG` flag is set, the TIFF decoder logs every tag and their values. This is already useful but require the developer to have the spec handy in order to decrypt each value to its signification. None of this information is available at runtime, but this is known by the Python generator. So by generating these debug logs, we drastically increase their value. As a bonus point, most of these functions should be useful when we will display image's metadata in Serenity. --- .../LibGfx/ImageFormats/TIFFLoader.cpp | 26 ------------------- Userland/Libraries/LibGfx/TIFFGenerator.py | 22 +++++++++++++++- 2 files changed, 21 insertions(+), 27 deletions(-) diff --git a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp index cac2d30555..ec71de91e7 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp @@ -421,32 +421,6 @@ private: return read_tiff_value(type, count, offset); }())); - if constexpr (TIFF_DEBUG) { - if (tiff_value.size() == 1) { - tiff_value[0].visit( - [&](ByteBuffer& value) { - dbgln("Read tag({}), type({}): size {}", tag, to_underlying(type), value.size()); - }, - [&](auto const& value) { - dbgln("Read tag({}), type({}): {}", tag, to_underlying(type), value); - }); - } else { - dbg("Read tag({}), type({}): [", tag, to_underlying(type)); - for (u32 i = 0; i < tiff_value.size(); ++i) { - tiff_value[i].visit( - [&](ByteBuffer&) { - VERIFY_NOT_REACHED(); - }, - [&](auto const& value) { - dbg("{}", value); - }); - if (i != tiff_value.size() - 1) - dbg(", "); - } - dbgln("]"); - } - } - TRY(handle_tag(m_metadata, tag, type, count, move(tiff_value))); return {}; diff --git a/Userland/Libraries/LibGfx/TIFFGenerator.py b/Userland/Libraries/LibGfx/TIFFGenerator.py index e180ed360b..c369a6cf07 100755 --- a/Userland/Libraries/LibGfx/TIFFGenerator.py +++ b/Userland/Libraries/LibGfx/TIFFGenerator.py @@ -357,6 +357,9 @@ def generate_tag_handler(tag: Tag) -> str: output = fR""" case {tag.id}: // {tag.name} + + dbgln_if(TIFF_DEBUG, "{tag.name}({{}}): {{}}", name_for_enum_tag_value(type), format_tiff_value(value)); + {pre_condition} {check_value} metadata.add_entry("{tag.name}"sv, move(value)); @@ -375,6 +378,23 @@ def generate_tag_handler_file(tags: List[Tag]) -> str: namespace Gfx::TIFF {{ +[[maybe_unused]] static String format_tiff_value(Vector const& values) {{ + if (values.size() == 1) + return MUST(String::formatted("{{}}", values[0])); + + StringBuilder builder; + builder.append('['); + + for (u32 i = 0; i < values.size(); ++i) {{ + builder.appendff("{{}}", values[i]); + if (i != values.size() - 1) + builder.append(", "sv); + }} + + builder.append(']'); + return MUST(builder.to_string()); +}} + {HANDLE_TAG_SIGNATURE} {{ switch (tag) {{ @@ -384,7 +404,7 @@ namespace Gfx::TIFF {{ output += R""" default: - dbgln_if(TIFF_DEBUG, "Unknown tag: {}", tag); + dbgln_if(TIFF_DEBUG, "UnknownTag({}, {}): {}", tag, name_for_enum_tag_value(type), format_tiff_value(value)); } return {};