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