OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/color_space.h" | 5 #include "ui/gfx/color_space.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/synchronization/lock.h" | 10 #include "base/synchronization/lock.h" |
11 #include "third_party/skia/include/core/SkColorSpace.h" | 11 #include "third_party/skia/include/core/SkColorSpace.h" |
| 12 #include "third_party/skia/include/core/SkData.h" |
| 13 #include "third_party/skia/include/core/SkICC.h" |
12 #include "ui/gfx/icc_profile.h" | 14 #include "ui/gfx/icc_profile.h" |
13 #include "ui/gfx/skia_color_space_util.h" | 15 #include "ui/gfx/skia_color_space_util.h" |
14 #include "ui/gfx/transform.h" | 16 #include "ui/gfx/transform.h" |
15 | 17 |
16 namespace gfx { | 18 namespace gfx { |
17 | 19 |
18 // static | 20 // static |
19 ColorSpace ColorSpace::CreateVideo(int video_primary, | 21 ColorSpace ColorSpace::CreateVideo(int video_primary, |
20 int video_transfer, | 22 int video_transfer, |
21 int video_matrix, | 23 int video_matrix, |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 matrix_ != MatrixID::INVALID && range_ != RangeID::INVALID; | 194 matrix_ != MatrixID::INVALID && range_ != RangeID::INVALID; |
193 } | 195 } |
194 | 196 |
195 // static | 197 // static |
196 ColorSpace ColorSpace::CreateSRGB() { | 198 ColorSpace ColorSpace::CreateSRGB() { |
197 return ColorSpace(PrimaryID::BT709, TransferID::IEC61966_2_1, MatrixID::RGB, | 199 return ColorSpace(PrimaryID::BT709, TransferID::IEC61966_2_1, MatrixID::RGB, |
198 RangeID::FULL); | 200 RangeID::FULL); |
199 } | 201 } |
200 | 202 |
201 // static | 203 // static |
| 204 ColorSpace ColorSpace::CreateCustom(const SkMatrix44& to_XYZD50, |
| 205 const SkColorSpaceTransferFn& fn) { |
| 206 ColorSpace result(ColorSpace::PrimaryID::CUSTOM, |
| 207 ColorSpace::TransferID::CUSTOM, ColorSpace::MatrixID::RGB, |
| 208 ColorSpace::RangeID::FULL); |
| 209 for (int row = 0; row < 3; ++row) { |
| 210 for (int col = 0; col < 3; ++col) { |
| 211 result.custom_primary_matrix_[3 * row + col] = to_XYZD50.get(row, col); |
| 212 } |
| 213 } |
| 214 result.custom_transfer_params_[0] = fn.fA; |
| 215 result.custom_transfer_params_[1] = fn.fB; |
| 216 result.custom_transfer_params_[2] = fn.fC; |
| 217 result.custom_transfer_params_[3] = fn.fD; |
| 218 result.custom_transfer_params_[4] = fn.fE; |
| 219 result.custom_transfer_params_[5] = fn.fF; |
| 220 result.custom_transfer_params_[6] = fn.fG; |
| 221 // TODO(ccameron): Use enums for near matches to know color spaces. |
| 222 return result; |
| 223 } |
| 224 |
| 225 // static |
202 ColorSpace ColorSpace::CreateSCRGBLinear() { | 226 ColorSpace ColorSpace::CreateSCRGBLinear() { |
203 return ColorSpace(PrimaryID::BT709, TransferID::LINEAR_HDR, MatrixID::RGB, | 227 return ColorSpace(PrimaryID::BT709, TransferID::LINEAR_HDR, MatrixID::RGB, |
204 RangeID::FULL); | 228 RangeID::FULL); |
205 } | 229 } |
206 | 230 |
207 // Static | 231 // Static |
208 ColorSpace ColorSpace::CreateXYZD50() { | 232 ColorSpace ColorSpace::CreateXYZD50() { |
209 return ColorSpace(PrimaryID::XYZ_D50, TransferID::LINEAR, MatrixID::RGB, | 233 return ColorSpace(PrimaryID::XYZ_D50, TransferID::LINEAR, MatrixID::RGB, |
210 RangeID::FULL); | 234 RangeID::FULL); |
211 } | 235 } |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 | 360 |
337 // Use the parametric transfer function if no other option is available. | 361 // Use the parametric transfer function if no other option is available. |
338 SkColorSpaceTransferFn fn; | 362 SkColorSpaceTransferFn fn; |
339 if (!GetTransferFunction(&fn)) { | 363 if (!GetTransferFunction(&fn)) { |
340 DLOG(ERROR) << "Failed to parameterize transfer function for SkColorSpace"; | 364 DLOG(ERROR) << "Failed to parameterize transfer function for SkColorSpace"; |
341 return nullptr; | 365 return nullptr; |
342 } | 366 } |
343 return SkColorSpace::MakeRGB(fn, to_xyz_d50); | 367 return SkColorSpace::MakeRGB(fn, to_xyz_d50); |
344 } | 368 } |
345 | 369 |
| 370 bool ColorSpace::GetICCProfile(ICCProfile* icc_profile) const { |
| 371 if (!IsValid()) |
| 372 return false; |
| 373 |
| 374 // If this was created from an ICC profile, retrieve that exact profile. |
| 375 ICCProfile result; |
| 376 if (ICCProfile::FromId(icc_profile_id_, false, icc_profile)) |
| 377 return true; |
| 378 |
| 379 // Otherwise, construct an ICC profile based on the best approximated |
| 380 // primaries and matrix. |
| 381 SkMatrix44 to_XYZD50_matrix; |
| 382 GetPrimaryMatrix(&to_XYZD50_matrix); |
| 383 SkColorSpaceTransferFn fn; |
| 384 if (!GetTransferFunction(&fn)) { |
| 385 DLOG(ERROR) << "Failed to get ColorSpace transfer function for ICCProfile."; |
| 386 return false; |
| 387 } |
| 388 sk_sp<SkData> data = SkICC::WriteToICC(fn, to_XYZD50_matrix); |
| 389 if (!data) { |
| 390 DLOG(ERROR) << "Failed to create SkICC."; |
| 391 return false; |
| 392 } |
| 393 *icc_profile = ICCProfile::FromData(data->data(), data->size()); |
| 394 DCHECK(icc_profile->IsValid()); |
| 395 return true; |
| 396 } |
| 397 |
346 void ColorSpace::GetPrimaryMatrix(SkMatrix44* to_XYZD50) const { | 398 void ColorSpace::GetPrimaryMatrix(SkMatrix44* to_XYZD50) const { |
347 SkColorSpacePrimaries primaries = {0}; | 399 SkColorSpacePrimaries primaries = {0}; |
348 switch (primaries_) { | 400 switch (primaries_) { |
349 case ColorSpace::PrimaryID::CUSTOM: | 401 case ColorSpace::PrimaryID::CUSTOM: |
350 to_XYZD50->set3x3RowMajorf(custom_primary_matrix_); | 402 to_XYZD50->set3x3RowMajorf(custom_primary_matrix_); |
351 return; | 403 return; |
352 | 404 |
353 case ColorSpace::PrimaryID::INVALID: | 405 case ColorSpace::PrimaryID::INVALID: |
354 to_XYZD50->setIdentity(); | 406 to_XYZD50->setIdentity(); |
355 return; | 407 return; |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 case MatrixID::BT2020_NCL: | 733 case MatrixID::BT2020_NCL: |
682 case MatrixID::BT2020_CL: | 734 case MatrixID::BT2020_CL: |
683 case MatrixID::YDZDX: | 735 case MatrixID::YDZDX: |
684 matrix->setScale(255.0f/219.0f, 255.0f/224.0f, 255.0f/224.0f); | 736 matrix->setScale(255.0f/219.0f, 255.0f/224.0f, 255.0f/224.0f); |
685 matrix->postTranslate(-16.0f/219.0f, -15.5f/224.0f, -15.5f/224.0f); | 737 matrix->postTranslate(-16.0f/219.0f, -15.5f/224.0f, -15.5f/224.0f); |
686 break; | 738 break; |
687 } | 739 } |
688 } | 740 } |
689 | 741 |
690 } // namespace gfx | 742 } // namespace gfx |
OLD | NEW |