Chromium Code Reviews| 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 ColorSpace::PrimaryID ColorSpace::PrimaryIDFromInt(int primary_id) { | 20 ColorSpace::PrimaryID ColorSpace::PrimaryIDFromInt(int primary_id) { |
| 19 if (primary_id < 0 || primary_id > static_cast<int>(PrimaryID::LAST)) | 21 if (primary_id < 0 || primary_id > static_cast<int>(PrimaryID::LAST)) |
| 20 return PrimaryID::UNKNOWN; | 22 return PrimaryID::UNKNOWN; |
| 21 if (primary_id > static_cast<int>(PrimaryID::LAST_STANDARD_VALUE) && | 23 if (primary_id > static_cast<int>(PrimaryID::LAST_STANDARD_VALUE) && |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 return *this != gfx::ColorSpace(); | 91 return *this != gfx::ColorSpace(); |
| 90 } | 92 } |
| 91 | 93 |
| 92 // static | 94 // static |
| 93 ColorSpace ColorSpace::CreateSRGB() { | 95 ColorSpace ColorSpace::CreateSRGB() { |
| 94 return ColorSpace(PrimaryID::BT709, TransferID::IEC61966_2_1, MatrixID::RGB, | 96 return ColorSpace(PrimaryID::BT709, TransferID::IEC61966_2_1, MatrixID::RGB, |
| 95 RangeID::FULL); | 97 RangeID::FULL); |
| 96 } | 98 } |
| 97 | 99 |
| 98 // static | 100 // static |
| 101 ColorSpace ColorSpace::CreateCustom(const SkMatrix44& to_XYZD50, | |
| 102 const SkColorSpaceTransferFn& fn) { | |
| 103 ColorSpace result(ColorSpace::PrimaryID::CUSTOM, | |
|
hubbe
2017/02/16 01:28:29
It would be really cool if we could map custom tra
ccameron
2017/02/16 01:58:47
Added a TODO. Might get to it soon (w/ some pendin
| |
| 104 ColorSpace::TransferID::CUSTOM, ColorSpace::MatrixID::RGB, | |
| 105 ColorSpace::RangeID::FULL); | |
| 106 for (int row = 0; row < 3; ++row) { | |
| 107 for (int col = 0; col < 3; ++col) { | |
| 108 result.custom_primary_matrix_[3 * row + col] = to_XYZD50.get(row, col); | |
| 109 } | |
| 110 } | |
| 111 result.custom_transfer_params_[0] = fn.fA; | |
| 112 result.custom_transfer_params_[1] = fn.fB; | |
| 113 result.custom_transfer_params_[2] = fn.fC; | |
| 114 result.custom_transfer_params_[3] = fn.fD; | |
| 115 result.custom_transfer_params_[4] = fn.fE; | |
| 116 result.custom_transfer_params_[5] = fn.fF; | |
| 117 result.custom_transfer_params_[6] = fn.fG; | |
| 118 return result; | |
| 119 } | |
| 120 | |
| 121 // static | |
| 99 ColorSpace ColorSpace::CreateSCRGBLinear() { | 122 ColorSpace ColorSpace::CreateSCRGBLinear() { |
| 100 return ColorSpace(PrimaryID::BT709, TransferID::LINEAR_HDR, MatrixID::RGB, | 123 return ColorSpace(PrimaryID::BT709, TransferID::LINEAR_HDR, MatrixID::RGB, |
| 101 RangeID::FULL); | 124 RangeID::FULL); |
| 102 } | 125 } |
| 103 | 126 |
| 104 // Static | 127 // Static |
| 105 ColorSpace ColorSpace::CreateXYZD50() { | 128 ColorSpace ColorSpace::CreateXYZD50() { |
| 106 return ColorSpace(PrimaryID::XYZ_D50, TransferID::LINEAR, MatrixID::RGB, | 129 return ColorSpace(PrimaryID::XYZ_D50, TransferID::LINEAR, MatrixID::RGB, |
| 107 RangeID::FULL); | 130 RangeID::FULL); |
| 108 } | 131 } |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 235 | 258 |
| 236 // Use the parametric transfer function if no other option is available. | 259 // Use the parametric transfer function if no other option is available. |
| 237 SkColorSpaceTransferFn fn; | 260 SkColorSpaceTransferFn fn; |
| 238 if (!GetTransferFunction(&fn)) { | 261 if (!GetTransferFunction(&fn)) { |
| 239 DLOG(ERROR) << "Failed to parameterize transfer function for SkColorSpace"; | 262 DLOG(ERROR) << "Failed to parameterize transfer function for SkColorSpace"; |
| 240 return nullptr; | 263 return nullptr; |
| 241 } | 264 } |
| 242 return SkColorSpace::MakeRGB(fn, to_xyz_d50); | 265 return SkColorSpace::MakeRGB(fn, to_xyz_d50); |
| 243 } | 266 } |
| 244 | 267 |
| 268 bool ColorSpace::GetICCProfile(ICCProfile* icc_profile) const { | |
| 269 if (!IsValid()) | |
| 270 return false; | |
| 271 | |
| 272 // If this was created from an ICC profile, retrieve that exact profile. | |
| 273 ICCProfile result; | |
| 274 if (ICCProfile::FromId(icc_profile_id_, false, icc_profile)) | |
| 275 return true; | |
| 276 | |
| 277 // Otherwise, construct an ICC profile based on the best approximated | |
| 278 // primaries and matrix. | |
| 279 SkMatrix44 to_XYZD50_matrix; | |
| 280 GetPrimaryMatrix(&to_XYZD50_matrix); | |
| 281 SkColorSpaceTransferFn fn; | |
| 282 if (!GetTransferFunction(&fn)) { | |
| 283 DLOG(ERROR) << "Failed to get ColorSpace transfer function for ICCProfile."; | |
| 284 return false; | |
| 285 } | |
| 286 sk_sp<SkData> data = SkICC::WriteToICC(fn, to_XYZD50_matrix); | |
| 287 if (!data) { | |
| 288 DLOG(ERROR) << "Failed to create SkICC."; | |
| 289 return false; | |
| 290 } | |
| 291 *icc_profile = ICCProfile::FromData(data->data(), data->size()); | |
| 292 DCHECK(icc_profile->IsValid()); | |
| 293 return true; | |
| 294 } | |
| 295 | |
| 245 void ColorSpace::GetPrimaryMatrix(SkMatrix44* to_XYZD50) const { | 296 void ColorSpace::GetPrimaryMatrix(SkMatrix44* to_XYZD50) const { |
| 246 SkColorSpacePrimaries primaries = {0}; | 297 SkColorSpacePrimaries primaries = {0}; |
| 247 switch (primaries_) { | 298 switch (primaries_) { |
| 248 case ColorSpace::PrimaryID::CUSTOM: | 299 case ColorSpace::PrimaryID::CUSTOM: |
| 249 to_XYZD50->set3x3RowMajorf(custom_primary_matrix_); | 300 to_XYZD50->set3x3RowMajorf(custom_primary_matrix_); |
| 250 return; | 301 return; |
| 251 | 302 |
| 252 case ColorSpace::PrimaryID::RESERVED0: | 303 case ColorSpace::PrimaryID::RESERVED0: |
| 253 case ColorSpace::PrimaryID::RESERVED: | 304 case ColorSpace::PrimaryID::RESERVED: |
| 254 case ColorSpace::PrimaryID::UNSPECIFIED: | 305 case ColorSpace::PrimaryID::UNSPECIFIED: |
| (...skipping 333 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 588 case MatrixID::BT2020_CL: | 639 case MatrixID::BT2020_CL: |
| 589 case MatrixID::YDZDX: | 640 case MatrixID::YDZDX: |
| 590 case MatrixID::UNKNOWN: | 641 case MatrixID::UNKNOWN: |
| 591 matrix->setScale(255.0f/219.0f, 255.0f/224.0f, 255.0f/224.0f); | 642 matrix->setScale(255.0f/219.0f, 255.0f/224.0f, 255.0f/224.0f); |
| 592 matrix->postTranslate(-16.0f/219.0f, -15.5f/224.0f, -15.5f/224.0f); | 643 matrix->postTranslate(-16.0f/219.0f, -15.5f/224.0f, -15.5f/224.0f); |
| 593 break; | 644 break; |
| 594 } | 645 } |
| 595 } | 646 } |
| 596 | 647 |
| 597 } // namespace gfx | 648 } // namespace gfx |
| OLD | NEW |