Profile.h 9.1 KB

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