WellKnownProfiles.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibGfx/ICC/Profile.h>
  7. #include <LibGfx/ICC/Tags.h>
  8. #include <LibGfx/ICC/WellKnownProfiles.h>
  9. #include <time.h>
  10. namespace Gfx::ICC {
  11. static ProfileHeader rgb_header()
  12. {
  13. ProfileHeader header;
  14. header.version = Version(4, 0x40);
  15. header.device_class = DeviceClass::DisplayDevice;
  16. header.data_color_space = ColorSpace::RGB;
  17. header.connection_space = ColorSpace::PCSXYZ;
  18. header.creation_timestamp = MUST(DateTime::from_time_t(0));
  19. header.rendering_intent = RenderingIntent::Perceptual;
  20. header.pcs_illuminant = XYZ { 0.9642, 1.0, 0.8249 };
  21. return header;
  22. }
  23. static ErrorOr<NonnullRefPtr<MultiLocalizedUnicodeTagData>> en_US(StringView text)
  24. {
  25. Vector<MultiLocalizedUnicodeTagData::Record> records;
  26. TRY(records.try_append({ ('e' << 8) | 'n', ('U' << 8) | 'S', TRY(String::from_utf8(text)) }));
  27. return try_make_ref_counted<MultiLocalizedUnicodeTagData>(0, 0, records);
  28. }
  29. static ErrorOr<NonnullRefPtr<XYZTagData>> XYZ_data(XYZ xyz)
  30. {
  31. Vector<XYZ> xyzs;
  32. TRY(xyzs.try_append(xyz));
  33. return try_make_ref_counted<XYZTagData>(0, 0, move(xyzs));
  34. }
  35. ErrorOr<NonnullRefPtr<TagData>> sRGB_curve()
  36. {
  37. // Numbers from https://en.wikipedia.org/wiki/SRGB#From_sRGB_to_CIE_XYZ
  38. Array<S15Fixed16, 7> curve_parameters = { 2.4, 1 / 1.055, 0.055 / 1.055, 1 / 12.92, 0.04045 };
  39. return try_make_ref_counted<ParametricCurveTagData>(0, 0, ParametricCurveTagData::FunctionType::sRGB, curve_parameters);
  40. }
  41. ErrorOr<NonnullRefPtr<Profile>> sRGB()
  42. {
  43. // Returns an sRGB profile.
  44. // https://en.wikipedia.org/wiki/SRGB
  45. // FIXME: There are many different sRGB ICC profiles in the wild.
  46. // Explain why, and why this picks the numbers it does.
  47. // In the meantime, https://github.com/SerenityOS/serenity/pull/17714 has a few notes.
  48. auto header = rgb_header();
  49. OrderedHashMap<TagSignature, NonnullRefPtr<TagData>> tag_table;
  50. TRY(tag_table.try_set(profileDescriptionTag, TRY(en_US("SerenityOS sRGB"sv))));
  51. TRY(tag_table.try_set(copyrightTag, TRY(en_US("Public Domain"sv))));
  52. // Transfer function.
  53. auto curve = TRY(sRGB_curve());
  54. TRY(tag_table.try_set(redTRCTag, curve));
  55. TRY(tag_table.try_set(greenTRCTag, curve));
  56. TRY(tag_table.try_set(blueTRCTag, curve));
  57. // White point.
  58. // ICC v4, 9.2.36 mediaWhitePointTag: "For displays, the values specified shall be those of the PCS illuminant as defined in 7.2.16."
  59. TRY(tag_table.try_set(mediaWhitePointTag, TRY(XYZ_data(header.pcs_illuminant))));
  60. // The chromatic_adaptation_matrix values are from https://www.color.org/chadtag.xalter
  61. // That leads to exactly the S15Fixed16 values in the sRGB profiles in GIMP, Android, RawTherapee (but not in Compact-ICC-Profiles's v4 sRGB profile).
  62. Vector<S15Fixed16, 9> chromatic_adaptation_matrix = { 1.047882, 0.022918, -0.050217, 0.029586, 0.990478, -0.017075, -0.009247, 0.015075, 0.751678 };
  63. TRY(tag_table.try_set(chromaticAdaptationTag, TRY(try_make_ref_counted<S15Fixed16ArrayTagData>(0, 0, move(chromatic_adaptation_matrix)))));
  64. // The chromaticity values are from https://www.color.org/srgb.pdf
  65. // The chromatic adaptation matrix in that document is slightly different from the one on https://www.color.org/chadtag.xalter,
  66. // so the values in our sRGB profile are currently not fully self-consistent.
  67. // FIXME: Make values self-consistent (probably by using slightly different chromaticities).
  68. TRY(tag_table.try_set(redMatrixColumnTag, TRY(XYZ_data(XYZ { 0.436030342570117, 0.222438466210245, 0.013897440074263 }))));
  69. TRY(tag_table.try_set(greenMatrixColumnTag, TRY(XYZ_data(XYZ { 0.385101860087134, 0.716942745571917, 0.097076381494207 }))));
  70. TRY(tag_table.try_set(blueMatrixColumnTag, TRY(XYZ_data(XYZ { 0.143067806654203, 0.060618777416563, 0.713926257896652 }))));
  71. return Profile::create(header, move(tag_table));
  72. }
  73. }