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 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 | |
reed1
2013/02/12 20:08:15
Performance note: perhaps the inner loops will run
motek.
2013/02/12 22:18:42
Right. It probably doesn't really matter that much
| |
423 for (int y = 0; y < bitmap.height(); ++y) { | |
424 SkPMColor* current_color = static_cast<uint32_t*>(bitmap.getAddr32(0, y)); | |
425 for (int x = 0; x < bitmap.width(); ++x, ++current_color) { | |
426 SkColor c = SkUnPreMultiply::PMColorToColor(*current_color); | |
427 SkColor r = SkColorGetR(c); | |
428 SkColor g = SkColorGetG(c); | |
429 SkColor b = SkColorGetB(c); | |
430 | |
431 r_sum += r; | |
432 g_sum += g; | |
433 b_sum += b; | |
434 rr_sum += r * r; | |
435 gg_sum += g * g; | |
436 bb_sum += b * b; | |
437 rg_sum += r * g; | |
438 rb_sum += r * b; | |
439 gb_sum += g * b; | |
440 } | |
441 } | |
442 | |
443 int pixel_n = bitmap.width() * bitmap.height(); | |
444 r_sum /= pixel_n; | |
445 g_sum /= pixel_n; | |
446 b_sum /= pixel_n; | |
447 rr_sum /= pixel_n; | |
448 gg_sum /= pixel_n; | |
449 bb_sum /= pixel_n; | |
450 rg_sum /= pixel_n; | |
451 rb_sum /= pixel_n; | |
452 gb_sum /= pixel_n; | |
453 | |
454 // Covariance (not normalized) is E(X*X.t) - m * m.t and this is how it | |
455 // is calculated below. | |
456 // Each row below represents a row of the matrix describing (co)variances | |
457 // of R, G and B channels with (R, G, B) | |
458 covariance.set( | |
459 rr_sum - r_sum * r_sum, rg_sum - r_sum * g_sum, rb_sum - r_sum * b_sum, | |
460 rg_sum - r_sum * g_sum, gg_sum - g_sum * g_sum, gb_sum - g_sum * b_sum, | |
461 rb_sum - r_sum * b_sum, gb_sum - g_sum * b_sum, bb_sum - b_sum * b_sum); | |
462 return covariance; | |
463 } | |
464 | |
402 } // color_utils | 465 } // color_utils |
OLD | NEW |