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 SkColor color = | |
402 CalculateKMeanColorOfBuffer(reinterpret_cast<uint8_t*>(image.get()), | |
Alexei Svitkine (slow)
2014/05/26 18:20:49
Nit: Just return it directly.
calamity
2014/05/27 01:19:42
Done.
| |
403 bitmap.width(), | |
404 bitmap.height(), | |
405 darkness_limit, | |
406 brightness_limit, | |
407 sampler); | |
408 return color; | |
409 } | |
410 | |
411 SkColor CalculateKMeanColorOfBitmap(const SkBitmap& bitmap) { | |
393 GridSampler sampler; | 412 GridSampler sampler; |
394 SkColor color = CalculateKMeanColorOfBuffer( | 413 return CalculateKMeanColorOfBitmap( |
395 reinterpret_cast<uint8_t*>(image.get()), | 414 bitmap, kMinDarkness, kMaxBrightness, &sampler); |
396 bitmap.width(), | |
397 bitmap.height(), | |
398 kMinDarkness, | |
399 kMaxBrightness, | |
400 &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 |