| 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_analysis.h" | 5 #include "ui/gfx/color_analysis.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 | 92 |
| 93 const HSL kDefaultLowerBound = {-1, -1, 0.15}; | 93 const HSL kDefaultLowerBound = {-1, -1, 0.15}; |
| 94 const HSL kDefaultUpperBound = {-1, -1, 0.85}; | 94 const HSL kDefaultUpperBound = {-1, -1, 0.85}; |
| 95 | 95 |
| 96 // Creates a 1-dimensional png of the pixel colors found in |colors|. | 96 // Creates a 1-dimensional png of the pixel colors found in |colors|. |
| 97 scoped_refptr<base::RefCountedMemory> CreateTestPNG( | 97 scoped_refptr<base::RefCountedMemory> CreateTestPNG( |
| 98 const std::vector<SkColor>& colors) { | 98 const std::vector<SkColor>& colors) { |
| 99 SkBitmap bitmap; | 99 SkBitmap bitmap; |
| 100 bitmap.allocN32Pixels(colors.size(), 1); | 100 bitmap.allocN32Pixels(colors.size(), 1); |
| 101 | 101 |
| 102 SkAutoLockPixels lock(bitmap); | |
| 103 for (size_t i = 0; i < colors.size(); ++i) { | 102 for (size_t i = 0; i < colors.size(); ++i) { |
| 104 bitmap.eraseArea(SkIRect::MakeXYWH(i, 0, 1, 1), colors[i]); | 103 bitmap.eraseArea(SkIRect::MakeXYWH(i, 0, 1, 1), colors[i]); |
| 105 } | 104 } |
| 106 return gfx::Image::CreateFrom1xBitmap(bitmap).As1xPNGBytes(); | 105 return gfx::Image::CreateFrom1xBitmap(bitmap).As1xPNGBytes(); |
| 107 } | 106 } |
| 108 | 107 |
| 109 class MockKMeanImageSampler : public KMeanImageSampler { | 108 class MockKMeanImageSampler : public KMeanImageSampler { |
| 110 public: | 109 public: |
| 111 MockKMeanImageSampler() : current_result_index_(0) { | 110 MockKMeanImageSampler() : current_result_index_(0) { |
| 112 } | 111 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 142 // Return true if a color channel is approximately equal to an expected value. | 141 // Return true if a color channel is approximately equal to an expected value. |
| 143 bool ChannelApproximatelyEqual(int expected, uint8_t channel) { | 142 bool ChannelApproximatelyEqual(int expected, uint8_t channel) { |
| 144 return (abs(expected - static_cast<int>(channel)) <= 1); | 143 return (abs(expected - static_cast<int>(channel)) <= 1); |
| 145 } | 144 } |
| 146 | 145 |
| 147 // Compute minimal and maximal graylevel (or alphalevel) of the input |bitmap|. | 146 // Compute minimal and maximal graylevel (or alphalevel) of the input |bitmap|. |
| 148 // |bitmap| has to be allocated and configured to kA8_Config. | 147 // |bitmap| has to be allocated and configured to kA8_Config. |
| 149 void Calculate8bitBitmapMinMax(const SkBitmap& bitmap, | 148 void Calculate8bitBitmapMinMax(const SkBitmap& bitmap, |
| 150 uint8_t* min_gl, | 149 uint8_t* min_gl, |
| 151 uint8_t* max_gl) { | 150 uint8_t* max_gl) { |
| 152 SkAutoLockPixels bitmap_lock(bitmap); | |
| 153 DCHECK(bitmap.getPixels()); | 151 DCHECK(bitmap.getPixels()); |
| 154 DCHECK_EQ(bitmap.colorType(), kAlpha_8_SkColorType); | 152 DCHECK_EQ(bitmap.colorType(), kAlpha_8_SkColorType); |
| 155 DCHECK(min_gl); | 153 DCHECK(min_gl); |
| 156 DCHECK(max_gl); | 154 DCHECK(max_gl); |
| 157 *min_gl = std::numeric_limits<uint8_t>::max(); | 155 *min_gl = std::numeric_limits<uint8_t>::max(); |
| 158 *max_gl = std::numeric_limits<uint8_t>::min(); | 156 *max_gl = std::numeric_limits<uint8_t>::min(); |
| 159 for (int y = 0; y < bitmap.height(); ++y) { | 157 for (int y = 0; y < bitmap.height(); ++y) { |
| 160 uint8_t* current_color = bitmap.getAddr8(0, y); | 158 uint8_t* current_color = bitmap.getAddr8(0, y); |
| 161 for (int x = 0; x < bitmap.width(); ++x, ++current_color) { | 159 for (int x = 0; x < bitmap.width(); ++x, ++current_color) { |
| 162 *min_gl = std::min(*min_gl, *current_color); | 160 *min_gl = std::min(*min_gl, *current_color); |
| (...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 554 bitmap = canvas.GetBitmap(); | 552 bitmap = canvas.GetBitmap(); |
| 555 expectations[1] = kPureGreen; | 553 expectations[1] = kPureGreen; |
| 556 for (size_t i = 0; i < arraysize(color_profiles); ++i) { | 554 for (size_t i = 0; i < arraysize(color_profiles); ++i) { |
| 557 EXPECT_EQ(expectations[i], | 555 EXPECT_EQ(expectations[i], |
| 558 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, | 556 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, |
| 559 color_profiles[i].saturation)); | 557 color_profiles[i].saturation)); |
| 560 } | 558 } |
| 561 } | 559 } |
| 562 | 560 |
| 563 } // namespace color_utils | 561 } // namespace color_utils |
| OLD | NEW |