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