icc.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Copyright (c) 2022-2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Random.h>
  7. #include <AK/String.h>
  8. #include <AK/StringView.h>
  9. #include <LibCore/ArgsParser.h>
  10. #include <LibCore/DateTime.h>
  11. #include <LibCore/File.h>
  12. #include <LibCore/MappedFile.h>
  13. #include <LibGfx/DeltaE.h>
  14. #include <LibGfx/ICC/BinaryWriter.h>
  15. #include <LibGfx/ICC/Profile.h>
  16. #include <LibGfx/ICC/Tags.h>
  17. #include <LibGfx/ICC/WellKnownProfiles.h>
  18. #include <LibGfx/ImageFormats/ImageDecoder.h>
  19. #include <LibVideo/Color/CodingIndependentCodePoints.h>
  20. template<class T>
  21. static ErrorOr<String> hyperlink(URL const& target, T const& label)
  22. {
  23. return String::formatted("\033]8;;{}\033\\{}\033]8;;\033\\", target, label);
  24. }
  25. template<class T>
  26. static void out_optional(char const* label, Optional<T> const& optional)
  27. {
  28. out("{}: ", label);
  29. if (optional.has_value())
  30. outln("{}", *optional);
  31. else
  32. outln("(not set)");
  33. }
  34. static void out_curve(Gfx::ICC::CurveTagData const& curve, int indent_amount)
  35. {
  36. if (curve.values().is_empty()) {
  37. outln("{: >{}}identity curve", "", indent_amount);
  38. } else if (curve.values().size() == 1) {
  39. outln("{: >{}}gamma: {}", "", indent_amount, FixedPoint<8, u16>::create_raw(curve.values()[0]));
  40. } else {
  41. // FIXME: Maybe print the actual points if -v is passed?
  42. outln("{: >{}}curve with {} points", "", indent_amount, curve.values().size());
  43. }
  44. }
  45. static void out_parametric_curve(Gfx::ICC::ParametricCurveTagData const& parametric_curve, int indent_amount)
  46. {
  47. switch (parametric_curve.function_type()) {
  48. case Gfx::ICC::ParametricCurveTagData::FunctionType::Type0:
  49. outln("{: >{}}Y = X**{}", "", indent_amount, parametric_curve.g());
  50. break;
  51. case Gfx::ICC::ParametricCurveTagData::FunctionType::Type1:
  52. outln("{: >{}}Y = ({}*X + {})**{} if X >= -{}/{}", "", indent_amount,
  53. parametric_curve.a(), parametric_curve.b(), parametric_curve.g(), parametric_curve.b(), parametric_curve.a());
  54. outln("{: >{}}Y = 0 else", "", indent_amount);
  55. break;
  56. case Gfx::ICC::ParametricCurveTagData::FunctionType::Type2:
  57. outln("{: >{}}Y = ({}*X + {})**{} + {} if X >= -{}/{}", "", indent_amount,
  58. parametric_curve.a(), parametric_curve.b(), parametric_curve.g(), parametric_curve.c(), parametric_curve.b(), parametric_curve.a());
  59. outln("{: >{}}Y = {} else", "", indent_amount, parametric_curve.c());
  60. break;
  61. case Gfx::ICC::ParametricCurveTagData::FunctionType::Type3:
  62. outln("{: >{}}Y = ({}*X + {})**{} if X >= {}", "", indent_amount,
  63. parametric_curve.a(), parametric_curve.b(), parametric_curve.g(), parametric_curve.d());
  64. outln("{: >{}}Y = {}*X else", "", indent_amount, parametric_curve.c());
  65. break;
  66. case Gfx::ICC::ParametricCurveTagData::FunctionType::Type4:
  67. outln("{: >{}}Y = ({}*X + {})**{} + {} if X >= {}", "", indent_amount,
  68. parametric_curve.a(), parametric_curve.b(), parametric_curve.g(), parametric_curve.e(), parametric_curve.d());
  69. outln("{: >{}}Y = {}*X + {} else", "", indent_amount, parametric_curve.c(), parametric_curve.f());
  70. break;
  71. }
  72. }
  73. static float curve_distance_u8(Gfx::ICC::TagData const& tag1, Gfx::ICC::TagData const& tag2)
  74. {
  75. VERIFY(tag1.type() == Gfx::ICC::CurveTagData::Type || tag1.type() == Gfx::ICC::ParametricCurveTagData::Type);
  76. VERIFY(tag2.type() == Gfx::ICC::CurveTagData::Type || tag2.type() == Gfx::ICC::ParametricCurveTagData::Type);
  77. float curve1_data[256];
  78. if (tag1.type() == Gfx::ICC::CurveTagData::Type) {
  79. auto& curve1 = static_cast<Gfx::ICC::CurveTagData const&>(tag1);
  80. for (int i = 0; i < 256; ++i)
  81. curve1_data[i] = curve1.evaluate(i / 255.f);
  82. } else {
  83. auto& parametric_curve1 = static_cast<Gfx::ICC::ParametricCurveTagData const&>(tag1);
  84. for (int i = 0; i < 256; ++i)
  85. curve1_data[i] = parametric_curve1.evaluate(i / 255.f);
  86. }
  87. float curve2_data[256];
  88. if (tag2.type() == Gfx::ICC::CurveTagData::Type) {
  89. auto& curve2 = static_cast<Gfx::ICC::CurveTagData const&>(tag2);
  90. for (int i = 0; i < 256; ++i)
  91. curve2_data[i] = curve2.evaluate(i / 255.f);
  92. } else {
  93. auto& parametric_curve2 = static_cast<Gfx::ICC::ParametricCurveTagData const&>(tag2);
  94. for (int i = 0; i < 256; ++i)
  95. curve2_data[i] = parametric_curve2.evaluate(i / 255.f);
  96. }
  97. float distance = 0;
  98. for (int i = 0; i < 256; ++i)
  99. distance += fabsf(curve1_data[i] - curve2_data[i]);
  100. return distance;
  101. }
  102. static ErrorOr<void> out_curve_tag(Gfx::ICC::TagData const& tag, int indent_amount)
  103. {
  104. VERIFY(tag.type() == Gfx::ICC::CurveTagData::Type || tag.type() == Gfx::ICC::ParametricCurveTagData::Type);
  105. if (tag.type() == Gfx::ICC::CurveTagData::Type)
  106. out_curve(static_cast<Gfx::ICC::CurveTagData const&>(tag), indent_amount);
  107. if (tag.type() == Gfx::ICC::ParametricCurveTagData::Type)
  108. out_parametric_curve(static_cast<Gfx::ICC::ParametricCurveTagData const&>(tag), indent_amount);
  109. auto sRGB_curve = TRY(Gfx::ICC::sRGB_curve());
  110. // Some example values (for abs distance summed over the 256 values of an u8):
  111. // In Compact-ICC-Profiles/profiles:
  112. // AdobeCompat-v2.icc: 1.14 (this is a gamma 2.2 curve, so not really sRGB but close)
  113. // AdobeCompat-v4.icc: 1.13
  114. // AppleCompat-v2.icc: 11.94 (gamma 1.8 curve)
  115. // DCI-P3-v4.icc: 8.29 (gamma 2.6 curve)
  116. // DisplayP3-v2-magic.icc: 0.000912 (looks sRGB-ish)
  117. // DisplayP3-v2-micro.icc: 0.010819
  118. // DisplayP3-v4.icc: 0.001062 (yes, definitely sRGB)
  119. // Rec2020-g24-v4.icc: 4.119216 (gamma 2.4 curve)
  120. // Rec2020-v4.icc: 7.805417 (custom non-sRGB curve)
  121. // Rec709-v4.icc: 7.783267 (same custom non-sRGB curve as Rec2020)
  122. // sRGB-v2-magic.icc: 0.000912
  123. // sRGB-v2-micro.icc: 0.010819
  124. // sRGB-v2-nano.icc: 0.052516
  125. // sRGB-v4.icc: 0.001062
  126. // scRGB-v2.icc: 48.379859 (linear identity curve)
  127. // Google sRGB IEC61966-2.1 (from a Pixel jpeg, parametric): 0
  128. // Google sRGB IEC61966-2.1 (from a Pixel jpeg, LUT curve): 0.00096
  129. // Apple 2015 Display P3 (from iPhone 7, parametric): 0.011427 (has the old, left intersection for switching from linear to exponent)
  130. // HP sRGB: 0.00096
  131. // color.org sRGB2014.icc: 0.00096
  132. // color.org sRGB_ICC_v4_Appearance.icc, AToB1Tag, a curves: 0.441926 -- but this is not _really_ sRGB
  133. // color.org sRGB_v4_ICC_preference.icc, AToB1Tag, a curves: 2.205453 -- not really sRGB either
  134. // So `< 0.06` identifies sRGB in practice (for u8 values).
  135. float u8_distance_to_sRGB = curve_distance_u8(*sRGB_curve, tag);
  136. if (u8_distance_to_sRGB < 0.06f)
  137. outln("{: >{}}Looks like sRGB's curve (distance {})", "", indent_amount, u8_distance_to_sRGB);
  138. else
  139. outln("{: >{}}Does not look like sRGB's curve (distance: {})", "", indent_amount, u8_distance_to_sRGB);
  140. return {};
  141. }
  142. static ErrorOr<void> out_curves(Vector<Gfx::ICC::LutCurveType> const& curves)
  143. {
  144. for (auto const& curve : curves) {
  145. VERIFY(curve->type() == Gfx::ICC::CurveTagData::Type || curve->type() == Gfx::ICC::ParametricCurveTagData::Type);
  146. outln(" type {}, relative offset {}, size {}", curve->type(), curve->offset(), curve->size());
  147. TRY(out_curve_tag(*curve, /*indent=*/12));
  148. }
  149. return {};
  150. }
  151. static ErrorOr<void> perform_debug_roundtrip(Gfx::ICC::Profile const& profile)
  152. {
  153. size_t num_channels = Gfx::ICC::number_of_components_in_color_space(profile.data_color_space());
  154. Vector<u8, 4> input, output;
  155. input.resize(num_channels);
  156. output.resize(num_channels);
  157. size_t const num_total_roundtrips = 500;
  158. size_t num_lossless_roundtrips = 0;
  159. for (size_t i = 0; i < num_total_roundtrips; ++i) {
  160. for (size_t j = 0; j < num_channels; ++j)
  161. input[j] = get_random<u8>();
  162. auto color_in_profile_connection_space = TRY(profile.to_pcs(input));
  163. TRY(profile.from_pcs(profile, color_in_profile_connection_space, output));
  164. if (input != output) {
  165. outln("roundtrip failed for {} -> {}", input, output);
  166. } else {
  167. ++num_lossless_roundtrips;
  168. }
  169. }
  170. outln("lossless roundtrips: {} / {}", num_lossless_roundtrips, num_total_roundtrips);
  171. return {};
  172. }
  173. static ErrorOr<void> print_profile_measurement(Gfx::ICC::Profile const& profile)
  174. {
  175. auto lab_from_rgb = [&profile](u8 r, u8 g, u8 b) {
  176. u8 rgb[3] = { r, g, b };
  177. return profile.to_lab(rgb);
  178. };
  179. float largest = -1, smallest = 1000;
  180. Color largest_color1, largest_color2, smallest_color1, smallest_color2;
  181. for (u8 r = 0; r < 254; ++r) {
  182. out("\r{}/254", r + 1);
  183. fflush(stdout);
  184. for (u8 g = 0; g < 254; ++g) {
  185. for (u8 b = 0; b < 254; ++b) {
  186. auto lab = TRY(lab_from_rgb(r, g, b));
  187. u8 delta_r[] = { 1, 0, 0 };
  188. u8 delta_g[] = { 0, 1, 0 };
  189. u8 delta_b[] = { 0, 0, 1 };
  190. for (unsigned i = 0; i < sizeof(delta_r); ++i) {
  191. auto lab2 = TRY(lab_from_rgb(r + delta_r[i], g + delta_g[i], b + delta_b[i]));
  192. float delta = Gfx::DeltaE(lab, lab2);
  193. if (delta > largest) {
  194. largest = delta;
  195. largest_color1 = Color(r, g, b);
  196. largest_color2 = Color(r + delta_r[i], g + delta_g[i], b + delta_b[i]);
  197. }
  198. if (delta < smallest) {
  199. smallest = delta;
  200. smallest_color1 = Color(r, g, b);
  201. smallest_color2 = Color(r + delta_r[i], g + delta_g[i], b + delta_b[i]);
  202. }
  203. }
  204. }
  205. }
  206. }
  207. outln("\rlargest difference between neighboring colors: {}, between {} and {}", largest, largest_color1, largest_color2);
  208. outln("smallest difference between neighboring colors: {}, between {} and {}", smallest, smallest_color1, smallest_color2);
  209. return {};
  210. }
  211. ErrorOr<int> serenity_main(Main::Arguments arguments)
  212. {
  213. Core::ArgsParser args_parser;
  214. StringView path;
  215. args_parser.add_positional_argument(path, "Path to ICC profile or to image containing ICC profile", "FILE", Core::ArgsParser::Required::No);
  216. StringView name;
  217. args_parser.add_option(name, "Name of a built-in profile, such as 'sRGB'", "name", 'n', "NAME");
  218. StringView dump_out_path;
  219. args_parser.add_option(dump_out_path, "Dump unmodified ICC profile bytes to this path", "dump-to", 0, "FILE");
  220. StringView reencode_out_path;
  221. args_parser.add_option(reencode_out_path, "Reencode ICC profile to this path", "reencode-to", 0, "FILE");
  222. bool debug_roundtrip = false;
  223. args_parser.add_option(debug_roundtrip, "Check how many u8 colors roundtrip losslessly through the profile. For debugging.", "debug-roundtrip", 0);
  224. bool measure = false;
  225. args_parser.add_option(measure, "For RGB ICC profiles, print perceptually smallest and largest color step", "measure", 0);
  226. bool force_print = false;
  227. args_parser.add_option(force_print, "Print profile even when writing ICC files", "print", 0);
  228. args_parser.parse(arguments);
  229. if (path.is_empty() && name.is_empty()) {
  230. warnln("need either a path or a profile name");
  231. return 1;
  232. }
  233. if (!path.is_empty() && !name.is_empty()) {
  234. warnln("can't have both a path and a profile name");
  235. return 1;
  236. }
  237. if (path.is_empty() && !dump_out_path.is_empty()) {
  238. warnln("--dump-to only valid with path, not with profile name; use --reencode-to instead");
  239. return 1;
  240. }
  241. ReadonlyBytes icc_bytes;
  242. NonnullRefPtr<Gfx::ICC::Profile> profile = TRY([&]() -> ErrorOr<NonnullRefPtr<Gfx::ICC::Profile>> {
  243. if (!name.is_empty()) {
  244. if (name == "sRGB")
  245. return Gfx::ICC::sRGB();
  246. return Error::from_string_literal("unknown profile name");
  247. }
  248. auto file = TRY(Core::MappedFile::map(path));
  249. auto decoder = Gfx::ImageDecoder::try_create_for_raw_bytes(file->bytes());
  250. if (decoder) {
  251. if (auto embedded_icc_bytes = TRY(decoder->icc_data()); embedded_icc_bytes.has_value()) {
  252. icc_bytes = *embedded_icc_bytes;
  253. } else {
  254. outln("image contains no embedded ICC profile");
  255. exit(1);
  256. }
  257. } else {
  258. icc_bytes = file->bytes();
  259. }
  260. if (!dump_out_path.is_empty()) {
  261. auto output_stream = TRY(Core::File::open(dump_out_path, Core::File::OpenMode::Write));
  262. TRY(output_stream->write_until_depleted(icc_bytes));
  263. }
  264. return Gfx::ICC::Profile::try_load_from_externally_owned_memory(icc_bytes);
  265. }());
  266. if (!reencode_out_path.is_empty()) {
  267. auto reencoded_bytes = TRY(Gfx::ICC::encode(profile));
  268. auto output_stream = TRY(Core::File::open(reencode_out_path, Core::File::OpenMode::Write));
  269. TRY(output_stream->write_until_depleted(reencoded_bytes));
  270. }
  271. if (debug_roundtrip) {
  272. TRY(perform_debug_roundtrip(*profile));
  273. return 0;
  274. }
  275. if (measure) {
  276. if (profile->data_color_space() != Gfx::ICC::ColorSpace::RGB) {
  277. warnln("--measure only works for RGB ICC profiles");
  278. return 1;
  279. }
  280. TRY(print_profile_measurement(*profile));
  281. }
  282. bool do_print = (dump_out_path.is_empty() && reencode_out_path.is_empty() && !measure) || force_print;
  283. if (!do_print)
  284. return 0;
  285. outln(" size: {} bytes", profile->on_disk_size());
  286. out_optional(" preferred CMM type", profile->preferred_cmm_type());
  287. outln(" version: {}", profile->version());
  288. outln(" device class: {}", Gfx::ICC::device_class_name(profile->device_class()));
  289. outln(" data color space: {}", Gfx::ICC::data_color_space_name(profile->data_color_space()));
  290. outln(" connection space: {}", Gfx::ICC::profile_connection_space_name(profile->connection_space()));
  291. if (auto time = profile->creation_timestamp().to_time_t(); !time.is_error()) {
  292. // Print in friendly localtime for valid profiles.
  293. outln("creation date and time: {}", Core::DateTime::from_timestamp(time.release_value()));
  294. } else {
  295. outln("creation date and time: {:04}-{:02}-{:02} {:02}:{:02}:{:02} UTC (invalid)",
  296. profile->creation_timestamp().year, profile->creation_timestamp().month, profile->creation_timestamp().day,
  297. profile->creation_timestamp().hours, profile->creation_timestamp().minutes, profile->creation_timestamp().seconds);
  298. }
  299. out_optional(" primary platform", profile->primary_platform().map([](auto platform) { return primary_platform_name(platform); }));
  300. auto flags = profile->flags();
  301. outln(" flags: 0x{:08x}", flags.bits());
  302. outln(" - {}embedded in file", flags.is_embedded_in_file() ? "" : "not ");
  303. outln(" - can{} be used independently of embedded color data", flags.can_be_used_independently_of_embedded_color_data() ? "" : "not");
  304. if (auto unknown_icc_bits = flags.icc_bits() & ~Gfx::ICC::Flags::KnownBitsMask)
  305. outln(" other unknown ICC bits: 0x{:04x}", unknown_icc_bits);
  306. if (auto color_management_module_bits = flags.color_management_module_bits())
  307. outln(" CMM bits: 0x{:04x}", color_management_module_bits);
  308. out_optional(" device manufacturer", TRY(profile->device_manufacturer().map([](auto device_manufacturer) {
  309. return hyperlink(device_manufacturer_url(device_manufacturer), device_manufacturer);
  310. })));
  311. out_optional(" device model", TRY(profile->device_model().map([](auto device_model) {
  312. return hyperlink(device_model_url(device_model), device_model);
  313. })));
  314. auto device_attributes = profile->device_attributes();
  315. outln(" device attributes: 0x{:016x}", device_attributes.bits());
  316. outln(" media is:");
  317. outln(" - {}",
  318. device_attributes.media_reflectivity() == Gfx::ICC::DeviceAttributes::MediaReflectivity::Reflective ? "reflective" : "transparent");
  319. outln(" - {}",
  320. device_attributes.media_glossiness() == Gfx::ICC::DeviceAttributes::MediaGlossiness::Glossy ? "glossy" : "matte");
  321. outln(" - {}",
  322. device_attributes.media_polarity() == Gfx::ICC::DeviceAttributes::MediaPolarity::Positive ? "of positive polarity" : "of negative polarity");
  323. outln(" - {}",
  324. device_attributes.media_color() == Gfx::ICC::DeviceAttributes::MediaColor::Colored ? "colored" : "black and white");
  325. VERIFY((flags.icc_bits() & ~Gfx::ICC::DeviceAttributes::KnownBitsMask) == 0);
  326. if (auto vendor_bits = device_attributes.vendor_bits())
  327. outln(" vendor bits: 0x{:08x}", vendor_bits);
  328. outln(" rendering intent: {}", Gfx::ICC::rendering_intent_name(profile->rendering_intent()));
  329. outln(" pcs illuminant: {}", profile->pcs_illuminant());
  330. out_optional(" creator", profile->creator());
  331. out_optional(" id", profile->id());
  332. size_t profile_disk_size = icc_bytes.size();
  333. if (profile_disk_size != profile->on_disk_size()) {
  334. VERIFY(profile_disk_size > profile->on_disk_size());
  335. outln("{} trailing bytes after profile data", profile_disk_size - profile->on_disk_size());
  336. }
  337. outln("");
  338. outln("tags:");
  339. HashMap<Gfx::ICC::TagData*, Gfx::ICC::TagSignature> tag_data_to_first_signature;
  340. TRY(profile->try_for_each_tag([&tag_data_to_first_signature](auto tag_signature, auto tag_data) -> ErrorOr<void> {
  341. if (auto name = tag_signature_spec_name(tag_signature); name.has_value())
  342. out("{} ({}): ", *name, tag_signature);
  343. else
  344. out("Unknown tag ({}): ", tag_signature);
  345. outln("type {}, offset {}, size {}", tag_data->type(), tag_data->offset(), tag_data->size());
  346. // Print tag data only the first time it's seen.
  347. // (Different sigatures can refer to the same data.)
  348. auto it = tag_data_to_first_signature.find(tag_data);
  349. if (it != tag_data_to_first_signature.end()) {
  350. outln(" (see {} above)", it->value);
  351. return {};
  352. }
  353. tag_data_to_first_signature.set(tag_data, tag_signature);
  354. if (tag_data->type() == Gfx::ICC::ChromaticityTagData::Type) {
  355. auto& chromaticity = static_cast<Gfx::ICC::ChromaticityTagData&>(*tag_data);
  356. outln(" phosphor or colorant type: {}", Gfx::ICC::ChromaticityTagData::phosphor_or_colorant_type_name(chromaticity.phosphor_or_colorant_type()));
  357. for (auto const& xy : chromaticity.xy_coordinates())
  358. outln(" x, y: {}, {}", xy.x, xy.y);
  359. } else if (tag_data->type() == Gfx::ICC::CicpTagData::Type) {
  360. auto& cicp = static_cast<Gfx::ICC::CicpTagData&>(*tag_data);
  361. outln(" color primaries: {} - {}", cicp.color_primaries(),
  362. Video::color_primaries_to_string((Video::ColorPrimaries)cicp.color_primaries()));
  363. outln(" transfer characteristics: {} - {}", cicp.transfer_characteristics(),
  364. Video::transfer_characteristics_to_string((Video::TransferCharacteristics)cicp.transfer_characteristics()));
  365. outln(" matrix coefficients: {} - {}", cicp.matrix_coefficients(),
  366. Video::matrix_coefficients_to_string((Video::MatrixCoefficients)cicp.matrix_coefficients()));
  367. outln(" video full range flag: {} - {}", cicp.video_full_range_flag(),
  368. Video::video_full_range_flag_to_string((Video::VideoFullRangeFlag)cicp.video_full_range_flag()));
  369. } else if (tag_data->type() == Gfx::ICC::CurveTagData::Type) {
  370. TRY(out_curve_tag(*tag_data, /*indent=*/4));
  371. } else if (tag_data->type() == Gfx::ICC::Lut16TagData::Type) {
  372. auto& lut16 = static_cast<Gfx::ICC::Lut16TagData&>(*tag_data);
  373. outln(" input table: {} channels x {} entries", lut16.number_of_input_channels(), lut16.number_of_input_table_entries());
  374. outln(" output table: {} channels x {} entries", lut16.number_of_output_channels(), lut16.number_of_output_table_entries());
  375. outln(" color lookup table: {} grid points, {} total entries", lut16.number_of_clut_grid_points(), lut16.clut_values().size());
  376. auto const& e = lut16.e_matrix();
  377. outln(" e = [ {}, {}, {},", e[0], e[1], e[2]);
  378. outln(" {}, {}, {},", e[3], e[4], e[5]);
  379. outln(" {}, {}, {} ]", e[6], e[7], e[8]);
  380. } else if (tag_data->type() == Gfx::ICC::Lut8TagData::Type) {
  381. auto& lut8 = static_cast<Gfx::ICC::Lut8TagData&>(*tag_data);
  382. outln(" input table: {} channels x {} entries", lut8.number_of_input_channels(), lut8.number_of_input_table_entries());
  383. outln(" output table: {} channels x {} entries", lut8.number_of_output_channels(), lut8.number_of_output_table_entries());
  384. outln(" color lookup table: {} grid points, {} total entries", lut8.number_of_clut_grid_points(), lut8.clut_values().size());
  385. auto const& e = lut8.e_matrix();
  386. outln(" e = [ {}, {}, {},", e[0], e[1], e[2]);
  387. outln(" {}, {}, {},", e[3], e[4], e[5]);
  388. outln(" {}, {}, {} ]", e[6], e[7], e[8]);
  389. } else if (tag_data->type() == Gfx::ICC::LutAToBTagData::Type) {
  390. auto& a_to_b = static_cast<Gfx::ICC::LutAToBTagData&>(*tag_data);
  391. outln(" {} input channels, {} output channels", a_to_b.number_of_input_channels(), a_to_b.number_of_output_channels());
  392. if (auto const& optional_a_curves = a_to_b.a_curves(); optional_a_curves.has_value()) {
  393. outln(" a curves: {} curves", optional_a_curves->size());
  394. TRY(out_curves(optional_a_curves.value()));
  395. } else {
  396. outln(" a curves: (not set)");
  397. }
  398. if (auto const& optional_clut = a_to_b.clut(); optional_clut.has_value()) {
  399. auto const& clut = optional_clut.value();
  400. outln(" color lookup table: {} grid points, {}",
  401. TRY(String::join(" x "sv, clut.number_of_grid_points_in_dimension)),
  402. TRY(clut.values.visit(
  403. [](Vector<u8> const& v) { return String::formatted("{} u8 entries", v.size()); },
  404. [](Vector<u16> const& v) { return String::formatted("{} u16 entries", v.size()); })));
  405. } else {
  406. outln(" color lookup table: (not set)");
  407. }
  408. if (auto const& optional_m_curves = a_to_b.m_curves(); optional_m_curves.has_value()) {
  409. outln(" m curves: {} curves", optional_m_curves->size());
  410. TRY(out_curves(optional_m_curves.value()));
  411. } else {
  412. outln(" m curves: (not set)");
  413. }
  414. if (auto const& optional_e = a_to_b.e_matrix(); optional_e.has_value()) {
  415. auto const& e = optional_e.value();
  416. outln(" e = [ {}, {}, {}, {},", e[0], e[1], e[2], e[9]);
  417. outln(" {}, {}, {}, {},", e[3], e[4], e[5], e[10]);
  418. outln(" {}, {}, {}, {} ]", e[6], e[7], e[8], e[11]);
  419. } else {
  420. outln(" e = (not set)");
  421. }
  422. outln(" b curves: {} curves", a_to_b.b_curves().size());
  423. TRY(out_curves(a_to_b.b_curves()));
  424. } else if (tag_data->type() == Gfx::ICC::LutBToATagData::Type) {
  425. auto& b_to_a = static_cast<Gfx::ICC::LutBToATagData&>(*tag_data);
  426. outln(" {} input channels, {} output channels", b_to_a.number_of_input_channels(), b_to_a.number_of_output_channels());
  427. outln(" b curves: {} curves", b_to_a.b_curves().size());
  428. TRY(out_curves(b_to_a.b_curves()));
  429. if (auto const& optional_e = b_to_a.e_matrix(); optional_e.has_value()) {
  430. auto const& e = optional_e.value();
  431. outln(" e = [ {}, {}, {}, {},", e[0], e[1], e[2], e[9]);
  432. outln(" {}, {}, {}, {},", e[3], e[4], e[5], e[10]);
  433. outln(" {}, {}, {}, {} ]", e[6], e[7], e[8], e[11]);
  434. } else {
  435. outln(" e = (not set)");
  436. }
  437. if (auto const& optional_m_curves = b_to_a.m_curves(); optional_m_curves.has_value()) {
  438. outln(" m curves: {} curves", optional_m_curves->size());
  439. TRY(out_curves(optional_m_curves.value()));
  440. } else {
  441. outln(" m curves: (not set)");
  442. }
  443. if (auto const& optional_clut = b_to_a.clut(); optional_clut.has_value()) {
  444. auto const& clut = optional_clut.value();
  445. outln(" color lookup table: {} grid points, {}",
  446. TRY(String::join(" x "sv, clut.number_of_grid_points_in_dimension)),
  447. TRY(clut.values.visit(
  448. [](Vector<u8> const& v) { return String::formatted("{} u8 entries", v.size()); },
  449. [](Vector<u16> const& v) { return String::formatted("{} u16 entries", v.size()); })));
  450. } else {
  451. outln(" color lookup table: (not set)");
  452. }
  453. if (auto const& optional_a_curves = b_to_a.a_curves(); optional_a_curves.has_value()) {
  454. outln(" a curves: {} curves", optional_a_curves->size());
  455. TRY(out_curves(optional_a_curves.value()));
  456. } else {
  457. outln(" a curves: (not set)");
  458. }
  459. } else if (tag_data->type() == Gfx::ICC::MeasurementTagData::Type) {
  460. auto& measurement = static_cast<Gfx::ICC::MeasurementTagData&>(*tag_data);
  461. outln(" standard observer: {}", Gfx::ICC::MeasurementTagData::standard_observer_name(measurement.standard_observer()));
  462. outln(" tristimulus value for measurement backing: {}", measurement.tristimulus_value_for_measurement_backing());
  463. outln(" measurement geometry: {}", Gfx::ICC::MeasurementTagData::measurement_geometry_name(measurement.measurement_geometry()));
  464. outln(" measurement flare: {} %", measurement.measurement_flare() * 100);
  465. outln(" standard illuminant: {}", Gfx::ICC::MeasurementTagData::standard_illuminant_name(measurement.standard_illuminant()));
  466. } else if (tag_data->type() == Gfx::ICC::MultiLocalizedUnicodeTagData::Type) {
  467. auto& multi_localized_unicode = static_cast<Gfx::ICC::MultiLocalizedUnicodeTagData&>(*tag_data);
  468. for (auto& record : multi_localized_unicode.records()) {
  469. outln(" {:c}{:c}/{:c}{:c}: \"{}\"",
  470. record.iso_639_1_language_code >> 8, record.iso_639_1_language_code & 0xff,
  471. record.iso_3166_1_country_code >> 8, record.iso_3166_1_country_code & 0xff,
  472. record.text);
  473. }
  474. } else if (tag_data->type() == Gfx::ICC::NamedColor2TagData::Type) {
  475. auto& named_colors = static_cast<Gfx::ICC::NamedColor2TagData&>(*tag_data);
  476. outln(" vendor specific flag: 0x{:08x}", named_colors.vendor_specific_flag());
  477. outln(" common name prefix: \"{}\"", named_colors.prefix());
  478. outln(" common name suffix: \"{}\"", named_colors.suffix());
  479. outln(" {} colors:", named_colors.size());
  480. for (size_t i = 0; i < min(named_colors.size(), 5u); ++i) {
  481. const auto& pcs = named_colors.pcs_coordinates(i);
  482. // FIXME: Display decoded values? (See ICC v4 6.3.4.2 and 10.8.)
  483. out(" \"{}\", PCS coordinates: 0x{:04x} 0x{:04x} 0x{:04x}", TRY(named_colors.color_name(i)), pcs.xyz.x, pcs.xyz.y, pcs.xyz.z);
  484. if (auto number_of_device_coordinates = named_colors.number_of_device_coordinates(); number_of_device_coordinates > 0) {
  485. out(", device coordinates:");
  486. for (size_t j = 0; j < number_of_device_coordinates; ++j)
  487. out(" 0x{:04x}", named_colors.device_coordinates(i)[j]);
  488. }
  489. outln();
  490. }
  491. if (named_colors.size() > 5u)
  492. outln(" ...");
  493. } else if (tag_data->type() == Gfx::ICC::ParametricCurveTagData::Type) {
  494. TRY(out_curve_tag(*tag_data, /*indent=*/4));
  495. } else if (tag_data->type() == Gfx::ICC::S15Fixed16ArrayTagData::Type) {
  496. // This tag can contain arbitrarily many fixed-point numbers, but in practice it's
  497. // exclusively used for the 'chad' tag, where it always contains 9 values that
  498. // represent a 3x3 matrix. So print the values in groups of 3.
  499. auto& fixed_array = static_cast<Gfx::ICC::S15Fixed16ArrayTagData&>(*tag_data);
  500. out(" [");
  501. int i = 0;
  502. for (auto value : fixed_array.values()) {
  503. if (i > 0) {
  504. out(",");
  505. if (i % 3 == 0) {
  506. outln();
  507. out(" ");
  508. }
  509. }
  510. out(" {}", value);
  511. i++;
  512. }
  513. outln(" ]");
  514. } else if (tag_data->type() == Gfx::ICC::SignatureTagData::Type) {
  515. auto& signature = static_cast<Gfx::ICC::SignatureTagData&>(*tag_data);
  516. if (auto name = signature.name_for_tag(tag_signature); name.has_value()) {
  517. outln(" signature: {}", name.value());
  518. } else {
  519. outln(" signature: Unknown ('{:c}{:c}{:c}{:c}' / 0x{:08x})",
  520. signature.signature() >> 24, (signature.signature() >> 16) & 0xff, (signature.signature() >> 8) & 0xff, signature.signature() & 0xff,
  521. signature.signature());
  522. }
  523. } else if (tag_data->type() == Gfx::ICC::TextDescriptionTagData::Type) {
  524. auto& text_description = static_cast<Gfx::ICC::TextDescriptionTagData&>(*tag_data);
  525. outln(" ascii: \"{}\"", text_description.ascii_description());
  526. out_optional(" unicode", TRY(text_description.unicode_description().map([](auto description) { return String::formatted("\"{}\"", description); })));
  527. outln(" unicode language code: 0x{}", text_description.unicode_language_code());
  528. out_optional(" macintosh", TRY(text_description.macintosh_description().map([](auto description) { return String::formatted("\"{}\"", description); })));
  529. } else if (tag_data->type() == Gfx::ICC::TextTagData::Type) {
  530. outln(" text: \"{}\"", static_cast<Gfx::ICC::TextTagData&>(*tag_data).text());
  531. } else if (tag_data->type() == Gfx::ICC::ViewingConditionsTagData::Type) {
  532. auto& viewing_conditions = static_cast<Gfx::ICC::ViewingConditionsTagData&>(*tag_data);
  533. outln(" unnormalized CIEXYZ values for illuminant (in which Y is in cd/m²): {}", viewing_conditions.unnormalized_ciexyz_values_for_illuminant());
  534. outln(" unnormalized CIEXYZ values for surround (in which Y is in cd/m²): {}", viewing_conditions.unnormalized_ciexyz_values_for_surround());
  535. outln(" illuminant type: {}", Gfx::ICC::MeasurementTagData::standard_illuminant_name(viewing_conditions.illuminant_type()));
  536. } else if (tag_data->type() == Gfx::ICC::XYZTagData::Type) {
  537. for (auto& xyz : static_cast<Gfx::ICC::XYZTagData&>(*tag_data).xyzs())
  538. outln(" {}", xyz);
  539. }
  540. return {};
  541. }));
  542. return 0;
  543. }