SkICC.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright 2016 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkICC.h"
  8. #include "include/private/SkFixed.h"
  9. #include "src/core/SkAutoMalloc.h"
  10. #include "src/core/SkColorSpacePriv.h"
  11. #include "src/core/SkEndian.h"
  12. #include "src/core/SkICCPriv.h"
  13. #include "src/core/SkMD5.h"
  14. #include "src/core/SkUtils.h"
  15. static constexpr char kDescriptionTagBodyPrefix[12] =
  16. { 'G', 'o', 'o', 'g', 'l', 'e', '/', 'S', 'k', 'i', 'a' , '/'};
  17. static constexpr size_t kICCDescriptionTagSize = 44;
  18. static_assert(kICCDescriptionTagSize ==
  19. sizeof(kDescriptionTagBodyPrefix) + 2 * sizeof(SkMD5::Digest), "");
  20. static constexpr size_t kDescriptionTagBodySize = kICCDescriptionTagSize * 2; // ascii->utf16be
  21. static_assert(SkIsAlign4(kDescriptionTagBodySize), "Description must be aligned to 4-bytes.");
  22. static constexpr uint32_t kDescriptionTagHeader[7] {
  23. SkEndian_SwapBE32(kTAG_TextType), // Type signature
  24. 0, // Reserved
  25. SkEndian_SwapBE32(1), // Number of records
  26. SkEndian_SwapBE32(12), // Record size (must be 12)
  27. SkEndian_SwapBE32(SkSetFourByteTag('e', 'n', 'U', 'S')), // English USA
  28. SkEndian_SwapBE32(kDescriptionTagBodySize), // Length of string
  29. SkEndian_SwapBE32(28), // Offset of string
  30. };
  31. static constexpr uint32_t kWhitePointTag[5] {
  32. SkEndian_SwapBE32(kXYZ_PCSSpace),
  33. 0,
  34. SkEndian_SwapBE32(0x0000f6d6), // X = 0.96420 (D50)
  35. SkEndian_SwapBE32(0x00010000), // Y = 1.00000 (D50)
  36. SkEndian_SwapBE32(0x0000d32d), // Z = 0.82491 (D50)
  37. };
  38. // Google Inc. 2016 (UTF-16)
  39. static constexpr uint8_t kCopyrightTagBody[] = {
  40. 0x00, 0x47, 0x00, 0x6f,
  41. 0x00, 0x6f, 0x00, 0x67,
  42. 0x00, 0x6c, 0x00, 0x65,
  43. 0x00, 0x20, 0x00, 0x49,
  44. 0x00, 0x6e, 0x00, 0x63,
  45. 0x00, 0x2e, 0x00, 0x20,
  46. 0x00, 0x32, 0x00, 0x30,
  47. 0x00, 0x31, 0x00, 0x36,
  48. };
  49. static_assert(SkIsAlign4(sizeof(kCopyrightTagBody)), "Copyright must be aligned to 4-bytes.");
  50. static constexpr uint32_t kCopyrightTagHeader[7] {
  51. SkEndian_SwapBE32(kTAG_TextType), // Type signature
  52. 0, // Reserved
  53. SkEndian_SwapBE32(1), // Number of records
  54. SkEndian_SwapBE32(12), // Record size (must be 12)
  55. SkEndian_SwapBE32(SkSetFourByteTag('e', 'n', 'U', 'S')), // English USA
  56. SkEndian_SwapBE32(sizeof(kCopyrightTagBody)), // Length of string
  57. SkEndian_SwapBE32(28), // Offset of string
  58. };
  59. // We will write a profile with the minimum nine required tags.
  60. static constexpr uint32_t kICCNumEntries = 9;
  61. static constexpr uint32_t kTAG_desc = SkSetFourByteTag('d', 'e', 's', 'c');
  62. static constexpr uint32_t kTAG_desc_Bytes = sizeof(kDescriptionTagHeader) +
  63. kDescriptionTagBodySize;
  64. static constexpr uint32_t kTAG_desc_Offset = kICCHeaderSize +
  65. kICCNumEntries * kICCTagTableEntrySize;
  66. static constexpr uint32_t kTAG_XYZ_Bytes = 20;
  67. static constexpr uint32_t kTAG_rXYZ_Offset = kTAG_desc_Offset + kTAG_desc_Bytes;
  68. static constexpr uint32_t kTAG_gXYZ_Offset = kTAG_rXYZ_Offset + kTAG_XYZ_Bytes;
  69. static constexpr uint32_t kTAG_bXYZ_Offset = kTAG_gXYZ_Offset + kTAG_XYZ_Bytes;
  70. static constexpr uint32_t kTAG_TRC_Bytes = 40;
  71. static constexpr uint32_t kTAG_rTRC_Offset = kTAG_bXYZ_Offset + kTAG_XYZ_Bytes;
  72. static constexpr uint32_t kTAG_gTRC_Offset = kTAG_rTRC_Offset;
  73. static constexpr uint32_t kTAG_bTRC_Offset = kTAG_rTRC_Offset;
  74. static constexpr uint32_t kTAG_wtpt = SkSetFourByteTag('w', 't', 'p', 't');
  75. static constexpr uint32_t kTAG_wtpt_Offset = kTAG_bTRC_Offset + kTAG_TRC_Bytes;
  76. static constexpr uint32_t kTAG_cprt = SkSetFourByteTag('c', 'p', 'r', 't');
  77. static constexpr uint32_t kTAG_cprt_Bytes = sizeof(kCopyrightTagHeader) +
  78. sizeof(kCopyrightTagBody);
  79. static constexpr uint32_t kTAG_cprt_Offset = kTAG_wtpt_Offset + kTAG_XYZ_Bytes;
  80. static constexpr uint32_t kICCProfileSize = kTAG_cprt_Offset + kTAG_cprt_Bytes;
  81. static constexpr uint32_t kICCHeader[kICCHeaderSize / 4] {
  82. SkEndian_SwapBE32(kICCProfileSize), // Size of the profile
  83. 0, // Preferred CMM type (ignored)
  84. SkEndian_SwapBE32(0x02100000), // Version 2.1
  85. SkEndian_SwapBE32(kDisplay_Profile), // Display device profile
  86. SkEndian_SwapBE32(kRGB_ColorSpace), // RGB input color space
  87. SkEndian_SwapBE32(kXYZ_PCSSpace), // XYZ profile connection space
  88. 0, 0, 0, // Date and time (ignored)
  89. SkEndian_SwapBE32(kACSP_Signature), // Profile signature
  90. 0, // Platform target (ignored)
  91. 0x00000000, // Flags: not embedded, can be used independently
  92. 0, // Device manufacturer (ignored)
  93. 0, // Device model (ignored)
  94. 0, 0, // Device attributes (ignored)
  95. SkEndian_SwapBE32(1), // Relative colorimetric rendering intent
  96. SkEndian_SwapBE32(0x0000f6d6), // D50 standard illuminant (X)
  97. SkEndian_SwapBE32(0x00010000), // D50 standard illuminant (Y)
  98. SkEndian_SwapBE32(0x0000d32d), // D50 standard illuminant (Z)
  99. 0, // Profile creator (ignored)
  100. 0, 0, 0, 0, // Profile id checksum (ignored)
  101. 0, 0, 0, 0, 0, 0, 0, // Reserved (ignored)
  102. SkEndian_SwapBE32(kICCNumEntries), // Number of tags
  103. };
  104. static constexpr uint32_t kICCTagTable[3 * kICCNumEntries] {
  105. // Profile description
  106. SkEndian_SwapBE32(kTAG_desc),
  107. SkEndian_SwapBE32(kTAG_desc_Offset),
  108. SkEndian_SwapBE32(kTAG_desc_Bytes),
  109. // rXYZ
  110. SkEndian_SwapBE32(kTAG_rXYZ),
  111. SkEndian_SwapBE32(kTAG_rXYZ_Offset),
  112. SkEndian_SwapBE32(kTAG_XYZ_Bytes),
  113. // gXYZ
  114. SkEndian_SwapBE32(kTAG_gXYZ),
  115. SkEndian_SwapBE32(kTAG_gXYZ_Offset),
  116. SkEndian_SwapBE32(kTAG_XYZ_Bytes),
  117. // bXYZ
  118. SkEndian_SwapBE32(kTAG_bXYZ),
  119. SkEndian_SwapBE32(kTAG_bXYZ_Offset),
  120. SkEndian_SwapBE32(kTAG_XYZ_Bytes),
  121. // rTRC
  122. SkEndian_SwapBE32(kTAG_rTRC),
  123. SkEndian_SwapBE32(kTAG_rTRC_Offset),
  124. SkEndian_SwapBE32(kTAG_TRC_Bytes),
  125. // gTRC
  126. SkEndian_SwapBE32(kTAG_gTRC),
  127. SkEndian_SwapBE32(kTAG_gTRC_Offset),
  128. SkEndian_SwapBE32(kTAG_TRC_Bytes),
  129. // bTRC
  130. SkEndian_SwapBE32(kTAG_bTRC),
  131. SkEndian_SwapBE32(kTAG_bTRC_Offset),
  132. SkEndian_SwapBE32(kTAG_TRC_Bytes),
  133. // White point
  134. SkEndian_SwapBE32(kTAG_wtpt),
  135. SkEndian_SwapBE32(kTAG_wtpt_Offset),
  136. SkEndian_SwapBE32(kTAG_XYZ_Bytes),
  137. // Copyright
  138. SkEndian_SwapBE32(kTAG_cprt),
  139. SkEndian_SwapBE32(kTAG_cprt_Offset),
  140. SkEndian_SwapBE32(kTAG_cprt_Bytes),
  141. };
  142. // This is like SkFloatToFixed, but rounds to nearest, preserving as much accuracy as possible
  143. // when going float -> fixed -> float (it has the same accuracy when going fixed -> float -> fixed).
  144. // The use of double is necessary to accomodate the full potential 32-bit mantissa of the 16.16
  145. // SkFixed value, and so avoiding rounding problems with float. Also, see the comment in SkFixed.h.
  146. static SkFixed float_round_to_fixed(float x) {
  147. return sk_float_saturate2int((float)floor((double)x * SK_Fixed1 + 0.5));
  148. }
  149. static void write_xyz_tag(uint32_t* ptr, const skcms_Matrix3x3& toXYZD50, int col) {
  150. ptr[0] = SkEndian_SwapBE32(kXYZ_PCSSpace);
  151. ptr[1] = 0;
  152. ptr[2] = SkEndian_SwapBE32(float_round_to_fixed(toXYZD50.vals[0][col]));
  153. ptr[3] = SkEndian_SwapBE32(float_round_to_fixed(toXYZD50.vals[1][col]));
  154. ptr[4] = SkEndian_SwapBE32(float_round_to_fixed(toXYZD50.vals[2][col]));
  155. }
  156. static void write_trc_tag(uint32_t* ptr, const skcms_TransferFunction& fn) {
  157. ptr[0] = SkEndian_SwapBE32(kTAG_ParaCurveType);
  158. ptr[1] = 0;
  159. ptr[2] = (uint32_t) (SkEndian_SwapBE16(kGABCDEF_ParaCurveType));
  160. ptr[3] = SkEndian_SwapBE32(float_round_to_fixed(fn.g));
  161. ptr[4] = SkEndian_SwapBE32(float_round_to_fixed(fn.a));
  162. ptr[5] = SkEndian_SwapBE32(float_round_to_fixed(fn.b));
  163. ptr[6] = SkEndian_SwapBE32(float_round_to_fixed(fn.c));
  164. ptr[7] = SkEndian_SwapBE32(float_round_to_fixed(fn.d));
  165. ptr[8] = SkEndian_SwapBE32(float_round_to_fixed(fn.e));
  166. ptr[9] = SkEndian_SwapBE32(float_round_to_fixed(fn.f));
  167. }
  168. static bool nearly_equal(float x, float y) {
  169. // A note on why I chose this tolerance: transfer_fn_almost_equal() uses a
  170. // tolerance of 0.001f, which doesn't seem to be enough to distinguish
  171. // between similar transfer functions, for example: gamma2.2 and sRGB.
  172. //
  173. // If the tolerance is 0.0f, then this we can't distinguish between two
  174. // different encodings of what is clearly the same colorspace. Some
  175. // experimentation with example files lead to this number:
  176. static constexpr float kTolerance = 1.0f / (1 << 11);
  177. return ::fabsf(x - y) <= kTolerance;
  178. }
  179. static bool nearly_equal(const skcms_TransferFunction& u,
  180. const skcms_TransferFunction& v) {
  181. return nearly_equal(u.g, v.g)
  182. && nearly_equal(u.a, v.a)
  183. && nearly_equal(u.b, v.b)
  184. && nearly_equal(u.c, v.c)
  185. && nearly_equal(u.d, v.d)
  186. && nearly_equal(u.e, v.e)
  187. && nearly_equal(u.f, v.f);
  188. }
  189. static bool nearly_equal(const skcms_Matrix3x3& u, const skcms_Matrix3x3& v) {
  190. for (int r = 0; r < 3; r++) {
  191. for (int c = 0; c < 3; c++) {
  192. if (!nearly_equal(u.vals[r][c], v.vals[r][c])) {
  193. return false;
  194. }
  195. }
  196. }
  197. return true;
  198. }
  199. // Return nullptr if the color profile doen't have a special name.
  200. const char* get_color_profile_description(const skcms_TransferFunction& fn,
  201. const skcms_Matrix3x3& toXYZD50) {
  202. bool srgb_xfer = nearly_equal(fn, SkNamedTransferFn::kSRGB);
  203. bool srgb_gamut = nearly_equal(toXYZD50, SkNamedGamut::kSRGB);
  204. if (srgb_xfer && srgb_gamut) {
  205. return "sRGB";
  206. }
  207. bool line_xfer = nearly_equal(fn, SkNamedTransferFn::kLinear);
  208. if (line_xfer && srgb_gamut) {
  209. return "Linear Transfer with sRGB Gamut";
  210. }
  211. bool twoDotTwo = nearly_equal(fn, SkNamedTransferFn::k2Dot2);
  212. if (twoDotTwo && srgb_gamut) {
  213. return "2.2 Transfer with sRGB Gamut";
  214. }
  215. if (twoDotTwo && nearly_equal(toXYZD50, SkNamedGamut::kAdobeRGB)) {
  216. return "AdobeRGB";
  217. }
  218. bool dcip3_gamut = nearly_equal(toXYZD50, SkNamedGamut::kDCIP3);
  219. if (srgb_xfer || line_xfer) {
  220. if (srgb_xfer && dcip3_gamut) {
  221. return "sRGB Transfer with DCI-P3 Gamut";
  222. }
  223. if (line_xfer && dcip3_gamut) {
  224. return "Linear Transfer with DCI-P3 Gamut";
  225. }
  226. bool rec2020 = nearly_equal(toXYZD50, SkNamedGamut::kRec2020);
  227. if (srgb_xfer && rec2020) {
  228. return "sRGB Transfer with Rec-BT-2020 Gamut";
  229. }
  230. if (line_xfer && rec2020) {
  231. return "Linear Transfer with Rec-BT-2020 Gamut";
  232. }
  233. }
  234. return nullptr;
  235. }
  236. static void get_color_profile_tag(char dst[kICCDescriptionTagSize],
  237. const skcms_TransferFunction& fn,
  238. const skcms_Matrix3x3& toXYZD50) {
  239. SkASSERT(dst);
  240. if (const char* description = get_color_profile_description(fn, toXYZD50)) {
  241. SkASSERT(strlen(description) < kICCDescriptionTagSize);
  242. strncpy(dst, description, kICCDescriptionTagSize);
  243. // "If the length of src is less than n, strncpy() writes additional
  244. // null bytes to dest to ensure that a total of n bytes are written."
  245. } else {
  246. strncpy(dst, kDescriptionTagBodyPrefix, sizeof(kDescriptionTagBodyPrefix));
  247. SkMD5 md5;
  248. md5.write(&toXYZD50, sizeof(toXYZD50));
  249. static_assert(sizeof(fn) == sizeof(float) * 7, "packed");
  250. md5.write(&fn, sizeof(fn));
  251. SkMD5::Digest digest = md5.finish();
  252. char* ptr = dst + sizeof(kDescriptionTagBodyPrefix);
  253. for (unsigned i = 0; i < sizeof(SkMD5::Digest); ++i) {
  254. uint8_t byte = digest.data[i];
  255. *ptr++ = SkHexadecimalDigits::gUpper[byte >> 4];
  256. *ptr++ = SkHexadecimalDigits::gUpper[byte & 0xF];
  257. }
  258. SkASSERT(ptr == dst + kICCDescriptionTagSize);
  259. }
  260. }
  261. sk_sp<SkData> SkWriteICCProfile(const skcms_TransferFunction& fn,
  262. const skcms_Matrix3x3& toXYZD50) {
  263. if (!is_valid_transfer_fn(fn)) {
  264. return nullptr;
  265. }
  266. SkAutoMalloc profile(kICCProfileSize);
  267. uint8_t* ptr = (uint8_t*) profile.get();
  268. // Write profile header
  269. memcpy(ptr, kICCHeader, sizeof(kICCHeader));
  270. ptr += sizeof(kICCHeader);
  271. // Write tag table
  272. memcpy(ptr, kICCTagTable, sizeof(kICCTagTable));
  273. ptr += sizeof(kICCTagTable);
  274. // Write profile description tag
  275. memcpy(ptr, kDescriptionTagHeader, sizeof(kDescriptionTagHeader));
  276. ptr += sizeof(kDescriptionTagHeader);
  277. {
  278. char colorProfileTag[kICCDescriptionTagSize];
  279. get_color_profile_tag(colorProfileTag, fn, toXYZD50);
  280. // ASCII --> big-endian UTF-16.
  281. for (size_t i = 0; i < kICCDescriptionTagSize; i++) {
  282. *ptr++ = 0;
  283. *ptr++ = colorProfileTag[i];
  284. }
  285. }
  286. // Write XYZ tags
  287. write_xyz_tag((uint32_t*) ptr, toXYZD50, 0);
  288. ptr += kTAG_XYZ_Bytes;
  289. write_xyz_tag((uint32_t*) ptr, toXYZD50, 1);
  290. ptr += kTAG_XYZ_Bytes;
  291. write_xyz_tag((uint32_t*) ptr, toXYZD50, 2);
  292. ptr += kTAG_XYZ_Bytes;
  293. // Write TRC tag
  294. write_trc_tag((uint32_t*) ptr, fn);
  295. ptr += kTAG_TRC_Bytes;
  296. // Write white point tag (must be D50)
  297. memcpy(ptr, kWhitePointTag, sizeof(kWhitePointTag));
  298. ptr += sizeof(kWhitePointTag);
  299. // Write copyright tag
  300. memcpy(ptr, kCopyrightTagHeader, sizeof(kCopyrightTagHeader));
  301. ptr += sizeof(kCopyrightTagHeader);
  302. memcpy(ptr, kCopyrightTagBody, sizeof(kCopyrightTagBody));
  303. ptr += sizeof(kCopyrightTagBody);
  304. SkASSERT(kICCProfileSize == ptr - (uint8_t*) profile.get());
  305. return SkData::MakeFromMalloc(profile.release(), kICCProfileSize);
  306. }