LibGfx/TIFF: Generate code for the u16 to TIFF::Type conversion

This commit is contained in:
Lucas CHOLLET 2024-01-11 23:41:37 -05:00 committed by Jelle Raaijmakers
parent a1255cb6c9
commit 3124c1616c
Notes: sideshowbarker 2024-07-17 21:16:31 +09:00
2 changed files with 16 additions and 27 deletions

View file

@ -423,32 +423,6 @@ private:
return {};
}
ErrorOr<Type> read_type()
{
switch (TRY(read_value<u16>())) {
case to_underlying(Type::Byte):
return Type::Byte;
case to_underlying(Type::ASCII):
return Type::ASCII;
case to_underlying(Type::UnsignedShort):
return Type::UnsignedShort;
case to_underlying(Type::UnsignedLong):
return Type::UnsignedLong;
case to_underlying(Type::UnsignedRational):
return Type::UnsignedRational;
case to_underlying(Type::Undefined):
return Type::Undefined;
case to_underlying(Type::SignedLong):
return Type::SignedLong;
case to_underlying(Type::SignedRational):
return Type::SignedRational;
case to_underlying(Type::UTF8):
return Type::UTF8;
default:
return Error::from_string_literal("TIFFImageDecoderPlugin: Unknown type");
}
}
static constexpr u8 size_of_type(Type type)
{
switch (type) {
@ -541,7 +515,8 @@ private:
ErrorOr<void> read_tag()
{
auto const tag = TRY(read_value<u16>());
auto const type = TRY(read_type());
auto const raw_type = TRY(read_value<u16>());
auto const type = TRY(tiff_type_from_u16(raw_type));
auto const count = TRY(read_value<u32>());
Checked<u32> checked_size = size_of_type(type);

View file

@ -136,6 +136,7 @@ HANDLE_TAG_SIGNATURE = HANDLE_TAG_SIGNATURE_TEMPLATE.format(namespace="")
HANDLE_TAG_SIGNATURE_TIFF_NAMESPACE = HANDLE_TAG_SIGNATURE_TEMPLATE.format(namespace="TIFF::")
ENSURE_BASELINE_TAG_PRESENCE = "ErrorOr<void> ensure_baseline_tags_are_present(Metadata const& metadata)"
TIFF_TYPE_FROM_U16 = "ErrorOr<Type> tiff_type_from_u16(u16 type)"
LICENSE = R"""/*
* Copyright (c) 2023, Lucas Chollet <lucas.chollet@serenityos.org>
@ -369,6 +370,7 @@ using Value = Variant<ByteBuffer, String, u32, Rational<u32>, i32, Rational<i32>
{HANDLE_TAG_SIGNATURE};
{ENSURE_BASELINE_TAG_PRESENCE};
{TIFF_TYPE_FROM_U16};
}}
@ -459,6 +461,9 @@ def generate_tag_handler_file(tags: List[Tag]) -> str:
return Error::from_string_literal("Unable to decode image, missing required tag {tag.name}.");
""" for tag in filter(lambda tag: tag.is_required, known_tags)])
tiff_type_from_u16_cases = '\n'.join([fR""" case to_underlying(Type::{t.name}):
return Type::{t.name};""" for t in TIFFType])
output = fR"""{LICENSE}
#include <AK/Debug.h>
@ -498,6 +503,15 @@ static String value_formatter(u32 tag_id, Value const& v) {{
return {{}};
}}
{TIFF_TYPE_FROM_U16}
{{
switch (type) {{
{tiff_type_from_u16_cases}
default:
return Error::from_string_literal("TIFFImageDecoderPlugin: Unknown type");
}}
}}
{HANDLE_TAG_SIGNATURE}
{{