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

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

Issue 12220123: Adds a function computing image color covariance. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed the reviewer's remarks. Created 7 years, 10 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
« no previous file with comments | « ui/gfx/color_analysis.h ('k') | ui/gfx/color_analysis_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #include "ui/gfx/color_analysis.h" 5 #include "ui/gfx/color_analysis.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
11 #include "third_party/skia/include/core/SkBitmap.h" 12 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkUnPreMultiply.h" 13 #include "third_party/skia/include/core/SkUnPreMultiply.h"
13 #include "ui/gfx/codec/png_codec.h" 14 #include "ui/gfx/codec/png_codec.h"
14 15
15 namespace { 16 namespace {
16 17
17 // RGBA KMean Constants 18 // RGBA KMean Constants
18 const uint32_t kNumberOfClusters = 4; 19 const uint32_t kNumberOfClusters = 4;
19 const int kNumberOfIterations = 50; 20 const int kNumberOfIterations = 50;
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after
392 SkColor color = CalculateKMeanColorOfBuffer( 393 SkColor color = CalculateKMeanColorOfBuffer(
393 reinterpret_cast<uint8_t*>(image.get()), 394 reinterpret_cast<uint8_t*>(image.get()),
394 bitmap.width(), 395 bitmap.width(),
395 bitmap.height(), 396 bitmap.height(),
396 kMinDarkness, 397 kMinDarkness,
397 kMaxBrightness, 398 kMaxBrightness,
398 &sampler); 399 &sampler);
399 return color; 400 return color;
400 } 401 }
401 402
403 gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap) {
404 // First need basic stats to normalize each channel separately.
405 SkAutoLockPixels bitmap_lock(bitmap);
406 gfx::Matrix3F covariance = gfx::Matrix3F::Zeros();
407 if (!bitmap.getPixels())
408 return covariance;
409
410 // Assume ARGB_8888 format.
411 DCHECK(bitmap.config() == SkBitmap::kARGB_8888_Config);
412
413 double r_sum = 0;
414 double g_sum = 0;
415 double b_sum = 0;
416 double rr_sum = 0;
417 double gg_sum = 0;
418 double bb_sum = 0;
419 double rg_sum = 0;
420 double rb_sum = 0;
421 double gb_sum = 0;
422
423 for (int y = 0; y < bitmap.height(); ++y) {
reed1 2013/02/12 19:34:51 SkBitmap stores its pixels in SkPMColor format, no
motek. 2013/02/12 19:58:13 Ah, I see. It seems I should un-pre-multiply and u
424 SkColor* current_color = static_cast<uint32_t*>(bitmap.getAddr32(0, y));
425 for (int x = 0; x < bitmap.width(); ++x, ++current_color) {
426 SkColor r = SkColorGetR(*current_color);
427 SkColor g = SkColorGetG(*current_color);
428 SkColor b = SkColorGetB(*current_color);
429
430 r_sum += r;
431 g_sum += g;
432 b_sum += b;
433 rr_sum += r * r;
434 gg_sum += g * g;
435 bb_sum += b * b;
436 rg_sum += r * g;
437 rb_sum += r * b;
438 gb_sum += g * b;
439 }
440 }
441
442 int pixel_n = bitmap.width() * bitmap.height();
443 r_sum /= pixel_n;
444 g_sum /= pixel_n;
445 b_sum /= pixel_n;
446 rr_sum /= pixel_n;
447 gg_sum /= pixel_n;
448 bb_sum /= pixel_n;
449 rg_sum /= pixel_n;
450 rb_sum /= pixel_n;
451 gb_sum /= pixel_n;
452
453 // Covariance (not normalized) is E(X*X.t) - m * m.t and this is how it
454 // is calculated below.
455 // Each row below represents a row of the matrix describing (co)variances
456 // of R, G and B channels with (R, G, B)
457 covariance.set(
458 rr_sum - r_sum * r_sum, rg_sum - r_sum * g_sum, rb_sum - r_sum * b_sum,
459 rg_sum - r_sum * g_sum, gg_sum - g_sum * g_sum, gb_sum - g_sum * b_sum,
460 rb_sum - r_sum * b_sum, gb_sum - g_sum * b_sum, bb_sum - b_sum * b_sum);
461 return covariance;
462 }
463
402 } // color_utils 464 } // color_utils
OLDNEW
« no previous file with comments | « ui/gfx/color_analysis.h ('k') | ui/gfx/color_analysis_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698