Profile.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * Copyright (c) 2022-2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Error.h>
  8. #include <AK/Format.h>
  9. #include <AK/HashMap.h>
  10. #include <AK/NonnullRefPtr.h>
  11. #include <AK/RefCounted.h>
  12. #include <AK/Span.h>
  13. #include <AK/URL.h>
  14. #include <LibCrypto/Hash/MD5.h>
  15. #include <LibGfx/ICC/DistinctFourCC.h>
  16. #include <LibGfx/ICC/TagTypes.h>
  17. namespace Gfx::ICC {
  18. URL device_manufacturer_url(DeviceManufacturer);
  19. URL device_model_url(DeviceModel);
  20. // ICC v4, 7.2.4 Profile version field
  21. class Version {
  22. public:
  23. Version() = default;
  24. Version(u8 major, u8 minor_and_bugfix)
  25. : m_major_version(major)
  26. , m_minor_and_bugfix_version(minor_and_bugfix)
  27. {
  28. }
  29. u8 major_version() const { return m_major_version; }
  30. u8 minor_version() const { return m_minor_and_bugfix_version >> 4; }
  31. u8 bugfix_version() const { return m_minor_and_bugfix_version & 0xf; }
  32. u8 minor_and_bugfix_version() const { return m_minor_and_bugfix_version; }
  33. private:
  34. u8 m_major_version = 0;
  35. u8 m_minor_and_bugfix_version = 0;
  36. };
  37. // ICC v4, 7.2.5 Profile/device class field
  38. enum class DeviceClass : u32 {
  39. InputDevice = 0x73636E72, // 'scnr'
  40. DisplayDevice = 0x6D6E7472, // 'mntr'
  41. OutputDevice = 0x70727472, // 'prtr'
  42. DeviceLink = 0x6C696E6B, // 'link'
  43. ColorSpace = 0x73706163, // 'spac'
  44. Abstract = 0x61627374, // 'abst'
  45. NamedColor = 0x6E6D636C, // 'nmcl'
  46. };
  47. StringView device_class_name(DeviceClass);
  48. // ICC v4, 7.2.6 Data colour space field, Table 19 — Data colour space signatures
  49. enum class ColorSpace : u32 {
  50. nCIEXYZ = 0x58595A20, // 'XYZ ', used in data color spaces.
  51. PCSXYZ = nCIEXYZ, // Used in profile connection space instead.
  52. CIELAB = 0x4C616220, // 'Lab ', used in data color spaces.
  53. PCSLAB = CIELAB, // Used in profile connection space instead.
  54. CIELUV = 0x4C757620, // 'Luv '
  55. YCbCr = 0x59436272, // 'YCbr'
  56. CIEYxy = 0x59787920, // 'Yxy '
  57. RGB = 0x52474220, // 'RGB '
  58. Gray = 0x47524159, // 'GRAY'
  59. HSV = 0x48535620, // 'HSV '
  60. HLS = 0x484C5320, // 'HLS '
  61. CMYK = 0x434D594B, // 'CMYK'
  62. CMY = 0x434D5920, // 'CMY '
  63. TwoColor = 0x32434C52, // '2CLR'
  64. ThreeColor = 0x33434C52, // '3CLR'
  65. FourColor = 0x34434C52, // '4CLR'
  66. FiveColor = 0x35434C52, // '5CLR'
  67. SixColor = 0x36434C52, // '6CLR'
  68. SevenColor = 0x37434C52, // '7CLR'
  69. EightColor = 0x38434C52, // '8CLR'
  70. NineColor = 0x39434C52, // '9CLR'
  71. TenColor = 0x41434C52, // 'ACLR'
  72. ElevenColor = 0x42434C52, // 'BCLR'
  73. TwelveColor = 0x43434C52, // 'CCLR'
  74. ThirteenColor = 0x44434C52, // 'DCLR'
  75. FourteenColor = 0x45434C52, // 'ECLR'
  76. FifteenColor = 0x46434C52, // 'FCLR'
  77. };
  78. StringView data_color_space_name(ColorSpace);
  79. StringView profile_connection_space_name(ColorSpace);
  80. // ICC v4, 7.2.10 Primary platform field, Table 20 — Primary platforms
  81. enum class PrimaryPlatform : u32 {
  82. Apple = 0x4150504C, // 'APPL'
  83. Microsoft = 0x4D534654, // 'MSFT'
  84. SiliconGraphics = 0x53474920, // 'SGI '
  85. Sun = 0x53554E57, // 'SUNW'
  86. };
  87. StringView primary_platform_name(PrimaryPlatform);
  88. // ICC v4, 7.2.15 Rendering intent field
  89. enum class RenderingIntent : u32 {
  90. Perceptual = 0,
  91. MediaRelativeColorimetric = 1,
  92. Saturation = 2,
  93. ICCAbsoluteColorimetric = 3,
  94. };
  95. StringView rendering_intent_name(RenderingIntent);
  96. // ICC v4, 7.2.11 Profile flags field
  97. class Flags {
  98. public:
  99. Flags();
  100. // "The profile flags field contains flags."
  101. Flags(u32);
  102. u32 bits() const { return m_bits; }
  103. // "These can indicate various hints for the CMM such as distributed processing and caching options."
  104. // "The least-significant 16 bits are reserved for the ICC."
  105. u16 color_management_module_bits() const { return bits() >> 16; }
  106. u16 icc_bits() const { return bits() & 0xffff; }
  107. // "Bit position 0: Embedded profile (0 if not embedded, 1 if embedded in file)"
  108. bool is_embedded_in_file() const { return (icc_bits() & 1) != 0; }
  109. // "Bit position 1: Profile cannot be used independently of the embedded colour data (set to 1 if true, 0 if false)"
  110. // Double negation isn't unconfusing, so this function uses the inverted, positive sense.
  111. bool can_be_used_independently_of_embedded_color_data() const { return (icc_bits() & 2) == 0; }
  112. static constexpr u32 KnownBitsMask = 3;
  113. private:
  114. u32 m_bits = 0;
  115. };
  116. // ICC v4, 7.2.14 Device attributes field
  117. class DeviceAttributes {
  118. public:
  119. DeviceAttributes();
  120. // "The device attributes field shall contain flags used to identify attributes
  121. // unique to the particular device setup for which the profile is applicable."
  122. DeviceAttributes(u64);
  123. u64 bits() const { return m_bits; }
  124. // "The least-significant 32 bits of this 64-bit value are defined by the ICC. "
  125. u32 icc_bits() const { return bits() & 0xffff'ffff; }
  126. // "Notice that bits 0, 1, 2, and 3 describe the media, not the device."
  127. // "0": "Reflective (0) or transparency (1)"
  128. enum class MediaReflectivity {
  129. Reflective,
  130. Transparent,
  131. };
  132. MediaReflectivity media_reflectivity() const { return MediaReflectivity(icc_bits() & 1); }
  133. // "1": "Glossy (0) or matte (1)"
  134. enum class MediaGlossiness {
  135. Glossy,
  136. Matte,
  137. };
  138. MediaGlossiness media_glossiness() const { return MediaGlossiness((icc_bits() >> 1) & 1); }
  139. // "2": "Media polarity, positive (0) or negative (1)"
  140. enum class MediaPolarity {
  141. Positive,
  142. Negative,
  143. };
  144. MediaPolarity media_polarity() const { return MediaPolarity((icc_bits() >> 2) & 1); }
  145. // "3": "Colour media (0), black & white media (1)"
  146. enum class MediaColor {
  147. Colored,
  148. BlackAndWhite,
  149. };
  150. MediaColor media_color() const { return MediaColor((icc_bits() >> 3) & 1); }
  151. // "4 to 31": Reserved (set to binary zero)"
  152. // "32 to 63": "Use not defined by ICC (vendor specific"
  153. u32 vendor_bits() const { return bits() >> 32; }
  154. static constexpr u64 KnownBitsMask = 0xf;
  155. private:
  156. u64 m_bits = 0;
  157. };
  158. struct ProfileHeader {
  159. u32 on_disk_size { 0 };
  160. Optional<PreferredCMMType> preferred_cmm_type;
  161. Version version;
  162. DeviceClass device_class {};
  163. ColorSpace data_color_space {};
  164. ColorSpace connection_space {};
  165. time_t creation_timestamp { 0 };
  166. Optional<PrimaryPlatform> primary_platform {};
  167. Flags flags;
  168. Optional<DeviceManufacturer> device_manufacturer;
  169. Optional<DeviceModel> device_model;
  170. DeviceAttributes device_attributes;
  171. RenderingIntent rendering_intent {};
  172. XYZ pcs_illuminant;
  173. Optional<Creator> creator;
  174. Optional<Crypto::Hash::MD5::DigestType> id;
  175. };
  176. class Profile : public RefCounted<Profile> {
  177. public:
  178. static ErrorOr<NonnullRefPtr<Profile>> try_load_from_externally_owned_memory(ReadonlyBytes);
  179. static ErrorOr<NonnullRefPtr<Profile>> create(ProfileHeader const& header, OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> tag_table);
  180. Optional<PreferredCMMType> preferred_cmm_type() const { return m_header.preferred_cmm_type; }
  181. Version version() const { return m_header.version; }
  182. DeviceClass device_class() const { return m_header.device_class; }
  183. ColorSpace data_color_space() const { return m_header.data_color_space; }
  184. // For non-DeviceLink profiles, always PCSXYZ or PCSLAB.
  185. ColorSpace connection_space() const { return m_header.connection_space; }
  186. u32 on_disk_size() const { return m_header.on_disk_size; }
  187. time_t creation_timestamp() const { return m_header.creation_timestamp; }
  188. Optional<PrimaryPlatform> primary_platform() const { return m_header.primary_platform; }
  189. Flags flags() const { return m_header.flags; }
  190. Optional<DeviceManufacturer> device_manufacturer() const { return m_header.device_manufacturer; }
  191. Optional<DeviceModel> device_model() const { return m_header.device_model; }
  192. DeviceAttributes device_attributes() const { return m_header.device_attributes; }
  193. RenderingIntent rendering_intent() const { return m_header.rendering_intent; }
  194. XYZ const& pcs_illuminant() const { return m_header.pcs_illuminant; }
  195. Optional<Creator> creator() const { return m_header.creator; }
  196. Optional<Crypto::Hash::MD5::DigestType> const& id() const { return m_header.id; }
  197. static Crypto::Hash::MD5::DigestType compute_id(ReadonlyBytes);
  198. template<typename Callback>
  199. void for_each_tag(Callback callback) const
  200. {
  201. for (auto const& tag : m_tag_table)
  202. callback(tag.key, tag.value);
  203. }
  204. template<FallibleFunction<TagSignature, NonnullRefPtr<TagData>> Callback>
  205. ErrorOr<void> try_for_each_tag(Callback&& callback) const
  206. {
  207. for (auto const& tag : m_tag_table)
  208. TRY(callback(tag.key, tag.value));
  209. return {};
  210. }
  211. size_t tag_count() const { return m_tag_table.size(); }
  212. // Only versions 2 and 4 are in use.
  213. bool is_v2() const { return version().major_version() == 2; }
  214. bool is_v4() const { return version().major_version() == 4; }
  215. private:
  216. Profile(ProfileHeader const& header, OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> tag_table)
  217. : m_header(header)
  218. , m_tag_table(move(tag_table))
  219. {
  220. }
  221. ErrorOr<void> check_required_tags();
  222. ErrorOr<void> check_tag_types();
  223. ProfileHeader m_header;
  224. OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> m_tag_table;
  225. };
  226. }
  227. template<>
  228. struct AK::Formatter<Gfx::ICC::Version> : Formatter<FormatString> {
  229. ErrorOr<void> format(FormatBuilder& builder, Gfx::ICC::Version const& version)
  230. {
  231. return Formatter<FormatString>::format(builder, "{}.{}.{}"sv, version.major_version(), version.minor_version(), version.bugfix_version());
  232. }
  233. };