Profile.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  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(AssertSize<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. static int number_of_components_in_color_space(ColorSpace color_space)
  409. {
  410. switch (color_space) {
  411. case ColorSpace::Gray:
  412. return 1;
  413. case ColorSpace::TwoColor:
  414. return 2;
  415. case ColorSpace::nCIEXYZ:
  416. case ColorSpace::CIELAB:
  417. case ColorSpace::CIELUV:
  418. case ColorSpace::YCbCr:
  419. case ColorSpace::CIEYxy:
  420. case ColorSpace::RGB:
  421. case ColorSpace::HSV:
  422. case ColorSpace::HLS:
  423. case ColorSpace::CMY:
  424. case ColorSpace::ThreeColor:
  425. return 3;
  426. case ColorSpace::CMYK:
  427. case ColorSpace::FourColor:
  428. return 4;
  429. case ColorSpace::FiveColor:
  430. return 5;
  431. case ColorSpace::SixColor:
  432. return 6;
  433. case ColorSpace::SevenColor:
  434. return 7;
  435. case ColorSpace::EightColor:
  436. return 8;
  437. case ColorSpace::NineColor:
  438. return 9;
  439. case ColorSpace::TenColor:
  440. return 10;
  441. case ColorSpace::ElevenColor:
  442. return 11;
  443. case ColorSpace::TwelveColor:
  444. return 12;
  445. case ColorSpace::ThirteenColor:
  446. return 13;
  447. case ColorSpace::FourteenColor:
  448. return 14;
  449. case ColorSpace::FifteenColor:
  450. return 15;
  451. }
  452. VERIFY_NOT_REACHED();
  453. }
  454. StringView profile_connection_space_name(ColorSpace color_space)
  455. {
  456. switch (color_space) {
  457. case ColorSpace::PCSXYZ:
  458. return "PCSXYZ"sv;
  459. case ColorSpace::PCSLAB:
  460. return "PCSLAB"sv;
  461. default:
  462. return data_color_space_name(color_space);
  463. }
  464. }
  465. StringView primary_platform_name(PrimaryPlatform primary_platform)
  466. {
  467. switch (primary_platform) {
  468. case PrimaryPlatform::Apple:
  469. return "Apple"sv;
  470. case PrimaryPlatform::Microsoft:
  471. return "Microsoft"sv;
  472. case PrimaryPlatform::SiliconGraphics:
  473. return "Silicon Graphics"sv;
  474. case PrimaryPlatform::Sun:
  475. return "Sun"sv;
  476. }
  477. VERIFY_NOT_REACHED();
  478. }
  479. StringView rendering_intent_name(RenderingIntent rendering_intent)
  480. {
  481. switch (rendering_intent) {
  482. case RenderingIntent::Perceptual:
  483. return "Perceptual"sv;
  484. case RenderingIntent::MediaRelativeColorimetric:
  485. return "Media-relative colorimetric"sv;
  486. case RenderingIntent::Saturation:
  487. return "Saturation"sv;
  488. case RenderingIntent::ICCAbsoluteColorimetric:
  489. return "ICC-absolute colorimetric"sv;
  490. }
  491. VERIFY_NOT_REACHED();
  492. }
  493. Flags::Flags() = default;
  494. Flags::Flags(u32 bits)
  495. : m_bits(bits)
  496. {
  497. }
  498. DeviceAttributes::DeviceAttributes() = default;
  499. DeviceAttributes::DeviceAttributes(u64 bits)
  500. : m_bits(bits)
  501. {
  502. }
  503. static ErrorOr<ProfileHeader> read_header(ReadonlyBytes bytes)
  504. {
  505. if (bytes.size() < sizeof(ICCHeader))
  506. return Error::from_string_literal("ICC::Profile: Not enough data for header");
  507. ProfileHeader header;
  508. auto raw_header = *bit_cast<ICCHeader const*>(bytes.data());
  509. TRY(parse_file_signature(raw_header));
  510. header.on_disk_size = TRY(parse_size(raw_header, bytes));
  511. header.preferred_cmm_type = parse_preferred_cmm_type(raw_header);
  512. header.version = TRY(parse_version(raw_header));
  513. header.device_class = TRY(parse_device_class(raw_header));
  514. header.data_color_space = TRY(parse_data_color_space(raw_header));
  515. header.connection_space = TRY(parse_connection_space(raw_header));
  516. header.creation_timestamp = TRY(parse_creation_date_time(raw_header));
  517. header.primary_platform = TRY(parse_primary_platform(raw_header));
  518. header.flags = Flags { raw_header.profile_flags };
  519. header.device_manufacturer = parse_device_manufacturer(raw_header);
  520. header.device_model = parse_device_model(raw_header);
  521. header.device_attributes = TRY(parse_device_attributes(raw_header));
  522. header.rendering_intent = TRY(parse_rendering_intent(raw_header));
  523. header.pcs_illuminant = TRY(parse_pcs_illuminant(raw_header));
  524. header.creator = parse_profile_creator(raw_header);
  525. header.id = TRY(parse_profile_id(raw_header, bytes));
  526. TRY(parse_reserved(raw_header));
  527. return header;
  528. }
  529. static ErrorOr<NonnullRefPtr<TagData>> read_tag(ReadonlyBytes bytes, u32 offset_to_beginning_of_tag_data_element, u32 size_of_tag_data_element)
  530. {
  531. // "All tag data elements shall start on a 4-byte boundary (relative to the start of the profile data stream)"
  532. if (offset_to_beginning_of_tag_data_element % 4 != 0)
  533. return Error::from_string_literal("ICC::Profile: Tag data not aligned");
  534. if (offset_to_beginning_of_tag_data_element + size_of_tag_data_element > bytes.size())
  535. return Error::from_string_literal("ICC::Profile: Tag data out of bounds");
  536. auto tag_bytes = bytes.slice(offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  537. // ICC v4, 9 Tag definitions
  538. // ICC v4, 9.1 General
  539. // "All tags, including private tags, have as their first four bytes a tag signature to identify to profile readers
  540. // what kind of data is contained within a tag."
  541. if (tag_bytes.size() < sizeof(u32))
  542. return Error::from_string_literal("ICC::Profile: Not enough data for tag type");
  543. auto type = tag_type(tag_bytes);
  544. switch (type) {
  545. case ChromaticityTagData::Type:
  546. return ChromaticityTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  547. case CicpTagData::Type:
  548. return CicpTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  549. case CurveTagData::Type:
  550. return CurveTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  551. case Lut16TagData::Type:
  552. return Lut16TagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  553. case Lut8TagData::Type:
  554. return Lut8TagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  555. case LutAToBTagData::Type:
  556. return LutAToBTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  557. case LutBToATagData::Type:
  558. return LutBToATagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  559. case MeasurementTagData::Type:
  560. return MeasurementTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  561. case MultiLocalizedUnicodeTagData::Type:
  562. return MultiLocalizedUnicodeTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  563. case NamedColor2TagData::Type:
  564. return NamedColor2TagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  565. case ParametricCurveTagData::Type:
  566. return ParametricCurveTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  567. case S15Fixed16ArrayTagData::Type:
  568. return S15Fixed16ArrayTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  569. case SignatureTagData::Type:
  570. return SignatureTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  571. case TextDescriptionTagData::Type:
  572. return TextDescriptionTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  573. case TextTagData::Type:
  574. return TextTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  575. case ViewingConditionsTagData::Type:
  576. return ViewingConditionsTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  577. case XYZTagData::Type:
  578. return XYZTagData::from_bytes(tag_bytes, offset_to_beginning_of_tag_data_element, size_of_tag_data_element);
  579. default:
  580. // FIXME: optionally ignore tags of unknown type
  581. return try_make_ref_counted<UnknownTagData>(offset_to_beginning_of_tag_data_element, size_of_tag_data_element, type);
  582. }
  583. }
  584. static ErrorOr<OrderedHashMap<TagSignature, NonnullRefPtr<TagData>>> read_tag_table(ReadonlyBytes bytes)
  585. {
  586. OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> tag_table;
  587. // ICC v4, 7.3 Tag table
  588. // ICC v4, 7.3.1 Overview
  589. // "The tag table acts as a table of contents for the tags and an index into the tag data element in the profiles. It
  590. // 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-
  591. // byte entries with one entry for each tag. The tag table therefore contains 4+12n bytes where n is the number of
  592. // tags contained in the profile. The entries for the tags within the table are not required to be in any particular
  593. // order nor are they required to match the sequence of tag data element within the profile.
  594. // Each 12-byte tag entry following the tag count shall consist of a 4-byte tag signature, a 4-byte offset to define
  595. // the beginning of the tag data element, and a 4-byte entry identifying the length of the tag data element in bytes.
  596. // [...]
  597. // The tag table shall define a contiguous sequence of unique tag elements, with no gaps between the last byte
  598. // of any tag data element referenced from the tag table (inclusive of any necessary additional pad bytes required
  599. // to reach a four-byte boundary) and the byte offset of the following tag element, or the end of the file.
  600. // Duplicate tag signatures shall not be included in the tag table.
  601. // Tag data elements shall not partially overlap, so there shall be no part of any tag data element that falls within
  602. // the range defined for another tag in the tag table."
  603. ReadonlyBytes tag_table_bytes = bytes.slice(sizeof(ICCHeader));
  604. if (tag_table_bytes.size() < sizeof(u32))
  605. return Error::from_string_literal("ICC::Profile: Not enough data for tag count");
  606. auto tag_count = *bit_cast<BigEndian<u32> const*>(tag_table_bytes.data());
  607. // ICC V4, 7.3 Tag table, Table 24 - Tag table structure
  608. struct TagTableEntry {
  609. BigEndian<TagSignature> tag_signature;
  610. BigEndian<u32> offset_to_beginning_of_tag_data_element;
  611. BigEndian<u32> size_of_tag_data_element;
  612. };
  613. static_assert(AssertSize<TagTableEntry, 12>());
  614. tag_table_bytes = tag_table_bytes.slice(sizeof(u32));
  615. if (tag_table_bytes.size() < tag_count * sizeof(TagTableEntry))
  616. return Error::from_string_literal("ICC::Profile: Not enough data for tag table entries");
  617. auto tag_table_entries = bit_cast<TagTableEntry const*>(tag_table_bytes.data());
  618. // "The tag table may contain multiple tags signatures that all reference the same tag data element offset, allowing
  619. // efficient reuse of tag data elements."
  620. HashMap<u32, NonnullRefPtr<TagData>> offset_to_tag_data;
  621. for (u32 i = 0; i < tag_count; ++i) {
  622. // FIXME: optionally ignore tags with unknown signature
  623. // Dedupe identical offset/sizes.
  624. NonnullRefPtr<TagData> tag_data = TRY(offset_to_tag_data.try_ensure(tag_table_entries[i].offset_to_beginning_of_tag_data_element, [&]() {
  625. return read_tag(bytes, tag_table_entries[i].offset_to_beginning_of_tag_data_element, tag_table_entries[i].size_of_tag_data_element);
  626. }));
  627. // "In such cases, both the offset and size of the tag data elements in the tag table shall be the same."
  628. if (tag_data->size() != tag_table_entries[i].size_of_tag_data_element)
  629. return Error::from_string_literal("ICC::Profile: two tags have same offset but different sizes");
  630. // "Duplicate tag signatures shall not be included in the tag table."
  631. if (TRY(tag_table.try_set(tag_table_entries[i].tag_signature, move(tag_data))) != AK::HashSetResult::InsertedNewEntry)
  632. return Error::from_string_literal("ICC::Profile: duplicate tag signature");
  633. }
  634. return tag_table;
  635. }
  636. static bool is_xCLR(ColorSpace color_space)
  637. {
  638. switch (color_space) {
  639. case ColorSpace::TwoColor:
  640. case ColorSpace::ThreeColor:
  641. case ColorSpace::FourColor:
  642. case ColorSpace::FiveColor:
  643. case ColorSpace::SixColor:
  644. case ColorSpace::SevenColor:
  645. case ColorSpace::EightColor:
  646. case ColorSpace::NineColor:
  647. case ColorSpace::TenColor:
  648. case ColorSpace::ElevenColor:
  649. case ColorSpace::TwelveColor:
  650. case ColorSpace::ThirteenColor:
  651. case ColorSpace::FourteenColor:
  652. case ColorSpace::FifteenColor:
  653. return true;
  654. default:
  655. return false;
  656. }
  657. }
  658. ErrorOr<void> Profile::check_required_tags()
  659. {
  660. // ICC v4, 8 Required tags
  661. // ICC v4, 8.2 Common requirements
  662. // "With the exception of DeviceLink profiles, all profiles shall contain the following tags:
  663. // - profileDescriptionTag (see 9.2.41);
  664. // - copyrightTag (see 9.2.21);
  665. // - mediaWhitePointTag (see 9.2.34);
  666. // - chromaticAdaptationTag, when the measurement data used to calculate the profile was specified for an
  667. // adopted white with a chromaticity different from that of the PCS adopted white (see 9.2.15).
  668. // NOTE A DeviceLink profile is not required to have either a mediaWhitePointTag or a chromaticAdaptationTag."
  669. // profileDescriptionTag, copyrightTag are required for DeviceLink too (see ICC v4, 8.6 DeviceLink profile).
  670. // profileDescriptionTag, copyrightTag, mediaWhitePointTag are required in ICC v2 as well.
  671. // chromaticAdaptationTag isn't required in v2 profiles as far as I can tell.
  672. if (!m_tag_table.contains(profileDescriptionTag))
  673. return Error::from_string_literal("ICC::Profile: required profileDescriptionTag is missing");
  674. if (!m_tag_table.contains(copyrightTag))
  675. return Error::from_string_literal("ICC::Profile: required copyrightTag is missing");
  676. if (device_class() != DeviceClass::DeviceLink) {
  677. if (!m_tag_table.contains(mediaWhitePointTag))
  678. return Error::from_string_literal("ICC::Profile: required mediaWhitePointTag is missing");
  679. // FIXME: Check for chromaticAdaptationTag after figuring out when exactly it needs to be present.
  680. }
  681. auto has_tag = [&](auto& tag) { return m_tag_table.contains(tag); };
  682. auto has_all_tags = [&]<class T>(T tags) { return all_of(tags, has_tag); };
  683. switch (device_class()) {
  684. case DeviceClass::InputDevice: {
  685. // ICC v4, 8.3 Input profiles
  686. // "8.3.1 General
  687. // Input profiles are generally used with devices such as scanners and digital cameras. The types of profiles
  688. // available for use as Input profiles are N-component LUT-based, Three-component matrix-based, and
  689. // monochrome.
  690. // 8.3.2 N-component LUT-based Input profiles
  691. // In addition to the tags listed in 8.2 an N-component LUT-based Input profile shall contain the following tag:
  692. // - AToB0Tag (see 9.2.1).
  693. // 8.3.3 Three-component matrix-based Input profiles
  694. // In addition to the tags listed in 8.2, a three-component matrix-based Input profile shall contain the following tags:
  695. // - redMatrixColumnTag (see 9.2.44);
  696. // - greenMatrixColumnTag (see 9.2.30);
  697. // - blueMatrixColumnTag (see 9.2.4);
  698. // - redTRCTag (see 9.2.45);
  699. // - greenTRCTag (see 9.2.31);
  700. // - blueTRCTag (see 9.2.5).
  701. // [...] Only the PCSXYZ encoding can be used with matrix/TRC models.
  702. // 8.3.4 Monochrome Input profiles
  703. // In addition to the tags listed in 8.2, a monochrome Input profile shall contain the following tag:
  704. // - grayTRCTag (see 9.2.29).
  705. bool has_n_component_lut_based_tags = has_tag(AToB0Tag);
  706. bool has_three_component_matrix_based_tags = has_all_tags(Array { redMatrixColumnTag, greenMatrixColumnTag, blueMatrixColumnTag, redTRCTag, greenTRCTag, blueTRCTag });
  707. bool has_monochrome_tags = has_tag(grayTRCTag);
  708. if (!has_n_component_lut_based_tags && !has_three_component_matrix_based_tags && !has_monochrome_tags)
  709. return Error::from_string_literal("ICC::Profile: InputDevice required tags are missing");
  710. if (!has_n_component_lut_based_tags && has_three_component_matrix_based_tags && connection_space() != ColorSpace::PCSXYZ)
  711. return Error::from_string_literal("ICC::Profile: InputDevice three-component matrix-based profile must use PCSXYZ");
  712. break;
  713. }
  714. case DeviceClass::DisplayDevice: {
  715. // ICC v4, 8.4 Display profiles
  716. // "8.4.1 General
  717. // This class of profiles represents display devices such as monitors. The types of profiles available for use as
  718. // Display profiles are N-component LUT-based, Three-component matrix-based, and monochrome.
  719. // 8.4.2 N-Component LUT-based Display profiles
  720. // In addition to the tags listed in 8.2 an N-component LUT-based Input profile shall contain the following tags:
  721. // - AToB0Tag (see 9.2.1);
  722. // - BToA0Tag (see 9.2.6).
  723. // 8.4.3 Three-component matrix-based Display profiles
  724. // In addition to the tags listed in 8.2, a three-component matrix-based Display profile shall contain the following
  725. // tags:
  726. // - redMatrixColumnTag (see 9.2.44);
  727. // - greenMatrixColumnTag (see 9.2.30);
  728. // - blueMatrixColumnTag (see 9.2.4);
  729. // - redTRCTag (see 9.2.45);
  730. // - greenTRCTag (see 9.2.31);
  731. // - blueTRCTag (see 9.2.5).
  732. // [...] Only the PCSXYZ encoding can be used with matrix/TRC models.
  733. // 8.4.4 Monochrome Display profiles
  734. // In addition to the tags listed in 8.2 a monochrome Display profile shall contain the following tag:
  735. // - grayTRCTag (see 9.2.29)."
  736. bool has_n_component_lut_based_tags = has_all_tags(Array { AToB0Tag, BToA0Tag });
  737. bool has_three_component_matrix_based_tags = has_all_tags(Array { redMatrixColumnTag, greenMatrixColumnTag, blueMatrixColumnTag, redTRCTag, greenTRCTag, blueTRCTag });
  738. bool has_monochrome_tags = has_tag(grayTRCTag);
  739. if (!has_n_component_lut_based_tags && !has_three_component_matrix_based_tags && !has_monochrome_tags)
  740. return Error::from_string_literal("ICC::Profile: DisplayDevice required tags are missing");
  741. if (!has_n_component_lut_based_tags && has_three_component_matrix_based_tags && connection_space() != ColorSpace::PCSXYZ)
  742. return Error::from_string_literal("ICC::Profile: DisplayDevice three-component matrix-based profile must use PCSXYZ");
  743. break;
  744. }
  745. case DeviceClass::OutputDevice: {
  746. // ICC v4, 8.5 Output profiles
  747. // "8.5.1 General
  748. // Output profiles are used to support devices such as printers and film recorders. The types of profiles available
  749. // for use as Output profiles are N-component LUT-based and Monochrome.
  750. // 8.5.2 N-component LUT-based Output profiles
  751. // In addition to the tags listed in 8.2 an N-component LUT-based Output profile shall contain the following tags:
  752. // - AToB0Tag (see 9.2.1);
  753. // - AToB1Tag (see 9.2.2);
  754. // - AToB2Tag (see 9.2.3);
  755. // - BToA0Tag (see 9.2.6);
  756. // - BToA1Tag (see 9.2.7);
  757. // - BToA2Tag (see 9.2.8);
  758. // - gamutTag (see 9.2.28);
  759. // - colorantTableTag (see 9.2.18), for the xCLR colour spaces (see 7.2.6)
  760. // 8.5.3 Monochrome Output profiles
  761. // In addition to the tags listed in 8.2 a monochrome Output profile shall contain the following tag:
  762. // - grayTRCTag (see 9.2.29)."
  763. // The colorantTableTag requirement is new in v4.
  764. Vector<TagSignature, 8> required_n_component_lut_based_tags = { AToB0Tag, AToB1Tag, AToB2Tag, BToA0Tag, BToA1Tag, BToA2Tag, gamutTag };
  765. if (is_v4() && is_xCLR(connection_space()))
  766. required_n_component_lut_based_tags.append(colorantTableTag);
  767. bool has_n_component_lut_based_tags = has_all_tags(required_n_component_lut_based_tags);
  768. bool has_monochrome_tags = has_tag(grayTRCTag);
  769. if (!has_n_component_lut_based_tags && !has_monochrome_tags)
  770. return Error::from_string_literal("ICC::Profile: OutputDevice required tags are missing");
  771. break;
  772. }
  773. case DeviceClass::DeviceLink: {
  774. // ICC v4, 8.6 DeviceLink profile
  775. // "A DeviceLink profile shall contain the following tags:
  776. // - profileDescriptionTag (see 9.2.41);
  777. // - copyrightTag (see 9.2.21);
  778. // - profileSequenceDescTag (see 9.2.42);
  779. // - AToB0Tag (see 9.2.1);
  780. // - colorantTableTag (see 9.2.18) which is required only if the data colour space field is xCLR, where x is
  781. // hexadecimal 2 to F (see 7.2.6);
  782. // - colorantTableOutTag (see 9.2.19), required only if the PCS field is xCLR, where x is hexadecimal 2 to F
  783. // (see 7.2.6)"
  784. // profileDescriptionTag and copyrightTag are already checked above, in the code for section 8.2.
  785. Vector<TagSignature, 4> required_tags = { profileSequenceDescTag, AToB0Tag };
  786. if (is_v4() && is_xCLR(connection_space())) { // This requirement is new in v4.
  787. required_tags.append(colorantTableTag);
  788. required_tags.append(colorantTableOutTag);
  789. }
  790. if (!has_all_tags(required_tags))
  791. return Error::from_string_literal("ICC::Profile: DeviceLink required tags are missing");
  792. // "The data colour space field (see 7.2.6) in the DeviceLink profile will be the same as the data colour space field
  793. // of the first profile in the sequence used to construct the device link. The PCS field (see 7.2.7) will be the same
  794. // as the data colour space field of the last profile in the sequence."
  795. // FIXME: Check that if profileSequenceDescType parsing is implemented.
  796. break;
  797. }
  798. case DeviceClass::ColorSpace:
  799. // ICC v4, 8.7 ColorSpace profile
  800. // "In addition to the tags listed in 8.2, a ColorSpace profile shall contain the following tags:
  801. // - BToA0Tag (see 9.2.6);
  802. // - AToB0Tag (see 9.2.1).
  803. // [...] ColorSpace profiles may be embedded in images."
  804. if (!has_all_tags(Array { AToB0Tag, BToA0Tag }))
  805. return Error::from_string_literal("ICC::Profile: ColorSpace required tags are missing");
  806. break;
  807. case DeviceClass::Abstract:
  808. // ICC v4, 8.8 Abstract profile
  809. // "In addition to the tags listed in 8.2, an Abstract profile shall contain the following tag:
  810. // - AToB0Tag (see 9.2.1).
  811. // [...] Abstract profiles cannot be embedded in images."
  812. if (!has_tag(AToB0Tag))
  813. return Error::from_string_literal("ICC::Profile: Abstract required AToB0Tag is missing");
  814. break;
  815. case DeviceClass::NamedColor:
  816. // ICC v4, 8.9 NamedColor profile
  817. // "In addition to the tags listed in 8.2, a NamedColor profile shall contain the following tag:
  818. // - namedColor2Tag (see 9.2.35)."
  819. if (!has_tag(namedColor2Tag))
  820. return Error::from_string_literal("ICC::Profile: NamedColor required namedColor2Tag is missing");
  821. break;
  822. }
  823. return {};
  824. }
  825. ErrorOr<void> Profile::check_tag_types()
  826. {
  827. // This uses m_tag_table.get() even for tags that are guaranteed to exist after check_required_tags()
  828. // so that the two functions can be called in either order.
  829. // Profile ID of /System/Library/ColorSync/Profiles/ITU-2020.icc on macOS 13.1.
  830. 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 };
  831. // Profile ID of the "Display P3" profiles embedded in the images on https://webkit.org/blog-files/color-gamut/comparison.html
  832. // (The macOS 13.1 /System/Library/ColorSync/Profiles/Display\ P3.icc file no longer has this quirk.)
  833. static constexpr Crypto::Hash::MD5::DigestType apple_p3_2015_id = { 0xe5, 0xbb, 0x0e, 0x98, 0x67, 0xbd, 0x46, 0xcd, 0x4b, 0xbe, 0x44, 0x6e, 0xbd, 0x1b, 0x75, 0x98 };
  834. auto has_type = [&](auto tag, std::initializer_list<TagTypeSignature> types, std::initializer_list<TagTypeSignature> v4_types) {
  835. if (auto type = m_tag_table.get(tag); type.has_value()) {
  836. auto type_matches = [&](auto wanted_type) { return type.value()->type() == wanted_type; };
  837. return any_of(types, type_matches) || (is_v4() && any_of(v4_types, type_matches));
  838. }
  839. return true;
  840. };
  841. // ICC v4, 9.2.1 AToB0Tag
  842. // "Permitted tag types: lut8Type or lut16Type or lutAToBType"
  843. // ICC v2, 6.4.1 AToB0Tag
  844. // "Tag Type: lut8Type or lut16Type"
  845. if (!has_type(AToB0Tag, { Lut8TagData::Type, Lut16TagData::Type }, { LutAToBTagData::Type }))
  846. return Error::from_string_literal("ICC::Profile: AToB0Tag has unexpected type");
  847. // ICC v4, 9.2.2 AToB1Tag
  848. // "Permitted tag types: lut8Type or lut16Type or lutAToBType"
  849. // ICC v2, 6.4.2 AToB1Tag
  850. // "Tag Type: lut8Type or lut16Type"
  851. if (!has_type(AToB1Tag, { Lut8TagData::Type, Lut16TagData::Type }, { LutAToBTagData::Type }))
  852. return Error::from_string_literal("ICC::Profile: AToB1Tag has unexpected type");
  853. // ICC v4, 9.2.3 AToB2Tag
  854. // "Permitted tag types: lut8Type or lut16Type or lutAToBType"
  855. // ICC v2, 6.4.3 AToB2Tag
  856. // "Tag Type: lut8Type or lut16Type"
  857. if (!has_type(AToB2Tag, { Lut8TagData::Type, Lut16TagData::Type }, { LutAToBTagData::Type }))
  858. return Error::from_string_literal("ICC::Profile: AToB2Tag has unexpected type");
  859. // ICC v4, 9.2.4 blueMatrixColumnTag
  860. // "Permitted tag types: XYZType
  861. // This tag contains the third column in the matrix used in matrix/TRC transforms."
  862. // (Called blueColorantTag in the v2 spec, otherwise identical there.)
  863. if (auto type = m_tag_table.get(blueMatrixColumnTag); type.has_value()) {
  864. if (type.value()->type() != XYZTagData::Type)
  865. return Error::from_string_literal("ICC::Profile: blueMatrixColumnTag has unexpected type");
  866. if (static_cast<XYZTagData const&>(*type.value()).xyzs().size() != 1)
  867. return Error::from_string_literal("ICC::Profile: blueMatrixColumnTag has unexpected size");
  868. }
  869. // ICC v4, 9.2.5 blueTRCTag
  870. // "Permitted tag types: curveType or parametricCurveType"
  871. // ICC v2, 6.4.5 blueTRCTag
  872. // "Tag Type: curveType"
  873. if (!has_type(blueTRCTag, { CurveTagData::Type }, { ParametricCurveTagData::Type }))
  874. return Error::from_string_literal("ICC::Profile: blueTRCTag has unexpected type");
  875. // ICC v4, 9.2.6 BToA0Tag
  876. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  877. // ICC v2, 6.4.6 BToA0Tag
  878. // "Tag Type: lut8Type or lut16Type"
  879. if (!has_type(BToA0Tag, { Lut8TagData::Type, Lut16TagData::Type }, { LutBToATagData::Type }))
  880. return Error::from_string_literal("ICC::Profile: BToA0Tag has unexpected type");
  881. // ICC v4, 9.2.7 BToA1Tag
  882. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  883. // ICC v2, 6.4.7 BToA1Tag
  884. // "Tag Type: lut8Type or lut16Type"
  885. if (!has_type(BToA1Tag, { Lut8TagData::Type, Lut16TagData::Type }, { LutBToATagData::Type }))
  886. return Error::from_string_literal("ICC::Profile: BToA1Tag has unexpected type");
  887. // ICC v4, 9.2.8 BToA2Tag
  888. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  889. // ICC v2, 6.4.8 BToA2Tag
  890. // "Tag Type: lut8Type or lut16Type"
  891. if (!has_type(BToA2Tag, { Lut8TagData::Type, Lut16TagData::Type }, { LutBToATagData::Type }))
  892. return Error::from_string_literal("ICC::Profile: BToA2Tag has unexpected type");
  893. // ICC v4, 9.2.9 BToD0Tag
  894. // "Permitted tag types: multiProcessElementsType"
  895. // FIXME
  896. // ICC v4, 9.2.10 BToD1Tag
  897. // "Permitted tag types: multiProcessElementsType"
  898. // FIXME
  899. // ICC v4, 9.2.11 BToD2Tag
  900. // "Permitted tag types: multiProcessElementsType"
  901. // FIXME
  902. // ICC v4, 9.2.12 BToD3Tag
  903. // "Permitted tag types: multiProcessElementsType"
  904. // FIXME
  905. // ICC v4, 9.2.13 calibrationDateTimeTag
  906. // "Permitted tag types: dateTimeType"
  907. // FIXME
  908. // ICC v4, 9.2.14 charTargetTag
  909. // "Permitted tag types: textType"
  910. if (!has_type(charTargetTag, { TextTagData::Type }, {}))
  911. return Error::from_string_literal("ICC::Profile: charTargetTag has unexpected type");
  912. // ICC v4, 9.2.15 chromaticAdaptationTag
  913. // "Permitted tag types: s15Fixed16ArrayType [...]
  914. // Such a 3 x 3 chromatic adaptation matrix is organized as a 9-element array"
  915. if (auto type = m_tag_table.get(chromaticAdaptationTag); type.has_value()) {
  916. if (type.value()->type() != S15Fixed16ArrayTagData::Type)
  917. return Error::from_string_literal("ICC::Profile: chromaticAdaptationTag has unexpected type");
  918. if (static_cast<S15Fixed16ArrayTagData const&>(*type.value()).values().size() != 9)
  919. return Error::from_string_literal("ICC::Profile: chromaticAdaptationTag has unexpected size");
  920. }
  921. // ICC v4, 9.2.16 chromaticityTag
  922. // "Permitted tag types: chromaticityType"
  923. if (!has_type(chromaticityTag, { ChromaticityTagData::Type }, {}))
  924. return Error::from_string_literal("ICC::Profile: ChromaticityTagData has unexpected type");
  925. // ICC v4, 9.2.17 cicpTag
  926. // "Permitted tag types: cicpType"
  927. if (auto type = m_tag_table.get(cicpTag); type.has_value()) {
  928. if (type.value()->type() != CicpTagData::Type)
  929. return Error::from_string_literal("ICC::Profile: cicpTag has unexpected type");
  930. // "The colour encoding specified by the CICP tag content shall be equivalent to the data colour space encoding
  931. // represented by this ICC profile.
  932. // NOTE The ICC colour transform cannot match every possible rendering of a CICP colour encoding."
  933. // FIXME: Figure out what that means and check for it.
  934. // "This tag may be present when the data colour space in the profile header is RGB, YCbCr, or XYZ, and the
  935. // profile class in the profile header is Input or Display. The tag shall not be present for other data colour spaces
  936. // or profile classes indicated in the profile header."
  937. bool is_color_space_allowed = data_color_space() == ColorSpace::RGB || data_color_space() == ColorSpace::YCbCr || data_color_space() == ColorSpace::nCIEXYZ;
  938. bool is_profile_class_allowed = device_class() == DeviceClass::InputDevice || device_class() == DeviceClass::DisplayDevice;
  939. bool cicp_is_allowed = is_color_space_allowed && is_profile_class_allowed;
  940. if (!cicp_is_allowed)
  941. return Error::from_string_literal("ICC::Profile: cicpTag present but not allowed");
  942. }
  943. // ICC v4, 9.2.18 colorantOrderTag
  944. // "Permitted tag types: colorantOrderType"
  945. // FIXME
  946. // ICC v4, 9.2.19 colorantTableTag
  947. // "Permitted tag types: colorantTableType"
  948. // FIXME
  949. // ICC v4, 9.2.20 colorantTableOutTag
  950. // "Permitted tag types: colorantTableType"
  951. // FIXME
  952. // ICC v4, 9.2.21 colorimetricIntentImageStateTag
  953. // "Permitted tag types: signatureType"
  954. if (!has_type(colorimetricIntentImageStateTag, { SignatureTagData::Type }, {}))
  955. return Error::from_string_literal("ICC::Profile: colorimetricIntentImageStateTag has unexpected type");
  956. // ICC v4, 9.2.22 copyrightTag
  957. // "Permitted tag types: multiLocalizedUnicodeType"
  958. // ICC v2, 6.4.13 copyrightTag
  959. // "Tag Type: textType"
  960. if (auto type = m_tag_table.get(copyrightTag); type.has_value()) {
  961. // The v4 spec requires multiLocalizedUnicodeType for this, but I'm aware of a single file
  962. // that still uses the v2 'text' type here: /System/Library/ColorSync/Profiles/ITU-2020.icc on macOS 13.1.
  963. // https://openradar.appspot.com/radar?id=5529765549178880
  964. bool has_v2_cprt_type_in_v4_file_quirk = id() == apple_itu_2020_id || id() == apple_p3_2015_id;
  965. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type && (!has_v2_cprt_type_in_v4_file_quirk || type.value()->type() != TextTagData::Type))
  966. return Error::from_string_literal("ICC::Profile: copyrightTag has unexpected v4 type");
  967. if (is_v2() && type.value()->type() != TextTagData::Type)
  968. return Error::from_string_literal("ICC::Profile: copyrightTag has unexpected v2 type");
  969. }
  970. // ICC v4, 9.2.23 deviceMfgDescTag
  971. // "Permitted tag types: multiLocalizedUnicodeType"
  972. // ICC v2, 6.4.15 deviceMfgDescTag
  973. // "Tag Type: textDescriptionType"
  974. if (auto type = m_tag_table.get(deviceMfgDescTag); type.has_value()) {
  975. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type)
  976. return Error::from_string_literal("ICC::Profile: deviceMfgDescTag has unexpected v4 type");
  977. if (is_v2() && type.value()->type() != TextDescriptionTagData::Type)
  978. return Error::from_string_literal("ICC::Profile: deviceMfgDescTag has unexpected v2 type");
  979. }
  980. // ICC v4, 9.2.24 deviceModelDescTag
  981. // "Permitted tag types: multiLocalizedUnicodeType"
  982. // ICC v2, 6.4.16 deviceModelDescTag
  983. // "Tag Type: textDescriptionType"
  984. if (auto type = m_tag_table.get(deviceModelDescTag); type.has_value()) {
  985. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type)
  986. return Error::from_string_literal("ICC::Profile: deviceModelDescTag has unexpected v4 type");
  987. if (is_v2() && type.value()->type() != TextDescriptionTagData::Type)
  988. return Error::from_string_literal("ICC::Profile: deviceModelDescTag has unexpected v2 type");
  989. }
  990. // ICC v4, 9.2.25 DToB0Tag
  991. // "Permitted tag types: multiProcessElementsType"
  992. // FIXME
  993. // ICC v4, 9.2.26 DToB1Tag
  994. // "Permitted tag types: multiProcessElementsType"
  995. // FIXME
  996. // ICC v4, 9.2.27 DToB2Tag
  997. // "Permitted tag types: multiProcessElementsType"
  998. // FIXME
  999. // ICC v4, 9.2.28 DToB3Tag
  1000. // "Permitted tag types: multiProcessElementsType"
  1001. // FIXME
  1002. // ICC v4, 9.2.29 gamutTag
  1003. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  1004. // ICC v2, 6.4.18 gamutTag
  1005. // "Tag Type: lut8Type or lut16Type"
  1006. if (!has_type(gamutTag, { Lut8TagData::Type, Lut16TagData::Type }, { LutBToATagData::Type }))
  1007. return Error::from_string_literal("ICC::Profile: gamutTag has unexpected type");
  1008. // ICC v4, 9.2.30 grayTRCTag
  1009. // "Permitted tag types: curveType or parametricCurveType"
  1010. // ICC v2, 6.4.19 grayTRCTag
  1011. // "Tag Type: curveType"
  1012. if (!has_type(grayTRCTag, { CurveTagData::Type }, { ParametricCurveTagData::Type }))
  1013. return Error::from_string_literal("ICC::Profile: grayTRCTag has unexpected type");
  1014. // ICC v4, 9.2.31 greenMatrixColumnTag
  1015. // "Permitted tag types: XYZType
  1016. // This tag contains the second column in the matrix, which is used in matrix/TRC transforms."
  1017. // (Called greenColorantTag in the v2 spec, otherwise identical there.)
  1018. if (auto type = m_tag_table.get(greenMatrixColumnTag); type.has_value()) {
  1019. if (type.value()->type() != XYZTagData::Type)
  1020. return Error::from_string_literal("ICC::Profile: greenMatrixColumnTag has unexpected type");
  1021. if (static_cast<XYZTagData const&>(*type.value()).xyzs().size() != 1)
  1022. return Error::from_string_literal("ICC::Profile: greenMatrixColumnTag has unexpected size");
  1023. }
  1024. // ICC v4, 9.2.32 greenTRCTag
  1025. // "Permitted tag types: curveType or parametricCurveType"
  1026. // ICC v2, 6.4.21 greenTRCTag
  1027. // "Tag Type: curveType"
  1028. if (!has_type(greenTRCTag, { CurveTagData::Type }, { ParametricCurveTagData::Type }))
  1029. return Error::from_string_literal("ICC::Profile: greenTRCTag has unexpected type");
  1030. // ICC v4, 9.2.33 luminanceTag
  1031. // "Permitted tag types: XYZType"
  1032. // This tag contains the absolute luminance of emissive devices in candelas per square metre as described by the
  1033. // Y channel.
  1034. // NOTE The X and Z values are set to zero."
  1035. // ICC v2, 6.4.22 luminanceTag
  1036. // "Absolute luminance of emissive devices in candelas per square meter as described by the Y channel. The
  1037. // X and Z channels are ignored in all cases."
  1038. if (auto type = m_tag_table.get(luminanceTag); type.has_value()) {
  1039. if (type.value()->type() != XYZTagData::Type)
  1040. return Error::from_string_literal("ICC::Profile: luminanceTag has unexpected type");
  1041. auto& xyz_type = static_cast<XYZTagData const&>(*type.value());
  1042. if (xyz_type.xyzs().size() != 1)
  1043. return Error::from_string_literal("ICC::Profile: luminanceTag has unexpected size");
  1044. if (is_v4() && xyz_type.xyzs()[0].x != 0)
  1045. return Error::from_string_literal("ICC::Profile: luminanceTag.x unexpectedly not 0");
  1046. if (is_v4() && xyz_type.xyzs()[0].z != 0)
  1047. return Error::from_string_literal("ICC::Profile: luminanceTag.z unexpectedly not 0");
  1048. }
  1049. // ICC v4, 9.2.34 measurementTag
  1050. // "Permitted tag types: measurementType"
  1051. if (!has_type(measurementTag, { MeasurementTagData::Type }, {}))
  1052. return Error::from_string_literal("ICC::Profile: measurementTag has unexpected type");
  1053. // ICC v4, 9.2.35 metadataTag
  1054. // "Permitted tag types: dictType"
  1055. // FIXME
  1056. // ICC v4, 9.2.36 mediaWhitePointTag
  1057. // "Permitted tag types: XYZType
  1058. // This tag, which is used for generating the ICC-absolute colorimetric intent, specifies the chromatically adapted
  1059. // nCIEXYZ tristimulus values of the media white point. When the measurement data used to create the profile
  1060. // were specified relative to an adopted white with a chromaticity different from that of the PCS adopted white, the
  1061. // media white point nCIEXYZ values shall be adapted to be relative to the PCS adopted white chromaticity using
  1062. // the chromaticAdaptationTag matrix, before recording in the tag. For capture devices, the media white point is
  1063. // the encoding maximum white for the capture encoding. For displays, the values specified shall be those of the
  1064. // PCS illuminant as defined in 7.2.16.
  1065. // See Clause 6 and Annex A for a more complete description of the use of the media white point."
  1066. // ICC v2, 6.4.25 mediaWhitePointTag
  1067. // "This tag specifies the media white point and is used for generating ICC-absolute colorimetric intent. See
  1068. // Annex A for a more complete description of its use."
  1069. if (auto type = m_tag_table.get(mediaWhitePointTag); type.has_value()) {
  1070. if (type.value()->type() != XYZTagData::Type)
  1071. return Error::from_string_literal("ICC::Profile: mediaWhitePointTag has unexpected type");
  1072. auto& xyz_type = static_cast<XYZTagData const&>(*type.value());
  1073. if (xyz_type.xyzs().size() != 1)
  1074. return Error::from_string_literal("ICC::Profile: mediaWhitePointTag has unexpected size");
  1075. // V4 requires "For displays, the values specified shall be those of the PCS illuminant".
  1076. // But in practice that's not always true. For example, on macOS 13.1, '/System/Library/ColorSync/Profiles/DCI(P3) RGB.icc'
  1077. // has these values in the header: 0000F6D6 00010000 0000D32D
  1078. // but these values in the tag: 0000F6D5 00010000 0000D32C
  1079. // These are close, but not equal.
  1080. // FIXME: File bug for these, and add id-based quirk instead.
  1081. // if (is_v4() && device_class() == DeviceClass::DisplayDevice && xyz_type.xyzs()[0] != pcs_illuminant())
  1082. // return Error::from_string_literal("ICC::Profile: mediaWhitePointTag for displays should be equal to PCS illuminant");
  1083. }
  1084. // ICC v4, 9.2.37 namedColor2Tag
  1085. // "Permitted tag types: namedColor2Type"
  1086. if (auto type = m_tag_table.get(namedColor2Tag); type.has_value()) {
  1087. if (type.value()->type() != NamedColor2TagData::Type)
  1088. return Error::from_string_literal("ICC::Profile: namedColor2Tag has unexpected type");
  1089. // ICC v4, 10.17 namedColor2Type
  1090. // "The device representation corresponds to the header’s “data colour space” field.
  1091. // This representation should be consistent with the “number of device coordinates” field in the namedColor2Type.
  1092. // If this field is 0, device coordinates are not provided."
  1093. if (int number_of_device_coordinates = static_cast<NamedColor2TagData const&>(*type.value()).number_of_device_coordinates();
  1094. number_of_device_coordinates != 0 && number_of_device_coordinates != number_of_components_in_color_space(data_color_space())) {
  1095. return Error::from_string_literal("ICC::Profile: namedColor2Tag number of device coordinates inconsistent with data color space");
  1096. }
  1097. }
  1098. // ICC v4, 9.2.38 outputResponseTag
  1099. // "Permitted tag types: responseCurveSet16Type"
  1100. // FIXME
  1101. // ICC v4, 9.2.39 perceptualRenderingIntentGamutTag
  1102. // "Permitted tag types: signatureType"
  1103. if (!has_type(perceptualRenderingIntentGamutTag, { SignatureTagData::Type }, {}))
  1104. return Error::from_string_literal("ICC::Profile: perceptualRenderingIntentGamutTag has unexpected type");
  1105. // ICC v4, 9.2.40 preview0Tag
  1106. // "Permitted tag types: lut8Type or lut16Type or lutAToBType or lutBToAType"
  1107. // ICC v2, 6.4.29 preview0Tag
  1108. // "Tag Type: lut8Type or lut16Type"
  1109. if (!has_type(preview0Tag, { Lut8TagData::Type, Lut16TagData::Type }, { LutBToATagData::Type, LutBToATagData::Type }))
  1110. return Error::from_string_literal("ICC::Profile: preview0Tag has unexpected type");
  1111. // ICC v4, 9.2.41 preview1Tag
  1112. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  1113. // ICC v2, 6.4.30 preview1Tag
  1114. // "Tag Type: lut8Type or lut16Type"
  1115. if (!has_type(preview1Tag, { Lut8TagData::Type, Lut16TagData::Type }, { LutBToATagData::Type }))
  1116. return Error::from_string_literal("ICC::Profile: preview1Tag has unexpected type");
  1117. // ICC v4, 9.2.42 preview2Tag
  1118. // "Permitted tag types: lut8Type or lut16Type or lutBToAType"
  1119. // ICC v2, 6.4.31 preview2Tag
  1120. // "Tag Type: lut8Type or lut16Type"
  1121. if (!has_type(preview2Tag, { Lut8TagData::Type, Lut16TagData::Type }, { LutBToATagData::Type }))
  1122. return Error::from_string_literal("ICC::Profile: preview2Tag has unexpected type");
  1123. // ICC v4, 9.2.43 profileDescriptionTag
  1124. // "Permitted tag types: multiLocalizedUnicodeType"
  1125. // ICC v2, 6.4.32 profileDescriptionTag
  1126. // "Tag Type: textDescriptionType"
  1127. if (auto type = m_tag_table.get(profileDescriptionTag); type.has_value()) {
  1128. // The v4 spec requires multiLocalizedUnicodeType for this, but I'm aware of a single file
  1129. // that still uses the v2 'desc' type here: /System/Library/ColorSync/Profiles/ITU-2020.icc on macOS 13.1.
  1130. // https://openradar.appspot.com/radar?id=5529765549178880
  1131. bool has_v2_desc_type_in_v4_file_quirk = id() == apple_itu_2020_id || id() == apple_p3_2015_id;
  1132. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type && (!has_v2_desc_type_in_v4_file_quirk || type.value()->type() != TextDescriptionTagData::Type))
  1133. return Error::from_string_literal("ICC::Profile: profileDescriptionTag has unexpected v4 type");
  1134. if (is_v2() && type.value()->type() != TextDescriptionTagData::Type)
  1135. return Error::from_string_literal("ICC::Profile: profileDescriptionTag has unexpected v2 type");
  1136. }
  1137. // ICC v4, 9.2.44 profileSequenceDescTag
  1138. // "Permitted tag types: profileSequenceDescType"
  1139. // FIXME
  1140. // ICC v4, 9.2.45 profileSequenceIdentifierTag
  1141. // "Permitted tag types: profileSequenceIdentifierType"
  1142. // FIXME
  1143. // ICC v4, 9.2.46 redMatrixColumnTag
  1144. // "Permitted tag types: XYZType
  1145. // This tag contains the first column in the matrix, which is used in matrix/TRC transforms."
  1146. // (Called redColorantTag in the v2 spec, otherwise identical there.)
  1147. if (auto type = m_tag_table.get(redMatrixColumnTag); type.has_value()) {
  1148. if (type.value()->type() != XYZTagData::Type)
  1149. return Error::from_string_literal("ICC::Profile: redMatrixColumnTag has unexpected type");
  1150. if (static_cast<XYZTagData const&>(*type.value()).xyzs().size() != 1)
  1151. return Error::from_string_literal("ICC::Profile: redMatrixColumnTag has unexpected size");
  1152. }
  1153. // ICC v4, 9.2.47 redTRCTag
  1154. // "Permitted tag types: curveType or parametricCurveType"
  1155. // ICC v2, 6.4.41 redTRCTag
  1156. // "Tag Type: curveType"
  1157. if (!has_type(redTRCTag, { CurveTagData::Type }, { ParametricCurveTagData::Type }))
  1158. return Error::from_string_literal("ICC::Profile: redTRCTag has unexpected type");
  1159. // ICC v4, 9.2.48 saturationRenderingIntentGamutTag
  1160. // "Permitted tag types: signatureType"
  1161. if (!has_type(saturationRenderingIntentGamutTag, { SignatureTagData::Type }, {}))
  1162. return Error::from_string_literal("ICC::Profile: saturationRenderingIntentGamutTag has unexpected type");
  1163. // ICC v4, 9.2.49 technologyTag
  1164. // "Permitted tag types: signatureType"
  1165. if (!has_type(technologyTag, { SignatureTagData::Type }, {}))
  1166. return Error::from_string_literal("ICC::Profile: technologyTag has unexpected type");
  1167. // ICC v4, 9.2.50 viewingCondDescTag
  1168. // "Permitted tag types: multiLocalizedUnicodeType"
  1169. // ICC v2, 6.4.46 viewingCondDescTag
  1170. // "Tag Type: textDescriptionType"
  1171. if (auto type = m_tag_table.get(viewingCondDescTag); type.has_value()) {
  1172. if (is_v4() && type.value()->type() != MultiLocalizedUnicodeTagData::Type)
  1173. return Error::from_string_literal("ICC::Profile: viewingCondDescTag has unexpected v4 type");
  1174. if (is_v2() && type.value()->type() != TextDescriptionTagData::Type)
  1175. return Error::from_string_literal("ICC::Profile: viewingCondDescTag has unexpected v2 type");
  1176. }
  1177. // ICC v4, 9.2.51 viewingConditionsTag
  1178. // "Permitted tag types: viewingConditionsType"
  1179. if (!has_type(viewingConditionsTag, { ViewingConditionsTagData::Type }, {}))
  1180. return Error::from_string_literal("ICC::Profile: viewingConditionsTag has unexpected type");
  1181. // FIXME: Add validation for v2-only tags:
  1182. // - ICC v2, 6.4.14 crdInfoTag
  1183. // "Tag Type: crdInfoType"
  1184. // - ICC v2, 6.4.17 deviceSettingsTag
  1185. // "Tag Type: deviceSettingsType"
  1186. // - ICC v2, 6.4.24 mediaBlackPointTag
  1187. // "Tag Type: XYZType"
  1188. // - ICC v2, 6.4.26 namedColorTag
  1189. // "Tag Type: namedColorType"
  1190. // - ICC v2, 6.4.34 ps2CRD0Tag
  1191. // "Tag Type: dataType"
  1192. // - ICC v2, 6.4.35 ps2CRD1Tag
  1193. // "Tag Type: dataType"
  1194. // - ICC v2, 6.4.36 ps2CRD2Tag
  1195. // "Tag Type: dataType"
  1196. // - ICC v2, 6.4.37 ps2CRD3Tag
  1197. // "Tag Type: dataType"
  1198. // - ICC v2, 6.4.38 ps2CSATag
  1199. // "Tag Type: dataType"
  1200. // - ICC v2, 6.4.39 ps2RenderingIntentTag
  1201. // "Tag Type: dataType"
  1202. // - ICC v2, 6.4.42 screeningDescTag
  1203. // "Tag Type: textDescriptionType"
  1204. // - ICC v2, 6.4.43 screeningTag
  1205. // "Tag Type: screeningType"
  1206. // - ICC v2, 6.4.45 ucrbgTag
  1207. // "Tag Type: ucrbgType"
  1208. // https://www.color.org/v2profiles.xalter says about these tags:
  1209. // "it is also recommended that optional tags in the v2 specification that have subsequently become
  1210. // obsolete are not included in future profiles made to the v2 specification."
  1211. return {};
  1212. }
  1213. ErrorOr<NonnullRefPtr<Profile>> Profile::try_load_from_externally_owned_memory(ReadonlyBytes bytes)
  1214. {
  1215. auto header = TRY(read_header(bytes));
  1216. bytes = bytes.trim(header.on_disk_size);
  1217. auto tag_table = TRY(read_tag_table(bytes));
  1218. auto profile = TRY(try_make_ref_counted<Profile>(header, move(tag_table)));
  1219. TRY(profile->check_required_tags());
  1220. TRY(profile->check_tag_types());
  1221. return profile;
  1222. }
  1223. Crypto::Hash::MD5::DigestType Profile::compute_id(ReadonlyBytes bytes)
  1224. {
  1225. // ICC v4, 7.2.18 Profile ID field
  1226. // "The Profile ID shall be calculated using the MD5 fingerprinting method as defined in Internet RFC 1321.
  1227. // The entire profile, whose length is given by the size field in the header, with the
  1228. // profile flags field (bytes 44 to 47, see 7.2.11),
  1229. // rendering intent field (bytes 64 to 67, see 7.2.15),
  1230. // and profile ID field (bytes 84 to 99)
  1231. // in the profile header temporarily set to zeros (00h),
  1232. // shall be used to calculate the ID."
  1233. const u8 zero[16] = {};
  1234. Crypto::Hash::MD5 md5;
  1235. md5.update(bytes.slice(0, 44));
  1236. md5.update(ReadonlyBytes { zero, 4 }); // profile flags field
  1237. md5.update(bytes.slice(48, 64 - 48));
  1238. md5.update(ReadonlyBytes { zero, 4 }); // rendering intent field
  1239. md5.update(bytes.slice(68, 84 - 68));
  1240. md5.update(ReadonlyBytes { zero, 16 }); // profile ID field
  1241. md5.update(bytes.slice(100));
  1242. return md5.digest();
  1243. }
  1244. }