Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Unified Diff: ui/gfx/image/image_family.cc

Issue 1424913007: Added ImageFamily::CreateExact, which scales the best image to size. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master-plus
Patch Set: Added test for CreateExact on an non-N32 bitmap. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/gfx/image/image_family.cc
diff --git a/ui/gfx/image/image_family.cc b/ui/gfx/image/image_family.cc
index 04f516b869ff91c54e30d78d333e136f35ed1dfa..af0c2bf3675b367d9c78c7a428f38d0ddd4d2f77 100644
--- a/ui/gfx/image/image_family.cc
+++ b/ui/gfx/image/image_family.cc
@@ -6,6 +6,7 @@
#include <cmath>
+#include "skia/ext/image_operations.h"
#include "ui/gfx/geometry/size.h"
#include "ui/gfx/image/image.h"
#include "ui/gfx/image/image_skia.h"
@@ -107,6 +108,28 @@ const gfx::Image* ImageFamily::GetBest(const gfx::Size& size) const {
return GetBest(size.width(), size.height());
}
+gfx::Image ImageFamily::CreateExact(int width, int height) const {
+ // Resize crashes if width or height is 0, so just return an empty image.
+ if (width == 0 || height == 0)
+ return gfx::Image();
+
+ const gfx::Image* image = GetBest(width, height);
+ if (!image)
+ return gfx::Image();
+
+ if (image->Width() == width && image->Height() == height)
+ return gfx::Image(*image);
+
+ SkBitmap bitmap = image->AsBitmap();
+ SkBitmap resized_bitmap = skia::ImageOperations::Resize(
+ bitmap, skia::ImageOperations::RESIZE_LANCZOS3, width, height);
+ return gfx::Image::CreateFrom1xBitmap(resized_bitmap);
+}
+
+gfx::Image ImageFamily::CreateExact(const gfx::Size& size) const {
+ return CreateExact(size.width(), size.height());
+}
+
const gfx::Image* ImageFamily::GetWithExactAspect(float aspect,
int width) const {
// Find the two images of given aspect ratio on either side of |width|.

Powered by Google App Engine
This is Rietveld 408576698