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

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

Issue 2304753002: Add SkColorSpacePrimaries to help with making D50 matrices (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Edits Created 4 years, 3 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
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 "SkColorSpacePriv.h" 10 #include "SkColorSpacePriv.h"
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 switch (gammaNamed) { 125 switch (gammaNamed) {
126 case kLinear_GammaNamed: 126 case kLinear_GammaNamed:
127 return SkColorSpace_Base::NewRGB(kLinear_SkGammaNamed, toXYZD50); 127 return SkColorSpace_Base::NewRGB(kLinear_SkGammaNamed, toXYZD50);
128 case kSRGB_GammaNamed: 128 case kSRGB_GammaNamed:
129 return SkColorSpace_Base::NewRGB(kSRGB_SkGammaNamed, toXYZD50); 129 return SkColorSpace_Base::NewRGB(kSRGB_SkGammaNamed, toXYZD50);
130 default: 130 default:
131 return nullptr; 131 return nullptr;
132 } 132 }
133 } 133 }
134 134
135 sk_sp<SkColorSpace> SkColorSpace::NewRGB(const SkTransferFn& coeffs, const SkMat rix44& toXYZD50) {
136 // TODO: Check if coeffs match sRGB, 2.2, or linear.
137 // TODO: Make sure coefficients describe a valid curve. Return nullptr if t hey don't.
138 void* memory = sk_malloc_throw(sizeof(SkGammas) + sizeof(SkTransferFn));
139 sk_sp<SkGammas> gammas = sk_sp<SkGammas>(new (memory) SkGammas());
140 void* storage = SkTAddOffset<void>(memory, sizeof(SkGammas));
141 memcpy(storage, &coeffs, sizeof(SkTransferFn));
142 gammas->fRedType = SkGammas::Type::kParam_Type;
143 gammas->fGreenType = SkGammas::Type::kParam_Type;
144 gammas->fBlueType = SkGammas::Type::kParam_Type;
145
146 SkGammas::Data data;
147 data.fParamOffset = 0;
148 gammas->fRedData = data;
149 gammas->fGreenData = data;
150 gammas->fBlueData = data;
151 return sk_sp<SkColorSpace>(new SkColorSpace_Base(nullptr, kNonStandard_SkGa mmaNamed,
152 std::move(gammas), toXYZD5 0, nullptr));
153 }
154
155 sk_sp<SkColorSpace> SkColorSpace::NewRGB(GammaNamed gammaNamed, const SkPrimarie s& primaries) {
156 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
157 if (!PrimariesToXYZD50(&toXYZD50, primaries)) {
158 return nullptr;
159 }
160
161 return NewRGB(gammaNamed, toXYZD50);
162 }
163
164 sk_sp<SkColorSpace> SkColorSpace::NewRGB(const SkTransferFn& coeffs, const SkPri maries& primaries) {
165 SkMatrix44 toXYZD50(SkMatrix44::kUninitialized_Constructor);
166 if (!PrimariesToXYZD50(&toXYZD50, primaries)) {
167 return nullptr;
168 }
169
170 return NewRGB(coeffs, toXYZD50);
171 }
172
135 static SkColorSpace* gAdobeRGB; 173 static SkColorSpace* gAdobeRGB;
136 static SkColorSpace* gSRGB; 174 static SkColorSpace* gSRGB;
137 175
138 sk_sp<SkColorSpace> SkColorSpace::NewNamed(Named named) { 176 sk_sp<SkColorSpace> SkColorSpace::NewNamed(Named named) {
139 static SkOnce sRGBOnce; 177 static SkOnce sRGBOnce;
140 static SkOnce adobeRGBOnce; 178 static SkOnce adobeRGBOnce;
141 179
142 switch (named) { 180 switch (named) {
143 case kSRGB_Named: { 181 case kSRGB_Named: {
144 sRGBOnce([] { 182 sRGBOnce([] {
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 if (memory) { 301 if (memory) {
264 *((ColorSpaceHeader*) memory) = 302 *((ColorSpaceHeader*) memory) =
265 ColorSpaceHeader::Pack(k0_Version, 0, as_CSB(this)-> fGammaNamed, 303 ColorSpaceHeader::Pack(k0_Version, 0, as_CSB(this)-> fGammaNamed,
266 ColorSpaceHeader::kMatrix_Fla g); 304 ColorSpaceHeader::kMatrix_Fla g);
267 memory = SkTAddOffset<void>(memory, sizeof(ColorSpaceHeader) ); 305 memory = SkTAddOffset<void>(memory, sizeof(ColorSpaceHeader) );
268 fToXYZD50.as4x3ColMajorf((float*) memory); 306 fToXYZD50.as4x3ColMajorf((float*) memory);
269 } 307 }
270 return sizeof(ColorSpaceHeader) + 12 * sizeof(float); 308 return sizeof(ColorSpaceHeader) + 12 * sizeof(float);
271 } 309 }
272 default: 310 default:
311 // TODO: This case gets trickier. The code below exists because I allow
312 // SkPngCodec to call a constructor with float gammas.
313 // I think the best thing to do would be:
314 // (1) Fix SkPngCodec to call one of the new constructors.
315 // (2) Handle serializing the parametric gammas here.
316 // TODO: In a separate CL, remove the idea of "exponential" gamm as.
317 // These can be consumed by the parametric case. Maybe th is CL
318 // should land first.
319
273 // Otherwise, write the gamma values and the matrix. 320 // Otherwise, write the gamma values and the matrix.
274 if (memory) { 321 if (memory) {
275 *((ColorSpaceHeader*) memory) = 322 *((ColorSpaceHeader*) memory) =
276 ColorSpaceHeader::Pack(k0_Version, 0, as_CSB(this)-> fGammaNamed, 323 ColorSpaceHeader::Pack(k0_Version, 0, as_CSB(this)-> fGammaNamed,
277 ColorSpaceHeader::kFloatGamma _Flag); 324 ColorSpaceHeader::kFloatGamma _Flag);
278 memory = SkTAddOffset<void>(memory, sizeof(ColorSpaceHeader) ); 325 memory = SkTAddOffset<void>(memory, sizeof(ColorSpaceHeader) );
279 326
280 const SkGammas* gammas = as_CSB(this)->gammas(); 327 const SkGammas* gammas = as_CSB(this)->gammas();
281 SkASSERT(gammas); 328 SkASSERT(gammas);
282 SkASSERT(SkGammas::Type::kValue_Type == gammas->fRedType && 329 SkASSERT(SkGammas::Type::kValue_Type == gammas->fRedType &&
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
362 uint32_t profileSize = *((uint32_t*) data); 409 uint32_t profileSize = *((uint32_t*) data);
363 data = SkTAddOffset<const void>(data, sizeof(uint32_t)); 410 data = SkTAddOffset<const void>(data, sizeof(uint32_t));
364 length -= sizeof(uint32_t); 411 length -= sizeof(uint32_t);
365 if (length < profileSize) { 412 if (length < profileSize) {
366 return nullptr; 413 return nullptr;
367 } 414 }
368 415
369 return NewICC(data, profileSize); 416 return NewICC(data, profileSize);
370 } 417 }
371 case ColorSpaceHeader::kFloatGamma_Flag: { 418 case ColorSpaceHeader::kFloatGamma_Flag: {
419 // TODO: This needs to be fixed to match the new version of serializ e().
420
372 if (length < 15 * sizeof(float)) { 421 if (length < 15 * sizeof(float)) {
373 return nullptr; 422 return nullptr;
374 } 423 }
375 424
376 float gammas[3]; 425 float gammas[3];
377 gammas[0] = *(((const float*) data) + 0); 426 gammas[0] = *(((const float*) data) + 0);
378 gammas[1] = *(((const float*) data) + 1); 427 gammas[1] = *(((const float*) data) + 1);
379 gammas[2] = *(((const float*) data) + 2); 428 gammas[2] = *(((const float*) data) + 2);
380 data = SkTAddOffset<const void>(data, 3 * sizeof(float)); 429 data = SkTAddOffset<const void>(data, 3 * sizeof(float));
381 430
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 switch (as_CSB(src)->fGammaNamed) { 462 switch (as_CSB(src)->fGammaNamed) {
414 case kSRGB_SkGammaNamed: 463 case kSRGB_SkGammaNamed:
415 case k2Dot2Curve_SkGammaNamed: 464 case k2Dot2Curve_SkGammaNamed:
416 case kLinear_SkGammaNamed: 465 case kLinear_SkGammaNamed:
417 return (as_CSB(src)->fGammaNamed == as_CSB(dst)->fGammaNamed) && 466 return (as_CSB(src)->fGammaNamed == as_CSB(dst)->fGammaNamed) &&
418 (src->fToXYZD50 == dst->fToXYZD50); 467 (src->fToXYZD50 == dst->fToXYZD50);
419 default: 468 default:
420 if (as_CSB(src)->fGammaNamed != as_CSB(dst)->fGammaNamed) { 469 if (as_CSB(src)->fGammaNamed != as_CSB(dst)->fGammaNamed) {
421 return false; 470 return false;
422 } 471 }
472 // This should still work :).
423 473
424 // It is unlikely that we will reach this case. 474 // It is unlikely that we will reach this case.
425 sk_sp<SkData> srcData = src->serialize(); 475 sk_sp<SkData> srcData = src->serialize();
426 sk_sp<SkData> dstData = dst->serialize(); 476 sk_sp<SkData> dstData = dst->serialize();
427 return srcData->size() == dstData->size() && 477 return srcData->size() == dstData->size() &&
428 0 == memcmp(srcData->data(), dstData->data(), srcData->size() ); 478 0 == memcmp(srcData->data(), dstData->data(), srcData->size() );
429 } 479 }
430 } 480 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698