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

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

Issue 1945693002: Create a single, unique pointer to sRGB color space (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 7 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
« src/core/SkColorSpace.h ('K') | « src/core/SkColorSpace.h ('k') | no next file » | 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 #include "SkOncePtr.h"
11
12 static bool color_space_almost_equal(float a, float b) {
13 return SkTAbs(a - b) < 0.01f;
14 }
10 15
11 void SkFloat3::dump() const { 16 void SkFloat3::dump() const {
12 SkDebugf("[%7.4f %7.4f %7.4f]\n", fVec[0], fVec[1], fVec[2]); 17 SkDebugf("[%7.4f %7.4f %7.4f]\n", fVec[0], fVec[1], fVec[2]);
13 } 18 }
14 19
15 void SkFloat3x3::dump() const { 20 void SkFloat3x3::dump() const {
16 SkDebugf("[%7.4f %7.4f %7.4f] [%7.4f %7.4f %7.4f] [%7.4f %7.4f %7.4f]\n", 21 SkDebugf("[%7.4f %7.4f %7.4f] [%7.4f %7.4f %7.4f] [%7.4f %7.4f %7.4f]\n",
17 fMat[0], fMat[1], fMat[2], 22 fMat[0], fMat[1], fMat[2],
18 fMat[3], fMat[4], fMat[5], 23 fMat[3], fMat[4], fMat[5],
19 fMat[6], fMat[7], fMat[8]); 24 fMat[6], fMat[7], fMat[8]);
(...skipping 14 matching lines...) Expand all
34 SkColorSpace::SkColorSpace(SkColorLookUpTable colorLUT, SkGammas gammas, 39 SkColorSpace::SkColorSpace(SkColorLookUpTable colorLUT, SkGammas gammas,
35 const SkFloat3x3& toXYZD50, const SkFloat3& toXYZOffs et) 40 const SkFloat3x3& toXYZD50, const SkFloat3& toXYZOffs et)
36 : fColorLUT(std::move(colorLUT)) 41 : fColorLUT(std::move(colorLUT))
37 , fGammas(std::move(gammas)) 42 , fGammas(std::move(gammas))
38 , fToXYZD50(toXYZD50) 43 , fToXYZD50(toXYZD50)
39 , fToXYZOffset(toXYZOffset) 44 , fToXYZOffset(toXYZOffset)
40 , fUniqueID(sk_atomic_inc(&gUniqueColorSpaceID)) 45 , fUniqueID(sk_atomic_inc(&gUniqueColorSpaceID))
41 , fNamed(kUnknown_Named) 46 , fNamed(kUnknown_Named)
42 {} 47 {}
43 48
44 sk_sp<SkColorSpace> SkColorSpace::NewRGB(const SkFloat3x3& toXYZD50, SkGammas ga mmas) {
45 return sk_sp<SkColorSpace>(new SkColorSpace(std::move(gammas), toXYZD50, kUn known_Named));
46 }
47
48 const SkFloat3x3 gSRGB_toXYZD50 {{ 49 const SkFloat3x3 gSRGB_toXYZD50 {{
49 0.4358f, 0.2224f, 0.0139f, // * R 50 0.4358f, 0.2224f, 0.0139f, // * R
50 0.3853f, 0.7170f, 0.0971f, // * G 51 0.3853f, 0.7170f, 0.0971f, // * G
51 0.1430f, 0.0606f, 0.7139f, // * B 52 0.1430f, 0.0606f, 0.7139f, // * B
52 }}; 53 }};
53 54
55 SK_DECLARE_STATIC_ONCE_PTR(SkColorSpace, sRGB);
56
57 sk_sp<SkColorSpace> SkColorSpace::NewRGB(SkGammas gammas, const SkFloat3x3& toXY ZD50) {
58 // Check if we really have sRGB
59 if (color_space_almost_equal(2.2f, gammas.fRed.fValue) &&
60 color_space_almost_equal(2.2f, gammas.fGreen.fValue) &&
61 color_space_almost_equal(2.2f, gammas.fBlue.fValue) &&
62 color_space_almost_equal(toXYZD50.fMat[0], gSRGB_toXYZD50.fMat[0]) &&
63 color_space_almost_equal(toXYZD50.fMat[1], gSRGB_toXYZD50.fMat[1]) &&
64 color_space_almost_equal(toXYZD50.fMat[2], gSRGB_toXYZD50.fMat[2]) &&
65 color_space_almost_equal(toXYZD50.fMat[3], gSRGB_toXYZD50.fMat[3]) &&
66 color_space_almost_equal(toXYZD50.fMat[4], gSRGB_toXYZD50.fMat[4]) &&
67 color_space_almost_equal(toXYZD50.fMat[5], gSRGB_toXYZD50.fMat[5]) &&
68 color_space_almost_equal(toXYZD50.fMat[6], gSRGB_toXYZD50.fMat[6]) &&
69 color_space_almost_equal(toXYZD50.fMat[7], gSRGB_toXYZD50.fMat[7]) &&
70 color_space_almost_equal(toXYZD50.fMat[8], gSRGB_toXYZD50.fMat[8]))
71 {
72 return SkColorSpace::NewNamed(kSRGB_Named);
73 }
74
75 return sk_sp<SkColorSpace>(new SkColorSpace(std::move(gammas), toXYZD50, kUn known_Named));
76 }
77
54 sk_sp<SkColorSpace> SkColorSpace::NewNamed(Named named) { 78 sk_sp<SkColorSpace> SkColorSpace::NewNamed(Named named) {
55 switch (named) { 79 switch (named) {
56 case kSRGB_Named: 80 case kSRGB_Named:
57 return sk_sp<SkColorSpace>(new SkColorSpace(SkGammas(2.2f, 2.2f, 2.2 f), gSRGB_toXYZD50, 81 return sk_ref_sp(sRGB.get([=]{
58 kSRGB_Named)); 82 return new SkColorSpace(SkGammas(2.2f, 2.2f, 2.2f), gSRGB_toXYZD 50, kSRGB_Named);
83 }));
59 default: 84 default:
60 break; 85 break;
61 } 86 }
62 return nullptr; 87 return nullptr;
63 } 88 }
64 89
65 //////////////////////////////////////////////////////////////////////////////// /////////////////// 90 //////////////////////////////////////////////////////////////////////////////// ///////////////////
66 91
67 #include "SkFixed.h" 92 #include "SkFixed.h"
68 #include "SkTemplates.h" 93 #include "SkTemplates.h"
(...skipping 19 matching lines...) Expand all
88 } 113 }
89 114
90 static uint32_t read_big_endian_uint(const uint8_t* ptr) { 115 static uint32_t read_big_endian_uint(const uint8_t* ptr) {
91 return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3]; 116 return ptr[0] << 24 | ptr[1] << 16 | ptr[2] << 8 | ptr[3];
92 } 117 }
93 118
94 static int32_t read_big_endian_int(const uint8_t* ptr) { 119 static int32_t read_big_endian_int(const uint8_t* ptr) {
95 return (int32_t) read_big_endian_uint(ptr); 120 return (int32_t) read_big_endian_uint(ptr);
96 } 121 }
97 122
98 static bool color_space_almost_equal(float a, float b) {
99 return SkTAbs(a - b) < 0.01f;
100 }
101
102 // This is equal to the header size according to the ICC specification (128) 123 // This is equal to the header size according to the ICC specification (128)
103 // plus the size of the tag count (4). We include the tag count since we 124 // plus the size of the tag count (4). We include the tag count since we
104 // always require it to be present anyway. 125 // always require it to be present anyway.
105 static const size_t kICCHeaderSize = 132; 126 static const size_t kICCHeaderSize = 132;
106 127
107 // Contains a signature (4), offset (4), and size (4). 128 // Contains a signature (4), offset (4), and size (4).
108 static const size_t kICCTagTableEntrySize = 12; 129 static const size_t kICCTagTableEntrySize = 12;
109 130
110 static const uint32_t kRGB_ColorSpace = SkSetFourByteTag('R', 'G', 'B', ' '); 131 static const uint32_t kRGB_ColorSpace = SkSetFourByteTag('R', 'G', 'B', ' ');
111 132
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 SkColorSpacePrintf("Failed to read R gamma tag.\n"); 591 SkColorSpacePrintf("Failed to read R gamma tag.\n");
571 } 592 }
572 if (!g || !SkColorSpace::LoadGammas(&gammas.fGreen, 1, 593 if (!g || !SkColorSpace::LoadGammas(&gammas.fGreen, 1,
573 g->addr((const uint8_t*) bas e), g->fLength)) { 594 g->addr((const uint8_t*) bas e), g->fLength)) {
574 SkColorSpacePrintf("Failed to read G gamma tag.\n"); 595 SkColorSpacePrintf("Failed to read G gamma tag.\n");
575 } 596 }
576 if (!b || !SkColorSpace::LoadGammas(&gammas.fBlue, 1, 597 if (!b || !SkColorSpace::LoadGammas(&gammas.fBlue, 1,
577 b->addr((const uint8_t*) bas e), b->fLength)) { 598 b->addr((const uint8_t*) bas e), b->fLength)) {
578 SkColorSpacePrintf("Failed to read B gamma tag.\n"); 599 SkColorSpacePrintf("Failed to read B gamma tag.\n");
579 } 600 }
580 return SkColorSpace::NewRGB(toXYZ, std::move(gammas)); 601 return SkColorSpace::NewRGB(std::move(gammas), toXYZ);
581 } 602 }
582 603
583 // Recognize color profile specified by A2B0 tag. 604 // Recognize color profile specified by A2B0 tag.
584 const ICCTag* a2b0 = ICCTag::Find(tags.get(), tagCount, kTAG_A2B0); 605 const ICCTag* a2b0 = ICCTag::Find(tags.get(), tagCount, kTAG_A2B0);
585 if (a2b0) { 606 if (a2b0) {
586 SkColorLookUpTable colorLUT; 607 SkColorLookUpTable colorLUT;
587 SkGammas gammas; 608 SkGammas gammas;
588 SkFloat3x3 toXYZ; 609 SkFloat3x3 toXYZ;
589 SkFloat3 toXYZOffset; 610 SkFloat3 toXYZOffset;
590 if (!SkColorSpace::LoadA2B0(&colorLUT, &gammas, &toXYZ, &toXYZOf fset, 611 if (!SkColorSpace::LoadA2B0(&colorLUT, &gammas, &toXYZ, &toXYZOf fset,
591 a2b0->addr((const uint8_t*) base), a 2b0->fLength)) { 612 a2b0->addr((const uint8_t*) base), a 2b0->fLength)) {
592 return_null("Failed to parse A2B0 tag"); 613 return_null("Failed to parse A2B0 tag");
593 } 614 }
594 615
616 // If there is no colorLUT or xyzOffset, use NewRGB. This allow s us to check
617 // if the profile is sRGB.
618 if (!colorLUT.fTable && !toXYZOffset.fVec[0] && !toXYZOffset.fVe c[1] &&
619 !toXYZOffset.fVec[2]) {
620 return SkColorSpace::NewRGB(std::move(gammas), toXYZ);
621 }
622
595 return sk_sp<SkColorSpace>(new SkColorSpace(std::move(colorLUT), std::move(gammas), 623 return sk_sp<SkColorSpace>(new SkColorSpace(std::move(colorLUT), std::move(gammas),
596 toXYZ, toXYZOffset)) ; 624 toXYZ, toXYZOffset)) ;
597 } 625 }
598 626
599 } 627 }
600 default: 628 default:
601 break; 629 break;
602 } 630 }
603 631
604 return_null("ICC profile contains unsupported colorspace"); 632 return_null("ICC profile contains unsupported colorspace");
605 } 633 }
OLDNEW
« src/core/SkColorSpace.h ('K') | « src/core/SkColorSpace.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698