Profile.cpp 60 KB

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