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 #ifndef UI_GFX_COLOR_ANALYSIS_H_ | 5 #ifndef UI_GFX_COLOR_ANALYSIS_H_ |
6 #define UI_GFX_COLOR_ANALYSIS_H_ | 6 #define UI_GFX_COLOR_ANALYSIS_H_ |
7 | 7 |
8 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
12 #include "third_party/skia/include/core/SkColor.h" | 12 #include "third_party/skia/include/core/SkColor.h" |
13 #include "ui/gfx/gfx_export.h" | 13 #include "ui/gfx/gfx_export.h" |
14 #include "ui/gfx/matrix3_f.h" | 14 #include "ui/gfx/matrix3_f.h" |
15 | 15 |
16 class SkBitmap; | 16 class SkBitmap; |
17 | 17 |
18 namespace color_utils { | 18 namespace color_utils { |
19 | 19 |
20 // This class exposes the sampling method to the caller, which allows | 20 // This class exposes the sampling method to the caller, which allows |
21 // stubbing out for things like unit tests. Might be useful to pass more | 21 // stubbing out for things like unit tests. Might be useful to pass more |
22 // arguments into the GetSample method in the future (such as which | 22 // arguments into the GetSample method in the future (such as which |
23 // cluster is being worked on, etc.). | 23 // cluster is being worked on, etc.). |
24 // | 24 // |
25 // Note: Samplers should be deterministic, as the same image may be analyzed | 25 // Note: Samplers should be deterministic, as the same image may be analyzed |
26 // twice with two sampler instances and the results displayed side-by-side | 26 // twice with two sampler instances and the results displayed side-by-side |
27 // to the user. | 27 // to the user. |
28 class UI_EXPORT KMeanImageSampler { | 28 class GFX_EXPORT KMeanImageSampler { |
29 public: | 29 public: |
30 virtual int GetSample(int width, int height) = 0; | 30 virtual int GetSample(int width, int height) = 0; |
31 | 31 |
32 protected: | 32 protected: |
33 KMeanImageSampler(); | 33 KMeanImageSampler(); |
34 virtual ~KMeanImageSampler(); | 34 virtual ~KMeanImageSampler(); |
35 }; | 35 }; |
36 | 36 |
37 // This sampler will pick pixels from an evenly spaced grid. | 37 // This sampler will pick pixels from an evenly spaced grid. |
38 class UI_EXPORT GridSampler : public KMeanImageSampler { | 38 class GFX_EXPORT GridSampler : public KMeanImageSampler { |
39 public: | 39 public: |
40 GridSampler(); | 40 GridSampler(); |
41 virtual ~GridSampler(); | 41 virtual ~GridSampler(); |
42 | 42 |
43 virtual int GetSample(int width, int height) OVERRIDE; | 43 virtual int GetSample(int width, int height) OVERRIDE; |
44 | 44 |
45 private: | 45 private: |
46 // The number of times GetSample has been called. | 46 // The number of times GetSample has been called. |
47 int calls_; | 47 int calls_; |
48 }; | 48 }; |
49 | 49 |
50 // Returns the color in an ARGB |image| that is closest in RGB-space to the | 50 // Returns the color in an ARGB |image| that is closest in RGB-space to the |
51 // provided |color|. Exported for testing. | 51 // provided |color|. Exported for testing. |
52 UI_EXPORT SkColor FindClosestColor(const uint8_t* image, int width, int height, | 52 GFX_EXPORT SkColor FindClosestColor(const uint8_t* image, int width, int height, |
53 SkColor color); | 53 SkColor color); |
54 | 54 |
55 // Returns an SkColor that represents the calculated dominant color in the png. | 55 // Returns an SkColor that represents the calculated dominant color in the png. |
56 // This uses a KMean clustering algorithm to find clusters of pixel colors in | 56 // This uses a KMean clustering algorithm to find clusters of pixel colors in |
57 // RGB space. | 57 // RGB space. |
58 // |png| represents the data of a png encoded image. | 58 // |png| represents the data of a png encoded image. |
59 // |darkness_limit| represents the minimum sum of the RGB components that is | 59 // |darkness_limit| represents the minimum sum of the RGB components that is |
60 // acceptable as a color choice. This can be from 0 to 765. | 60 // acceptable as a color choice. This can be from 0 to 765. |
61 // |brightness_limit| represents the maximum sum of the RGB components that is | 61 // |brightness_limit| represents the maximum sum of the RGB components that is |
62 // acceptable as a color choice. This can be from 0 to 765. | 62 // acceptable as a color choice. This can be from 0 to 765. |
63 // | 63 // |
(...skipping 18 matching lines...) Expand all Loading... |
82 // the clusters by weight (where weight is the number of pixels that make up | 82 // the clusters by weight (where weight is the number of pixels that make up |
83 // this cluster). | 83 // this cluster). |
84 // 6.Going through the sorted list of clusters, pick the first cluster with the | 84 // 6.Going through the sorted list of clusters, pick the first cluster with the |
85 // largest weight that's centroid fulfills the equation | 85 // largest weight that's centroid fulfills the equation |
86 // |darkness_limit| < SUM(R, G, B) < |brightness_limit|. Return that color. | 86 // |darkness_limit| < SUM(R, G, B) < |brightness_limit|. Return that color. |
87 // If no color fulfills that requirement return the color with the largest | 87 // If no color fulfills that requirement return the color with the largest |
88 // weight regardless of whether or not it fulfills the equation above. | 88 // weight regardless of whether or not it fulfills the equation above. |
89 // | 89 // |
90 // Note: Switching to HSV space did not improve the results of this algorithm | 90 // Note: Switching to HSV space did not improve the results of this algorithm |
91 // for typical favicon images. | 91 // for typical favicon images. |
92 UI_EXPORT SkColor CalculateKMeanColorOfPNG( | 92 GFX_EXPORT SkColor CalculateKMeanColorOfPNG( |
93 scoped_refptr<base::RefCountedMemory> png, | 93 scoped_refptr<base::RefCountedMemory> png, |
94 uint32_t darkness_limit, | 94 uint32_t darkness_limit, |
95 uint32_t brightness_limit, | 95 uint32_t brightness_limit, |
96 KMeanImageSampler* sampler); | 96 KMeanImageSampler* sampler); |
97 | 97 |
98 // Computes a dominant color for an SkBitmap using the above algorithm and | 98 // Computes a dominant color for an SkBitmap using the above algorithm and |
99 // reasonable defaults for |darkness_limit|, |brightness_limit| and |sampler|. | 99 // reasonable defaults for |darkness_limit|, |brightness_limit| and |sampler|. |
100 UI_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap); | 100 GFX_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap); |
101 | 101 |
102 // Compute color covariance matrix for the input bitmap. | 102 // Compute color covariance matrix for the input bitmap. |
103 UI_EXPORT gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap); | 103 GFX_EXPORT gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap); |
104 | 104 |
105 // Apply a color reduction transform defined by |color_transform| vector to | 105 // Apply a color reduction transform defined by |color_transform| vector to |
106 // |source_bitmap|. The result is put into |target_bitmap|, which is expected | 106 // |source_bitmap|. The result is put into |target_bitmap|, which is expected |
107 // to be initialized to the required size and type (SkBitmap::kA8_Config). | 107 // to be initialized to the required size and type (SkBitmap::kA8_Config). |
108 // If |fit_to_range|, result is transfored linearly to fit 0-0xFF range. | 108 // If |fit_to_range|, result is transfored linearly to fit 0-0xFF range. |
109 // Otherwise, data is clipped. | 109 // Otherwise, data is clipped. |
110 // Returns true if the target has been computed. | 110 // Returns true if the target has been computed. |
111 UI_EXPORT bool ApplyColorReduction(const SkBitmap& source_bitmap, | 111 GFX_EXPORT bool ApplyColorReduction(const SkBitmap& source_bitmap, |
112 const gfx::Vector3dF& color_transform, | 112 const gfx::Vector3dF& color_transform, |
113 bool fit_to_range, | 113 bool fit_to_range, |
114 SkBitmap* target_bitmap); | 114 SkBitmap* target_bitmap); |
115 | 115 |
116 // Compute a monochrome image representing the principal color component of | 116 // Compute a monochrome image representing the principal color component of |
117 // the |source_bitmap|. The result is stored in |target_bitmap|, which must be | 117 // the |source_bitmap|. The result is stored in |target_bitmap|, which must be |
118 // initialized to the required size and type (SkBitmap::kA8_Config). | 118 // initialized to the required size and type (SkBitmap::kA8_Config). |
119 // Returns true if the conversion succeeded. Note that there might be legitimate | 119 // Returns true if the conversion succeeded. Note that there might be legitimate |
120 // reasons for the process to fail even if all input was correct. This is a | 120 // reasons for the process to fail even if all input was correct. This is a |
121 // condition the caller must be able to handle. | 121 // condition the caller must be able to handle. |
122 UI_EXPORT bool ComputePrincipalComponentImage(const SkBitmap& source_bitmap, | 122 GFX_EXPORT bool ComputePrincipalComponentImage(const SkBitmap& source_bitmap, |
123 SkBitmap* target_bitmap); | 123 SkBitmap* target_bitmap); |
124 | 124 |
125 } // namespace color_utils | 125 } // namespace color_utils |
126 | 126 |
127 #endif // UI_GFX_COLOR_ANALYSIS_H_ | 127 #endif // UI_GFX_COLOR_ANALYSIS_H_ |
OLD | NEW |