Profile.cpp 63 KB

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