Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(92)

Side by Side Diff: src/core/SkColorSpace.cpp

Issue 1726823002: Set SkColorSpace object for PNGs and parse ICC profiles (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Rebase Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/core/SkColorSpace.h ('k') | tests/ColorSpaceTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 Google Inc. 2 * Copyright 2016 Google Inc.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license that can be 4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file. 5 * found in the LICENSE file.
6 */ 6 */
7 7
8 #include "SkAtomics.h" 8 #include "SkAtomics.h"
9 #include "SkColorSpace.h" 9 #include "SkColorSpace.h"
10 10
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 case kSRGB_Named: 151 case kSRGB_Named:
152 return new SkColorSpace(gSRGB_toXYZD50, gSRGB_gamma, kSRGB_Named); 152 return new SkColorSpace(gSRGB_toXYZD50, gSRGB_gamma, kSRGB_Named);
153 default: 153 default:
154 break; 154 break;
155 } 155 }
156 return nullptr; 156 return nullptr;
157 } 157 }
158 158
159 //////////////////////////////////////////////////////////////////////////////// /////////////////// 159 //////////////////////////////////////////////////////////////////////////////// ///////////////////
160 160
161 #include "SkFixed.h"
162 #include "SkTemplates.h"
163
164 #define SkColorSpacePrintf(...)
165
166 #define return_if_false(pred, msg) \
167 do { \
168 if (!(pred)) { \
169 SkColorSpacePrintf("Invalid ICC Profile: %s.\n", (msg)); \
170 return false; \
171 } \
172 } while (0)
173
174 #define return_null(msg) \
175 do { \
176 SkDebugf("Invalid ICC Profile: %s.\n", (msg)); \
177 return nullptr; \
178 } while (0)
179
180 static uint16_t read_big_endian_short(const uint8_t* ptr) {
181 return ptr[0] << 8 | ptr[1];
182 }
183
184 static uint32_t read_big_endian_int(const uint8_t* ptr) {
185 return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
186 }
187
188 // This is equal to the header size according to the ICC specification (128)
189 // plus the size of the tag count (4). We include the tag count since we
190 // always require it to be present anyway.
191 static const size_t kICCHeaderSize = 132;
192
193 // Contains a signature (4), offset (4), and size (4).
194 static const size_t kICCTagTableEntrySize = 12;
195
196 static const uint32_t kRGB_ColorSpace = SkSetFourByteTag('R', 'G', 'B', ' ');
197 static const uint32_t kGray_ColorSpace = SkSetFourByteTag('G', 'R', 'A', 'Y');
198
199 struct ICCProfileHeader {
200 // TODO (msarett):
201 // Can we ignore less of these fields?
202 uint32_t fSize;
203 uint32_t fCMMType_ignored;
204 uint32_t fVersion;
205 uint32_t fClassProfile;
206 uint32_t fColorSpace;
207 uint32_t fPCS;
208 uint32_t fDateTime_ignored[3];
209 uint32_t fSignature;
210 uint32_t fPlatformTarget_ignored;
211 uint32_t fFlags_ignored;
212 uint32_t fManufacturer_ignored;
213 uint32_t fDeviceModel_ignored;
214 uint32_t fDeviceAttributes_ignored[2];
215 uint32_t fRenderingIntent;
216 uint32_t fIlluminantXYZ_ignored[3];
217 uint32_t fCreator_ignored;
218 uint32_t fProfileId_ignored[4];
219 uint32_t fReserved_ignored[7];
220 uint32_t fTagCount;
221
222 void init(const uint8_t* src, size_t len) {
223 SkASSERT(kICCHeaderSize == sizeof(*this));
224
225 uint32_t* dst = (uint32_t*) this;
226 for (uint32_t i = 0; i < kICCHeaderSize / 4; i++, src+=4) {
227 dst[i] = read_big_endian_int(src);
228 }
229 }
230
231 bool valid() const {
232 // TODO (msarett):
233 // For now it's nice to fail loudly on invalid inputs. But, can we
234 // recover from some of these errors?
235
236 return_if_false(fSize >= kICCHeaderSize, "Size is too small");
237
238 uint8_t majorVersion = fVersion >> 24;
239 return_if_false(majorVersion <= 4, "Unsupported version");
240
241 const uint32_t kDisplay_Profile = SkSetFourByteTag('m', 'n', 't', 'r');
242 const uint32_t kInput_Profile = SkSetFourByteTag('s', 'c', 'n', 'r');
243 const uint32_t kOutput_Profile = SkSetFourByteTag('p', 'r', 't', 'r');
244 // TODO (msarett):
245 // Should we also support DeviceLink, ColorSpace, Abstract, or NamedColo r?
246 return_if_false(fClassProfile == kDisplay_Profile ||
247 fClassProfile == kInput_Profile ||
248 fClassProfile == kOutput_Profile,
249 "Unsupported class profile");
250
251 // TODO (msarett):
252 // There are many more color spaces that we could try to support.
253 return_if_false(fColorSpace == kRGB_ColorSpace || fColorSpace == kGray_C olorSpace,
254 "Unsupported color space");
255
256 const uint32_t kXYZ_PCSSpace = SkSetFourByteTag('X', 'Y', 'Z', ' ');
257 // TODO (msarett):
258 // Can we support PCS LAB as well?
259 return_if_false(fPCS == kXYZ_PCSSpace, "Unsupported PCS space");
260
261 return_if_false(fSignature == SkSetFourByteTag('a', 'c', 's', 'p'), "Bad signature");
262
263 // TODO (msarett):
264 // Should we treat different rendering intents differently?
265 // Valid rendering intents include kPerceptual (0), kRelative (1),
266 // kSaturation (2), and kAbsolute (3).
267 return_if_false(fRenderingIntent <= 3, "Bad rendering intent");
268
269 return_if_false(fTagCount <= 100, "Too many tags");
270
271 return true;
272 }
273 };
274
275 struct ICCTag {
276 uint32_t fSignature;
277 uint32_t fOffset;
278 uint32_t fLength;
279
280 const uint8_t* init(const uint8_t* src) {
281 fSignature = read_big_endian_int(src);
282 fOffset = read_big_endian_int(src + 4);
283 fLength = read_big_endian_int(src + 8);
284 return src + 12;
285 }
286
287 bool valid(size_t len) {
288 return_if_false(fOffset + fLength <= len, "Tag too large for ICC profile ");
289 return true;
290 }
291
292 const uint8_t* addr(const uint8_t* src) const {
293 return src + fOffset;
294 }
295
296 static const ICCTag* Find(const ICCTag tags[], int count, uint32_t signature ) {
297 for (int i = 0; i < count; ++i) {
298 if (tags[i].fSignature == signature) {
299 return &tags[i];
300 }
301 }
302 return nullptr;
303 }
304 };
305
306 // TODO (msarett):
307 // Should we recognize more tags?
308 static const uint32_t kTAG_rXYZ = SkSetFourByteTag('r', 'X', 'Y', 'Z');
309 static const uint32_t kTAG_gXYZ = SkSetFourByteTag('g', 'X', 'Y', 'Z');
310 static const uint32_t kTAG_bXYZ = SkSetFourByteTag('b', 'X', 'Y', 'Z');
311 static const uint32_t kTAG_rTRC = SkSetFourByteTag('r', 'T', 'R', 'C');
312 static const uint32_t kTAG_gTRC = SkSetFourByteTag('g', 'T', 'R', 'C');
313 static const uint32_t kTAG_bTRC = SkSetFourByteTag('b', 'T', 'R', 'C');
314
315 bool load_xyz(float dst[3], const uint8_t* src, size_t len) {
316 if (len < 20) {
317 SkColorSpacePrintf("XYZ tag is too small (%d bytes)", len);
318 return false;
319 }
320
321 dst[0] = SkFixedToFloat(read_big_endian_int(src + 8));
322 dst[1] = SkFixedToFloat(read_big_endian_int(src + 12));
323 dst[2] = SkFixedToFloat(read_big_endian_int(src + 16));
324 SkColorSpacePrintf("XYZ %g %g %g\n", dst[0], dst[1], dst[2]);
325 return true;
326 }
327
328 static const uint32_t kTAG_CurveType = SkSetFourByteTag('c', 'u', 'r', 'v');
329 static const uint32_t kTAG_ParaCurveType = SkSetFourByteTag('p', 'a', 'r', 'a');
330
331 static bool load_gamma(float* gamma, const uint8_t* src, size_t len) {
332 if (len < 14) {
333 SkColorSpacePrintf("gamma tag is too small (%d bytes)", len);
334 return false;
335 }
336
337 uint32_t type = read_big_endian_int(src);
338 switch (type) {
339 case kTAG_CurveType: {
340 uint32_t count = read_big_endian_int(src + 8);
341 if (0 == count) {
342 return false;
343 }
344
345 const uint16_t* table = (const uint16_t*) (src + 12);
346 if (1 == count) {
347 // Table entry is the exponent (bias 256).
348 uint16_t value = read_big_endian_short((const uint8_t*) table);
349 *gamma = value / 256.0f;
350 SkColorSpacePrintf("gamma %d %g\n", value, *gamma);
351 return true;
352 }
353
354 // Check length again if we have a table.
355 if (len < 12 + 2 * count) {
356 SkColorSpacePrintf("gamma tag is too small (%d bytes)", len);
357 return false;
358 }
359
360 // Print the interpolation table. For now, we ignore this and guess 2.2f.
361 for (uint32_t i = 0; i < count; i++) {
362 SkColorSpacePrintf("curve[%d] %d\n", i,
363 read_big_endian_short((const uint8_t*) &table[i]));
364 }
365
366 *gamma = 2.2f;
367 return true;
368 }
369 case kTAG_ParaCurveType:
370 // Guess 2.2f.
371 SkColorSpacePrintf("parametric curve\n");
372 *gamma = 2.2f;
373 return true;
374 default:
375 SkColorSpacePrintf("Unsupported gamma tag type %d\n", type);
376 return false;
377 }
378 }
379
380 SkColorSpace* SkColorSpace::NewICC(const void* base, size_t len) {
381 const uint8_t* ptr = (const uint8_t*) base;
382
383 if (len < kICCHeaderSize) {
384 return_null("Data is not large enough to contain an ICC profile");
385 }
386
387 // Read the ICC profile header and check to make sure that it is valid.
388 ICCProfileHeader header;
389 header.init(ptr, len);
390 if (!header.valid()) {
391 return nullptr;
392 }
393
394 // Adjust ptr and len before reading the tags.
395 if (len < header.fSize) {
396 SkColorSpacePrintf("ICC profile might be truncated.\n");
397 } else if (len > header.fSize) {
398 SkColorSpacePrintf("Caller provided extra data beyond the end of the ICC profile.\n");
399 len = header.fSize;
400 }
401 ptr += kICCHeaderSize;
402 len -= kICCHeaderSize;
403
404 // Parse tag headers.
405 uint32_t tagCount = header.fTagCount;
406 SkColorSpacePrintf("ICC profile contains %d tags.\n", tagCount);
407 if (len < kICCTagTableEntrySize * tagCount) {
408 return_null("Not enough input data to read tag table entries");
409 }
410
411 SkAutoTArray<ICCTag> tags(tagCount);
412 for (uint32_t i = 0; i < tagCount; i++) {
413 ptr = tags[i].init(ptr);
414 SkColorSpacePrintf("[%d] %c%c%c%c %d %d\n", i, (tags[i].fSignature >> 24 ) & 0xFF,
415 (tags[i].fSignature >> 16) & 0xFF, (tags[i].fSignature >> 8) & 0xFF,
416 (tags[i].fSignature >> 0) & 0xFF, tags[i].fOffset, tags[i].fLen gth);
417
418 if (!tags[i].valid(kICCHeaderSize + len)) {
419 return_null("Tag is too large to fit in ICC profile");
420 }
421 }
422
423 // Load our XYZ and gamma matrices.
424 SkFloat3x3 toXYZ;
425 SkFloat3 gamma {{ 1.0f, 1.0f, 1.0f }};
426 switch (header.fColorSpace) {
427 case kRGB_ColorSpace: {
428 const ICCTag* r = ICCTag::Find(tags.get(), tagCount, kTAG_rXYZ);
429 const ICCTag* g = ICCTag::Find(tags.get(), tagCount, kTAG_gXYZ);
430 const ICCTag* b = ICCTag::Find(tags.get(), tagCount, kTAG_bXYZ);
431 if (!r || !g || !b) {
432 return_null("Need rgb tags for XYZ space");
433 }
434
435 if (!load_xyz(&toXYZ.fMat[0], r->addr((const uint8_t*) base), r->fLe ngth) ||
436 !load_xyz(&toXYZ.fMat[3], g->addr((const uint8_t*) base), g->fLe ngth) ||
437 !load_xyz(&toXYZ.fMat[6], b->addr((const uint8_t*) base), b->fLe ngth))
438 {
439 return_null("Need valid rgb tags for XYZ space");
440 }
441
442 r = ICCTag::Find(tags.get(), tagCount, kTAG_rTRC);
443 g = ICCTag::Find(tags.get(), tagCount, kTAG_gTRC);
444 b = ICCTag::Find(tags.get(), tagCount, kTAG_bTRC);
445 if (!r || !load_gamma(&gamma.fVec[0], r->addr((const uint8_t*) base) , r->fLength)) {
446 SkColorSpacePrintf("Failed to read R gamma tag.\n");
447 }
448 if (!g || !load_gamma(&gamma.fVec[1], g->addr((const uint8_t*) base) , g->fLength)) {
449 SkColorSpacePrintf("Failed to read G gamma tag.\n");
450 }
451 if (!b || !load_gamma(&gamma.fVec[2], b->addr((const uint8_t*) base) , b->fLength)) {
452 SkColorSpacePrintf("Failed to read B gamma tag.\n");
453 }
454 return SkColorSpace::NewRGB(toXYZ, gamma);
455 }
456 default:
457 break;
458 }
459
460 return_null("ICC profile contains unsupported colorspace");
461 }
462
463 //////////////////////////////////////////////////////////////////////////////// ///////////////////
464
161 SkColorSpace::Result SkColorSpace::Concat(const SkColorSpace* src, const SkColor Space* dst, 465 SkColorSpace::Result SkColorSpace::Concat(const SkColorSpace* src, const SkColor Space* dst,
162 SkFloat3x3* result) { 466 SkFloat3x3* result) {
163 if (!src || !dst || (src->named() == kDevice_Named) || (src->named() == dst- >named())) { 467 if (!src || !dst || (src->named() == kDevice_Named) || (src->named() == dst- >named())) {
164 if (result) { 468 if (result) {
165 *result = {{ 1, 0, 0, 0, 1, 0, 0, 0, 1 }}; 469 *result = {{ 1, 0, 0, 0, 1, 0, 0, 0, 1 }};
166 } 470 }
167 return kIdentity_Result; 471 return kIdentity_Result;
168 } 472 }
169 if (result) { 473 if (result) {
170 *result = concat(src->fToXYZD50, invert(dst->fToXYZD50)); 474 *result = concat(src->fToXYZD50, invert(dst->fToXYZD50));
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 } 540 }
237 541
238 // D65 white point of Rec. 709 [8] are: 542 // D65 white point of Rec. 709 [8] are:
239 // 543 //
240 // D65 white-point in unit luminance XYZ = 0.9505, 1.0000, 1.0890 544 // D65 white-point in unit luminance XYZ = 0.9505, 1.0000, 1.0890
241 // 545 //
242 // R G B white 546 // R G B white
243 // x 0.640 0.300 0.150 0.3127 547 // x 0.640 0.300 0.150 0.3127
244 // y 0.330 0.600 0.060 0.3290 548 // y 0.330 0.600 0.060 0.3290
245 // z 0.030 0.100 0.790 0.3582 549 // z 0.030 0.100 0.790 0.3582
OLDNEW
« no previous file with comments | « src/core/SkColorSpace.h ('k') | tests/ColorSpaceTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698