Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(650)

Side by Side Diff: ui/gfx/color_analysis.h

Issue 289283004: Add ability to constrain dominant color selection to a HSL range. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 19 matching lines...) Expand all
49 51
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. This uses a KMean clustering algorithm to find clusters of pixel 58 // image. This uses a KMean clustering algorithm to find clusters of pixel
57 // colors in RGB space. 59 // colors in RGB space.
58 // |png|/|bitmap| represents the data of a png/bitmap encoded image. 60 // |png|/|bitmap| represents the data of a png/bitmap encoded image.
59 // |darkness_limit| represents the minimum sum of the RGB components that is 61 // |lower_bound| represents the minimum bound of HSL values to allow.
60 // acceptable as a color choice. This can be from 0 to 765. 62 // |upper_bound| represents the maximum bound of HSL values to allow.
61 // |brightness_limit| represents the maximum sum of the RGB components that is 63 // See color_utils::IsWithinHSLRange() for description of these bounds.
62 // acceptable as a color choice. This can be from 0 to 765.
63 // 64 //
64 // RGB KMean Algorithm (N clusters, M iterations): 65 // RGB KMean Algorithm (N clusters, M iterations):
65 // 1.Pick N starting colors by randomly sampling the pixels. If you see a 66 // 1.Pick N starting colors by randomly sampling the pixels. If you see a
66 // color you already saw keep sampling. After a certain number of tries 67 // color you already saw keep sampling. After a certain number of tries
67 // just remove the cluster and continue with N = N-1 clusters (for an image 68 // just remove the cluster and continue with N = N-1 clusters (for an image
68 // with just one color this should devolve to N=1). These colors are the 69 // with just one color this should devolve to N=1). These colors are the
69 // centers of your N clusters. 70 // centers of your N clusters.
70 // 2.For each pixel in the image find the cluster that it is closest to in RGB 71 // 2.For each pixel in the image find the cluster that it is closest to in RGB
71 // space. Add that pixel's color to that cluster (we keep a sum and a count 72 // space. Add that pixel's color to that cluster (we keep a sum and a count
72 // of all of the pixels added to the space, so just add it to the sum and 73 // of all of the pixels added to the space, so just add it to the sum and
73 // increment count). 74 // increment count).
74 // 3.Calculate the new cluster centroids by getting the average color of all of 75 // 3.Calculate the new cluster centroids by getting the average color of all of
75 // the pixels in each cluster (dividing the sum by the count). 76 // the pixels in each cluster (dividing the sum by the count).
76 // 4.See if the new centroids are the same as the old centroids. 77 // 4.See if the new centroids are the same as the old centroids.
77 // a) If this is the case for all N clusters than we have converged and 78 // a) If this is the case for all N clusters than we have converged and
78 // can move on. 79 // can move on.
79 // b) If any centroid moved, repeat step 2 with the new centroids for up 80 // b) If any centroid moved, repeat step 2 with the new centroids for up
80 // to M iterations. 81 // to M iterations.
81 // 5.Once the clusters have converged or M iterations have been tried, sort 82 // 5.Once the clusters have converged or M iterations have been tried, sort
82 // the clusters by weight (where weight is the number of pixels that make up 83 // the clusters by weight (where weight is the number of pixels that make up
83 // this cluster). 84 // this cluster).
84 // 6.Going through the sorted list of clusters, pick the first cluster with the 85 // 6.Going through the sorted list of clusters, pick the first cluster with the
85 // largest weight that's centroid fulfills the equation 86 // largest weight that's centroid falls between |lower_bound| and
86 // |darkness_limit| < SUM(R, G, B) < |brightness_limit|. Return that color. 87 // |upper_bound|. Return that color.
87 // If no color fulfills that requirement return the color with the largest 88 // If no color fulfills that requirement return the color with the largest
88 // weight regardless of whether or not it fulfills the equation above. 89 // weight regardless of whether or not it fulfills the equation above.
89 // 90 GFX_EXPORT SkColor
90 // Note: Switching to HSV space did not improve the results of this algorithm 91 CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png,
91 // for typical favicon images. 92 const HSL& lower_bound,
92 GFX_EXPORT SkColor CalculateKMeanColorOfPNG( 93 const HSL& upper_bound,
93 scoped_refptr<base::RefCountedMemory> png, 94 KMeanImageSampler* sampler);
94 uint32_t darkness_limit,
95 uint32_t brightness_limit,
96 KMeanImageSampler* sampler);
97 // Computes a dominant color using the above algorithm and reasonable defaults 95 // Computes a dominant color using the above algorithm and reasonable defaults
98 // for |darkness_limit|, |brightness_limit| and |sampler|. 96 // for |lower_bound|, |upper_bound| and |sampler|.
99 GFX_EXPORT SkColor CalculateKMeanColorOfPNG( 97 GFX_EXPORT SkColor CalculateKMeanColorOfPNG(
100 scoped_refptr<base::RefCountedMemory> png); 98 scoped_refptr<base::RefCountedMemory> png);
101 99
102 // Returns an SkColor that represents the calculated dominant color in the 100 // Returns an SkColor that represents the calculated dominant color in the
103 // image. See CalculateKMeanColorOfPNG() for details. 101 // image. See CalculateKMeanColorOfPNG() for details.
104 GFX_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap, 102 GFX_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap,
105 uint32_t darkness_limit, 103 const HSL& lower_bound,
106 uint32_t brightness_limit, 104 const HSL& upper_bound,
107 KMeanImageSampler* sampler); 105 KMeanImageSampler* sampler);
108 // Computes a dominant color using the above algorithm and reasonable defaults 106 // Computes a dominant color using the above algorithm and reasonable defaults
109 // for |darkness_limit|, |brightness_limit| and |sampler|. 107 // for |lower_bound|, |upper_bound| and |sampler|.
110 GFX_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap); 108 GFX_EXPORT SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap);
111 109
112 // Compute color covariance matrix for the input bitmap. 110 // Compute color covariance matrix for the input bitmap.
113 GFX_EXPORT gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap); 111 GFX_EXPORT gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap);
114 112
115 // Apply a color reduction transform defined by |color_transform| vector to 113 // Apply a color reduction transform defined by |color_transform| vector to
116 // |source_bitmap|. The result is put into |target_bitmap|, which is expected 114 // |source_bitmap|. The result is put into |target_bitmap|, which is expected
117 // to be initialized to the required size and type (SkBitmap::kA8_Config). 115 // to be initialized to the required size and type (SkBitmap::kA8_Config).
118 // If |fit_to_range|, result is transfored linearly to fit 0-0xFF range. 116 // If |fit_to_range|, result is transfored linearly to fit 0-0xFF range.
119 // Otherwise, data is clipped. 117 // Otherwise, data is clipped.
120 // Returns true if the target has been computed. 118 // Returns true if the target has been computed.
121 GFX_EXPORT bool ApplyColorReduction(const SkBitmap& source_bitmap, 119 GFX_EXPORT bool ApplyColorReduction(const SkBitmap& source_bitmap,
122 const gfx::Vector3dF& color_transform, 120 const gfx::Vector3dF& color_transform,
123 bool fit_to_range, 121 bool fit_to_range,
124 SkBitmap* target_bitmap); 122 SkBitmap* target_bitmap);
125 123
126 // Compute a monochrome image representing the principal color component of 124 // Compute a monochrome image representing the principal color component of
127 // the |source_bitmap|. The result is stored in |target_bitmap|, which must be 125 // the |source_bitmap|. The result is stored in |target_bitmap|, which must be
128 // initialized to the required size and type (SkBitmap::kA8_Config). 126 // initialized to the required size and type (SkBitmap::kA8_Config).
129 // Returns true if the conversion succeeded. Note that there might be legitimate 127 // Returns true if the conversion succeeded. Note that there might be legitimate
130 // reasons for the process to fail even if all input was correct. This is a 128 // reasons for the process to fail even if all input was correct. This is a
131 // condition the caller must be able to handle. 129 // condition the caller must be able to handle.
132 GFX_EXPORT bool ComputePrincipalComponentImage(const SkBitmap& source_bitmap, 130 GFX_EXPORT bool ComputePrincipalComponentImage(const SkBitmap& source_bitmap,
133 SkBitmap* target_bitmap); 131 SkBitmap* target_bitmap);
134 132
135 } // namespace color_utils 133 } // namespace color_utils
136 134
137 #endif // UI_GFX_COLOR_ANALYSIS_H_ 135 #endif // UI_GFX_COLOR_ANALYSIS_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698