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 <limits> | 8 #include <limits> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 return CalculateKMeanColorOfBuffer(&decoded_data[0], | 375 return CalculateKMeanColorOfBuffer(&decoded_data[0], |
376 img_width, | 376 img_width, |
377 img_height, | 377 img_height, |
378 darkness_limit, | 378 darkness_limit, |
379 brightness_limit, | 379 brightness_limit, |
380 sampler); | 380 sampler); |
381 } | 381 } |
382 return color; | 382 return color; |
383 } | 383 } |
384 | 384 |
385 SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap) { | 385 SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap, |
| 386 uint32_t darkness_limit, |
| 387 uint32_t brightness_limit, |
| 388 KMeanImageSampler* sampler) { |
386 // SkBitmap uses pre-multiplied alpha but the KMean clustering function | 389 // SkBitmap uses pre-multiplied alpha but the KMean clustering function |
387 // above uses non-pre-multiplied alpha. Transform the bitmap before we | 390 // above uses non-pre-multiplied alpha. Transform the bitmap before we |
388 // analyze it because the function reads each pixel multiple times. | 391 // analyze it because the function reads each pixel multiple times. |
389 int pixel_count = bitmap.width() * bitmap.height(); | 392 int pixel_count = bitmap.width() * bitmap.height(); |
390 scoped_ptr<uint32_t[]> image(new uint32_t[pixel_count]); | 393 scoped_ptr<uint32_t[]> image(new uint32_t[pixel_count]); |
391 UnPreMultiply(bitmap, image.get(), pixel_count); | 394 UnPreMultiply(bitmap, image.get(), pixel_count); |
392 | 395 |
| 396 SkColor color = |
| 397 CalculateKMeanColorOfBuffer(reinterpret_cast<uint8_t*>(image.get()), |
| 398 bitmap.width(), |
| 399 bitmap.height(), |
| 400 darkness_limit, |
| 401 brightness_limit, |
| 402 sampler); |
| 403 return color; |
| 404 } |
| 405 |
| 406 SkColor CalculateKMeanColorOfPNG(scoped_refptr<base::RefCountedMemory> png) { |
393 GridSampler sampler; | 407 GridSampler sampler; |
394 SkColor color = CalculateKMeanColorOfBuffer( | 408 return CalculateKMeanColorOfPNG(png, kMinDarkness, kMaxBrightness, &sampler); |
395 reinterpret_cast<uint8_t*>(image.get()), | 409 } |
396 bitmap.width(), | 410 |
397 bitmap.height(), | 411 SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap) { |
398 kMinDarkness, | 412 GridSampler sampler; |
399 kMaxBrightness, | 413 return CalculateKMeanColorOfBitmap( |
400 &sampler); | 414 bitmap, kMinDarkness, kMaxBrightness, &sampler); |
401 return color; | |
402 } | 415 } |
403 | 416 |
404 gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap) { | 417 gfx::Matrix3F ComputeColorCovariance(const SkBitmap& bitmap) { |
405 // First need basic stats to normalize each channel separately. | 418 // First need basic stats to normalize each channel separately. |
406 SkAutoLockPixels bitmap_lock(bitmap); | 419 SkAutoLockPixels bitmap_lock(bitmap); |
407 gfx::Matrix3F covariance = gfx::Matrix3F::Zeros(); | 420 gfx::Matrix3F covariance = gfx::Matrix3F::Zeros(); |
408 if (!bitmap.getPixels()) | 421 if (!bitmap.getPixels()) |
409 return covariance; | 422 return covariance; |
410 | 423 |
411 // Assume ARGB_8888 format. | 424 // Assume ARGB_8888 format. |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 gfx::Matrix3F covariance = ComputeColorCovariance(source_bitmap); | 567 gfx::Matrix3F covariance = ComputeColorCovariance(source_bitmap); |
555 gfx::Matrix3F eigenvectors = gfx::Matrix3F::Zeros(); | 568 gfx::Matrix3F eigenvectors = gfx::Matrix3F::Zeros(); |
556 gfx::Vector3dF eigenvals = covariance.SolveEigenproblem(&eigenvectors); | 569 gfx::Vector3dF eigenvals = covariance.SolveEigenproblem(&eigenvectors); |
557 gfx::Vector3dF principal = eigenvectors.get_column(0); | 570 gfx::Vector3dF principal = eigenvectors.get_column(0); |
558 if (eigenvals == gfx::Vector3dF() || principal == gfx::Vector3dF()) | 571 if (eigenvals == gfx::Vector3dF() || principal == gfx::Vector3dF()) |
559 return false; // This may happen for some edge cases. | 572 return false; // This may happen for some edge cases. |
560 return ApplyColorReduction(source_bitmap, principal, true, target_bitmap); | 573 return ApplyColorReduction(source_bitmap, principal, true, target_bitmap); |
561 } | 574 } |
562 | 575 |
563 } // color_utils | 576 } // color_utils |
OLD | NEW |