BinaryWriter.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /*
  2. * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Utf16View.h>
  7. #include <LibGfx/ICC/BinaryFormat.h>
  8. #include <LibGfx/ICC/BinaryWriter.h>
  9. #include <LibGfx/ICC/Profile.h>
  10. #include <time.h>
  11. #pragma GCC diagnostic ignored "-Warray-bounds"
  12. namespace Gfx::ICC {
  13. static ErrorOr<ByteBuffer> encode_chromaticity(ChromaticityTagData const& tag_data)
  14. {
  15. // ICC v4, 10.2 chromaticityType
  16. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + 2 * sizeof(u16) + tag_data.xy_coordinates().size() * 2 * sizeof(u16Fixed16Number)));
  17. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(ChromaticityTagData::Type);
  18. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  19. *bit_cast<BigEndian<u16>*>(bytes.data() + 8) = tag_data.xy_coordinates().size();
  20. *bit_cast<BigEndian<u16>*>(bytes.data() + 10) = static_cast<u16>(tag_data.phosphor_or_colorant_type());
  21. auto* coordinates = bit_cast<BigEndian<u16Fixed16Number>*>(bytes.data() + 12);
  22. for (size_t i = 0; i < tag_data.xy_coordinates().size(); ++i) {
  23. coordinates[2 * i] = tag_data.xy_coordinates()[i].x.raw();
  24. coordinates[2 * i + 1] = tag_data.xy_coordinates()[i].y.raw();
  25. }
  26. return bytes;
  27. }
  28. static ErrorOr<ByteBuffer> encode_cipc(CicpTagData const& tag_data)
  29. {
  30. // ICC v4, 10.3 cicpType
  31. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + 4));
  32. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(CicpTagData::Type);
  33. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  34. bytes.data()[8] = tag_data.color_primaries();
  35. bytes.data()[9] = tag_data.transfer_characteristics();
  36. bytes.data()[10] = tag_data.matrix_coefficients();
  37. bytes.data()[11] = tag_data.video_full_range_flag();
  38. return bytes;
  39. }
  40. static ErrorOr<ByteBuffer> encode_curve(CurveTagData const& tag_data)
  41. {
  42. // ICC v4, 10.6 curveType
  43. auto bytes = TRY(ByteBuffer::create_uninitialized(3 * sizeof(u32) + tag_data.values().size() * sizeof(u16)));
  44. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(CurveTagData::Type);
  45. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  46. *bit_cast<BigEndian<u32>*>(bytes.data() + 8) = tag_data.values().size();
  47. auto* values = bit_cast<BigEndian<u16>*>(bytes.data() + 12);
  48. for (size_t i = 0; i < tag_data.values().size(); ++i)
  49. values[i] = tag_data.values()[i];
  50. return bytes;
  51. }
  52. static ErrorOr<ByteBuffer> encode_lut_16(Lut16TagData const& tag_data)
  53. {
  54. // ICC v4, 10.10 lut16Type
  55. u32 input_tables_size = tag_data.input_tables().size();
  56. u32 clut_values_size = tag_data.clut_values().size();
  57. u32 output_tables_size = tag_data.output_tables().size();
  58. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(LUTHeader) + 2 * sizeof(u16) + sizeof(u16) * (input_tables_size + clut_values_size + output_tables_size)));
  59. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(Lut16TagData::Type);
  60. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  61. auto& header = *bit_cast<LUTHeader*>(bytes.data() + 8);
  62. header.number_of_input_channels = tag_data.number_of_input_channels();
  63. header.number_of_output_channels = tag_data.number_of_output_channels();
  64. header.number_of_clut_grid_points = tag_data.number_of_clut_grid_points();
  65. header.reserved_for_padding = 0;
  66. for (int i = 0; i < 9; ++i)
  67. header.e_parameters[i] = tag_data.e_matrix().e[i].raw();
  68. *bit_cast<BigEndian<u16>*>(bytes.data() + 8 + sizeof(LUTHeader)) = tag_data.number_of_input_table_entries();
  69. *bit_cast<BigEndian<u16>*>(bytes.data() + 8 + sizeof(LUTHeader) + 2) = tag_data.number_of_output_table_entries();
  70. auto* values = bit_cast<BigEndian<u16>*>(bytes.data() + 8 + sizeof(LUTHeader) + 4);
  71. for (u16 input_value : tag_data.input_tables())
  72. *values++ = input_value;
  73. for (u16 clut_value : tag_data.clut_values())
  74. *values++ = clut_value;
  75. for (u16 output_value : tag_data.output_tables())
  76. *values++ = output_value;
  77. return bytes;
  78. }
  79. static ErrorOr<ByteBuffer> encode_lut_8(Lut8TagData const& tag_data)
  80. {
  81. // ICC v4, 10.11 lut8Type
  82. u32 input_tables_size = tag_data.input_tables().size();
  83. u32 clut_values_size = tag_data.clut_values().size();
  84. u32 output_tables_size = tag_data.output_tables().size();
  85. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(LUTHeader) + input_tables_size + clut_values_size + output_tables_size));
  86. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(Lut8TagData::Type);
  87. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  88. auto& header = *bit_cast<LUTHeader*>(bytes.data() + 8);
  89. header.number_of_input_channels = tag_data.number_of_input_channels();
  90. header.number_of_output_channels = tag_data.number_of_output_channels();
  91. header.number_of_clut_grid_points = tag_data.number_of_clut_grid_points();
  92. header.reserved_for_padding = 0;
  93. for (int i = 0; i < 9; ++i)
  94. header.e_parameters[i] = tag_data.e_matrix().e[i].raw();
  95. u8* values = bytes.data() + 8 + sizeof(LUTHeader);
  96. memcpy(values, tag_data.input_tables().data(), input_tables_size);
  97. values += input_tables_size;
  98. memcpy(values, tag_data.clut_values().data(), clut_values_size);
  99. values += clut_values_size;
  100. memcpy(values, tag_data.output_tables().data(), output_tables_size);
  101. return bytes;
  102. }
  103. static ErrorOr<ByteBuffer> encode_measurement(MeasurementTagData const& tag_data)
  104. {
  105. // ICC v4, 10.14 measurementType
  106. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(MeasurementHeader)));
  107. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(MeasurementTagData::Type);
  108. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  109. auto& header = *bit_cast<MeasurementHeader*>(bytes.data() + 8);
  110. header.standard_observer = tag_data.standard_observer();
  111. header.tristimulus_value_for_measurement_backing = tag_data.tristimulus_value_for_measurement_backing();
  112. header.measurement_geometry = tag_data.measurement_geometry();
  113. header.measurement_flare = tag_data.measurement_flare().raw();
  114. header.standard_illuminant = tag_data.standard_illuminant();
  115. return bytes;
  116. }
  117. static ErrorOr<ByteBuffer> encode_multi_localized_unicode(MultiLocalizedUnicodeTagData const& tag_data)
  118. {
  119. // ICC v4, 10.15 multiLocalizedUnicodeType
  120. // "The Unicode strings in storage should be encoded as 16-bit big-endian, UTF-16BE,
  121. // and should not be NULL terminated."
  122. size_t number_of_records = tag_data.records().size();
  123. size_t header_and_record_size = 4 * sizeof(u32) + number_of_records * sizeof(MultiLocalizedUnicodeRawRecord);
  124. size_t number_of_codepoints = 0;
  125. Vector<Utf16Data> utf16_strings;
  126. TRY(utf16_strings.try_ensure_capacity(number_of_records));
  127. for (auto const& record : tag_data.records()) {
  128. TRY(utf16_strings.try_append(TRY(utf8_to_utf16(record.text))));
  129. number_of_codepoints += utf16_strings.last().size();
  130. }
  131. size_t string_table_size = number_of_codepoints * sizeof(u16);
  132. auto bytes = TRY(ByteBuffer::create_uninitialized(header_and_record_size + string_table_size));
  133. auto* header = bit_cast<BigEndian<u32>*>(bytes.data());
  134. header[0] = static_cast<u32>(MultiLocalizedUnicodeTagData::Type);
  135. header[1] = 0;
  136. header[2] = number_of_records;
  137. header[3] = sizeof(MultiLocalizedUnicodeRawRecord);
  138. size_t offset = header_and_record_size;
  139. auto* records = bit_cast<MultiLocalizedUnicodeRawRecord*>(bytes.data() + 16);
  140. for (size_t i = 0; i < number_of_records; ++i) {
  141. records[i].language_code = tag_data.records()[i].iso_639_1_language_code;
  142. records[i].country_code = tag_data.records()[i].iso_3166_1_country_code;
  143. records[i].string_length_in_bytes = utf16_strings[i].size() * sizeof(u16);
  144. records[i].string_offset_in_bytes = offset;
  145. offset += records[i].string_length_in_bytes;
  146. }
  147. auto* string_table = bit_cast<BigEndian<u16>*>(bytes.data() + header_and_record_size);
  148. for (auto const& utf16_string : utf16_strings) {
  149. for (size_t i = 0; i < utf16_string.size(); ++i)
  150. string_table[i] = utf16_string[i];
  151. string_table += utf16_string.size();
  152. }
  153. return bytes;
  154. }
  155. static ErrorOr<ByteBuffer> encode_named_color_2(NamedColor2TagData const& tag_data)
  156. {
  157. // ICC v4, 10.17 namedColor2Type
  158. unsigned const record_byte_size = 32 + sizeof(u16) * (3 + tag_data.number_of_device_coordinates());
  159. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(NamedColorHeader) + tag_data.size() * record_byte_size));
  160. *bit_cast<BigEndian<u32>*>(bytes.data()) = (u32)NamedColor2TagData::Type;
  161. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  162. auto& header = *bit_cast<NamedColorHeader*>(bytes.data() + 8);
  163. header.vendor_specific_flag = tag_data.vendor_specific_flag();
  164. header.count_of_named_colors = tag_data.size();
  165. header.number_of_device_coordinates_of_each_named_color = tag_data.number_of_device_coordinates();
  166. memset(header.prefix_for_each_color_name, 0, 32);
  167. memcpy(header.prefix_for_each_color_name, tag_data.prefix().bytes().data(), tag_data.prefix().bytes().size());
  168. memset(header.suffix_for_each_color_name, 0, 32);
  169. memcpy(header.suffix_for_each_color_name, tag_data.suffix().bytes().data(), tag_data.suffix().bytes().size());
  170. u8* record = bytes.data() + 8 + sizeof(NamedColorHeader);
  171. for (size_t i = 0; i < tag_data.size(); ++i) {
  172. memset(record, 0, 32);
  173. memcpy(record, tag_data.root_name(i).bytes().data(), tag_data.root_name(i).bytes().size());
  174. auto* components = bit_cast<BigEndian<u16>*>(record + 32);
  175. components[0] = tag_data.pcs_coordinates(i).xyz.x;
  176. components[1] = tag_data.pcs_coordinates(i).xyz.y;
  177. components[2] = tag_data.pcs_coordinates(i).xyz.z;
  178. for (size_t j = 0; j < tag_data.number_of_device_coordinates(); ++j)
  179. components[3 + j] = tag_data.device_coordinates(i)[j];
  180. record += record_byte_size;
  181. }
  182. return bytes;
  183. }
  184. static ErrorOr<ByteBuffer> encode_parametric_curve(ParametricCurveTagData const& tag_data)
  185. {
  186. // ICC v4, 10.18 parametricCurveType
  187. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + 2 * sizeof(u16) + tag_data.parameter_count() * sizeof(s15Fixed16Number)));
  188. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(ParametricCurveTagData::Type);
  189. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  190. *bit_cast<BigEndian<u16>*>(bytes.data() + 8) = static_cast<u16>(tag_data.function_type());
  191. *bit_cast<BigEndian<u16>*>(bytes.data() + 10) = 0;
  192. auto* parameters = bit_cast<BigEndian<s15Fixed16Number>*>(bytes.data() + 12);
  193. for (size_t i = 0; i < tag_data.parameter_count(); ++i)
  194. parameters[i] = tag_data.parameter(i).raw();
  195. return bytes;
  196. }
  197. static ErrorOr<ByteBuffer> encode_s15_fixed_array(S15Fixed16ArrayTagData const& tag_data)
  198. {
  199. // ICC v4, 10.22 s15Fixed16ArrayType
  200. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + tag_data.values().size() * sizeof(s15Fixed16Number)));
  201. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(S15Fixed16ArrayTagData::Type);
  202. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  203. auto* values = bit_cast<BigEndian<s15Fixed16Number>*>(bytes.data() + 8);
  204. for (size_t i = 0; i < tag_data.values().size(); ++i)
  205. values[i] = tag_data.values()[i].raw();
  206. return bytes;
  207. }
  208. static ErrorOr<ByteBuffer> encode_signature(SignatureTagData const& tag_data)
  209. {
  210. // ICC v4, 10.23 signatureType
  211. auto bytes = TRY(ByteBuffer::create_uninitialized(3 * sizeof(u32)));
  212. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(SignatureTagData::Type);
  213. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  214. *bit_cast<BigEndian<u32>*>(bytes.data() + 8) = tag_data.signature();
  215. return bytes;
  216. }
  217. static ErrorOr<ByteBuffer> encode_text_description(TextDescriptionTagData const& tag_data)
  218. {
  219. // ICC v2, 6.5.17 textDescriptionType
  220. // All lengths include room for a trailing nul character.
  221. // See also the many comments in TextDescriptionTagData::from_bytes().
  222. u32 ascii_size = sizeof(u32) + tag_data.ascii_description().bytes().size() + 1;
  223. // FIXME: Include tag_data.unicode_description() if it's set.
  224. u32 unicode_size = 2 * sizeof(u32);
  225. // FIXME: Include tag_data.macintosh_description() if it's set.
  226. u32 macintosh_size = sizeof(u16) + sizeof(u8) + 67;
  227. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + ascii_size + unicode_size + macintosh_size));
  228. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(TextDescriptionTagData::Type);
  229. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  230. // ASCII
  231. *bit_cast<BigEndian<u32>*>(bytes.data() + 8) = tag_data.ascii_description().bytes().size() + 1;
  232. memcpy(bytes.data() + 12, tag_data.ascii_description().bytes().data(), tag_data.ascii_description().bytes().size());
  233. bytes.data()[12 + tag_data.ascii_description().bytes().size()] = '\0';
  234. // Unicode
  235. // "Because the Unicode language code and Unicode count immediately follow the ASCII description,
  236. // their alignment is not correct when the ASCII count is not a multiple of four"
  237. // So we can't use BigEndian<u32> here.
  238. u8* cursor = bytes.data() + 8 + ascii_size;
  239. u32 unicode_language_code = 0; // FIXME: Set to tag_data.unicode_language_code() once this writes unicode data.
  240. cursor[0] = unicode_language_code >> 24;
  241. cursor[1] = (unicode_language_code >> 16) & 0xff;
  242. cursor[2] = (unicode_language_code >> 8) & 0xff;
  243. cursor[3] = unicode_language_code & 0xff;
  244. cursor += 4;
  245. // FIXME: Include tag_data.unicode_description() if it's set.
  246. u32 ucs2_count = 0; // FIXME: If tag_data.unicode_description() is set, set this to its length plus room for one nul character.
  247. cursor[0] = ucs2_count >> 24;
  248. cursor[1] = (ucs2_count >> 16) & 0xff;
  249. cursor[2] = (ucs2_count >> 8) & 0xff;
  250. cursor[3] = ucs2_count & 0xff;
  251. cursor += 4;
  252. // Macintosh scriptcode
  253. u16 scriptcode_code = 0; // MacRoman
  254. cursor[0] = (scriptcode_code >> 8) & 0xff;
  255. cursor[1] = scriptcode_code & 0xff;
  256. cursor += 2;
  257. u8 macintosh_description_length = 0; // FIXME: If tag_data.macintosh_description() is set, set this to tis length plus room for one nul character.
  258. cursor[0] = macintosh_description_length;
  259. cursor += 1;
  260. memset(cursor, 0, 67);
  261. return bytes;
  262. }
  263. static ErrorOr<ByteBuffer> encode_text(TextTagData const& tag_data)
  264. {
  265. // ICC v4, 10.24 textType
  266. // "The textType is a simple text structure that contains a 7-bit ASCII text string. The length of the string is obtained
  267. // by subtracting 8 from the element size portion of the tag itself. This string shall be terminated with a 00h byte."
  268. auto text_bytes = tag_data.text().bytes();
  269. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + text_bytes.size() + 1));
  270. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(TextTagData::Type);
  271. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  272. memcpy(bytes.data() + 8, text_bytes.data(), text_bytes.size());
  273. *(bytes.data() + 8 + text_bytes.size()) = '\0';
  274. return bytes;
  275. }
  276. static ErrorOr<ByteBuffer> encode_viewing_conditions(ViewingConditionsTagData const& tag_data)
  277. {
  278. // ICC v4, 10.30 viewingConditionsType
  279. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(ViewingConditionsHeader)));
  280. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(ViewingConditionsTagData::Type);
  281. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  282. auto& header = *bit_cast<ViewingConditionsHeader*>(bytes.data() + 8);
  283. header.unnormalized_ciexyz_values_for_illuminant = tag_data.unnormalized_ciexyz_values_for_illuminant();
  284. header.unnormalized_ciexyz_values_for_surround = tag_data.unnormalized_ciexyz_values_for_surround();
  285. header.illuminant_type = tag_data.illuminant_type();
  286. return bytes;
  287. }
  288. static ErrorOr<ByteBuffer> encode_xyz(XYZTagData const& tag_data)
  289. {
  290. // ICC v4, 10.31 XYZType
  291. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + tag_data.xyzs().size() * sizeof(XYZNumber)));
  292. *bit_cast<BigEndian<u32>*>(bytes.data()) = static_cast<u32>(XYZTagData::Type);
  293. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  294. auto* xyzs = bit_cast<XYZNumber*>(bytes.data() + 8);
  295. for (size_t i = 0; i < tag_data.xyzs().size(); ++i)
  296. xyzs[i] = tag_data.xyzs()[i];
  297. return bytes;
  298. }
  299. static ErrorOr<ByteBuffer> encode_tag_data(TagData const& tag_data)
  300. {
  301. switch (tag_data.type()) {
  302. case ChromaticityTagData::Type:
  303. return encode_chromaticity(static_cast<ChromaticityTagData const&>(tag_data));
  304. case CicpTagData::Type:
  305. return encode_cipc(static_cast<CicpTagData const&>(tag_data));
  306. case CurveTagData::Type:
  307. return encode_curve(static_cast<CurveTagData const&>(tag_data));
  308. case Lut16TagData::Type:
  309. return encode_lut_16(static_cast<Lut16TagData const&>(tag_data));
  310. case Lut8TagData::Type:
  311. return encode_lut_8(static_cast<Lut8TagData const&>(tag_data));
  312. case MeasurementTagData::Type:
  313. return encode_measurement(static_cast<MeasurementTagData const&>(tag_data));
  314. case MultiLocalizedUnicodeTagData::Type:
  315. return encode_multi_localized_unicode(static_cast<MultiLocalizedUnicodeTagData const&>(tag_data));
  316. case NamedColor2TagData::Type:
  317. return encode_named_color_2(static_cast<NamedColor2TagData const&>(tag_data));
  318. case ParametricCurveTagData::Type:
  319. return encode_parametric_curve(static_cast<ParametricCurveTagData const&>(tag_data));
  320. case S15Fixed16ArrayTagData::Type:
  321. return encode_s15_fixed_array(static_cast<S15Fixed16ArrayTagData const&>(tag_data));
  322. case SignatureTagData::Type:
  323. return encode_signature(static_cast<SignatureTagData const&>(tag_data));
  324. case TextDescriptionTagData::Type:
  325. return encode_text_description(static_cast<TextDescriptionTagData const&>(tag_data));
  326. case TextTagData::Type:
  327. return encode_text(static_cast<TextTagData const&>(tag_data));
  328. case ViewingConditionsTagData::Type:
  329. return encode_viewing_conditions(static_cast<ViewingConditionsTagData const&>(tag_data));
  330. case XYZTagData::Type:
  331. return encode_xyz(static_cast<XYZTagData const&>(tag_data));
  332. }
  333. // FIXME: If this gets hit, we always write an invalid icc output file.
  334. // Make this return an Optional and don't write tags that have types we can't encode.
  335. // Not ideal, but better than writing invalid outputs.
  336. return ByteBuffer {};
  337. }
  338. static ErrorOr<Vector<ByteBuffer>> encode_tag_datas(Profile const& profile, HashMap<TagData*, size_t>& tag_data_map)
  339. {
  340. Vector<ByteBuffer> tag_data_bytes;
  341. TRY(tag_data_bytes.try_ensure_capacity(profile.tag_count()));
  342. TRY(profile.try_for_each_tag([&](auto, auto tag_data) -> ErrorOr<void> {
  343. if (tag_data_map.contains(tag_data.ptr()))
  344. return {};
  345. tag_data_bytes.append(TRY(encode_tag_data(tag_data)));
  346. TRY(tag_data_map.try_set(tag_data.ptr(), tag_data_bytes.size() - 1));
  347. return {};
  348. }));
  349. return tag_data_bytes;
  350. }
  351. static ErrorOr<void> encode_tag_table(ByteBuffer& bytes, Profile const& profile, Vector<size_t> const& offsets, Vector<ByteBuffer> const& tag_data_bytes, HashMap<TagData*, size_t> const& tag_data_map)
  352. {
  353. // ICC v4, 7.3 Tag table
  354. // ICC v4, 7.3.1 Overview
  355. VERIFY(bytes.size() >= sizeof(ICCHeader) + sizeof(u32) + profile.tag_count() * sizeof(TagTableEntry));
  356. *bit_cast<BigEndian<u32>*>(bytes.data() + sizeof(ICCHeader)) = profile.tag_count();
  357. TagTableEntry* tag_table_entries = bit_cast<TagTableEntry*>(bytes.data() + sizeof(ICCHeader) + sizeof(u32));
  358. int i = 0;
  359. profile.for_each_tag([&](auto tag_signature, auto tag_data) {
  360. tag_table_entries[i].tag_signature = tag_signature;
  361. auto index = tag_data_map.get(tag_data.ptr()).value();
  362. tag_table_entries[i].offset_to_beginning_of_tag_data_element = offsets[index];
  363. tag_table_entries[i].size_of_tag_data_element = tag_data_bytes[index].size();
  364. ++i;
  365. });
  366. return {};
  367. }
  368. static ErrorOr<void> encode_header(ByteBuffer& bytes, Profile const& profile)
  369. {
  370. VERIFY(bytes.size() >= sizeof(ICCHeader));
  371. auto& raw_header = *bit_cast<ICCHeader*>(bytes.data());
  372. raw_header.profile_size = bytes.size();
  373. raw_header.preferred_cmm_type = profile.preferred_cmm_type().value_or(PreferredCMMType { 0 });
  374. raw_header.profile_version_major = profile.version().major_version();
  375. raw_header.profile_version_minor_bugfix = profile.version().minor_and_bugfix_version();
  376. raw_header.profile_version_zero = 0;
  377. raw_header.profile_device_class = profile.device_class();
  378. raw_header.data_color_space = profile.data_color_space();
  379. raw_header.profile_connection_space = profile.connection_space();
  380. time_t profile_timestamp = profile.creation_timestamp();
  381. struct tm tm;
  382. if (!gmtime_r(&profile_timestamp, &tm))
  383. return Error::from_errno(errno);
  384. raw_header.profile_creation_time.year = tm.tm_year + 1900;
  385. raw_header.profile_creation_time.month = tm.tm_mon + 1;
  386. raw_header.profile_creation_time.day = tm.tm_mday;
  387. raw_header.profile_creation_time.hours = tm.tm_hour;
  388. raw_header.profile_creation_time.minutes = tm.tm_min;
  389. raw_header.profile_creation_time.seconds = tm.tm_sec;
  390. raw_header.profile_file_signature = ProfileFileSignature;
  391. raw_header.primary_platform = profile.primary_platform().value_or(PrimaryPlatform { 0 });
  392. raw_header.profile_flags = profile.flags().bits();
  393. raw_header.device_manufacturer = profile.device_manufacturer().value_or(DeviceManufacturer { 0 });
  394. raw_header.device_model = profile.device_model().value_or(DeviceModel { 0 });
  395. raw_header.device_attributes = profile.device_attributes().bits();
  396. raw_header.rendering_intent = profile.rendering_intent();
  397. raw_header.pcs_illuminant = profile.pcs_illuminant();
  398. raw_header.profile_creator = profile.creator().value_or(Creator { 0 });
  399. memset(raw_header.reserved, 0, sizeof(raw_header.reserved));
  400. auto id = Profile::compute_id(bytes);
  401. static_assert(sizeof(id.data) == sizeof(raw_header.profile_id));
  402. memcpy(raw_header.profile_id, id.data, sizeof(id.data));
  403. return {};
  404. }
  405. ErrorOr<ByteBuffer> encode(Profile const& profile)
  406. {
  407. // Valid profiles always have tags. Profile only represents valid profiles.
  408. VERIFY(profile.tag_count() > 0);
  409. HashMap<TagData*, size_t> tag_data_map;
  410. Vector<ByteBuffer> tag_data_bytes = TRY(encode_tag_datas(profile, tag_data_map));
  411. size_t tag_table_size = sizeof(u32) + profile.tag_count() * sizeof(TagTableEntry);
  412. size_t offset = sizeof(ICCHeader) + tag_table_size;
  413. Vector<size_t> offsets;
  414. for (auto const& bytes : tag_data_bytes) {
  415. TRY(offsets.try_append(offset));
  416. offset += align_up_to(bytes.size(), 4);
  417. }
  418. // Omit padding after last element.
  419. size_t total_size = offsets.last() + tag_data_bytes.last().size();
  420. auto bytes = TRY(ByteBuffer::create_zeroed(total_size));
  421. for (size_t i = 0; i < tag_data_bytes.size(); ++i)
  422. memcpy(bytes.data() + offsets[i], tag_data_bytes[i].data(), tag_data_bytes[i].size());
  423. TRY(encode_tag_table(bytes, profile, offsets, tag_data_bytes, tag_data_map));
  424. TRY(encode_header(bytes, profile));
  425. return bytes;
  426. }
  427. }