Profile.cpp 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. /*
  2. * Copyright (c) 2022-2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Endian.h>
  7. #include <LibGfx/ICC/Profile.h>
  8. #include <LibGfx/ICC/Tags.h>
  9. #include <math.h>
  10. #include <time.h>
  11. // V2 spec: https://color.org/specification/ICC.1-2001-04.pdf
  12. // V4 spec: https://color.org/specification/ICC.1-2022-05.pdf
  13. namespace Gfx::ICC {
  14. namespace {
  15. // ICC V4, 4.2 dateTimeNumber
  16. // "All the dateTimeNumber values in a profile shall be in Coordinated Universal Time [...]."
  17. struct DateTimeNumber {
  18. BigEndian<u16> year;
  19. BigEndian<u16> month;
  20. BigEndian<u16> day;
  21. BigEndian<u16> hours;
  22. BigEndian<u16> minutes;
  23. BigEndian<u16> seconds;
  24. };
  25. // ICC V4, 4.6 s15Fixed16Number
  26. using s15Fixed16Number = i32;
  27. // ICC V4, 4.14 XYZNumber
  28. struct XYZNumber {
  29. BigEndian<s15Fixed16Number> x;
  30. BigEndian<s15Fixed16Number> y;
  31. BigEndian<s15Fixed16Number> z;
  32. operator XYZ() const
  33. {
  34. return XYZ { x / (double)0x1'0000, y / (double)0x1'0000, z / (double)0x1'0000 };
  35. }
  36. };
  37. ErrorOr<time_t> parse_date_time_number(DateTimeNumber const& date_time)
  38. {
  39. // ICC V4, 4.2 dateTimeNumber
  40. // "Number of the month (1 to 12)"
  41. if (date_time.month < 1 || date_time.month > 12)
  42. return Error::from_string_literal("ICC::Profile: dateTimeNumber month out of bounds");
  43. // "Number of the day of the month (1 to 31)"
  44. if (date_time.day < 1 || date_time.day > 31)
  45. return Error::from_string_literal("ICC::Profile: dateTimeNumber day out of bounds");
  46. // "Number of hours (0 to 23)"
  47. if (date_time.hours > 23)
  48. return Error::from_string_literal("ICC::Profile: dateTimeNumber hours out of bounds");
  49. // "Number of minutes (0 to 59)"
  50. if (date_time.minutes > 59)
  51. return Error::from_string_literal("ICC::Profile: dateTimeNumber minutes out of bounds");
  52. // "Number of seconds (0 to 59)"
  53. // ICC profiles apparently can't be created during leap seconds (seconds would be 60 there, but the spec doesn't allow that).
  54. if (date_time.seconds > 59)
  55. return Error::from_string_literal("ICC::Profile: dateTimeNumber seconds out of bounds");
  56. struct tm tm = {};
  57. tm.tm_year = date_time.year - 1900;
  58. tm.tm_mon = date_time.month - 1;
  59. tm.tm_mday = date_time.day;
  60. tm.tm_hour = date_time.hours;
  61. tm.tm_min = date_time.minutes;
  62. tm.tm_sec = date_time.seconds;
  63. // timegm() doesn't read tm.tm_isdst, tm.tm_wday, and tm.tm_yday, no need to fill them in.
  64. time_t timestamp = timegm(&tm);
  65. if (timestamp == -1)
  66. return Error::from_string_literal("ICC::Profile: dateTimeNumber not representable as timestamp");
  67. return timestamp;
  68. }
  69. // ICC V4, 7.2 Profile header
  70. struct ICCHeader {
  71. BigEndian<u32> profile_size;
  72. BigEndian<PreferredCMMType> preferred_cmm_type;
  73. u8 profile_version_major;
  74. u8 profile_version_minor_bugfix;
  75. BigEndian<u16> profile_version_zero;
  76. BigEndian<DeviceClass> profile_device_class;
  77. BigEndian<ColorSpace> data_color_space;
  78. BigEndian<ColorSpace> profile_connection_space; // "PCS" in the spec.
  79. DateTimeNumber profile_creation_time;
  80. BigEndian<u32> profile_file_signature;
  81. BigEndian<PrimaryPlatform> primary_platform;
  82. BigEndian<u32> profile_flags;
  83. BigEndian<DeviceManufacturer> device_manufacturer;
  84. BigEndian<DeviceModel> device_model;
  85. BigEndian<u64> device_attributes;
  86. BigEndian<u32> rendering_intent;
  87. XYZNumber pcs_illuminant;
  88. BigEndian<Creator> profile_creator;
  89. u8 profile_id[16];
  90. u8 reserved[28];
  91. };
  92. static_assert(sizeof(ICCHeader) == 128);
  93. ErrorOr<u32> parse_size(ICCHeader const& header, ReadonlyBytes icc_bytes)
  94. {
  95. // ICC v4, 7.2.2 Profile size field
  96. // "The value in the profile size field shall be the exact size obtained by combining the profile header,
  97. // the tag table, and the tagged element data, including the pad bytes for the last tag."
  98. // Valid files have enough data for profile header and tag table entry count.
  99. if (header.profile_size < sizeof(ICCHeader) + sizeof(u32))
  100. return Error::from_string_literal("ICC::Profile: Profile size too small");
  101. if (header.profile_size > icc_bytes.size())
  102. return Error::from_string_literal("ICC::Profile: Profile size larger than input data");
  103. return header.profile_size;
  104. }
  105. Optional<PreferredCMMType> parse_preferred_cmm_type(ICCHeader const& header)
  106. {
  107. // ICC v4, 7.2.3 Preferred CMM type field
  108. // "This field may be used to identify the preferred CMM to be used.
  109. // If used, it shall match a CMM type signature registered in the ICC Tag Registry"
  110. // https://www.color.org/signatures2.xalter currently links to
  111. // https://www.color.org/registry/signature/TagRegistry-2021-03.pdf, which contains
  112. // some CMM signatures.
  113. // This requirement is often honored in practice, but not always. For example,
  114. // JPEGs exported in Adobe Lightroom contain profiles that set this to 'Lino',
  115. // which is not present in the "CMM Signatures" table in that PDF.
  116. // "If no preferred CMM is identified, this field shall be set to zero (00000000h)."
  117. if (header.preferred_cmm_type == PreferredCMMType { 0 })
  118. return {};
  119. return header.preferred_cmm_type;
  120. }
  121. ErrorOr<Version> parse_version(ICCHeader const& header)
  122. {
  123. // ICC v4, 7.2.4 Profile version field
  124. if (header.profile_version_zero != 0)
  125. return Error::from_string_literal("ICC::Profile: Reserved version bytes not zero");
  126. return Version(header.profile_version_major, header.profile_version_minor_bugfix);
  127. }
  128. ErrorOr<DeviceClass> parse_device_class(ICCHeader const& header)
  129. {
  130. // ICC v4, 7.2.5 Profile/device class field
  131. switch (header.profile_device_class) {
  132. case DeviceClass::InputDevice:
  133. case DeviceClass::DisplayDevice:
  134. case DeviceClass::OutputDevice:
  135. case DeviceClass::DeviceLink:
  136. case DeviceClass::ColorSpace:
  137. case DeviceClass::Abstract:
  138. case DeviceClass::NamedColor:
  139. return header.profile_device_class;
  140. }
  141. return Error::from_string_literal("ICC::Profile: Invalid device class");
  142. }
  143. ErrorOr<ColorSpace> parse_color_space(ColorSpace color_space)
  144. {
  145. // ICC v4, Table 19 — Data colour space signatures
  146. switch (color_space) {
  147. case ColorSpace::nCIEXYZ:
  148. case ColorSpace::CIELAB:
  149. case ColorSpace::CIELUV:
  150. case ColorSpace::YCbCr:
  151. case ColorSpace::CIEYxy:
  152. case ColorSpace::RGB:
  153. case ColorSpace::Gray:
  154. case ColorSpace::HSV:
  155. case ColorSpace::HLS:
  156. case ColorSpace::CMYK:
  157. case ColorSpace::CMY:
  158. case ColorSpace::TwoColor:
  159. case ColorSpace::ThreeColor:
  160. case ColorSpace::FourColor:
  161. case ColorSpace::FiveColor:
  162. case ColorSpace::SixColor:
  163. case ColorSpace::SevenColor:
  164. case ColorSpace::EightColor:
  165. case ColorSpace::NineColor:
  166. case ColorSpace::TenColor:
  167. case ColorSpace::ElevenColor:
  168. case ColorSpace::TwelveColor:
  169. case ColorSpace::ThirteenColor:
  170. case ColorSpace::FourteenColor:
  171. case ColorSpace::FifteenColor:
  172. return color_space;
  173. }
  174. return Error::from_string_literal("ICC::Profile: Invalid color space");
  175. }
  176. ErrorOr<ColorSpace> parse_data_color_space(ICCHeader const& header)
  177. {
  178. // ICC v4, 7.2.6 Data colour space field
  179. return parse_color_space(header.data_color_space);
  180. }
  181. ErrorOr<ColorSpace> parse_connection_space(ICCHeader const& header)
  182. {
  183. // ICC v4, 7.2.7 PCS field
  184. // and Annex D
  185. auto space = TRY(parse_color_space(header.profile_connection_space));
  186. if (header.profile_device_class != DeviceClass::DeviceLink && (space != ColorSpace::PCSXYZ && space != ColorSpace::PCSLAB))
  187. return Error::from_string_literal("ICC::Profile: Invalid profile connection space: Non-PCS space on non-DeviceLink profile");
  188. return space;
  189. }
  190. ErrorOr<time_t> parse_creation_date_time(ICCHeader const& header)
  191. {
  192. // ICC v4, 7.2.8 Date and time field
  193. return parse_date_time_number(header.profile_creation_time);
  194. }
  195. ErrorOr<void> parse_file_signature(ICCHeader const& header)
  196. {
  197. // ICC v4, 7.2.9 Profile file signature field
  198. // "The profile file signature field shall contain the value “acsp” (61637370h) as a profile file signature."
  199. if (header.profile_file_signature != 0x61637370)
  200. return Error::from_string_literal("ICC::Profile: profile file signature not 'acsp'");
  201. return {};
  202. }
  203. ErrorOr<Optional<PrimaryPlatform>> parse_primary_platform(ICCHeader const& header)
  204. {
  205. // ICC v4, 7.2.10 Primary platform field
  206. // "If there is no primary platform identified, this field shall be set to zero (00000000h)."
  207. if (header.primary_platform == PrimaryPlatform { 0 })
  208. return OptionalNone {};
  209. switch (header.primary_platform) {
  210. case PrimaryPlatform::Apple:
  211. case PrimaryPlatform::Microsoft:
  212. case PrimaryPlatform::SiliconGraphics:
  213. case PrimaryPlatform::Sun:
  214. return header.primary_platform;
  215. }
  216. return Error::from_string_literal("ICC::Profile: Invalid primary platform");
  217. }
  218. Optional<DeviceManufacturer> parse_device_manufacturer(ICCHeader const& header)
  219. {
  220. // ICC v4, 7.2.12 Device manufacturer field
  221. // "This field may be used to identify a device manufacturer.
  222. // If used the signature shall match the signature contained in the appropriate section of the ICC signature registry found at www.color.org"
  223. // Device manufacturers can be looked up at https://www.color.org/signatureRegistry/index.xalter
  224. // For example: https://www.color.org/signatureRegistry/?entityEntry=APPL-4150504C
  225. // Some icc files use codes not in that registry. For example. D50_XYZ.icc from https://www.color.org/XYZprofiles.xalter
  226. // has its device manufacturer set to 'none', but https://www.color.org/signatureRegistry/?entityEntry=none-6E6F6E65 does not exist.
  227. // "If not used this field shall be set to zero (00000000h)."
  228. if (header.device_manufacturer == DeviceManufacturer { 0 })
  229. return {};
  230. return header.device_manufacturer;
  231. }
  232. Optional<DeviceModel> parse_device_model(ICCHeader const& header)
  233. {
  234. // ICC v4, 7.2.13 Device model field
  235. // "This field may be used to identify a device model.
  236. // If used the signature shall match the signature contained in the appropriate section of the ICC signature registry found at www.color.org"
  237. // Device models can be looked up at https://www.color.org/signatureRegistry/deviceRegistry/index.xalter
  238. // For example: https://www.color.org/signatureRegistry/deviceRegistry/?entityEntry=7FD8-37464438
  239. // Some icc files use codes not in that registry. For example. D50_XYZ.icc from https://www.color.org/XYZprofiles.xalter
  240. // has its device model set to 'none', but https://www.color.org/signatureRegistry/deviceRegistry?entityEntry=none-6E6F6E65 does not exist.
  241. // "If not used this field shall be set to zero (00000000h)."
  242. if (header.device_model == DeviceModel { 0 })
  243. return {};
  244. return header.device_model;
  245. }
  246. ErrorOr<DeviceAttributes> parse_device_attributes(ICCHeader const& header)
  247. {
  248. // ICC v4, 7.2.14 Device attributes field
  249. // "4 to 31": "Reserved (set to binary zero)"
  250. if (header.device_attributes & 0xffff'fff0)
  251. return Error::from_string_literal("ICC::Profile: Device attributes reserved bits not set to 0");
  252. return DeviceAttributes { header.device_attributes };
  253. }
  254. ErrorOr<RenderingIntent> parse_rendering_intent(ICCHeader const& header)
  255. {
  256. // ICC v4, 7.2.15 Rendering intent field
  257. switch (header.rendering_intent) {
  258. case 0:
  259. return RenderingIntent::Perceptual;
  260. case 1:
  261. return RenderingIntent::MediaRelativeColorimetric;
  262. case 2:
  263. return RenderingIntent::Saturation;
  264. case 3:
  265. return RenderingIntent::ICCAbsoluteColorimetric;
  266. }
  267. return Error::from_string_literal("ICC::Profile: Invalid rendering intent");
  268. }
  269. ErrorOr<XYZ> parse_pcs_illuminant(ICCHeader const& header)
  270. {
  271. // ICC v4, 7.2.16 PCS illuminant field
  272. XYZ xyz = (XYZ)header.pcs_illuminant;
  273. /// "The value, when rounded to four decimals, shall be X = 0,9642, Y = 1,0 and Z = 0,8249."
  274. if (round(xyz.x * 10'000) != 9'642 || round(xyz.y * 10'000) != 10'000 || round(xyz.z * 10'000) != 8'249)
  275. return Error::from_string_literal("ICC::Profile: Invalid pcs illuminant");
  276. return xyz;
  277. }
  278. Optional<Creator> parse_profile_creator(ICCHeader const& header)
  279. {
  280. // ICC v4, 7.2.17 Profile creator field
  281. // "This field may be used to identify the creator of the profile.
  282. // If used the signature should match the signature contained in the device manufacturer section of the ICC signature registry found at www.color.org."
  283. // This is not always true in practice.
  284. // For example, .icc files in /System/ColorSync/Profiles on macOS 12.6 set this to 'appl', which is a CMM signature, not a device signature (that one would be 'APPL').
  285. // "If not used this field shall be set to zero (00000000h)."
  286. if (header.profile_creator == Creator { 0 })
  287. return {};
  288. return header.profile_creator;
  289. }
  290. template<size_t N>
  291. bool all_bytes_are_zero(const u8 (&bytes)[N])
  292. {
  293. for (u8 byte : bytes) {
  294. if (byte != 0)
  295. return false;
  296. }
  297. return true;
  298. }
  299. ErrorOr<Optional<Crypto::Hash::MD5::DigestType>> parse_profile_id(ICCHeader const& header, ReadonlyBytes icc_bytes)
  300. {
  301. // ICC v4, 7.2.18 Profile ID field
  302. // "A profile ID field value of zero (00h) shall indicate that a profile ID has not been calculated."
  303. if (all_bytes_are_zero(header.profile_id))
  304. return OptionalNone {};
  305. Crypto::Hash::MD5::DigestType id;
  306. static_assert(sizeof(id.data) == sizeof(header.profile_id));
  307. memcpy(id.data, header.profile_id, sizeof(id.data));
  308. auto computed_id = Profile::compute_id(icc_bytes);
  309. if (id != computed_id)
  310. return Error::from_string_literal("ICC::Profile: Invalid profile id");
  311. return id;
  312. }
  313. ErrorOr<void> parse_reserved(ICCHeader const& header)
  314. {
  315. // ICC v4, 7.2.19 Reserved field
  316. // "This field of the profile header is reserved for future ICC definition and shall be set to zero."
  317. if (!all_bytes_are_zero(header.reserved))
  318. return Error::from_string_literal("ICC::Profile: Reserved header bytes are not zero");
  319. return {};
  320. }
  321. }
  322. URL device_manufacturer_url(DeviceManufacturer device_manufacturer)
  323. {
  324. return URL(DeprecatedString::formatted("https://www.color.org/signatureRegistry/?entityEntry={:c}{:c}{:c}{:c}-{:08X}",
  325. device_manufacturer.c0(), device_manufacturer.c1(), device_manufacturer.c2(), device_manufacturer.c3(), device_manufacturer.value));
  326. }
  327. URL device_model_url(DeviceModel device_model)
  328. {
  329. return URL(DeprecatedString::formatted("https://www.color.org/signatureRegistry/deviceRegistry/?entityEntry={:c}{:c}{:c}{:c}-{:08X}",
  330. device_model.c0(), device_model.c1(), device_model.c2(), device_model.c3(), device_model.value));
  331. }
  332. StringView device_class_name(DeviceClass device_class)
  333. {
  334. switch (device_class) {
  335. case DeviceClass::InputDevice:
  336. return "InputDevice"sv;
  337. case DeviceClass::DisplayDevice:
  338. return "DisplayDevice"sv;
  339. case DeviceClass::OutputDevice:
  340. return "OutputDevice"sv;
  341. case DeviceClass::DeviceLink:
  342. return "DeviceLink"sv;
  343. case DeviceClass::ColorSpace:
  344. return "ColorSpace"sv;
  345. case DeviceClass::Abstract:
  346. return "Abstract"sv;
  347. case DeviceClass::NamedColor:
  348. return "NamedColor"sv;
  349. }
  350. VERIFY_NOT_REACHED();
  351. }
  352. StringView data_color_space_name(ColorSpace color_space)
  353. {
  354. switch (color_space) {
  355. case ColorSpace::nCIEXYZ:
  356. return "nCIEXYZ"sv;
  357. case ColorSpace::CIELAB:
  358. return "CIELAB"sv;
  359. case ColorSpace::CIELUV:
  360. return "CIELUV"sv;
  361. case ColorSpace::YCbCr:
  362. return "YCbCr"sv;
  363. case ColorSpace::CIEYxy:
  364. return "CIEYxy"sv;
  365. case ColorSpace::RGB:
  366. return "RGB"sv;
  367. case ColorSpace::Gray:
  368. return "Gray"sv;
  369. case ColorSpace::HSV:
  370. return "HSV"sv;
  371. case ColorSpace::HLS:
  372. return "HLS"sv;
  373. case ColorSpace::CMYK:
  374. return "CMYK"sv;
  375. case ColorSpace::CMY:
  376. return "CMY"sv;
  377. case ColorSpace::TwoColor:
  378. return "2 color"sv;
  379. case ColorSpace::ThreeColor:
  380. return "3 color (other than XYZ, Lab, Luv, YCbCr, CIEYxy, RGB, HSV, HLS, CMY)"sv;
  381. case ColorSpace::FourColor:
  382. return "4 color (other than CMYK)"sv;
  383. case ColorSpace::FiveColor:
  384. return "5 color"sv;
  385. case ColorSpace::SixColor:
  386. return "6 color"sv;
  387. case ColorSpace::SevenColor:
  388. return "7 color"sv;
  389. case ColorSpace::EightColor:
  390. return "8 color"sv;
  391. case ColorSpace::NineColor:
  392. return "9 color"sv;
  393. case ColorSpace::TenColor:
  394. return "10 color"sv;
  395. case ColorSpace::ElevenColor:
  396. return "11 color"sv;
  397. case ColorSpace::TwelveColor:
  398. return "12 color"sv;
  399. case ColorSpace::ThirteenColor:
  400. return "13 color"sv;
  401. case ColorSpace::FourteenColor:
  402. return "14 color"sv;
  403. case ColorSpace::FifteenColor:
  404. return "15 color"sv;
  405. }
  406. VERIFY_NOT_REACHED();
  407. }
  408. StringView profile_connection_space_name(ColorSpace color_space)
  409. {
  410. switch (color_space) {
  411. case ColorSpace::PCSXYZ:
  412. return "PCSXYZ"sv;
  413. case ColorSpace::PCSLAB:
  414. return "PCSLAB"sv;
  415. default:
  416. return data_color_space_name(color_space);
  417. }
  418. }
  419. StringView primary_platform_name(PrimaryPlatform primary_platform)
  420. {
  421. switch (primary_platform) {
  422. case PrimaryPlatform::Apple:
  423. return "Apple"sv;
  424. case PrimaryPlatform::Microsoft:
  425. return "Microsoft"sv;
  426. case PrimaryPlatform::SiliconGraphics:
  427. return "Silicon Graphics"sv;
  428. case PrimaryPlatform::Sun:
  429. return "Sun"sv;
  430. }
  431. VERIFY_NOT_REACHED();
  432. }
  433. StringView rendering_intent_name(RenderingIntent rendering_intent)
  434. {
  435. switch (rendering_intent) {
  436. case RenderingIntent::Perceptual:
  437. return "Perceptual"sv;
  438. case RenderingIntent::MediaRelativeColorimetric:
  439. return "Media-relative colorimetric"sv;
  440. case RenderingIntent::Saturation:
  441. return "Saturation"sv;
  442. case RenderingIntent::ICCAbsoluteColorimetric:
  443. return "ICC-absolute colorimetric"sv;
  444. }
  445. VERIFY_NOT_REACHED();
  446. }
  447. Flags::Flags() = default;
  448. Flags::Flags(u32 bits)
  449. : m_bits(bits)
  450. {
  451. }
  452. DeviceAttributes::DeviceAttributes() = default;
  453. DeviceAttributes::DeviceAttributes(u64 bits)
  454. : m_bits(bits)
  455. {
  456. }
  457. ErrorOr<void> Profile::read_header(ReadonlyBytes bytes)
  458. {
  459. if (bytes.size() < sizeof(ICCHeader))
  460. return Error::from_string_literal("ICC::Profile: Not enough data for header");
  461. auto header = *bit_cast<ICCHeader const*>(bytes.data());
  462. TRY(parse_file_signature(header));
  463. m_on_disk_size = TRY(parse_size(header, bytes));
  464. m_preferred_cmm_type = parse_preferred_cmm_type(header);
  465. m_version = TRY(parse_version(header));
  466. m_device_class = TRY(parse_device_class(header));
  467. m_data_color_space = TRY(parse_data_color_space(header));
  468. m_connection_space = TRY(parse_connection_space(header));
  469. m_creation_timestamp = TRY(parse_creation_date_time(header));
  470. m_primary_platform = TRY(parse_primary_platform(header));
  471. m_flags = Flags { header.profile_flags };
  472. m_device_manufacturer = parse_device_manufacturer(header);
  473. m_device_model = parse_device_model(header);
  474. m_device_attributes = TRY(parse_device_attributes(header));
  475. m_rendering_intent = TRY(parse_rendering_intent(header));
  476. m_pcs_illuminant = TRY(parse_pcs_illuminant(header));
  477. m_creator = parse_profile_creator(header);
  478. m_id = TRY(parse_profile_id(header, bytes));
  479. TRY(parse_reserved(header));
  480. return {};
  481. }
  482. ErrorOr<NonnullRefPtr<TagData>> Profile::read_tag(ReadonlyBytes bytes, u32 offset_to_beginning_of_tag_data_element, u32 size_of_tag_data_element)
  483. {
  484. if (offset_to_beginning_of_tag_data_element + size_of_tag_data_element > bytes.size())
  485. return Error::from_string_literal("ICC::Profile: Tag data out of bounds");
  486. auto tag_bytes = bytes.slice(offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  487. // ICC v4, 9 Tag definitions
  488. // ICC v4, 9.1 General
  489. // "All tags, including private tags, have as their first four bytes a tag signature to identify to profile readers
  490. // what kind of data is contained within a tag."
  491. if (tag_bytes.size() < sizeof(u32))
  492. return Error::from_string_literal("ICC::Profile: Not enough data for tag type");
  493. auto type = tag_type(tag_bytes);
  494. switch (type) {
  495. case CurveTagData::Type:
  496. return CurveTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  497. case MultiLocalizedUnicodeTagData::Type:
  498. return MultiLocalizedUnicodeTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  499. case ParametricCurveTagData::Type:
  500. return ParametricCurveTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  501. case S15Fixed16ArrayTagData::Type:
  502. return S15Fixed16ArrayTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  503. case TextDescriptionTagData::Type:
  504. return TextDescriptionTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  505. case TextTagData::Type:
  506. return TextTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  507. case XYZTagData::Type:
  508. return XYZTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  509. default:
  510. // FIXME: optionally ignore tags of unknown type
  511. return adopt_ref(*new UnknownTagData(offset_to_beginning_of_tag_data_element, size_of_tag_data_element, type));
  512. }
  513. }
  514. ErrorOr<void> Profile::read_tag_table(ReadonlyBytes bytes)
  515. {
  516. // ICC v4, 7.3 Tag table
  517. // ICC v4, 7.3.1 Overview
  518. // "The tag table acts as a table of contents for the tags and an index into the tag data element in the profiles. It
  519. // shall consist of a 4-byte entry that contains a count of the number of tags in the table followed by a series of 12-
  520. // byte entries with one entry for each tag. The tag table therefore contains 4+12n bytes where n is the number of
  521. // tags contained in the profile. The entries for the tags within the table are not required to be in any particular
  522. // order nor are they required to match the sequence of tag data element within the profile.
  523. // Each 12-byte tag entry following the tag count shall consist of a 4-byte tag signature, a 4-byte offset to define
  524. // the beginning of the tag data element, and a 4-byte entry identifying the length of the tag data element in bytes.
  525. // [...]
  526. // The tag table shall define a contiguous sequence of unique tag elements, with no gaps between the last byte
  527. // of any tag data element referenced from the tag table (inclusive of any necessary additional pad bytes required
  528. // to reach a four-byte boundary) and the byte offset of the following tag element, or the end of the file.
  529. // Duplicate tag signatures shall not be included in the tag table.
  530. // Tag data elements shall not partially overlap, so there shall be no part of any tag data element that falls within
  531. // the range defined for another tag in the tag table."
  532. ReadonlyBytes tag_table_bytes = bytes.slice(sizeof(ICCHeader));
  533. if (tag_table_bytes.size() < sizeof(u32))
  534. return Error::from_string_literal("ICC::Profile: Not enough data for tag count");
  535. auto tag_count = *bit_cast<BigEndian<u32> const*>(tag_table_bytes.data());
  536. // ICC V4, 7.3 Tag table, Table 24 - Tag table structure
  537. struct TagTableEntry {
  538. BigEndian<TagSignature> tag_signature;
  539. BigEndian<u32> offset_to_beginning_of_tag_data_element;
  540. BigEndian<u32> size_of_tag_data_element;
  541. };
  542. static_assert(sizeof(TagTableEntry) == 12);
  543. tag_table_bytes = tag_table_bytes.slice(sizeof(u32));
  544. if (tag_table_bytes.size() < tag_count * sizeof(TagTableEntry))
  545. return Error::from_string_literal("ICC::Profile: Not enough data for tag table entries");
  546. auto tag_table_entries = bit_cast<TagTableEntry const*>(tag_table_bytes.data());
  547. // "The tag table may contain multiple tags signatures that all reference the same tag data element offset, allowing
  548. // efficient reuse of tag data elements."
  549. HashMap<u32, NonnullRefPtr<TagData>> offset_to_tag_data;
  550. for (u32 i = 0; i < tag_count; ++i) {
  551. // FIXME: optionally ignore tags with unknown signature
  552. // Dedupe identical offset/sizes.
  553. NonnullRefPtr<TagData> tag_data = TRY(offset_to_tag_data.try_ensure(tag_table_entries[i].offset_to_beginning_of_tag_data_element, [=, this]() {
  554. return read_tag(bytes, tag_table_entries[i].offset_to_beginning_of_tag_data_element, tag_table_entries[i].size_of_tag_data_element);
  555. }));
  556. // "In such cases, both the offset and size of the tag data elements in the tag table shall be the same."
  557. if (tag_data->size() != tag_table_entries[i].size_of_tag_data_element)
  558. return Error::from_string_literal("ICC::Profile: two tags have same offset but different sizes");
  559. // "Duplicate tag signatures shall not be included in the tag table."
  560. if (TRY(m_tag_table.try_set(tag_table_entries[i].tag_signature, move(tag_data))) != AK::HashSetResult::InsertedNewEntry)
  561. return Error::from_string_literal("ICC::Profile: duplicate tag signature");
  562. }
  563. return {};
  564. }
  565. static bool is_xCLR(ColorSpace color_space)
  566. {
  567. switch (color_space) {
  568. case ColorSpace::TwoColor:
  569. case ColorSpace::ThreeColor:
  570. case ColorSpace::FourColor:
  571. case ColorSpace::FiveColor:
  572. case ColorSpace::SixColor:
  573. case ColorSpace::SevenColor:
  574. case ColorSpace::EightColor:
  575. case ColorSpace::NineColor:
  576. case ColorSpace::TenColor:
  577. case ColorSpace::ElevenColor:
  578. case ColorSpace::TwelveColor:
  579. case ColorSpace::ThirteenColor:
  580. case ColorSpace::FourteenColor:
  581. case ColorSpace::FifteenColor:
  582. return true;
  583. default:
  584. return false;
  585. }
  586. }
  587. ErrorOr<void> Profile::check_required_tags()
  588. {
  589. // ICC v4, 8 Required tags
  590. // ICC v4, 8.2 Common requirements
  591. // "With the exception of DeviceLink profiles, all profiles shall contain the following tags:
  592. // - profileDescriptionTag (see 9.2.41);
  593. // - copyrightTag (see 9.2.21);
  594. // - mediaWhitePointTag (see 9.2.34);
  595. // - chromaticAdaptationTag, when the measurement data used to calculate the profile was specified for an
  596. // adopted white with a chromaticity different from that of the PCS adopted white (see 9.2.15).
  597. // NOTE A DeviceLink profile is not required to have either a mediaWhitePointTag or a chromaticAdaptationTag."
  598. // profileDescriptionTag, copyrightTag are required for DeviceLink too (see ICC v4, 8.6 DeviceLink profile).
  599. // profileDescriptionTag, copyrightTag, mediaWhitePointTag are required in ICC v2 as well.
  600. // chromaticAdaptationTag isn't required in v2 profiles as far as I can tell.
  601. if (!m_tag_table.contains(profileDescriptionTag))
  602. return Error::from_string_literal("ICC::Profile: required profileDescriptionTag is missing");
  603. if (!m_tag_table.contains(copyrightTag))
  604. return Error::from_string_literal("ICC::Profile: required copyrightTag is missing");
  605. if (device_class() != DeviceClass::DeviceLink) {
  606. if (!m_tag_table.contains(mediaWhitePointTag))
  607. return Error::from_string_literal("ICC::Profile: required mediaWhitePointTag is missing");
  608. // FIXME: Check for chromaticAdaptationTag after figuring out when exactly it needs to be present.
  609. }
  610. auto has_tag = [&](auto& tag) { return m_tag_table.contains(tag); };
  611. auto has_all_tags = [&]<class T>(T tags) { return all_of(tags, has_tag); };
  612. switch (device_class()) {
  613. case DeviceClass::InputDevice: {
  614. // ICC v4, 8.3 Input profiles
  615. // "8.3.1 General
  616. // Input profiles are generally used with devices such as scanners and digital cameras. The types of profiles
  617. // available for use as Input profiles are N-component LUT-based, Three-component matrix-based, and
  618. // monochrome.
  619. // 8.3.2 N-component LUT-based Input profiles
  620. // In addition to the tags listed in 8.2 an N-component LUT-based Input profile shall contain the following tag:
  621. // - AToB0Tag (see 9.2.1).
  622. // 8.3.3 Three-component matrix-based Input profiles
  623. // In addition to the tags listed in 8.2, a three-component matrix-based Input profile shall contain the following tags:
  624. // - redMatrixColumnTag (see 9.2.44);
  625. // - greenMatrixColumnTag (see 9.2.30);
  626. // - blueMatrixColumnTag (see 9.2.4);
  627. // - redTRCTag (see 9.2.45);
  628. // - greenTRCTag (see 9.2.31);
  629. // - blueTRCTag (see 9.2.5).
  630. // [...] Only the PCSXYZ encoding can be used with matrix/TRC models.
  631. // 8.3.4 Monochrome Input profiles
  632. // In addition to the tags listed in 8.2, a monochrome Input profile shall contain the following tag:
  633. // - grayTRCTag (see 9.2.29).
  634. bool has_n_component_lut_based_tags = has_tag(AToB0Tag);
  635. bool has_three_component_matrix_based_tags = has_all_tags(Array { redMatrixColumnTag, greenMatrixColumnTag, blueMatrixColumnTag, redTRCTag, greenTRCTag, blueTRCTag });
  636. bool has_monochrome_tags = has_tag(grayTRCTag);
  637. if (!has_n_component_lut_based_tags && !has_three_component_matrix_based_tags && !has_monochrome_tags)
  638. return Error::from_string_literal("ICC::Profile: InputDevice required tags are missing");
  639. if (!has_n_component_lut_based_tags && has_three_component_matrix_based_tags && connection_space() != ColorSpace::PCSXYZ)
  640. return Error::from_string_literal("ICC::Profile: InputDevice three-component matrix-based profile must use PCSXYZ");
  641. break;
  642. }
  643. case DeviceClass::DisplayDevice: {
  644. // ICC v4, 8.4 Display profiles
  645. // "8.4.1 General
  646. // This class of profiles represents display devices such as monitors. The types of profiles available for use as
  647. // Display profiles are N-component LUT-based, Three-component matrix-based, and monochrome.
  648. // 8.4.2 N-Component LUT-based Display profiles
  649. // In addition to the tags listed in 8.2 an N-component LUT-based Input profile shall contain the following tags:
  650. // - AToB0Tag (see 9.2.1);
  651. // - BToA0Tag (see 9.2.6).
  652. // 8.4.3 Three-component matrix-based Display profiles
  653. // In addition to the tags listed in 8.2, a three-component matrix-based Display profile shall contain the following
  654. // tags:
  655. // - redMatrixColumnTag (see 9.2.44);
  656. // - greenMatrixColumnTag (see 9.2.30);
  657. // - blueMatrixColumnTag (see 9.2.4);
  658. // - redTRCTag (see 9.2.45);
  659. // - greenTRCTag (see 9.2.31);
  660. // - blueTRCTag (see 9.2.5).
  661. // [...] Only the PCSXYZ encoding can be used with matrix/TRC models.
  662. // 8.4.4 Monochrome Display profiles
  663. // In addition to the tags listed in 8.2 a monochrome Display profile shall contain the following tag:
  664. // - grayTRCTag (see 9.2.29)."
  665. bool has_n_component_lut_based_tags = has_all_tags(Array { AToB0Tag, BToA0Tag });
  666. bool has_three_component_matrix_based_tags = has_all_tags(Array { redMatrixColumnTag, greenMatrixColumnTag, blueMatrixColumnTag, redTRCTag, greenTRCTag, blueTRCTag });
  667. bool has_monochrome_tags = has_tag(grayTRCTag);
  668. if (!has_n_component_lut_based_tags && !has_three_component_matrix_based_tags && !has_monochrome_tags)
  669. return Error::from_string_literal("ICC::Profile: DisplayDevice required tags are missing");
  670. if (!has_n_component_lut_based_tags && has_three_component_matrix_based_tags && connection_space() != ColorSpace::PCSXYZ)
  671. return Error::from_string_literal("ICC::Profile: DisplayDevice three-component matrix-based profile must use PCSXYZ");
  672. break;
  673. }
  674. case DeviceClass::OutputDevice: {
  675. // ICC v4, 8.5 Output profiles
  676. // "8.5.1 General
  677. // Output profiles are used to support devices such as printers and film recorders. The types of profiles available
  678. // for use as Output profiles are N-component LUT-based and Monochrome.
  679. // 8.5.2 N-component LUT-based Output profiles
  680. // In addition to the tags listed in 8.2 an N-component LUT-based Output profile shall contain the following tags:
  681. // - AToB0Tag (see 9.2.1);
  682. // - AToB1Tag (see 9.2.2);
  683. // - AToB2Tag (see 9.2.3);
  684. // - BToA0Tag (see 9.2.6);
  685. // - BToA1Tag (see 9.2.7);
  686. // - BToA2Tag (see 9.2.8);
  687. // - gamutTag (see 9.2.28);
  688. // - colorantTableTag (see 9.2.18), for the xCLR colour spaces (see 7.2.6)
  689. // 8.5.3 Monochrome Output profiles
  690. // In addition to the tags listed in 8.2 a monochrome Output profile shall contain the following tag:
  691. // - grayTRCTag (see 9.2.29)."
  692. // The colorantTableTag requirement is new in v4.
  693. Vector<TagSignature, 8> required_n_component_lut_based_tags = { AToB0Tag, AToB1Tag, AToB2Tag, BToA0Tag, BToA1Tag, BToA2Tag, gamutTag };
  694. if (is_v4() && is_xCLR(connection_space()))
  695. required_n_component_lut_based_tags.append(colorantTableTag);
  696. bool has_n_component_lut_based_tags = has_all_tags(required_n_component_lut_based_tags);
  697. bool has_monochrome_tags = has_tag(grayTRCTag);
  698. if (!has_n_component_lut_based_tags && !has_monochrome_tags)
  699. return Error::from_string_literal("ICC::Profile: OutputDevice required tags are missing");
  700. break;
  701. }
  702. case DeviceClass::DeviceLink: {
  703. // ICC v4, 8.6 DeviceLink profile
  704. // "A DeviceLink profile shall contain the following tags:
  705. // - profileDescriptionTag (see 9.2.41);
  706. // - copyrightTag (see 9.2.21);
  707. // - profileSequenceDescTag (see 9.2.42);
  708. // - AToB0Tag (see 9.2.1);
  709. // - colorantTableTag (see 9.2.18) which is required only if the data colour space field is xCLR, where x is
  710. // hexadecimal 2 to F (see 7.2.6);
  711. // - colorantTableOutTag (see 9.2.19), required only if the PCS field is xCLR, where x is hexadecimal 2 to F
  712. // (see 7.2.6)"
  713. // profileDescriptionTag and copyrightTag are already checked above, in the code for section 8.2.
  714. Vector<TagSignature, 4> required_tags = { profileSequenceDescTag, AToB0Tag };
  715. if (is_v4() && is_xCLR(connection_space())) { // This requirement is new in v4.
  716. required_tags.append(colorantTableTag);
  717. required_tags.append(colorantTableOutTag);
  718. }
  719. if (!has_all_tags(required_tags))
  720. return Error::from_string_literal("ICC::Profile: DeviceLink required tags are missing");
  721. // "The data colour space field (see 7.2.6) in the DeviceLink profile will be the same as the data colour space field
  722. // of the first profile in the sequence used to construct the device link. The PCS field (see 7.2.7) will be the same
  723. // as the data colour space field of the last profile in the sequence."
  724. // FIXME: Check that if profileSequenceDescType parsing is implemented.
  725. break;
  726. }
  727. case DeviceClass::ColorSpace:
  728. // ICC v4, 8.7 ColorSpace profile
  729. // "In addition to the tags listed in 8.2, a ColorSpace profile shall contain the following tags:
  730. // - BToA0Tag (see 9.2.6);
  731. // - AToB0Tag (see 9.2.1).
  732. // [...] ColorSpace profiles may be embedded in images."
  733. if (!has_all_tags(Array { AToB0Tag, BToA0Tag }))
  734. return Error::from_string_literal("ICC::Profile: ColorSpace required tags are missing");
  735. break;
  736. case DeviceClass::Abstract:
  737. // ICC v4, 8.8 Abstract profile
  738. // "In addition to the tags listed in 8.2, an Abstract profile shall contain the following tag:
  739. // - AToB0Tag (see 9.2.1).
  740. // [...] Abstract profiles cannot be embedded in images."
  741. if (!has_tag(AToB0Tag))
  742. return Error::from_string_literal("ICC::Profile: Abstract required AToB0Tag is missing");
  743. break;
  744. case DeviceClass::NamedColor:
  745. // ICC v4, 8.9 NamedColor profile
  746. // "In addition to the tags listed in 8.2, a NamedColor profile shall contain the following tag:
  747. // - namedColor2Tag (see 9.2.35)."
  748. if (!has_tag(namedColor2Tag))
  749. return Error::from_string_literal("ICC::Profile: NamedColor required namedColor2Tag is missing");
  750. break;
  751. }
  752. return {};
  753. }
  754. ErrorOr<void> Profile::check_tag_types()
  755. {
  756. // This uses m_tag_table.get() even for tags that are guaranteed to exist after check_required_tags()
  757. // so that the two functions can be called in either order.
  758. // Profile ID of /System/Library/ColorSync/Profiles/ITU-2020.icc on macOS 13.1.
  759. static constexpr Crypto::Hash::MD5::DigestType apple_itu_2020_id = { 0x57, 0x0b, 0x1b, 0x76, 0xc6, 0xa0, 0x50, 0xaa, 0x9f, 0x6c, 0x53, 0x8d, 0xbe, 0x2d, 0x3e, 0xf0 };
  760. // ICC v4, 9.2.1 AToB0Tag
  761. // "Permitted tag types: lut8Type or lut16Type or lutAToBType"
  762. // FIXME
  763. // ICC v4, 9.2.2 AToB1Tag
  764. // "Permitted tag types: lut8Type or lut16Type or lutAToBType"
  765. // FIXME
  766. // ICC v4, 9.2.3 AToB2Tag
  767. // "Permitted tag types: lut8Type or lut16Type or lutAToBType"
  768. // FIXME
  769. // ICC v4, 9.2.4 blueMatrixColumnTag
  770. // "Permitted tag types: XYZType
  771. // This tag contains the third column in the matrix used in matrix/TRC transforms."
  772. // (Called blueColorantTag in the v2 spec, otherwise identical there.)
  773. if (auto type = m_tag_table.get(blueMatrixColumnTag); type.has_value()) {
  774. if (type.value()->type() != XYZTagData::Type)
  775. return Error::from_string_literal("ICC::Profile: blueMatrixColumnTag has unexpected type");
  776. if (static_cast<XYZTagData const&>(*type.value()).xyzs().size() != 1)
  777. return Error::from_string_literal("ICC::Profile: blueMatrixColumnTag has unexpected size");
  778. }
  779. // ICC v4, 9.2.5 blueTRCTag
  780. // "Permitted tag types: curveType or parametricCurveType"
  781. // ICC v2, 6.4.5 blueTRCTag
  782. // "Tag Type: curveType"
  783. if (auto type = m_tag_table.get(blueTRCTag); type.has_value() && type.value()->type() != CurveTagData::Type && (is_v2() || type.value()->type() != ParametricCurveTagData::Type))
  784. return Error::from_string_literal("ICC::Profile: blueTRCTag has unexpected type");
  785. // ICC v4, 9.2.6 BToA0Tag
  786. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  787. // FIXME
  788. // ICC v4, 9.2.7 BToA1Tag
  789. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  790. // FIXME
  791. // ICC v4, 9.2.8 BToA2Tag
  792. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  793. // FIXME
  794. // ICC v4, 9.2.9 BToD0Tag
  795. // "Permitted tag types: multiProcessElementsType"
  796. // FIXME
  797. // ICC v4, 9.2.10 BToD1Tag
  798. // "Permitted tag types: multiProcessElementsType"
  799. // FIXME
  800. // ICC v4, 9.2.11 BToD2Tag
  801. // "Permitted tag types: multiProcessElementsType"
  802. // FIXME
  803. // ICC v4, 9.2.12 BToD3Tag
  804. // "Permitted tag types: multiProcessElementsType"
  805. // FIXME
  806. // ICC v4, 9.2.13 calibrationDateTimeTag
  807. // "Permitted tag types: dateTimeType"
  808. // FIXME
  809. // ICC v4, 9.2.14 charTargetTag
  810. // "Permitted tag types: textType"
  811. if (auto type = m_tag_table.get(charTargetTag); type.has_value() && type.value()->type() != TextTagData::Type)
  812. return Error::from_string_literal("ICC::Profile: charTargetTag has unexpected type");
  813. // ICC v4, 9.2.15 chromaticAdaptationTag
  814. // "Permitted tag types: s15Fixed16ArrayType [...]
  815. // Such a 3 x 3 chromatic adaptation matrix is organized as a 9-element array"
  816. if (auto type = m_tag_table.get(chromaticAdaptationTag); type.has_value()) {
  817. if (type.value()->type() != S15Fixed16ArrayTagData::Type)
  818. return Error::from_string_literal("ICC::Profile: chromaticAdaptationTag has unexpected type");
  819. if (static_cast<S15Fixed16ArrayTagData const&>(*type.value()).values().size() != 9)
  820. return Error::from_string_literal("ICC::Profile: chromaticAdaptationTag has unexpected size");
  821. }
  822. // ICC v4, 9.2.16 chromaticityTag
  823. // "Permitted tag types: chromaticityType"
  824. // FIXME
  825. // ICC v4, 9.2.17 cicpTag
  826. // "Permitted tag types: cicpType"
  827. // FIXME
  828. // ICC v4, 9.2.18 colorantOrderTag
  829. // "Permitted tag types: colorantOrderType"
  830. // FIXME
  831. // ICC v4, 9.2.19 colorantTableTag
  832. // "Permitted tag types: colorantTableType"
  833. // FIXME
  834. // ICC v4, 9.2.20 colorantTableOutTag
  835. // "Permitted tag types: colorantTableType"
  836. // FIXME
  837. // ICC v4, 9.2.21 colorimetricIntentImageStateTag
  838. // "Permitted tag types: signatureType"
  839. // FIXME
  840. // ICC v4, 9.2.22 copyrightTag
  841. // "Permitted tag types: multiLocalizedUnicodeType"
  842. // ICC v2, 6.4.13 copyrightTag
  843. // "Tag Type: textType"
  844. if (auto type = m_tag_table.get(copyrightTag); type.has_value()) {
  845. // The v4 spec requires multiLocalizedUnicodeType for this, but I'm aware of a single file
  846. // that still uses the v2 'text' type here: /System/Library/ColorSync/Profiles/ITU-2020.icc on macOS 13.1.
  847. // https://openradar.appspot.com/radar?id=5529765549178880
  848. bool has_v2_cprt_type_in_v4_file_quirk = id() == apple_itu_2020_id;
  849. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type && (!has_v2_cprt_type_in_v4_file_quirk || type.value()->type() != TextTagData::Type))
  850. return Error::from_string_literal("ICC::Profile: copyrightTag has unexpected v4 type");
  851. if (is_v2() && type.value()->type() != TextTagData::Type)
  852. return Error::from_string_literal("ICC::Profile: copyrightTag has unexpected v2 type");
  853. }
  854. // ICC v4, 9.2.23 deviceMfgDescTag
  855. // "Permitted tag types: multiLocalizedUnicodeType"
  856. // ICC v2, 6.4.15 deviceMfgDescTag
  857. // "Tag Type: textDescriptionType"
  858. if (auto type = m_tag_table.get(deviceMfgDescTag); type.has_value()) {
  859. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type)
  860. return Error::from_string_literal("ICC::Profile: deviceMfgDescTag has unexpected v4 type");
  861. if (is_v2() && type.value()->type() != TextDescriptionTagData::Type)
  862. return Error::from_string_literal("ICC::Profile: deviceMfgDescTag has unexpected v2 type");
  863. }
  864. // ICC v4, 9.2.24 deviceModelDescTag
  865. // "Permitted tag types: multiLocalizedUnicodeType"
  866. // ICC v2, 6.4.16 deviceModelDescTag
  867. // "Tag Type: textDescriptionType"
  868. if (auto type = m_tag_table.get(deviceModelDescTag); type.has_value()) {
  869. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type)
  870. return Error::from_string_literal("ICC::Profile: deviceModelDescTag has unexpected v4 type");
  871. if (is_v2() && type.value()->type() != TextDescriptionTagData::Type)
  872. return Error::from_string_literal("ICC::Profile: deviceModelDescTag has unexpected v2 type");
  873. }
  874. // ICC v4, 9.2.25 DToB0Tag
  875. // "Permitted tag types: multiProcessElementsType"
  876. // FIXME
  877. // ICC v4, 9.2.26 DToB1Tag
  878. // "Permitted tag types: multiProcessElementsType"
  879. // FIXME
  880. // ICC v4, 9.2.27 DToB2Tag
  881. // "Permitted tag types: multiProcessElementsType"
  882. // FIXME
  883. // ICC v4, 9.2.28 DToB3Tag
  884. // "Permitted tag types: multiProcessElementsType"
  885. // FIXME
  886. // ICC v4, 9.2.29 gamutTag
  887. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  888. // FIXME
  889. // ICC v4, 9.2.30 grayTRCTag
  890. // "Permitted tag types: curveType or parametricCurveType"
  891. // ICC v2, 6.4.19 grayTRCTag
  892. // "Tag Type: curveType"
  893. if (auto type = m_tag_table.get(grayTRCTag); type.has_value() && type.value()->type() != CurveTagData::Type && (is_v2() || type.value()->type() != ParametricCurveTagData::Type))
  894. return Error::from_string_literal("ICC::Profile: grayTRCTag has unexpected type");
  895. // ICC v4, 9.2.31 greenMatrixColumnTag
  896. // "Permitted tag types: XYZType
  897. // This tag contains the second column in the matrix, which is used in matrix/TRC transforms."
  898. // (Called greenColorantTag in the v2 spec, otherwise identical there.)
  899. if (auto type = m_tag_table.get(greenMatrixColumnTag); type.has_value()) {
  900. if (type.value()->type() != XYZTagData::Type)
  901. return Error::from_string_literal("ICC::Profile: greenMatrixColumnTag has unexpected type");
  902. if (static_cast<XYZTagData const&>(*type.value()).xyzs().size() != 1)
  903. return Error::from_string_literal("ICC::Profile: greenMatrixColumnTag has unexpected size");
  904. }
  905. // ICC v4, 9.2.32 greenTRCTag
  906. // "Permitted tag types: curveType or parametricCurveType"
  907. // ICC v2, 6.4.21 greenTRCTag
  908. // "Tag Type: curveType"
  909. if (auto type = m_tag_table.get(greenTRCTag); type.has_value() && type.value()->type() != CurveTagData::Type && (is_v2() || type.value()->type() != ParametricCurveTagData::Type))
  910. return Error::from_string_literal("ICC::Profile: greenTRCTag has unexpected type");
  911. // ICC v4, 9.2.33 luminanceTag
  912. // "Permitted tag types: XYZType"
  913. // This tag contains the absolute luminance of emissive devices in candelas per square metre as described by the
  914. // Y channel.
  915. // NOTE The X and Z values are set to zero."
  916. // ICC v2, 6.4.22 luminanceTag
  917. // "Absolute luminance of emissive devices in candelas per square meter as described by the Y channel. The
  918. // X and Z channels are ignored in all cases."
  919. if (auto type = m_tag_table.get(luminanceTag); type.has_value()) {
  920. if (type.value()->type() != XYZTagData::Type)
  921. return Error::from_string_literal("ICC::Profile: luminanceTag has unexpected type");
  922. auto& xyz_type = static_cast<XYZTagData const&>(*type.value());
  923. if (xyz_type.xyzs().size() != 1)
  924. return Error::from_string_literal("ICC::Profile: luminanceTag has unexpected size");
  925. if (is_v4() && xyz_type.xyzs()[0].x != 0)
  926. return Error::from_string_literal("ICC::Profile: luminanceTag.x unexpectedly not 0");
  927. if (is_v4() && xyz_type.xyzs()[0].z != 0)
  928. return Error::from_string_literal("ICC::Profile: luminanceTag.z unexpectedly not 0");
  929. }
  930. // ICC v4, 9.2.34 measurementTag
  931. // "Permitted tag types: measurementType"
  932. // FIXME
  933. // ICC v4, 9.2.35 metadataTag
  934. // "Permitted tag types: dictType"
  935. // FIXME
  936. // ICC v4, 9.2.36 mediaWhitePointTag
  937. // "Permitted tag types: XYZType
  938. // This tag, which is used for generating the ICC-absolute colorimetric intent, specifies the chromatically adapted
  939. // nCIEXYZ tristimulus values of the media white point. When the measurement data used to create the profile
  940. // were specified relative to an adopted white with a chromaticity different from that of the PCS adopted white, the
  941. // media white point nCIEXYZ values shall be adapted to be relative to the PCS adopted white chromaticity using
  942. // the chromaticAdaptationTag matrix, before recording in the tag. For capture devices, the media white point is
  943. // the encoding maximum white for the capture encoding. For displays, the values specified shall be those of the
  944. // PCS illuminant as defined in 7.2.16.
  945. // See Clause 6 and Annex A for a more complete description of the use of the media white point."
  946. // ICC v2, 6.4.25 mediaWhitePointTag
  947. // "This tag specifies the media white point and is used for generating ICC-absolute colorimetric intent. See
  948. // Annex A for a more complete description of its use."
  949. if (auto type = m_tag_table.get(mediaWhitePointTag); type.has_value()) {
  950. if (type.value()->type() != XYZTagData::Type)
  951. return Error::from_string_literal("ICC::Profile: mediaWhitePointTag has unexpected type");
  952. auto& xyz_type = static_cast<XYZTagData const&>(*type.value());
  953. if (xyz_type.xyzs().size() != 1)
  954. return Error::from_string_literal("ICC::Profile: mediaWhitePointTag has unexpected size");
  955. // V4 requires "For displays, the values specified shall be those of the PCS illuminant".
  956. // But in practice that's not always true. For example, on macOS 13.1, '/System/Library/ColorSync/Profiles/DCI(P3) RGB.icc'
  957. // has these values in the header: 0000F6D6 00010000 0000D32D
  958. // but these values in the tag: 0000F6D5 00010000 0000D32C
  959. // These are close, but not equal.
  960. // FIXME: File bug for these, and add id-based quirk instead.
  961. // if (is_v4() && device_class() == DeviceClass::DisplayDevice && xyz_type.xyzs()[0] != pcs_illuminant())
  962. // return Error::from_string_literal("ICC::Profile: mediaWhitePointTag for displays should be equal to PCS illuminant");
  963. }
  964. // ICC v4, 9.2.37 namedColor2Tag
  965. // "Permitted tag types: namedColor2Type"
  966. // FIXME
  967. // ICC v4, 9.2.38 outputResponseTag
  968. // "Permitted tag types: responseCurveSet16Type"
  969. // FIXME
  970. // ICC v4, 9.2.39 perceptualRenderingIntentGamutTag
  971. // "Permitted tag types: signatureType"
  972. // FIXME
  973. // ICC v4, 9.2.40 preview0Tag
  974. // "Permitted tag types: lut8Type or lut16Type or lutAToBType or lutBToAType"
  975. // FIXME
  976. // ICC v4, 9.2.41 preview1Tag
  977. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  978. // FIXME
  979. // ICC v4, 9.2.42 preview2Tag
  980. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  981. // FIXME
  982. // ICC v4, 9.2.43 profileDescriptionTag
  983. // "Permitted tag types: multiLocalizedUnicodeType"
  984. // ICC v2, 6.4.32 profileDescriptionTag
  985. // "Tag Type: textDescriptionType"
  986. if (auto type = m_tag_table.get(profileDescriptionTag); type.has_value()) {
  987. // The v4 spec requires multiLocalizedUnicodeType for this, but I'm aware of a single file
  988. // that still uses the v2 'desc' type here: /System/Library/ColorSync/Profiles/ITU-2020.icc on macOS 13.1.
  989. // https://openradar.appspot.com/radar?id=5529765549178880
  990. bool has_v2_desc_type_in_v4_file_quirk = id() == apple_itu_2020_id;
  991. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type && (!has_v2_desc_type_in_v4_file_quirk || type.value()->type() != TextDescriptionTagData::Type))
  992. return Error::from_string_literal("ICC::Profile: profileDescriptionTag has unexpected v4 type");
  993. if (is_v2() && type.value()->type() != TextDescriptionTagData::Type)
  994. return Error::from_string_literal("ICC::Profile: profileDescriptionTag has unexpected v2 type");
  995. }
  996. // ICC v4, 9.2.44 profileSequenceDescTag
  997. // "Permitted tag types: profileSequenceDescType"
  998. // FIXME
  999. // ICC v4, 9.2.45 profileSequenceIdentifierTag
  1000. // "Permitted tag types: profileSequenceIdentifierType"
  1001. // FIXME
  1002. // ICC v4, 9.2.46 redMatrixColumnTag
  1003. // "Permitted tag types: XYZType
  1004. // This tag contains the first column in the matrix, which is used in matrix/TRC transforms."
  1005. // (Called redColorantTag in the v2 spec, otherwise identical there.)
  1006. if (auto type = m_tag_table.get(redMatrixColumnTag); type.has_value()) {
  1007. if (type.value()->type() != XYZTagData::Type)
  1008. return Error::from_string_literal("ICC::Profile: redMatrixColumnTag has unexpected type");
  1009. if (static_cast<XYZTagData const&>(*type.value()).xyzs().size() != 1)
  1010. return Error::from_string_literal("ICC::Profile: redMatrixColumnTag has unexpected size");
  1011. }
  1012. // ICC v4, 9.2.47 redTRCTag
  1013. // "Permitted tag types: curveType or parametricCurveType"
  1014. // ICC v2, 6.4.41 redTRCTag
  1015. // "Tag Type: curveType"
  1016. if (auto type = m_tag_table.get(redTRCTag); type.has_value() && type.value()->type() != CurveTagData::Type && (is_v2() || type.value()->type() != ParametricCurveTagData::Type))
  1017. return Error::from_string_literal("ICC::Profile: redTRCTag has unexpected type");
  1018. // ICC v4, 9.2.48 saturationRenderingIntentGamutTag
  1019. // "Permitted tag types: signatureType"
  1020. // FIXME
  1021. // ICC v4, 9.2.49 technologyTag
  1022. // "Permitted tag types: signatureType"
  1023. // FIXME
  1024. // ICC v4, 9.2.50 viewingCondDescTag
  1025. // "Permitted tag types: multiLocalizedUnicodeType"
  1026. // ICC v2, 6.4.46 viewingCondDescTag
  1027. // "Tag Type: textDescriptionType"
  1028. if (auto type = m_tag_table.get(viewingCondDescTag); type.has_value()) {
  1029. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type)
  1030. return Error::from_string_literal("ICC::Profile: viewingCondDescTag has unexpected v4 type");
  1031. if (is_v2() && type.value()->type() != TextDescriptionTagData::Type)
  1032. return Error::from_string_literal("ICC::Profile: viewingCondDescTag has unexpected v2 type");
  1033. }
  1034. // ICC v4, 9.2.51 viewingConditionsTag
  1035. // "Permitted tag types: viewingConditionsType"
  1036. // FIXME
  1037. return {};
  1038. }
  1039. ErrorOr<NonnullRefPtr<Profile>> Profile::try_load_from_externally_owned_memory(ReadonlyBytes bytes)
  1040. {
  1041. auto profile = adopt_ref(*new Profile());
  1042. TRY(profile->read_header(bytes));
  1043. bytes = bytes.trim(profile->on_disk_size());
  1044. TRY(profile->read_tag_table(bytes));
  1045. TRY(profile->check_required_tags());
  1046. TRY(profile->check_tag_types());
  1047. return profile;
  1048. }
  1049. Crypto::Hash::MD5::DigestType Profile::compute_id(ReadonlyBytes bytes)
  1050. {
  1051. // ICC v4, 7.2.18 Profile ID field
  1052. // "The Profile ID shall be calculated using the MD5 fingerprinting method as defined in Internet RFC 1321.
  1053. // The entire profile, whose length is given by the size field in the header, with the
  1054. // profile flags field (bytes 44 to 47, see 7.2.11),
  1055. // rendering intent field (bytes 64 to 67, see 7.2.15),
  1056. // and profile ID field (bytes 84 to 99)
  1057. // in the profile header temporarily set to zeros (00h),
  1058. // shall be used to calculate the ID."
  1059. const u8 zero[16] = {};
  1060. Crypto::Hash::MD5 md5;
  1061. md5.update(bytes.slice(0, 44));
  1062. md5.update(ReadonlyBytes { zero, 4 }); // profile flags field
  1063. md5.update(bytes.slice(48, 64 - 48));
  1064. md5.update(ReadonlyBytes { zero, 4 }); // rendering intent field
  1065. md5.update(bytes.slice(68, 84 - 68));
  1066. md5.update(ReadonlyBytes { zero, 16 }); // profile ID field
  1067. md5.update(bytes.slice(100));
  1068. return md5.digest();
  1069. }
  1070. }