OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/icc_profile.h" | 5 #include "ui/gfx/icc_profile.h" |
6 | 6 |
7 #include <list> | 7 #include <list> |
8 | 8 |
9 #include "base/containers/mru_cache.h" | 9 #include "base/containers/mru_cache.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "base/synchronization/lock.h" | 11 #include "base/synchronization/lock.h" |
| 12 #include "ui/gfx/color_transform.h" |
12 | 13 |
13 namespace gfx { | 14 namespace gfx { |
14 | 15 |
15 namespace { | 16 namespace { |
16 const size_t kMinProfileLength = 128; | 17 const size_t kMinProfileLength = 128; |
17 const size_t kMaxProfileLength = 4 * 1024 * 1024; | 18 const size_t kMaxProfileLength = 4 * 1024 * 1024; |
18 | 19 |
19 // Allow keeping around a maximum of 8 cached ICC profiles. Beware that | 20 // Allow keeping around a maximum of 8 cached ICC profiles. Beware that |
20 // we will do a linear search thorugh currently-cached ICC profiles, | 21 // we will do a linear search thorugh currently-cached ICC profiles, |
21 // when creating a new ICC profile. | 22 // when creating a new ICC profile. |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 #endif | 84 #endif |
84 | 85 |
85 // static | 86 // static |
86 ICCProfile ICCProfile::FromColorSpace(const gfx::ColorSpace& color_space) { | 87 ICCProfile ICCProfile::FromColorSpace(const gfx::ColorSpace& color_space) { |
87 // Retrieve ICC profiles from the cache. | 88 // Retrieve ICC profiles from the cache. |
88 if (color_space.icc_profile_id_) { | 89 if (color_space.icc_profile_id_) { |
89 Cache& cache = g_cache.Get(); | 90 Cache& cache = g_cache.Get(); |
90 base::AutoLock lock(cache.lock); | 91 base::AutoLock lock(cache.lock); |
91 | 92 |
92 auto found = cache.id_to_icc_profile_mru.Get(color_space.icc_profile_id_); | 93 auto found = cache.id_to_icc_profile_mru.Get(color_space.icc_profile_id_); |
93 if (found != cache.id_to_icc_profile_mru.end()) | 94 if (found != cache.id_to_icc_profile_mru.end()) { |
94 return found->second; | 95 return found->second; |
| 96 } |
95 } | 97 } |
96 // TODO(ccameron): Support constructing ICC profiles from arbitrary ColorSpace | 98 // TODO(ccameron): Support constructing ICC profiles from arbitrary ColorSpace |
97 // objects. | 99 // objects. |
98 return ICCProfile(); | 100 return ICCProfile(); |
99 } | 101 } |
100 | 102 |
101 const std::vector<char>& ICCProfile::GetData() const { | 103 const std::vector<char>& ICCProfile::GetData() const { |
102 return data_; | 104 return data_; |
103 } | 105 } |
104 | 106 |
105 ColorSpace ICCProfile::GetColorSpace() const { | 107 ColorSpace ICCProfile::GetColorSpace() const { |
106 ColorSpace color_space(ColorSpace::PrimaryID::CUSTOM, | 108 ColorSpace color_space(ColorSpace::PrimaryID::CUSTOM, |
107 ColorSpace::TransferID::CUSTOM, | 109 ColorSpace::TransferID::CUSTOM, |
108 ColorSpace::MatrixID::RGB, ColorSpace::RangeID::FULL); | 110 ColorSpace::MatrixID::RGB, ColorSpace::RangeID::FULL); |
109 color_space.icc_profile_id_ = id_; | 111 color_space.icc_profile_id_ = id_; |
110 | 112 |
111 // Move this ICC profile to the most recently used end of the cache. | 113 // Move this ICC profile to the most recently used end of the cache. |
112 { | 114 { |
113 Cache& cache = g_cache.Get(); | 115 Cache& cache = g_cache.Get(); |
114 base::AutoLock lock(cache.lock); | 116 base::AutoLock lock(cache.lock); |
115 | 117 |
116 auto found = cache.id_to_icc_profile_mru.Get(id_); | 118 auto found = cache.id_to_icc_profile_mru.Get(id_); |
117 if (found == cache.id_to_icc_profile_mru.end()) | 119 if (found == cache.id_to_icc_profile_mru.end()) |
118 cache.id_to_icc_profile_mru.Put(id_, *this); | 120 cache.id_to_icc_profile_mru.Put(id_, *this); |
119 } | 121 } |
| 122 |
| 123 ColorSpace unity_colorspace( |
| 124 ColorSpace::PrimaryID::CUSTOM, ColorSpace::TransferID::LINEAR, |
| 125 ColorSpace::MatrixID::RGB, ColorSpace::RangeID::FULL); |
| 126 unity_colorspace.custom_primary_matrix_[0] = 1.0f; |
| 127 unity_colorspace.custom_primary_matrix_[1] = 0.0f; |
| 128 unity_colorspace.custom_primary_matrix_[2] = 0.0f; |
| 129 unity_colorspace.custom_primary_matrix_[3] = 0.0f; |
| 130 |
| 131 unity_colorspace.custom_primary_matrix_[4] = 0.0f; |
| 132 unity_colorspace.custom_primary_matrix_[5] = 1.0f; |
| 133 unity_colorspace.custom_primary_matrix_[6] = 0.0f; |
| 134 unity_colorspace.custom_primary_matrix_[7] = 0.0f; |
| 135 |
| 136 unity_colorspace.custom_primary_matrix_[8] = 0.0f; |
| 137 unity_colorspace.custom_primary_matrix_[9] = 0.0f; |
| 138 unity_colorspace.custom_primary_matrix_[10] = 1.0f; |
| 139 unity_colorspace.custom_primary_matrix_[11] = 0.0f; |
| 140 |
| 141 // This will look up and use the ICC profile. |
| 142 std::unique_ptr<ColorTransform> transform(ColorTransform::NewColorTransform( |
| 143 color_space, unity_colorspace, ColorTransform::Intent::INTENT_ABSOLUTE)); |
| 144 |
| 145 ColorTransform::TriStim tmp[4]; |
| 146 tmp[0].set_x(1.0f); |
| 147 tmp[1].set_y(1.0f); |
| 148 tmp[2].set_z(1.0f); |
| 149 transform->transform(tmp, arraysize(tmp)); |
| 150 |
| 151 color_space.custom_primary_matrix_[0] = tmp[0].x() - tmp[3].x(); |
| 152 color_space.custom_primary_matrix_[1] = tmp[1].x() - tmp[3].x(); |
| 153 color_space.custom_primary_matrix_[2] = tmp[2].x() - tmp[3].x(); |
| 154 color_space.custom_primary_matrix_[3] = tmp[3].x(); |
| 155 |
| 156 color_space.custom_primary_matrix_[4] = tmp[0].y() - tmp[3].y(); |
| 157 color_space.custom_primary_matrix_[5] = tmp[1].y() - tmp[3].y(); |
| 158 color_space.custom_primary_matrix_[6] = tmp[2].y() - tmp[3].y(); |
| 159 color_space.custom_primary_matrix_[7] = tmp[3].y(); |
| 160 |
| 161 color_space.custom_primary_matrix_[8] = tmp[0].z() - tmp[3].z(); |
| 162 color_space.custom_primary_matrix_[9] = tmp[1].z() - tmp[3].z(); |
| 163 color_space.custom_primary_matrix_[10] = tmp[2].z() - tmp[3].z(); |
| 164 color_space.custom_primary_matrix_[11] = tmp[3].z(); |
| 165 |
120 return color_space; | 166 return color_space; |
121 } | 167 } |
122 | 168 |
123 // static | 169 // static |
124 bool ICCProfile::IsValidProfileLength(size_t length) { | 170 bool ICCProfile::IsValidProfileLength(size_t length) { |
125 return length >= kMinProfileLength && length <= kMaxProfileLength; | 171 return length >= kMinProfileLength && length <= kMaxProfileLength; |
126 } | 172 } |
127 | 173 |
128 } // namespace gfx | 174 } // namespace gfx |
OLD | NEW |