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 #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 Loading... | |
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 int pixel_width = bitmap.width(); | |
Alexei Svitkine (slow)
2013/02/12 19:19:29
Nit: Just use bitmap.width() directly.
motek.
2013/02/12 19:28:56
Done.
| |
414 int pixel_height = bitmap.height(); | |
Alexei Svitkine (slow)
2013/02/12 19:19:29
Nit: Just use bitmap.height() directly.
motek.
2013/02/12 19:28:56
Done.
| |
415 int pixel_n = pixel_width * pixel_height; | |
Alexei Svitkine (slow)
2013/02/12 19:19:29
Nit: Move this down closer to where it's being use
motek.
2013/02/12 19:28:56
Done.
| |
416 double r_sum = 0; | |
417 double g_sum = 0; | |
418 double b_sum = 0; | |
419 double rr_sum = 0; | |
420 double gg_sum = 0; | |
421 double bb_sum = 0; | |
422 double rg_sum = 0; | |
423 double rb_sum = 0; | |
424 double gb_sum = 0; | |
425 | |
426 for (int y = 0; y < pixel_height; ++y) { | |
427 SkColor* current_color = static_cast<uint32_t*>(bitmap.getAddr32(0, y)); | |
428 for (int x = 0; x < pixel_width; ++x, ++current_color) { | |
429 SkColor r = SkColorGetR(*current_color); | |
430 SkColor g = SkColorGetG(*current_color); | |
431 SkColor b = SkColorGetB(*current_color); | |
432 | |
433 r_sum += r; | |
434 g_sum += g; | |
435 b_sum += b; | |
436 rr_sum += r * r; | |
437 gg_sum += g * g; | |
438 bb_sum += b * b; | |
439 rg_sum += r * g; | |
440 rb_sum += r * b; | |
441 gb_sum += g * b; | |
442 } | |
443 } | |
444 | |
445 r_sum /= pixel_n; | |
446 g_sum /= pixel_n; | |
447 b_sum /= pixel_n; | |
448 rr_sum /= pixel_n; | |
449 gg_sum /= pixel_n; | |
450 bb_sum /= pixel_n; | |
451 rg_sum /= pixel_n; | |
452 rb_sum /= pixel_n; | |
453 gb_sum /= pixel_n; | |
454 | |
455 // Covariance (not normalized) is E(X*X.t) - m * m.t and this is how it | |
456 // is calculated below. | |
457 // Each row below represents a row of the matrix describing (co)variances | |
458 // of R, G and B channels with (R, G, B) | |
459 covariance.set( | |
460 rr_sum - r_sum * r_sum, rg_sum - r_sum * g_sum, rb_sum - r_sum * b_sum, | |
461 rg_sum - r_sum * g_sum, gg_sum - g_sum * g_sum, gb_sum - g_sum * b_sum, | |
462 rb_sum - r_sum * b_sum, gb_sum - g_sum * b_sum, bb_sum - b_sum * b_sum); | |
463 return covariance; | |
464 } | |
465 | |
402 } // color_utils | 466 } // color_utils |
OLD | NEW |