1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:47:45 +00:00

LibGfx/TIFF: Honor the default value for single count tags

Some tags have a default value, we should return this value in
Metadata's getters when no value has been read from the input file.

Note that we don't support default values for tags with a count bigger
than one.
This commit is contained in:
Lucas CHOLLET 2024-01-07 00:17:19 -05:00 committed by Andreas Kling
parent ef10a58522
commit 34e9059ae8

View file

@ -9,7 +9,7 @@ import re
from enum import Enum from enum import Enum
from collections import namedtuple from collections import namedtuple
from pathlib import Path from pathlib import Path
from typing import List, Type from typing import Any, List, Type
class EnumWithExportName(Enum): class EnumWithExportName(Enum):
@ -251,6 +251,12 @@ def pascal_case_to_snake_case(name: str) -> str:
return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower() return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
def default_value_to_cpp(value: Any) -> str:
if isinstance(value, EnumWithExportName):
return f'TIFF::{value.export_name()}::{value.name}'
return str(value)
def generate_getter(tag: Tag) -> str: def generate_getter(tag: Tag) -> str:
biggest_type = retrieve_biggest_type(tag.types) biggest_type = retrieve_biggest_type(tag.types)
variant_inner_type = tiff_type_to_cpp(biggest_type) variant_inner_type = tiff_type_to_cpp(biggest_type)
@ -262,7 +268,8 @@ def generate_getter(tag: Tag) -> str:
tag_final_type = f"TIFF::{tag.associated_enum.__name__}" tag_final_type = f"TIFF::{tag.associated_enum.__name__}"
extracted_value_template = f"static_cast<{tag_final_type}>({extracted_value_template})" extracted_value_template = f"static_cast<{tag_final_type}>({extracted_value_template})"
if len(tag.counts) == 1 and tag.counts[0] == 1 or is_container(biggest_type): single_count = len(tag.counts) == 1 and tag.counts[0] == 1 or is_container(biggest_type)
if single_count:
return_type = tag_final_type return_type = tag_final_type
if is_container(biggest_type): if is_container(biggest_type):
return_type += ' const&' return_type += ' const&'
@ -289,11 +296,16 @@ def generate_getter(tag: Tag) -> str:
signature = fR" Optional<{return_type}> {pascal_case_to_snake_case(tag.name)}() const" signature = fR" Optional<{return_type}> {pascal_case_to_snake_case(tag.name)}() const"
if tag.default and single_count:
return_if_empty = f'{default_value_to_cpp(tag.default)}'
else:
return_if_empty = 'OptionalNone {}'
body = fR""" body = fR"""
{{ {{
auto const& possible_value = m_data.get("{tag.name}"sv); auto const& possible_value = m_data.get("{tag.name}"sv);
if (!possible_value.has_value()) if (!possible_value.has_value())
return OptionalNone {{}}; return {return_if_empty};
{unpacked_if_needed} {unpacked_if_needed}
}} }}
""" """