TIFFGenerator.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2023, Lucas Chollet <lucas.chollet@serenityos.org>
  3. #
  4. # SPDX-License-Identifier: BSD-2-Clause
  5. import argparse
  6. import re
  7. from enum import Enum
  8. from collections import namedtuple
  9. from pathlib import Path
  10. from typing import List, Optional, Type
  11. class EnumWithExportName(Enum):
  12. @classmethod
  13. def export_name(cls) -> str:
  14. return cls.__name__
  15. class TIFFType(EnumWithExportName):
  16. @classmethod
  17. def export_name(cls) -> str:
  18. return "Type"
  19. Byte = 1
  20. ASCII = 2
  21. UnsignedShort = 3
  22. UnsignedLong = 4
  23. UnsignedRational = 5
  24. Undefined = 7
  25. SignedLong = 9
  26. SignedRational = 10
  27. Float = 11
  28. Double = 12
  29. UTF8 = 129
  30. class Predictor(EnumWithExportName):
  31. NoPrediction = 1
  32. HorizontalDifferencing = 2
  33. class Compression(EnumWithExportName):
  34. NoCompression = 1
  35. CCITT = 2
  36. Group3Fax = 3
  37. Group4Fax = 4
  38. LZW = 5
  39. JPEG = 6
  40. PackBits = 32773
  41. tag_fields = ['id', 'types', 'counts', 'default', 'name', 'associated_enum']
  42. Tag = namedtuple(
  43. 'Tag',
  44. field_names=tag_fields,
  45. defaults=(None,) * len(tag_fields)
  46. )
  47. # FIXME: Some tag have only a few allowed values, we should ensure that
  48. known_tags: List[Tag] = [
  49. Tag('256', [TIFFType.UnsignedShort, TIFFType.UnsignedLong], [1], None, "ImageWidth"),
  50. Tag('257', [TIFFType.UnsignedShort, TIFFType.UnsignedLong], [1], None, "ImageHeight"),
  51. Tag('258', [TIFFType.UnsignedShort], [], None, "BitsPerSample"),
  52. Tag('259', [TIFFType.UnsignedShort], [1], None, "Compression", Compression),
  53. Tag('273', [TIFFType.UnsignedShort, TIFFType.UnsignedLong], [], None, "StripOffsets"),
  54. Tag('277', [TIFFType.UnsignedShort], [1], None, "SamplesPerPixel"),
  55. Tag('278', [TIFFType.UnsignedShort, TIFFType.UnsignedLong], [1], None, "RowsPerStrip"),
  56. Tag('279', [TIFFType.UnsignedShort, TIFFType.UnsignedLong], [], None, "StripByteCounts"),
  57. Tag('317', [TIFFType.UnsignedShort], [1], Predictor.NoPrediction, "Predictor", Predictor),
  58. Tag('34675', [TIFFType.Undefined], [], None, "ICCProfile"),
  59. ]
  60. HANDLE_TAG_SIGNATURE_TEMPLATE = ("ErrorOr<void> {namespace}handle_tag(Metadata& metadata, u16 tag,"
  61. " {namespace}Type type, u32 count, Vector<{namespace}Value>&& value)")
  62. HANDLE_TAG_SIGNATURE = HANDLE_TAG_SIGNATURE_TEMPLATE.format(namespace="")
  63. HANDLE_TAG_SIGNATURE_TIFF_NAMESPACE = HANDLE_TAG_SIGNATURE_TEMPLATE.format(namespace="TIFF::")
  64. LICENSE = R"""/*
  65. * Copyright (c) 2023, Lucas Chollet <lucas.chollet@serenityos.org>
  66. *
  67. * SPDX-License-Identifier: BSD-2-Clause
  68. */"""
  69. def export_enum_to_cpp(e: Type[EnumWithExportName], special_name: Optional[str] = None) -> str:
  70. output = f'enum class {e.export_name()} {{\n'
  71. for entry in e:
  72. output += f' {entry.name} = {entry.value},\n'
  73. output += "};\n"
  74. return output
  75. def export_enum_to_string_converter(enums: List[Type[EnumWithExportName]]) -> str:
  76. stringifier_internals = []
  77. for e in enums:
  78. single_stringifier = fR""" if constexpr (IsSame<E, {e.export_name()}>) {{
  79. switch (value) {{
  80. default:
  81. return "Invalid value for {e.export_name()}"sv;"""
  82. for entry in e:
  83. single_stringifier += fR"""
  84. case {e.export_name()}::{entry.name}:
  85. return "{entry.name}"sv;"""
  86. single_stringifier += R"""
  87. }
  88. }"""
  89. stringifier_internals.append(single_stringifier)
  90. stringifier_internals_str = '\n'.join(stringifier_internals)
  91. out = fR"""template<Enum E>
  92. StringView name_for_enum_tag_value(E value) {{
  93. {stringifier_internals_str}
  94. VERIFY_NOT_REACHED();
  95. }}"""
  96. return out
  97. def export_tag_related_enums(tags: List[Tag]) -> str:
  98. exported_enums = []
  99. for tag in tags:
  100. if tag.associated_enum:
  101. exported_enums.append(export_enum_to_cpp(tag.associated_enum))
  102. return '\n'.join(exported_enums)
  103. def promote_type(t: TIFFType) -> TIFFType:
  104. if t == TIFFType.UnsignedShort:
  105. return TIFFType.UnsignedLong
  106. return t
  107. def tiff_type_to_cpp(t: TIFFType, without_promotion: bool = False) -> str:
  108. # To simplify the code generator and the Metadata class API, all u16 are promoted to u32
  109. # Note that the Value<> type doesn't include u16 for this reason
  110. if not without_promotion:
  111. t = promote_type(t)
  112. if t in [TIFFType.ASCII, TIFFType.UTF8]:
  113. return 'String'
  114. if t == TIFFType.Undefined:
  115. return 'ByteBuffer'
  116. if t == TIFFType.UnsignedShort:
  117. return 'u16'
  118. if t == TIFFType.UnsignedLong:
  119. return 'u32'
  120. raise RuntimeError(f'Type "{t}" not recognized, please update tiff_type_to_read_only_cpp()')
  121. def is_container(t: TIFFType) -> bool:
  122. """
  123. Some TIFF types are defined on the unit scale but are intended to be used within a collection.
  124. An example of that are ASCII strings defined as N * byte. Let's intercept that and generate
  125. a nice API instead of Vector<u8>.
  126. """
  127. return t in [TIFFType.ASCII, TIFFType.Byte, TIFFType.Undefined, TIFFType.UTF8]
  128. def export_promoter() -> str:
  129. output = R"""template<typename T>
  130. struct TypePromoter {
  131. using Type = T;
  132. };
  133. """
  134. specialization_template = R"""template<>
  135. struct TypePromoter<{}> {{
  136. using Type = {};
  137. }};
  138. """
  139. for t in TIFFType:
  140. if promote_type(t) != t:
  141. output += specialization_template.format(tiff_type_to_cpp(t, without_promotion=True), tiff_type_to_cpp(t))
  142. return output
  143. def retrieve_biggest_type(types: List[TIFFType]) -> TIFFType:
  144. return TIFFType(max([t.value for t in types]))
  145. def pascal_case_to_snake_case(name: str) -> str:
  146. name = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
  147. return re.sub('([a-z0-9])([A-Z])', r'\1_\2', name).lower()
  148. def generate_getter(tag: Tag) -> str:
  149. biggest_type = retrieve_biggest_type(tag.types)
  150. variant_inner_type = tiff_type_to_cpp(biggest_type)
  151. extracted_value_template = f"(*possible_value)[{{}}].get<{variant_inner_type}>()"
  152. tag_final_type = variant_inner_type
  153. if tag.associated_enum:
  154. tag_final_type = f"TIFF::{tag.associated_enum.__name__}"
  155. extracted_value_template = f"static_cast<{tag_final_type}>({extracted_value_template})"
  156. if len(tag.counts) == 1 and tag.counts[0] == 1 or is_container(biggest_type):
  157. return_type = tag_final_type
  158. if is_container(biggest_type):
  159. return_type += ' const&'
  160. unpacked_if_needed = f"return {extracted_value_template.format(0)};"
  161. else:
  162. if len(tag.counts) == 1:
  163. container_type = f'Array<{tag_final_type}, {tag.counts[0]}>'
  164. container_initialization = f'{container_type} tmp{{}};'
  165. else:
  166. container_type = f'Vector<{tag_final_type}>'
  167. container_initialization = fR"""{container_type} tmp{{}};
  168. auto maybe_failure = tmp.try_resize(possible_value->size());
  169. if (maybe_failure.is_error())
  170. return OptionalNone {{}};
  171. """
  172. return_type = container_type
  173. unpacked_if_needed = fR"""
  174. {container_initialization}
  175. for (u32 i = 0; i < possible_value->size(); ++i)
  176. tmp[i] = {extracted_value_template.format('i')};
  177. return tmp;"""
  178. signature = fR" Optional<{return_type}> {pascal_case_to_snake_case(tag.name)}() const"
  179. body = fR"""
  180. {{
  181. auto const& possible_value = m_data.get("{tag.name}"sv);
  182. if (!possible_value.has_value())
  183. return OptionalNone {{}};
  184. {unpacked_if_needed}
  185. }}
  186. """
  187. return signature + body
  188. def generate_metadata_class(tags: List[Tag]) -> str:
  189. getters = '\n'.join([generate_getter(tag) for tag in tags])
  190. output = fR"""class Metadata {{
  191. public:
  192. {getters}
  193. private:
  194. friend {HANDLE_TAG_SIGNATURE_TIFF_NAMESPACE};
  195. void add_entry(StringView key, Vector<TIFF::Value>&& value) {{
  196. m_data.set(key, move(value));
  197. }}
  198. HashMap<StringView, Vector<TIFF::Value>> m_data;
  199. }};
  200. """
  201. return output
  202. def generate_metadata_file(tags: List[Tag]) -> str:
  203. output = fR"""{LICENSE}
  204. #pragma once
  205. #include <AK/HashMap.h>
  206. #include <AK/Variant.h>
  207. #include <AK/Vector.h>
  208. #include <LibGfx/Size.h>
  209. namespace Gfx {{
  210. class Metadata;
  211. namespace TIFF {{
  212. {export_enum_to_cpp(TIFFType)}
  213. template<OneOf<u32, i32> x32>
  214. struct Rational {{
  215. using Type = x32;
  216. x32 numerator;
  217. x32 denominator;
  218. }};
  219. {export_promoter()}
  220. // Note that u16 is not include on purpose
  221. using Value = Variant<ByteBuffer, String, u32, Rational<u32>, i32, Rational<i32>>;
  222. {export_tag_related_enums(known_tags)}
  223. {export_enum_to_string_converter([tag.associated_enum for tag in known_tags if tag.associated_enum] + [TIFFType])}
  224. {HANDLE_TAG_SIGNATURE};
  225. }}
  226. {generate_metadata_class(tags)}
  227. }}
  228. template<typename T>
  229. struct AK::Formatter<Gfx::TIFF::Rational<T>> : Formatter<FormatString> {{
  230. ErrorOr<void> format(FormatBuilder& builder, Gfx::TIFF::Rational<T> value)
  231. {{
  232. return Formatter<FormatString>::format(builder, "{{}} ({{}}/{{}})"sv,
  233. static_cast<double>(value.numerator) / value.denominator, value.numerator, value.denominator);
  234. }}
  235. }};
  236. template<>
  237. struct AK::Formatter<Gfx::TIFF::Value> : Formatter<FormatString> {{
  238. ErrorOr<void> format(FormatBuilder& builder, Gfx::TIFF::Value const& value)
  239. {{
  240. String content;
  241. value.visit(
  242. [&](ByteBuffer const& buffer) {{
  243. content = MUST(String::formatted("Buffer of size: {{}}"sv, buffer.size()));
  244. }},
  245. [&](auto const& other) {{
  246. content = MUST(String::formatted("{{}}", other));
  247. }}
  248. );
  249. return Formatter<FormatString>::format(builder, "{{}}"sv, content);
  250. }}
  251. }};
  252. """
  253. return output
  254. def generate_tag_handler(tag: Tag) -> str:
  255. not_in_type_list = f"({' && '.join([f'type != Type::{t.name}' for t in tag.types])})"
  256. not_in_count_list = ''
  257. if len(tag.counts) != 0:
  258. not_in_count_list = f"|| ({' && '.join([f'count != {c}' for c in tag.counts])})"
  259. pre_condition = fR"""if ({not_in_type_list}
  260. {not_in_count_list})
  261. return Error::from_string_literal("TIFFImageDecoderPlugin: Tag {tag.name} invalid");"""
  262. check_value = ''
  263. if tag.associated_enum is not None:
  264. not_in_value_list = f"({' && '.join([f'v != {v.value}' for v in tag.associated_enum])})"
  265. check_value = fR"""TRY(value[0].visit(
  266. []({tiff_type_to_cpp(tag.types[0])} const& v) -> ErrorOr<void> {{
  267. if ({not_in_value_list})
  268. return Error::from_string_literal("TIFFImageDecoderPlugin: Invalid value for tag {tag.name}");
  269. return {{}};
  270. }},
  271. [&](auto const&) -> ErrorOr<void> {{
  272. VERIFY_NOT_REACHED();
  273. }}));
  274. """
  275. output = fR""" case {tag.id}:
  276. // {tag.name}
  277. {pre_condition}
  278. {check_value}
  279. metadata.add_entry("{tag.name}"sv, move(value));
  280. break;
  281. """
  282. return output
  283. def generate_tag_handler_file(tags: List[Tag]) -> str:
  284. output = fR"""{LICENSE}
  285. #include <AK/Debug.h>
  286. #include <AK/String.h>
  287. #include <LibGfx/ImageFormats/TIFFMetadata.h>
  288. namespace Gfx::TIFF {{
  289. {HANDLE_TAG_SIGNATURE}
  290. {{
  291. switch (tag) {{
  292. """
  293. output += '\n'.join([generate_tag_handler(t) for t in tags])
  294. output += R"""
  295. default:
  296. dbgln_if(TIFF_DEBUG, "Unknown tag: {}", tag);
  297. }
  298. return {};
  299. }
  300. }
  301. """
  302. return output
  303. def update_file(target: Path, new_content: str):
  304. should_update = True
  305. if target.exists():
  306. with target.open('r') as file:
  307. content = file.read()
  308. if content == new_content:
  309. should_update = False
  310. if should_update:
  311. with target.open('w') as file:
  312. file.write(new_content)
  313. def main():
  314. parser = argparse.ArgumentParser()
  315. parser.add_argument('-o', '--output')
  316. args = parser.parse_args()
  317. output_path = Path(args.output)
  318. update_file(output_path / 'TIFFMetadata.h', generate_metadata_file(known_tags))
  319. update_file(output_path / 'TIFFTagHandler.cpp', generate_tag_handler_file(known_tags))
  320. if __name__ == '__main__':
  321. main()