BinaryWriter.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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()) = (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) = (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()) = (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()) = (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_measurement(MeasurementTagData const& tag_data)
  53. {
  54. // ICC v4, 10.14 measurementType
  55. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(MeasurementHeader)));
  56. *bit_cast<BigEndian<u32>*>(bytes.data()) = (u32)MeasurementTagData::Type;
  57. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  58. auto& header = *bit_cast<MeasurementHeader*>(bytes.data() + 8);
  59. header.standard_observer = tag_data.standard_observer();
  60. header.tristimulus_value_for_measurement_backing = tag_data.tristimulus_value_for_measurement_backing();
  61. header.measurement_geometry = tag_data.measurement_geometry();
  62. header.measurement_flare = tag_data.measurement_flare().raw();
  63. header.standard_illuminant = tag_data.standard_illuminant();
  64. return bytes;
  65. }
  66. static ErrorOr<ByteBuffer> encode_multi_localized_unicode(MultiLocalizedUnicodeTagData const& tag_data)
  67. {
  68. // ICC v4, 10.15 multiLocalizedUnicodeType
  69. // "The Unicode strings in storage should be encoded as 16-bit big-endian, UTF-16BE,
  70. // and should not be NULL terminated."
  71. size_t number_of_records = tag_data.records().size();
  72. size_t header_and_record_size = 4 * sizeof(u32) + number_of_records * sizeof(MultiLocalizedUnicodeRawRecord);
  73. size_t number_of_codepoints = 0;
  74. Vector<Utf16Data> utf16_strings;
  75. TRY(utf16_strings.try_ensure_capacity(number_of_records));
  76. for (auto const& record : tag_data.records()) {
  77. TRY(utf16_strings.try_append(TRY(utf8_to_utf16(record.text))));
  78. number_of_codepoints += utf16_strings.last().size();
  79. }
  80. size_t string_table_size = number_of_codepoints * sizeof(u16);
  81. auto bytes = TRY(ByteBuffer::create_uninitialized(header_and_record_size + string_table_size));
  82. auto* header = bit_cast<BigEndian<u32>*>(bytes.data());
  83. header[0] = (u32)MultiLocalizedUnicodeTagData::Type;
  84. header[1] = 0;
  85. header[2] = number_of_records;
  86. header[3] = sizeof(MultiLocalizedUnicodeRawRecord);
  87. size_t offset = header_and_record_size;
  88. auto* records = bit_cast<MultiLocalizedUnicodeRawRecord*>(bytes.data() + 16);
  89. for (size_t i = 0; i < number_of_records; ++i) {
  90. records[i].language_code = tag_data.records()[i].iso_639_1_language_code;
  91. records[i].country_code = tag_data.records()[i].iso_3166_1_country_code;
  92. records[i].string_length_in_bytes = utf16_strings[i].size() * sizeof(u16);
  93. records[i].string_offset_in_bytes = offset;
  94. offset += records[i].string_offset_in_bytes;
  95. }
  96. auto* string_table = bit_cast<BigEndian<u16>*>(bytes.data() + header_and_record_size);
  97. for (auto const& utf16_string : utf16_strings) {
  98. for (size_t i = 0; i < utf16_string.size(); ++i)
  99. string_table[i] = utf16_string[i];
  100. string_table += utf16_string.size();
  101. }
  102. return bytes;
  103. }
  104. static ErrorOr<ByteBuffer> encode_parametric_curve(ParametricCurveTagData const& tag_data)
  105. {
  106. // ICC v4, 10.18 parametricCurveType
  107. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + 2 * sizeof(u16) + tag_data.parameter_count() * sizeof(s15Fixed16Number)));
  108. *bit_cast<BigEndian<u32>*>(bytes.data()) = (u32)ParametricCurveTagData::Type;
  109. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  110. *bit_cast<BigEndian<u16>*>(bytes.data() + 8) = (u16)tag_data.function_type();
  111. *bit_cast<BigEndian<u16>*>(bytes.data() + 10) = 0;
  112. auto* parameters = bit_cast<BigEndian<s15Fixed16Number>*>(bytes.data() + 12);
  113. for (size_t i = 0; i < tag_data.parameter_count(); ++i)
  114. parameters[i] = tag_data.parameter(i).raw();
  115. return bytes;
  116. }
  117. static ErrorOr<ByteBuffer> encode_s15_fixed_array(S15Fixed16ArrayTagData const& tag_data)
  118. {
  119. // ICC v4, 10.22 s15Fixed16ArrayType
  120. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + tag_data.values().size() * sizeof(s15Fixed16Number)));
  121. *bit_cast<BigEndian<u32>*>(bytes.data()) = (u32)S15Fixed16ArrayTagData::Type;
  122. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  123. auto* values = bit_cast<BigEndian<s15Fixed16Number>*>(bytes.data() + 8);
  124. for (size_t i = 0; i < tag_data.values().size(); ++i)
  125. values[i] = tag_data.values()[i].raw();
  126. return bytes;
  127. }
  128. static ErrorOr<ByteBuffer> encode_signature(SignatureTagData const& tag_data)
  129. {
  130. // ICC v4, 10.23 signatureType
  131. auto bytes = TRY(ByteBuffer::create_uninitialized(3 * sizeof(u32)));
  132. *bit_cast<BigEndian<u32>*>(bytes.data()) = (u32)SignatureTagData::Type;
  133. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  134. *bit_cast<BigEndian<u32>*>(bytes.data() + 8) = tag_data.signature();
  135. return bytes;
  136. }
  137. static ErrorOr<ByteBuffer> encode_text(TextTagData const& tag_data)
  138. {
  139. // ICC v4, 10.24 textType
  140. // "The textType is a simple text structure that contains a 7-bit ASCII text string. The length of the string is obtained
  141. // by subtracting 8 from the element size portion of the tag itself. This string shall be terminated with a 00h byte."
  142. auto text_bytes = tag_data.text().bytes();
  143. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + text_bytes.size() + 1));
  144. *bit_cast<BigEndian<u32>*>(bytes.data()) = (u32)TextTagData::Type;
  145. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  146. memcpy(bytes.data() + 8, text_bytes.data(), text_bytes.size());
  147. *(bytes.data() + 8 + text_bytes.size()) = '\0';
  148. return bytes;
  149. }
  150. static ErrorOr<ByteBuffer> encode_viewing_conditions(ViewingConditionsTagData const& tag_data)
  151. {
  152. // ICC v4, 10.30 viewingConditionsType
  153. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + sizeof(ViewingConditionsHeader)));
  154. *bit_cast<BigEndian<u32>*>(bytes.data()) = (u32)ViewingConditionsTagData::Type;
  155. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  156. auto& header = *bit_cast<ViewingConditionsHeader*>(bytes.data() + 8);
  157. header.unnormalized_ciexyz_values_for_illuminant = tag_data.unnormalized_ciexyz_values_for_illuminant();
  158. header.unnormalized_ciexyz_values_for_surround = tag_data.unnormalized_ciexyz_values_for_surround();
  159. header.illuminant_type = tag_data.illuminant_type();
  160. return bytes;
  161. }
  162. static ErrorOr<ByteBuffer> encode_xyz(XYZTagData const& tag_data)
  163. {
  164. // ICC v4, 10.31 XYZType
  165. auto bytes = TRY(ByteBuffer::create_uninitialized(2 * sizeof(u32) + tag_data.xyzs().size() * sizeof(XYZNumber)));
  166. *bit_cast<BigEndian<u32>*>(bytes.data()) = (u32)XYZTagData::Type;
  167. *bit_cast<BigEndian<u32>*>(bytes.data() + 4) = 0;
  168. auto* xyzs = bit_cast<XYZNumber*>(bytes.data() + 8);
  169. for (size_t i = 0; i < tag_data.xyzs().size(); ++i)
  170. xyzs[i] = tag_data.xyzs()[i];
  171. return bytes;
  172. }
  173. static ErrorOr<ByteBuffer> encode_tag_data(TagData const& tag_data)
  174. {
  175. switch (tag_data.type()) {
  176. case ChromaticityTagData::Type:
  177. return encode_chromaticity(static_cast<ChromaticityTagData const&>(tag_data));
  178. case CicpTagData::Type:
  179. return encode_cipc(static_cast<CicpTagData const&>(tag_data));
  180. case CurveTagData::Type:
  181. return encode_curve(static_cast<CurveTagData const&>(tag_data));
  182. case MeasurementTagData::Type:
  183. return encode_measurement(static_cast<MeasurementTagData const&>(tag_data));
  184. case MultiLocalizedUnicodeTagData::Type:
  185. return encode_multi_localized_unicode(static_cast<MultiLocalizedUnicodeTagData const&>(tag_data));
  186. case ParametricCurveTagData::Type:
  187. return encode_parametric_curve(static_cast<ParametricCurveTagData const&>(tag_data));
  188. case S15Fixed16ArrayTagData::Type:
  189. return encode_s15_fixed_array(static_cast<S15Fixed16ArrayTagData const&>(tag_data));
  190. case SignatureTagData::Type:
  191. return encode_signature(static_cast<SignatureTagData const&>(tag_data));
  192. case TextTagData::Type:
  193. return encode_text(static_cast<TextTagData const&>(tag_data));
  194. case ViewingConditionsTagData::Type:
  195. return encode_viewing_conditions(static_cast<ViewingConditionsTagData const&>(tag_data));
  196. case XYZTagData::Type:
  197. return encode_xyz(static_cast<XYZTagData const&>(tag_data));
  198. }
  199. return ByteBuffer {};
  200. }
  201. static ErrorOr<Vector<ByteBuffer>> encode_tag_datas(Profile const& profile, HashMap<TagData*, size_t>& tag_data_map)
  202. {
  203. Vector<ByteBuffer> tag_data_bytes;
  204. TRY(tag_data_bytes.try_ensure_capacity(profile.tag_count()));
  205. profile.for_each_tag([&](auto, auto tag_data) {
  206. if (tag_data_map.contains(tag_data.ptr()))
  207. return;
  208. // FIXME: Come up with a way to allow TRY instead of MUST here.
  209. tag_data_bytes.append(MUST(encode_tag_data(tag_data)));
  210. MUST(tag_data_map.try_set(tag_data.ptr(), tag_data_bytes.size() - 1));
  211. });
  212. return tag_data_bytes;
  213. }
  214. 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)
  215. {
  216. VERIFY(bytes.size() >= sizeof(ICCHeader) + sizeof(u32) + profile.tag_count() * sizeof(TagTableEntry));
  217. *bit_cast<BigEndian<u32>*>(bytes.data() + sizeof(ICCHeader)) = profile.tag_count();
  218. TagTableEntry* tag_table_entries = bit_cast<TagTableEntry*>(bytes.data() + sizeof(ICCHeader) + sizeof(u32));
  219. int i = 0;
  220. profile.for_each_tag([&](auto tag_signature, auto tag_data) {
  221. tag_table_entries[i].tag_signature = tag_signature;
  222. auto index = tag_data_map.get(tag_data.ptr()).value();
  223. tag_table_entries[i].offset_to_beginning_of_tag_data_element = offsets[index];
  224. tag_table_entries[i].size_of_tag_data_element = tag_data_bytes[index].size();
  225. ++i;
  226. });
  227. return {};
  228. }
  229. static ErrorOr<void> encode_header(ByteBuffer& bytes, Profile const& profile)
  230. {
  231. VERIFY(bytes.size() >= sizeof(ICCHeader));
  232. auto& raw_header = *bit_cast<ICCHeader*>(bytes.data());
  233. raw_header.profile_size = bytes.size();
  234. raw_header.preferred_cmm_type = profile.preferred_cmm_type().value_or(PreferredCMMType { 0 });
  235. raw_header.profile_version_major = profile.version().major_version();
  236. raw_header.profile_version_minor_bugfix = profile.version().minor_and_bugfix_version();
  237. raw_header.profile_version_zero = 0;
  238. raw_header.profile_device_class = profile.device_class();
  239. raw_header.data_color_space = profile.data_color_space();
  240. raw_header.profile_connection_space = profile.connection_space();
  241. time_t profile_timestamp = profile.creation_timestamp();
  242. struct tm tm;
  243. if (!gmtime_r(&profile_timestamp, &tm))
  244. return Error::from_errno(errno);
  245. raw_header.profile_creation_time.year = tm.tm_year + 1900;
  246. raw_header.profile_creation_time.month = tm.tm_mon + 1;
  247. raw_header.profile_creation_time.day = tm.tm_mday;
  248. raw_header.profile_creation_time.hours = tm.tm_hour;
  249. raw_header.profile_creation_time.minutes = tm.tm_min;
  250. raw_header.profile_creation_time.seconds = tm.tm_sec;
  251. raw_header.profile_file_signature = ProfileFileSignature;
  252. raw_header.primary_platform = profile.primary_platform().value_or(PrimaryPlatform { 0 });
  253. raw_header.profile_flags = profile.flags().bits();
  254. raw_header.device_manufacturer = profile.device_manufacturer().value_or(DeviceManufacturer { 0 });
  255. raw_header.device_model = profile.device_model().value_or(DeviceModel { 0 });
  256. raw_header.device_attributes = profile.device_attributes().bits();
  257. raw_header.rendering_intent = profile.rendering_intent();
  258. raw_header.pcs_illuminant = profile.pcs_illuminant();
  259. raw_header.profile_creator = profile.creator().value_or(Creator { 0 });
  260. memset(raw_header.reserved, 0, sizeof(raw_header.reserved));
  261. auto id = Profile::compute_id(bytes);
  262. static_assert(sizeof(id.data) == sizeof(raw_header.profile_id));
  263. memcpy(raw_header.profile_id, id.data, sizeof(id.data));
  264. return {};
  265. }
  266. ErrorOr<ByteBuffer> encode(Profile const& profile)
  267. {
  268. // Valid profiles always have tags. Profile only represents valid profiles.
  269. VERIFY(profile.tag_count() > 0);
  270. HashMap<TagData*, size_t> tag_data_map;
  271. Vector<ByteBuffer> tag_data_bytes = TRY(encode_tag_datas(profile, tag_data_map));
  272. size_t tag_table_size = sizeof(u32) + profile.tag_count() * sizeof(TagTableEntry);
  273. size_t offset = sizeof(ICCHeader) + tag_table_size;
  274. Vector<size_t> offsets;
  275. for (auto const& bytes : tag_data_bytes) {
  276. TRY(offsets.try_append(offset));
  277. offset += align_up_to(bytes.size(), 4);
  278. }
  279. // Omit padding after last element.
  280. // FIXME: Is that correct?
  281. size_t total_size = offsets.last() + tag_data_bytes.last().size();
  282. // Leave enough room for the profile header and the tag table count.
  283. auto bytes = TRY(ByteBuffer::create_zeroed(total_size));
  284. for (size_t i = 0; i < tag_data_bytes.size(); ++i)
  285. memcpy(bytes.data() + offsets[i], tag_data_bytes[i].data(), tag_data_bytes[i].size());
  286. TRY(encode_tag_table(bytes, profile, offsets, tag_data_bytes, tag_data_map));
  287. TRY(encode_header(bytes, profile));
  288. return bytes;
  289. }
  290. }