• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
8 #include "SkAutoMalloc.h"
9 #include "SkColorSpacePriv.h"
10 #include "SkEndian.h"
11 #include "SkFixed.h"
12 #include "SkICC.h"
13 #include "SkICCPriv.h"
14 #include "SkMD5.h"
15 #include "SkUtils.h"
16 
17 static constexpr char kDescriptionTagBodyPrefix[12] =
18         { 'G', 'o', 'o', 'g', 'l', 'e', '/', 'S', 'k', 'i', 'a' , '/'};
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
25 
26 static_assert(SkIsAlign4(kDescriptionTagBodySize), "Description must be aligned to 4-bytes.");
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
33     SkEndian_SwapBE32(kDescriptionTagBodySize),              // Length of string
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[] = {
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,
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 
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');
71 static constexpr uint32_t kTAG_desc_Bytes = sizeof(kDescriptionTagHeader) +
72                                             kDescriptionTagBodySize;
73 static constexpr uint32_t kTAG_desc_Offset = kICCHeaderSize +
74                                              kICCNumEntries * kICCTagTableEntrySize;
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 
81 static constexpr uint32_t kTAG_TRC_Bytes = 40;
82 static constexpr uint32_t kTAG_rTRC_Offset = kTAG_bXYZ_Offset + kTAG_XYZ_Bytes;
83 static constexpr uint32_t kTAG_gTRC_Offset = kTAG_rTRC_Offset;
84 static constexpr uint32_t kTAG_bTRC_Offset = kTAG_rTRC_Offset;
85 
86 static constexpr uint32_t kTAG_wtpt = SkSetFourByteTag('w', 't', 'p', 't');
87 static constexpr uint32_t kTAG_wtpt_Offset = kTAG_bTRC_Offset + kTAG_TRC_Bytes;
88 
89 static constexpr uint32_t kTAG_cprt = SkSetFourByteTag('c', 'p', 'r', 't');
90 static constexpr uint32_t kTAG_cprt_Bytes = sizeof(kCopyrightTagHeader) +
91                                             sizeof(kCopyrightTagBody);
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 
96 static constexpr uint32_t kICCHeader[kICCHeaderSize / 4] {
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 
120 static constexpr uint32_t kICCTagTable[3 * kICCNumEntries] {
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 
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.
float_round_to_fixed(float x)171 static SkFixed float_round_to_fixed(float x) {
172     return sk_float_saturate2int((float)floor((double)x * SK_Fixed1 + 0.5));
173 }
174 
write_xyz_tag(uint32_t * ptr,const skcms_Matrix3x3 & toXYZD50,int col)175 static void write_xyz_tag(uint32_t* ptr, const skcms_Matrix3x3& toXYZD50, int col) {
176     ptr[0] = SkEndian_SwapBE32(kXYZ_PCSSpace);
177     ptr[1] = 0;
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]));
181 }
182 
write_trc_tag(uint32_t * ptr,const skcms_TransferFunction & fn)183 static void write_trc_tag(uint32_t* ptr, const skcms_TransferFunction& fn) {
184     ptr[0] = SkEndian_SwapBE32(kTAG_ParaCurveType);
185     ptr[1] = 0;
186     ptr[2] = (uint32_t) (SkEndian_SwapBE16(kGABCDEF_ParaCurveType));
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));
194 }
195 
nearly_equal(float x,float y)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 }
207 
nearly_equal(const skcms_TransferFunction & u,const skcms_TransferFunction & v)208 static bool nearly_equal(const skcms_TransferFunction& u,
209                          const skcms_TransferFunction& v) {
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);
217 }
218 
nearly_equal(const skcms_Matrix3x3 & u,const skcms_Matrix3x3 & v)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             }
225         }
226     }
227     return true;
228 }
229 
230 // Return nullptr if the color profile doen't have a special name.
get_color_profile_description(const skcms_TransferFunction & fn,const skcms_Matrix3x3 & toXYZD50)231 const char* get_color_profile_description(const skcms_TransferFunction& fn,
232                                           const skcms_Matrix3x3& toXYZD50) {
233     bool srgb_xfer = nearly_equal(fn, SkNamedTransferFn::kSRGB);
234     bool srgb_gamut = nearly_equal(toXYZD50, SkNamedGamut::kSRGB);
235     if (srgb_xfer && srgb_gamut) {
236         return "sRGB";
237     }
238     bool line_xfer = nearly_equal(fn, SkNamedTransferFn::kLinear);
239     if (line_xfer && srgb_gamut) {
240         return "Linear Transfer with sRGB Gamut";
241     }
242     bool twoDotTwo = nearly_equal(fn, SkNamedTransferFn::k2Dot2);
243     if (twoDotTwo && srgb_gamut) {
244         return "2.2 Transfer with sRGB Gamut";
245     }
246     if (twoDotTwo && nearly_equal(toXYZD50, SkNamedGamut::kAdobeRGB)) {
247         return "AdobeRGB";
248     }
249     bool dcip3_gamut = nearly_equal(toXYZD50, SkNamedGamut::kDCIP3);
250     if (srgb_xfer || line_xfer) {
251         if (srgb_xfer && dcip3_gamut) {
252             return "sRGB Transfer with DCI-P3 Gamut";
253         }
254         if (line_xfer && dcip3_gamut) {
255             return "Linear Transfer with DCI-P3 Gamut";
256         }
257         bool rec2020 = nearly_equal(toXYZD50, SkNamedGamut::kRec2020);
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     }
265     return nullptr;
266 }
267 
get_color_profile_tag(char dst[kICCDescriptionTagSize],const skcms_TransferFunction & fn,const skcms_Matrix3x3 & toXYZD50)268 static void get_color_profile_tag(char dst[kICCDescriptionTagSize],
269                                  const skcms_TransferFunction& fn,
270                                  const skcms_Matrix3x3& toXYZD50) {
271     SkASSERT(dst);
272     if (const char* description = get_color_profile_description(fn, toXYZD50)) {
273         SkASSERT(strlen(description) < kICCDescriptionTagSize);
274         strncpy(dst, description, kICCDescriptionTagSize);
275         // "If the length of src is less than n, strncpy() writes additional
276         // null bytes to dest to ensure that a total of n bytes are written."
277     } else {
278         strncpy(dst, kDescriptionTagBodyPrefix, sizeof(kDescriptionTagBodyPrefix));
279         SkMD5 md5;
280         md5.write(&toXYZD50, sizeof(toXYZD50));
281         static_assert(sizeof(fn) == sizeof(float) * 7, "packed");
282         md5.write(&fn, sizeof(fn));
283         SkMD5::Digest digest;
284         md5.finish(digest);
285         char* ptr = dst + sizeof(kDescriptionTagBodyPrefix);
286         for (unsigned i = 0; i < sizeof(SkMD5::Digest); ++i) {
287             uint8_t byte = digest.data[i];
288             *ptr++ = SkHexadecimalDigits::gUpper[byte >> 4];
289             *ptr++ = SkHexadecimalDigits::gUpper[byte & 0xF];
290         }
291         SkASSERT(ptr == dst + kICCDescriptionTagSize);
292     }
293 }
294 
SkWriteICCProfile(const skcms_TransferFunction & fn,const skcms_Matrix3x3 & toXYZD50)295 sk_sp<SkData> SkWriteICCProfile(const skcms_TransferFunction& fn,
296                                 const skcms_Matrix3x3& toXYZD50) {
297     if (!is_valid_transfer_fn(fn)) {
298         return nullptr;
299     }
300 
301     SkAutoMalloc profile(kICCProfileSize);
302     uint8_t* ptr = (uint8_t*) profile.get();
303 
304     // Write profile header
305     memcpy(ptr, kICCHeader, sizeof(kICCHeader));
306     ptr += sizeof(kICCHeader);
307 
308     // Write tag table
309     memcpy(ptr, kICCTagTable, sizeof(kICCTagTable));
310     ptr += sizeof(kICCTagTable);
311 
312     // Write profile description tag
313     memcpy(ptr, kDescriptionTagHeader, sizeof(kDescriptionTagHeader));
314     ptr += sizeof(kDescriptionTagHeader);
315     {
316         char colorProfileTag[kICCDescriptionTagSize];
317         get_color_profile_tag(colorProfileTag, fn, toXYZD50);
318 
319         // ASCII --> big-endian UTF-16.
320         for (size_t i = 0; i < kICCDescriptionTagSize; i++) {
321             *ptr++ = 0;
322             *ptr++ = colorProfileTag[i];
323         }
324     }
325 
326     // Write XYZ tags
327     write_xyz_tag((uint32_t*) ptr, toXYZD50, 0);
328     ptr += kTAG_XYZ_Bytes;
329     write_xyz_tag((uint32_t*) ptr, toXYZD50, 1);
330     ptr += kTAG_XYZ_Bytes;
331     write_xyz_tag((uint32_t*) ptr, toXYZD50, 2);
332     ptr += kTAG_XYZ_Bytes;
333 
334     // Write TRC tag
335     write_trc_tag((uint32_t*) ptr, fn);
336     ptr += kTAG_TRC_Bytes;
337 
338     // Write white point tag (must be D50)
339     memcpy(ptr, kWhitePointTag, sizeof(kWhitePointTag));
340     ptr += sizeof(kWhitePointTag);
341 
342     // Write copyright tag
343     memcpy(ptr, kCopyrightTagHeader, sizeof(kCopyrightTagHeader));
344     ptr += sizeof(kCopyrightTagHeader);
345     memcpy(ptr, kCopyrightTagBody, sizeof(kCopyrightTagBody));
346     ptr += sizeof(kCopyrightTagBody);
347 
348     SkASSERT(kICCProfileSize == ptr - (uint8_t*) profile.get());
349     return SkData::MakeFromMalloc(profile.release(), kICCProfileSize);
350 }
351