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

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

Issue 2085653003: Enable flattening and unflattening of SkColorSpace (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Fix unit tests Created 4 years, 6 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 | « include/core/SkMatrix44.h ('k') | src/core/SkColorSpace_Base.h » ('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 "SkColorSpace.h" 8 #include "SkColorSpace.h"
9 #include "SkColorSpace_Base.h" 9 #include "SkColorSpace_Base.h"
10 #include "SkEndian.h" 10 #include "SkEndian.h"
11 #include "SkOnce.h" 11 #include "SkOnce.h"
12 #include "SkReadBuffer.h"
13 #include "SkWriteBuffer.h"
12 14
13 #define SkColorSpacePrintf(...) 15 #define SkColorSpacePrintf(...)
14 16
15 static bool color_space_almost_equal(float a, float b) { 17 static bool color_space_almost_equal(float a, float b) {
16 return SkTAbs(a - b) < 0.01f; 18 return SkTAbs(a - b) < 0.01f;
17 } 19 }
18 20
19 //////////////////////////////////////////////////////////////////////////////// ////////////////// 21 //////////////////////////////////////////////////////////////////////////////// //////////////////
20 22
21 SkColorSpace::SkColorSpace(GammaNamed gammaNamed, const SkMatrix44& toXYZD50, Na med named) 23 SkColorSpace::SkColorSpace(GammaNamed gammaNamed, const SkMatrix44& toXYZD50, Na med named)
22 : fGammaNamed(gammaNamed) 24 : fGammaNamed(gammaNamed)
23 , fToXYZD50(toXYZD50) 25 , fToXYZD50(toXYZD50)
24 , fNamed(named) 26 , fNamed(named)
25 {} 27 {}
26 28
27 SkColorSpace_Base::SkColorSpace_Base(GammaNamed gammaNamed, const SkMatrix44& to XYZD50, Named named, 29 SkColorSpace_Base::SkColorSpace_Base(GammaNamed gammaNamed, const SkMatrix44& to XYZD50, Named named)
28 sk_sp<SkData> profileData)
29 : INHERITED(gammaNamed, toXYZD50, named) 30 : INHERITED(gammaNamed, toXYZD50, named)
30 , fGammas(nullptr) 31 , fGammas(nullptr)
31 , fProfileData(std::move(profileData)) 32 , fProfileData(nullptr)
32 {} 33 {}
33 34
34 SkColorSpace_Base::SkColorSpace_Base(SkColorLookUpTable* colorLUT, sk_sp<SkGamma s> gammas, 35 SkColorSpace_Base::SkColorSpace_Base(SkColorLookUpTable* colorLUT, sk_sp<SkGamma s> gammas,
35 const SkMatrix44& toXYZD50, sk_sp<SkData> p rofileData) 36 const SkMatrix44& toXYZD50, sk_sp<SkData> p rofileData)
36 : INHERITED(kNonStandard_GammaNamed, toXYZD50, kUnknown_Named) 37 : INHERITED(kNonStandard_GammaNamed, toXYZD50, kUnknown_Named)
37 , fColorLUT(colorLUT) 38 , fColorLUT(colorLUT)
38 , fGammas(std::move(gammas)) 39 , fGammas(std::move(gammas))
39 , fProfileData(std::move(profileData)) 40 , fProfileData(std::move(profileData))
40 {} 41 {}
41 42
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
95 set_gamma_value(&curves[1], values[1]); 96 set_gamma_value(&curves[1], values[1]);
96 set_gamma_value(&curves[2], values[2]); 97 set_gamma_value(&curves[2], values[2]);
97 98
98 GammaNamed gammaNamed = SkGammas::Named(curves); 99 GammaNamed gammaNamed = SkGammas::Named(curves);
99 if (kNonStandard_GammaNamed == gammaNamed) { 100 if (kNonStandard_GammaNamed == gammaNamed) {
100 sk_sp<SkGammas> gammas(new SkGammas(std::move(curves[0]), std::move(curv es[1]), 101 sk_sp<SkGammas> gammas(new SkGammas(std::move(curves[0]), std::move(curv es[1]),
101 std::move(curves[2]))); 102 std::move(curves[2])));
102 return sk_sp<SkColorSpace>(new SkColorSpace_Base(nullptr, gammas, toXYZD 50, nullptr)); 103 return sk_sp<SkColorSpace>(new SkColorSpace_Base(nullptr, gammas, toXYZD 50, nullptr));
103 } 104 }
104 105
105 return SkColorSpace_Base::NewRGB(gammaNamed, toXYZD50, nullptr); 106 return SkColorSpace_Base::NewRGB(gammaNamed, toXYZD50);
106 } 107 }
107 108
108 sk_sp<SkColorSpace> SkColorSpace_Base::NewRGB(GammaNamed gammaNamed, const SkMat rix44& toXYZD50, 109 sk_sp<SkColorSpace> SkColorSpace_Base::NewRGB(GammaNamed gammaNamed, const SkMat rix44& toXYZD50) {
109 sk_sp<SkData> profileData) {
110 switch (gammaNamed) { 110 switch (gammaNamed) {
111 case kSRGB_GammaNamed: 111 case kSRGB_GammaNamed:
112 if (xyz_almost_equal(toXYZD50, gSRGB_toXYZD50)) { 112 if (xyz_almost_equal(toXYZD50, gSRGB_toXYZD50)) {
113 return SkColorSpace::NewNamed(kSRGB_Named); 113 return SkColorSpace::NewNamed(kSRGB_Named);
114 } 114 }
115 break; 115 break;
116 case k2Dot2Curve_GammaNamed: 116 case k2Dot2Curve_GammaNamed:
117 if (xyz_almost_equal(toXYZD50, gAdobeRGB_toXYZD50)) { 117 if (xyz_almost_equal(toXYZD50, gAdobeRGB_toXYZD50)) {
118 return SkColorSpace::NewNamed(kAdobeRGB_Named); 118 return SkColorSpace::NewNamed(kAdobeRGB_Named);
119 } 119 }
120 break; 120 break;
121 case kNonStandard_GammaNamed: 121 case kNonStandard_GammaNamed:
122 // This is not allowed. 122 // This is not allowed.
123 return nullptr; 123 return nullptr;
124 default: 124 default:
125 break; 125 break;
126 } 126 }
127 127
128 return sk_sp<SkColorSpace>(new SkColorSpace_Base(gammaNamed, toXYZD50, kUnkn own_Named, 128 return sk_sp<SkColorSpace>(new SkColorSpace_Base(gammaNamed, toXYZD50, kUnkn own_Named));
129 profileData));
130 } 129 }
131 130
132 sk_sp<SkColorSpace> SkColorSpace::NewRGB(GammaNamed gammaNamed, const SkMatrix44 & toXYZD50) { 131 sk_sp<SkColorSpace> SkColorSpace::NewRGB(GammaNamed gammaNamed, const SkMatrix44 & toXYZD50) {
133 return SkColorSpace_Base::NewRGB(gammaNamed, toXYZD50, nullptr); 132 return SkColorSpace_Base::NewRGB(gammaNamed, toXYZD50);
134 } 133 }
135 134
136 sk_sp<SkColorSpace> SkColorSpace::NewNamed(Named named) { 135 sk_sp<SkColorSpace> SkColorSpace::NewNamed(Named named) {
137 static SkOnce sRGBOnce; 136 static SkOnce sRGBOnce;
138 static SkColorSpace* sRGB; 137 static SkColorSpace* sRGB;
139 static SkOnce adobeRGBOnce; 138 static SkOnce adobeRGBOnce;
140 static SkColorSpace* adobeRGB; 139 static SkColorSpace* adobeRGB;
141 140
142 switch (named) { 141 switch (named) {
143 case kSRGB_Named: { 142 case kSRGB_Named: {
144 sRGBOnce([] { 143 sRGBOnce([] {
145 SkMatrix44 srgbToxyzD50(SkMatrix44::kUninitialized_Constructor); 144 SkMatrix44 srgbToxyzD50(SkMatrix44::kUninitialized_Constructor);
146 srgbToxyzD50.set3x3ColMajorf(gSRGB_toXYZD50); 145 srgbToxyzD50.set3x3ColMajorf(gSRGB_toXYZD50);
147 sRGB = new SkColorSpace_Base(kSRGB_GammaNamed, srgbToxyzD50, kSR GB_Named, nullptr); 146 sRGB = new SkColorSpace_Base(kSRGB_GammaNamed, srgbToxyzD50, kSR GB_Named);
148 }); 147 });
149 return sk_ref_sp(sRGB); 148 return sk_ref_sp(sRGB);
150 } 149 }
151 case kAdobeRGB_Named: { 150 case kAdobeRGB_Named: {
152 adobeRGBOnce([] { 151 adobeRGBOnce([] {
153 SkMatrix44 adobergbToxyzD50(SkMatrix44::kUninitialized_Construct or); 152 SkMatrix44 adobergbToxyzD50(SkMatrix44::kUninitialized_Construct or);
154 adobergbToxyzD50.set3x3ColMajorf(gAdobeRGB_toXYZD50); 153 adobergbToxyzD50.set3x3ColMajorf(gAdobeRGB_toXYZD50);
155 adobeRGB = new SkColorSpace_Base(k2Dot2Curve_GammaNamed, adoberg bToxyzD50, 154 adobeRGB = new SkColorSpace_Base(k2Dot2Curve_GammaNamed, adoberg bToxyzD50,
156 kAdobeRGB_Named, nullptr); 155 kAdobeRGB_Named);
157 }); 156 });
158 return sk_ref_sp(adobeRGB); 157 return sk_ref_sp(adobeRGB);
159 } 158 }
160 default: 159 default:
161 break; 160 break;
162 } 161 }
163 return nullptr; 162 return nullptr;
164 } 163 }
165 164
166 //////////////////////////////////////////////////////////////////////////////// /////////////////// 165 //////////////////////////////////////////////////////////////////////////////// ///////////////////
(...skipping 756 matching lines...) Expand 10 before | Expand all | Expand 10 after
923 } 922 }
924 923
925 GammaNamed gammaNamed = SkGammas::Named(curves); 924 GammaNamed gammaNamed = SkGammas::Named(curves);
926 if (kNonStandard_GammaNamed == gammaNamed) { 925 if (kNonStandard_GammaNamed == gammaNamed) {
927 sk_sp<SkGammas> gammas = sk_make_sp<SkGammas>(std::move(curv es[0]), 926 sk_sp<SkGammas> gammas = sk_make_sp<SkGammas>(std::move(curv es[0]),
928 std::move(curv es[1]), 927 std::move(curv es[1]),
929 std::move(curv es[2])); 928 std::move(curv es[2]));
930 return sk_sp<SkColorSpace>(new SkColorSpace_Base(nullptr, st d::move(gammas), 929 return sk_sp<SkColorSpace>(new SkColorSpace_Base(nullptr, st d::move(gammas),
931 mat, std::m ove(data))); 930 mat, std::m ove(data)));
932 } else { 931 } else {
933 return SkColorSpace_Base::NewRGB(gammaNamed, mat, std::move( data)); 932 return SkColorSpace_Base::NewRGB(gammaNamed, mat);
934 } 933 }
935 } 934 }
936 935
937 // Recognize color profile specified by A2B0 tag. 936 // Recognize color profile specified by A2B0 tag.
938 const ICCTag* a2b0 = ICCTag::Find(tags.get(), tagCount, kTAG_A2B0); 937 const ICCTag* a2b0 = ICCTag::Find(tags.get(), tagCount, kTAG_A2B0);
939 if (a2b0) { 938 if (a2b0) {
940 SkAutoTDelete<SkColorLookUpTable> colorLUT(new SkColorLookUpTabl e()); 939 SkAutoTDelete<SkColorLookUpTable> colorLUT(new SkColorLookUpTabl e());
941 SkGammaCurve curves[3]; 940 SkGammaCurve curves[3];
942 SkMatrix44 toXYZ(SkMatrix44::kUninitialized_Constructor); 941 SkMatrix44 toXYZ(SkMatrix44::kUninitialized_Constructor);
943 if (!load_a2b0(colorLUT, curves, &toXYZ, a2b0->addr((const uint8 _t*) base), 942 if (!load_a2b0(colorLUT, curves, &toXYZ, a2b0->addr((const uint8 _t*) base),
944 a2b0->fLength)) { 943 a2b0->fLength)) {
945 return_null("Failed to parse A2B0 tag"); 944 return_null("Failed to parse A2B0 tag");
946 } 945 }
947 946
948 GammaNamed gammaNamed = SkGammas::Named(curves); 947 GammaNamed gammaNamed = SkGammas::Named(curves);
949 if (colorLUT->fTable || kNonStandard_GammaNamed == gammaNamed) { 948 if (colorLUT->fTable || kNonStandard_GammaNamed == gammaNamed) {
950 sk_sp<SkGammas> gammas = sk_make_sp<SkGammas>(std::move(curv es[0]), 949 sk_sp<SkGammas> gammas = sk_make_sp<SkGammas>(std::move(curv es[0]),
951 std::move(curv es[1]), 950 std::move(curv es[1]),
952 std::move(curv es[2])); 951 std::move(curv es[2]));
953 952
954 return sk_sp<SkColorSpace>(new SkColorSpace_Base(colorLUT.re lease(), 953 return sk_sp<SkColorSpace>(new SkColorSpace_Base(colorLUT.re lease(),
955 std::move(g ammas), toXYZ, 954 std::move(g ammas), toXYZ,
956 std::move(d ata))); 955 std::move(d ata)));
957 } else { 956 } else {
958 return SkColorSpace_Base::NewRGB(gammaNamed, toXYZ, std::mov e(data)); 957 return SkColorSpace_Base::NewRGB(gammaNamed, toXYZ);
959 } 958 }
960 } 959 }
961 } 960 }
962 default: 961 default:
963 break; 962 break;
964 } 963 }
965 964
966 return_null("ICC profile contains unsupported colorspace"); 965 return_null("ICC profile contains unsupported colorspace");
967 } 966 }
968 967
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
1201 ptr32[4] = SkEndian_SwapBE32(0x000116cc); 1200 ptr32[4] = SkEndian_SwapBE32(0x000116cc);
1202 ptr += kTAG_XYZ_Bytes; 1201 ptr += kTAG_XYZ_Bytes;
1203 1202
1204 // Write copyright tag 1203 // Write copyright tag
1205 memcpy(ptr, gEmptyTextTag, sizeof(gEmptyTextTag)); 1204 memcpy(ptr, gEmptyTextTag, sizeof(gEmptyTextTag));
1206 1205
1207 // TODO (msarett): Should we try to hold onto the data so we can return imme diately if 1206 // TODO (msarett): Should we try to hold onto the data so we can return imme diately if
1208 // the client calls again? 1207 // the client calls again?
1209 return SkData::MakeFromMalloc(profile.release(), kICCProfileSize); 1208 return SkData::MakeFromMalloc(profile.release(), kICCProfileSize);
1210 } 1209 }
1210
1211 //////////////////////////////////////////////////////////////////////////////// ///////////////////
1212
1213 enum Version {
1214 k0_Version, // Initial version, header + flags for matrix and profile
1215 };
1216
1217 struct ColorSpaceHeader {
1218 /**
1219 * If kMatrix_Flag is set, we will write 12 floats after the header.
1220 * Should not be set at the same time as the kICC_Flag.
1221 */
1222 static constexpr uint8_t kMatrix_Flag = 1 << 0;
1223
1224 /**
1225 * If kICC_Flag is set, we will write an ICC profile after the header.
1226 * The ICC profile will be written as a uint32 size, followed immediately
1227 * by the data (padded to 4 bytes).
1228 * Should not be set at the same time as the kMatrix_Flag.
1229 */
1230 static constexpr uint8_t kICC_Flag = 1 << 1;
1231
1232 static ColorSpaceHeader Pack(Version version, SkColorSpace::Named named,
1233 SkColorSpace::GammaNamed gammaNamed, uint8_t fl ags) {
1234 ColorSpaceHeader header;
1235
1236 SkASSERT(k0_Version == version);
1237 header.fVersion = (uint8_t) version;
1238
1239 SkASSERT(named <= SkColorSpace::kAdobeRGB_Named);
1240 header.fNamed = (uint8_t) named;
1241
1242 SkASSERT(gammaNamed <= SkColorSpace::kNonStandard_GammaNamed);
1243 header.fGammaNamed = (uint8_t) gammaNamed;
1244
1245 SkASSERT(flags <= kICC_Flag);
1246 header.fFlags = flags;
1247 return header;
1248 }
1249
1250 uint8_t fVersion; // Always zero
1251 uint8_t fNamed; // Must be a SkColorSpace::Named
1252 uint8_t fGammaNamed; // Must be a SkColorSpace::GammaNamed
1253 uint8_t fFlags; // Some combination of the flags listed above
1254 };
1255
1256 sk_sp<SkData> SkColorSpace::serialize() const {
1257 // If we have a named profile, only write the enum.
1258 switch (fNamed) {
1259 case kSRGB_Named:
1260 case kAdobeRGB_Named: {
1261 sk_sp<SkData> data = SkData::MakeUninitialized(sizeof(ColorSpaceHead er));
1262 *((ColorSpaceHeader*) data->writable_data()) =
1263 ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed, 0);
1264 return data;
1265 }
1266 default:
1267 break;
1268 }
1269
1270 // If we have a named gamma, write the enum and the matrix.
1271 switch (fGammaNamed) {
1272 case kSRGB_GammaNamed:
1273 case k2Dot2Curve_GammaNamed:
1274 case kLinear_GammaNamed: {
1275 sk_sp<SkData> data = SkData::MakeUninitialized(sizeof(ColorSpaceHead er) +
1276 12 * sizeof(float));
1277 void* dataPtr = data->writable_data();
1278
1279 *((ColorSpaceHeader*) dataPtr) = ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed,
1280 ColorSpaceHe ader::kMatrix_Flag);
1281 dataPtr = SkTAddOffset<void>(dataPtr, sizeof(ColorSpaceHeader));
1282
1283 fToXYZD50.as4x3ColMajorf((float*) dataPtr);
1284 return data;
1285 }
1286 default:
1287 break;
1288 }
1289
1290 // If we do not have a named gamma, this must have been created from an ICC profile.
1291 // Since we were unable to recognize the gamma, we will have saved the ICC d ata.
1292 SkASSERT(as_CSB(this)->fProfileData);
1293
1294 size_t profileSize = as_CSB(this)->fProfileData->size();
1295 if (SkAlign4(profileSize) != (uint32_t) SkAlign4(profileSize)) {
1296 return nullptr;
1297 }
1298
1299 sk_sp<SkData> data = SkData::MakeUninitialized(sizeof(ColorSpaceHeader) + si zeof(uint32_t) +
1300 SkAlign4(profileSize));
1301 void* dataPtr = data->writable_data();
1302
1303 *((ColorSpaceHeader*) dataPtr) = ColorSpaceHeader::Pack(k0_Version, fNamed, fGammaNamed,
1304 ColorSpaceHeader::kI CC_Flag);
1305 dataPtr = SkTAddOffset<void>(dataPtr, sizeof(ColorSpaceHeader));
1306
1307 *((uint32_t*) dataPtr) = (uint32_t) SkAlign4(profileSize);
1308 dataPtr = SkTAddOffset<void>(dataPtr, sizeof(uint32_t));
1309
1310 memcpy(dataPtr, as_CSB(this)->fProfileData->data(), profileSize);
1311 memset(SkTAddOffset<void>(dataPtr, profileSize), 0, SkAlign4(profileSize) - profileSize);
1312 return data;
1313 }
1314
1315 sk_sp<SkColorSpace> SkColorSpace::Deserialize(const void* data, size_t length) {
1316 if (length < sizeof(ColorSpaceHeader)) {
1317 return nullptr;
1318 }
1319
1320 ColorSpaceHeader header = *((const ColorSpaceHeader*) data);
1321 data = SkTAddOffset<const void>(data, sizeof(ColorSpaceHeader));
1322 length -= sizeof(ColorSpaceHeader);
1323 switch ((Named) header.fNamed) {
1324 case kSRGB_Named:
1325 case kAdobeRGB_Named:
1326 return NewNamed((Named) header.fNamed);
1327 default:
1328 break;
1329 }
1330
1331 switch ((GammaNamed) header.fGammaNamed) {
1332 case kSRGB_GammaNamed:
1333 case k2Dot2Curve_GammaNamed:
1334 case kLinear_GammaNamed: {
1335 if (ColorSpaceHeader::kMatrix_Flag != header.fFlags || length < 12 * sizeof(float)) {
1336 return nullptr;
1337 }
1338
1339 SkMatrix44 toXYZ(SkMatrix44::kUninitialized_Constructor);
1340 toXYZ.set4x3ColMajorf((const float*) data);
1341 return NewRGB((GammaNamed) header.fGammaNamed, toXYZ);
1342 }
1343 default:
1344 break;
1345 }
1346
1347 if (ColorSpaceHeader::kICC_Flag != header.fFlags || length < sizeof(uint32_t )) {
1348 return nullptr;
1349 }
1350
1351 uint32_t profileSize = *((uint32_t*) data);
1352 data = SkTAddOffset<const void>(data, sizeof(uint32_t));
1353 length -= sizeof(uint32_t);
1354 if (length < profileSize) {
1355 return nullptr;
1356 }
1357
1358 return NewICC(data, profileSize);
1359 }
OLDNEW
« no previous file with comments | « include/core/SkMatrix44.h ('k') | src/core/SkColorSpace_Base.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698