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 struct HSL; |
| 21 |
20 // This class exposes the sampling method to the caller, which allows | 22 // 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 | 23 // 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 | 24 // arguments into the GetSample method in the future (such as which |
23 // cluster is being worked on, etc.). | 25 // cluster is being worked on, etc.). |
24 // | 26 // |
25 // Note: Samplers should be deterministic, as the same image may be analyzed | 27 // 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 | 28 // twice with two sampler instances and the results displayed side-by-side |
27 // to the user. | 29 // to the user. |
28 class GFX_EXPORT KMeanImageSampler { | 30 class GFX_EXPORT KMeanImageSampler { |
29 public: | 31 public: |
(...skipping 20 matching lines...) Expand all Loading... |
50 // Returns the color in an ARGB |image| that is closest in RGB-space to the | 52 // Returns the color in an ARGB |image| that is closest in RGB-space to the |
51 // provided |color|. Exported for testing. | 53 // provided |color|. Exported for testing. |
52 GFX_EXPORT SkColor FindClosestColor(const uint8_t* image, int width, int height, | 54 GFX_EXPORT SkColor FindClosestColor(const uint8_t* image, int width, int height, |
53 SkColor color); | 55 SkColor color); |
54 | 56 |
55 // Returns an SkColor that represents the calculated dominant color in the | 57 // Returns an SkColor that represents the calculated dominant color in the |
56 // image. | 58 // image. |
57 // This uses a KMean clustering algorithm to find clusters of pixel colors in | 59 // This uses a KMean clustering algorithm to find clusters of pixel colors in |
58 // RGB space. | 60 // RGB space. |
59 // |png/bitmap| represents the data of a png/bitmap encoded image. | 61 // |png/bitmap| represents the data of a png/bitmap encoded image. |
60 // |darkness_limit| represents the minimum sum of the RGB components that is | 62 // |lower_bound| represents the minimum bound of HSL values to allow. |
61 // acceptable as a color choice. This can be from 0 to 765. | 63 // |upper_bound| represents the maximum bound of HSL values to allow. |
62 // |brightness_limit| represents the maximum sum of the RGB components that is | 64 // See color_utils::IsWithinHSLRange() for description of these bounds. |
63 // acceptable as a color choice. This can be from 0 to 765. | |
64 // | 65 // |
65 // RGB KMean Algorithm (N clusters, M iterations): | 66 // RGB KMean Algorithm (N clusters, M iterations): |
66 // 1.Pick N starting colors by randomly sampling the pixels. If you see a | 67 // 1.Pick N starting colors by randomly sampling the pixels. If you see a |
67 // color you already saw keep sampling. After a certain number of tries | 68 // color you already saw keep sampling. After a certain number of tries |
68 // just remove the cluster and continue with N = N-1 clusters (for an image | 69 // just remove the cluster and continue with N = N-1 clusters (for an image |
69 // with just one color this should devolve to N=1). These colors are the | 70 // with just one color this should devolve to N=1). These colors are the |
70 // centers of your N clusters. | 71 // centers of your N clusters. |
71 // 2.For each pixel in the image find the cluster that it is closest to in RGB | 72 // 2.For each pixel in the image find the cluster that it is closest to in RGB |
72 // space. Add that pixel's color to that cluster (we keep a sum and a count | 73 // space. Add that pixel's color to that cluster (we keep a sum and a count |
73 // of all of the pixels added to the space, so just add it to the sum and | 74 // of all of the pixels added to the space, so just add it to the sum and |
74 // increment count). | 75 // increment count). |
75 // 3.Calculate the new cluster centroids by getting the average color of all of | 76 // 3.Calculate the new cluster centroids by getting the average color of all of |
76 // the pixels in each cluster (dividing the sum by the count). | 77 // the pixels in each cluster (dividing the sum by the count). |
77 // 4.See if the new centroids are the same as the old centroids. | 78 // 4.See if the new centroids are the same as the old centroids. |
78 // a) If this is the case for all N clusters than we have converged and | 79 // a) If this is the case for all N clusters than we have converged and |
79 // can move on. | 80 // can move on. |
80 // b) If any centroid moved, repeat step 2 with the new centroids for up | 81 // b) If any centroid moved, repeat step 2 with the new centroids for up |
81 // to M iterations. | 82 // to M iterations. |
82 // 5.Once the clusters have converged or M iterations have been tried, sort | 83 // 5.Once the clusters have converged or M iterations have been tried, sort |
83 // the clusters by weight (where weight is the number of pixels that make up | 84 // the clusters by weight (where weight is the number of pixels that make up |
84 // this cluster). | 85 // this cluster). |
85 // 6.Going through the sorted list of clusters, pick the first cluster with the | 86 // 6.Going through the sorted list of clusters, pick the first cluster with the |
86 // largest weight that's centroid fulfills the equation | 87 // largest weight that's centroid falls between |lower_bound| and |
87 // |darkness_limit| < SUM(R, G, B) < |brightness_limit|. Return that color. | 88 // |upper_bound|. Return that color. |
88 // If no color fulfills that requirement return the color with the largest | 89 // If no color fulfills that requirement return the color with the largest |
89 // weight regardless of whether or not it fulfills the equation above. | 90 // weight regardless of whether or not it fulfills the equation above. |
90 // | 91 GFX_EXPORT SkColor |
91 // Note: Switching to HSV space did not improve the results of this algorithm | 92 CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png, |
92 // for typical favicon images. | 93 const HSL& lower_bound, |
93 GFX_EXPORT SkColor CalculateKMeanColorOfPNG( | 94 const HSL& upper_bound, |
94 scoped_refptr<base::RefCountedMemory> png, | 95 KMeanImageSampler* sampler); |
95 uint32_t darkness_limit, | |
96 uint32_t brightness_limit, | |
97 KMeanImageSampler* sampler); | |
98 GFX_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap, | 96 GFX_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap, |
99 uint32_t darkness_limit, | 97 const HSL& lower_bound, |
100 uint32_t brightness_limit, | 98 const HSL& upper_bound, |
101 KMeanImageSampler* sampler); | 99 KMeanImageSampler* sampler); |
102 | 100 |
103 // Computes a dominant color using the above algorithm and reasonable defaults | 101 // Computes a dominant color for using the above algorithm and reasonable |
104 // for |darkness_limit|, |brightness_limit| and |sampler|. | 102 // defaults for |lower_bound|, |upper_bound| and |sampler|. |
105 GFX_EXPORT SkColor | 103 GFX_EXPORT SkColor |
106 CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png); | 104 CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png); |
107 GFX_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap); | 105 GFX_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap); |
108 | 106 |
109 // Compute color covariance matrix for the input bitmap. | 107 // Compute color covariance matrix for the input bitmap. |
110 GFX_EXPORT gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap); | 108 GFX_EXPORT gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap); |
111 | 109 |
112 // Apply a color reduction transform defined by |color_transform| vector to | 110 // Apply a color reduction transform defined by |color_transform| vector to |
113 // |source_bitmap|. The result is put into |target_bitmap|, which is expected | 111 // |source_bitmap|. The result is put into |target_bitmap|, which is expected |
114 // to be initialized to the required size and type (SkBitmap::kA8_Config). | 112 // to be initialized to the required size and type (SkBitmap::kA8_Config). |
(...skipping 10 matching lines...) Expand all Loading... |
125 // initialized to the required size and type (SkBitmap::kA8_Config). | 123 // initialized to the required size and type (SkBitmap::kA8_Config). |
126 // Returns true if the conversion succeeded. Note that there might be legitimate | 124 // Returns true if the conversion succeeded. Note that there might be legitimate |
127 // reasons for the process to fail even if all input was correct. This is a | 125 // reasons for the process to fail even if all input was correct. This is a |
128 // condition the caller must be able to handle. | 126 // condition the caller must be able to handle. |
129 GFX_EXPORT bool ComputePrincipalComponentImage(const SkBitmap& source_bitmap, | 127 GFX_EXPORT bool ComputePrincipalComponentImage(const SkBitmap& source_bitmap, |
130 SkBitmap* target_bitmap); | 128 SkBitmap* target_bitmap); |
131 | 129 |
132 } // namespace color_utils | 130 } // namespace color_utils |
133 | 131 |
134 #endif // UI_GFX_COLOR_ANALYSIS_H_ | 132 #endif // UI_GFX_COLOR_ANALYSIS_H_ |
OLD | NEW |