1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
|
/*
* Copyright 2016 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkAutoMalloc.h"
#include "SkColorSpace_Base.h"
#include "SkColorSpace_XYZ.h"
#include "SkColorSpacePriv.h"
#include "SkColorSpaceXformPriv.h"
#include "SkEndian.h"
#include "SkFixed.h"
#include "SkICC.h"
#include "SkICCPriv.h"
SkICC::SkICC(sk_sp<SkColorSpace> colorSpace)
: fColorSpace(std::move(colorSpace))
{}
sk_sp<SkICC> SkICC::Make(const void* ptr, size_t len) {
sk_sp<SkColorSpace> colorSpace = SkColorSpace::MakeICC(ptr, len);
if (!colorSpace) {
return nullptr;
}
return sk_sp<SkICC>(new SkICC(std::move(colorSpace)));
}
bool SkICC::toXYZD50(SkMatrix44* toXYZD50) const {
const SkMatrix44* m = as_CSB(fColorSpace)->toXYZD50();
if (!m) {
return false;
}
*toXYZD50 = *m;
return true;
}
bool SkICC::isNumericalTransferFn(SkColorSpaceTransferFn* coeffs) const {
return as_CSB(fColorSpace)->onIsNumericalTransferFn(coeffs);
}
static const int kDefaultTableSize = 512; // Arbitrary
void fn_to_table(float* tablePtr, const SkColorSpaceTransferFn& fn) {
// Y = (aX + b)^g + e for X >= d
// Y = cX + f otherwise
for (int i = 0; i < kDefaultTableSize; i++) {
float x = ((float) i) / ((float) (kDefaultTableSize - 1));
if (x >= fn.fD) {
tablePtr[i] = clamp_0_1(powf(fn.fA * x + fn.fB, fn.fG) + fn.fE);
} else {
tablePtr[i] = clamp_0_1(fn.fC * x + fn.fF);
}
}
}
void copy_to_table(float* tablePtr, const SkGammas* gammas, int index) {
SkASSERT(gammas->isTable(index));
const float* ptr = gammas->table(index);
const size_t bytes = gammas->tableSize(index) * sizeof(float);
memcpy(tablePtr, ptr, bytes);
}
bool SkICC::rawTransferFnData(Tables* tables) const {
if (SkColorSpace_Base::Type::kA2B == as_CSB(fColorSpace)->type()) {
return false;
}
SkColorSpace_XYZ* colorSpace = (SkColorSpace_XYZ*) fColorSpace.get();
SkColorSpaceTransferFn fn;
if (this->isNumericalTransferFn(&fn)) {
tables->fStorage = SkData::MakeUninitialized(kDefaultTableSize * sizeof(float));
fn_to_table((float*) tables->fStorage->writable_data(), fn);
tables->fRed.fOffset = tables->fGreen.fOffset = tables->fBlue.fOffset = 0;
tables->fRed.fCount = tables->fGreen.fCount = tables->fBlue.fCount = kDefaultTableSize;
return true;
}
const SkGammas* gammas = colorSpace->gammas();
SkASSERT(gammas);
if (gammas->data(0) == gammas->data(1) && gammas->data(0) == gammas->data(2)) {
SkASSERT(gammas->isTable(0));
tables->fStorage = SkData::MakeUninitialized(gammas->tableSize(0) * sizeof(float));
copy_to_table((float*) tables->fStorage->writable_data(), gammas, 0);
tables->fRed.fOffset = tables->fGreen.fOffset = tables->fBlue.fOffset = 0;
tables->fRed.fCount = tables->fGreen.fCount = tables->fBlue.fCount = gammas->tableSize(0);
return true;
}
// Determine the storage size.
size_t storageSize = 0;
for (int i = 0; i < 3; i++) {
if (gammas->isTable(i)) {
storageSize += gammas->tableSize(i) * sizeof(float);
} else {
storageSize += kDefaultTableSize * sizeof(float);
}
}
// Fill in the tables.
tables->fStorage = SkData::MakeUninitialized(storageSize);
float* ptr = (float*) tables->fStorage->writable_data();
size_t offset = 0;
Channel rgb[3];
for (int i = 0; i < 3; i++) {
if (gammas->isTable(i)) {
copy_to_table(ptr, gammas, i);
rgb[i].fOffset = offset;
rgb[i].fCount = gammas->tableSize(i);
offset += rgb[i].fCount * sizeof(float);
ptr += rgb[i].fCount;
continue;
}
if (gammas->isNamed(i)) {
SkAssertResult(named_to_parametric(&fn, gammas->data(i).fNamed));
} else if (gammas->isValue(i)) {
value_to_parametric(&fn, gammas->data(i).fValue);
} else {
SkASSERT(gammas->isParametric(i));
fn = gammas->params(i);
}
fn_to_table(ptr, fn);
rgb[i].fOffset = offset;
rgb[i].fCount = kDefaultTableSize;
offset += kDefaultTableSize * sizeof(float);
ptr += kDefaultTableSize;
}
tables->fRed = rgb[0];
tables->fGreen = rgb[1];
tables->fBlue = rgb[2];
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
// Google Skia (UTF-16)
static constexpr uint8_t kDescriptionTagBody[] = {
0x00, 0x47, 0x00, 0x6f, 0x00, 0x6f, 0x00, 0x67, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x20, 0x00,
0x53, 0x00, 0x6b, 0x00, 0x69, 0x00, 0x61, 0x00, 0x20,
};
static_assert(SkIsAlign4(sizeof(kDescriptionTagBody)), "Description must be aligned to 4-bytes.");
static constexpr uint32_t kDescriptionTagHeader[7] {
SkEndian_SwapBE32(kTAG_TextType), // Type signature
0, // Reserved
SkEndian_SwapBE32(1), // Number of records
SkEndian_SwapBE32(12), // Record size (must be 12)
SkEndian_SwapBE32(SkSetFourByteTag('e', 'n', 'U', 'S')), // English USA
SkEndian_SwapBE32(sizeof(kDescriptionTagBody)), // Length of string
SkEndian_SwapBE32(28), // Offset of string
};
static constexpr uint32_t kWhitePointTag[5] {
SkEndian_SwapBE32(kXYZ_PCSSpace),
0,
SkEndian_SwapBE32(0x0000f6d6), // X = 0.96420 (D50)
SkEndian_SwapBE32(0x00010000), // Y = 1.00000 (D50)
SkEndian_SwapBE32(0x0000d32d), // Z = 0.82491 (D50)
};
// Google Inc. 2016 (UTF-16)
static constexpr uint8_t kCopyrightTagBody[] = {
0x00, 0x47, 0x00, 0x6f, 0x00, 0x6f, 0x00, 0x67, 0x00, 0x6c, 0x00, 0x65, 0x00, 0x20, 0x00,
0x49, 0x00, 0x6e, 0x00, 0x63, 0x00, 0x2e, 0x00, 0x20, 0x00, 0x32, 0x00, 0x30, 0x00, 0x31,
0x00, 0x36,
};
static_assert(SkIsAlign4(sizeof(kCopyrightTagBody)), "Copyright must be aligned to 4-bytes.");
static constexpr uint32_t kCopyrightTagHeader[7] {
SkEndian_SwapBE32(kTAG_TextType), // Type signature
0, // Reserved
SkEndian_SwapBE32(1), // Number of records
SkEndian_SwapBE32(12), // Record size (must be 12)
SkEndian_SwapBE32(SkSetFourByteTag('e', 'n', 'U', 'S')), // English USA
SkEndian_SwapBE32(sizeof(kCopyrightTagBody)), // Length of string
SkEndian_SwapBE32(28), // Offset of string
};
// We will write a profile with the minimum nine required tags.
static constexpr uint32_t kICCNumEntries = 9;
static constexpr uint32_t kTAG_desc = SkSetFourByteTag('d', 'e', 's', 'c');
static constexpr uint32_t kTAG_desc_Bytes = sizeof(kDescriptionTagHeader) +
sizeof(kDescriptionTagBody);
static constexpr uint32_t kTAG_desc_Offset = kICCHeaderSize +
kICCNumEntries * kICCTagTableEntrySize;
static constexpr uint32_t kTAG_XYZ_Bytes = 20;
static constexpr uint32_t kTAG_rXYZ_Offset = kTAG_desc_Offset + kTAG_desc_Bytes;
static constexpr uint32_t kTAG_gXYZ_Offset = kTAG_rXYZ_Offset + kTAG_XYZ_Bytes;
static constexpr uint32_t kTAG_bXYZ_Offset = kTAG_gXYZ_Offset + kTAG_XYZ_Bytes;
static constexpr uint32_t kTAG_TRC_Bytes = 40;
static constexpr uint32_t kTAG_rTRC_Offset = kTAG_bXYZ_Offset + kTAG_XYZ_Bytes;
static constexpr uint32_t kTAG_gTRC_Offset = kTAG_rTRC_Offset;
static constexpr uint32_t kTAG_bTRC_Offset = kTAG_rTRC_Offset;
static constexpr uint32_t kTAG_wtpt = SkSetFourByteTag('w', 't', 'p', 't');
static constexpr uint32_t kTAG_wtpt_Offset = kTAG_bTRC_Offset + kTAG_TRC_Bytes;
static constexpr uint32_t kTAG_cprt = SkSetFourByteTag('c', 'p', 'r', 't');
static constexpr uint32_t kTAG_cprt_Bytes = sizeof(kCopyrightTagHeader) +
sizeof(kCopyrightTagBody);
static constexpr uint32_t kTAG_cprt_Offset = kTAG_wtpt_Offset + kTAG_XYZ_Bytes;
static constexpr uint32_t kICCProfileSize = kTAG_cprt_Offset + kTAG_cprt_Bytes;
static constexpr uint32_t kICCHeader[kICCHeaderSize / 4] {
SkEndian_SwapBE32(kICCProfileSize), // Size of the profile
0, // Preferred CMM type (ignored)
SkEndian_SwapBE32(0x02100000), // Version 2.1
SkEndian_SwapBE32(kDisplay_Profile), // Display device profile
SkEndian_SwapBE32(kRGB_ColorSpace), // RGB input color space
SkEndian_SwapBE32(kXYZ_PCSSpace), // XYZ profile connection space
0, 0, 0, // Date and time (ignored)
SkEndian_SwapBE32(kACSP_Signature), // Profile signature
0, // Platform target (ignored)
0x00000000, // Flags: not embedded, can be used independently
0, // Device manufacturer (ignored)
0, // Device model (ignored)
0, 0, // Device attributes (ignored)
SkEndian_SwapBE32(1), // Relative colorimetric rendering intent
SkEndian_SwapBE32(0x0000f6d6), // D50 standard illuminant (X)
SkEndian_SwapBE32(0x00010000), // D50 standard illuminant (Y)
SkEndian_SwapBE32(0x0000d32d), // D50 standard illuminant (Z)
0, // Profile creator (ignored)
0, 0, 0, 0, // Profile id checksum (ignored)
0, 0, 0, 0, 0, 0, 0, // Reserved (ignored)
SkEndian_SwapBE32(kICCNumEntries), // Number of tags
};
static constexpr uint32_t kICCTagTable[3 * kICCNumEntries] {
// Profile description
SkEndian_SwapBE32(kTAG_desc),
SkEndian_SwapBE32(kTAG_desc_Offset),
SkEndian_SwapBE32(kTAG_desc_Bytes),
// rXYZ
SkEndian_SwapBE32(kTAG_rXYZ),
SkEndian_SwapBE32(kTAG_rXYZ_Offset),
SkEndian_SwapBE32(kTAG_XYZ_Bytes),
// gXYZ
SkEndian_SwapBE32(kTAG_gXYZ),
SkEndian_SwapBE32(kTAG_gXYZ_Offset),
SkEndian_SwapBE32(kTAG_XYZ_Bytes),
// bXYZ
SkEndian_SwapBE32(kTAG_bXYZ),
SkEndian_SwapBE32(kTAG_bXYZ_Offset),
SkEndian_SwapBE32(kTAG_XYZ_Bytes),
// rTRC
SkEndian_SwapBE32(kTAG_rTRC),
SkEndian_SwapBE32(kTAG_rTRC_Offset),
SkEndian_SwapBE32(kTAG_TRC_Bytes),
// gTRC
SkEndian_SwapBE32(kTAG_gTRC),
SkEndian_SwapBE32(kTAG_gTRC_Offset),
SkEndian_SwapBE32(kTAG_TRC_Bytes),
// bTRC
SkEndian_SwapBE32(kTAG_bTRC),
SkEndian_SwapBE32(kTAG_bTRC_Offset),
SkEndian_SwapBE32(kTAG_TRC_Bytes),
// White point
SkEndian_SwapBE32(kTAG_wtpt),
SkEndian_SwapBE32(kTAG_wtpt_Offset),
SkEndian_SwapBE32(kTAG_XYZ_Bytes),
// Copyright
SkEndian_SwapBE32(kTAG_cprt),
SkEndian_SwapBE32(kTAG_cprt_Offset),
SkEndian_SwapBE32(kTAG_cprt_Bytes),
};
static void write_xyz_tag(uint32_t* ptr, const SkMatrix44& toXYZ, int col) {
ptr[0] = SkEndian_SwapBE32(kXYZ_PCSSpace);
ptr[1] = 0;
ptr[2] = SkEndian_SwapBE32(SkFloatToFixed(toXYZ.getFloat(0, col)));
ptr[3] = SkEndian_SwapBE32(SkFloatToFixed(toXYZ.getFloat(1, col)));
ptr[4] = SkEndian_SwapBE32(SkFloatToFixed(toXYZ.getFloat(2, col)));
}
static void write_trc_tag(uint32_t* ptr, const SkColorSpaceTransferFn& fn) {
ptr[0] = SkEndian_SwapBE32(kTAG_ParaCurveType);
ptr[1] = 0;
ptr[2] = (uint32_t) (SkEndian_SwapBE16(kGABCDEF_ParaCurveType));
ptr[3] = SkEndian_SwapBE32(SkFloatToFixed(fn.fG));
ptr[4] = SkEndian_SwapBE32(SkFloatToFixed(fn.fA));
ptr[5] = SkEndian_SwapBE32(SkFloatToFixed(fn.fB));
ptr[6] = SkEndian_SwapBE32(SkFloatToFixed(fn.fC));
ptr[7] = SkEndian_SwapBE32(SkFloatToFixed(fn.fD));
ptr[8] = SkEndian_SwapBE32(SkFloatToFixed(fn.fE));
ptr[9] = SkEndian_SwapBE32(SkFloatToFixed(fn.fF));
}
static bool is_3x3(const SkMatrix44& toXYZD50) {
return 0.0f == toXYZD50.get(3, 0) && 0.0f == toXYZD50.get(3, 1) && 0.0f == toXYZD50.get(3, 2) &&
0.0f == toXYZD50.get(0, 3) && 0.0f == toXYZD50.get(1, 3) && 0.0f == toXYZD50.get(2, 3) &&
1.0f == toXYZD50.get(3, 3);
}
sk_sp<SkData> SkICC::WriteToICC(const SkColorSpaceTransferFn& fn, const SkMatrix44& toXYZD50) {
if (!is_3x3(toXYZD50) || !is_valid_transfer_fn(fn)) {
return nullptr;
}
SkAutoMalloc profile(kICCProfileSize);
uint8_t* ptr = (uint8_t*) profile.get();
// Write profile header
memcpy(ptr, kICCHeader, sizeof(kICCHeader));
ptr += sizeof(kICCHeader);
// Write tag table
memcpy(ptr, kICCTagTable, sizeof(kICCTagTable));
ptr += sizeof(kICCTagTable);
// Write profile description tag
memcpy(ptr, kDescriptionTagHeader, sizeof(kDescriptionTagHeader));
ptr += sizeof(kDescriptionTagHeader);
memcpy(ptr, kDescriptionTagBody, sizeof(kDescriptionTagBody));
ptr += sizeof(kDescriptionTagBody);
// Write XYZ tags
write_xyz_tag((uint32_t*) ptr, toXYZD50, 0);
ptr += kTAG_XYZ_Bytes;
write_xyz_tag((uint32_t*) ptr, toXYZD50, 1);
ptr += kTAG_XYZ_Bytes;
write_xyz_tag((uint32_t*) ptr, toXYZD50, 2);
ptr += kTAG_XYZ_Bytes;
// Write TRC tag
write_trc_tag((uint32_t*) ptr, fn);
ptr += kTAG_TRC_Bytes;
// Write white point tag (must be D50)
memcpy(ptr, kWhitePointTag, sizeof(kWhitePointTag));
ptr += sizeof(kWhitePointTag);
// Write copyright tag
memcpy(ptr, kCopyrightTagHeader, sizeof(kCopyrightTagHeader));
ptr += sizeof(kCopyrightTagHeader);
memcpy(ptr, kCopyrightTagBody, sizeof(kCopyrightTagBody));
ptr += sizeof(kCopyrightTagBody);
SkASSERT(kICCProfileSize == ptr - (uint8_t*) profile.get());
return SkData::MakeFromMalloc(profile.release(), kICCProfileSize);
}
|