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
3 // found in the LICENSE file.
5 #ifndef UI_GFX_COLOR_ANALYSIS_H_
6 #define UI_GFX_COLOR_ANALYSIS_H_
8 #include "base/basictypes.h"
9 #include "base/compiler_specific.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/ref_counted_memory.h"
12 #include "third_party/skia/include/core/SkColor.h"
13 #include "ui/base/ui_export.h"
14 #include "ui/gfx/matrix3_f.h"
18 namespace color_utils
{
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
22 // arguments into the GetSample method in the future (such as which
23 // cluster is being worked on, etc.).
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
28 class UI_EXPORT KMeanImageSampler
{
30 virtual int GetSample(int width
, int height
) = 0;
34 virtual ~KMeanImageSampler();
37 // This sampler will pick pixels from an evenly spaced grid.
38 class UI_EXPORT GridSampler
: public KMeanImageSampler
{
41 virtual ~GridSampler();
43 virtual int GetSample(int width
, int height
) OVERRIDE
;
46 // The number of times GetSample has been called.
50 // Returns the color in an ARGB |image| that is closest in RGB-space to the
51 // provided |color|. Exported for testing.
52 UI_EXPORT SkColor
FindClosestColor(const uint8_t* image
, int width
, int height
,
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
58 // |png| represents the data of a png encoded image.
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.
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.
64 // RGB KMean Algorithm (N clusters, M iterations):
65 // 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 // 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 // centers of your N clusters.
70 // 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 // of all of the pixels added to the space, so just add it to the sum and
74 // 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 // 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
79 // b) If any centroid moved, repeat step 2 with the new centroids for up
81 // 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
84 // 6.Going through the sorted list of clusters, pick the first cluster with the
85 // largest weight that's centroid fulfills the equation
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
88 // weight regardless of whether or not it fulfills the equation above.
90 // Note: Switching to HSV space did not improve the results of this algorithm
91 // for typical favicon images.
92 UI_EXPORT SkColor
CalculateKMeanColorOfPNG(
93 scoped_refptr
<base::RefCountedMemory
> png
,
94 uint32_t darkness_limit
,
95 uint32_t brightness_limit
,
96 KMeanImageSampler
* sampler
);
98 // Computes a dominant color for an SkBitmap using the above algorithm and
99 // reasonable defaults for |darkness_limit|, |brightness_limit| and |sampler|.
100 UI_EXPORT SkColor
CalculateKMeanColorOfBitmap(const SkBitmap
& bitmap
);
102 // Compute color covariance matrix for the input bitmap.
103 UI_EXPORT
gfx::Matrix3F
ComputeColorCovariance(const SkBitmap
& bitmap
);
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
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.
109 // Otherwise, data is clipped.
110 // Returns true if the target has been computed.
111 UI_EXPORT
bool ApplyColorReduction(const SkBitmap
& source_bitmap
,
112 const gfx::Vector3dF
& color_transform
,
114 SkBitmap
* target_bitmap
);
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
118 // initialized to the required size and type (SkBitmap::kA8_Config).
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
121 // condition the caller must be able to handle.
122 UI_EXPORT
bool ComputePrincipalComponentImage(const SkBitmap
& source_bitmap
,
123 SkBitmap
* target_bitmap
);
125 } // namespace color_utils
127 #endif // UI_GFX_COLOR_ANALYSIS_H_