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

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

Issue 10086023: Expose array of bitmaps contained by gfx::Image similar to NSImage (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 8 months 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_skia.cc
diff --git a/ui/gfx/image/image_skia.cc b/ui/gfx/image/image_skia.cc
new file mode 100644
index 0000000000000000000000000000000000000000..cae5e36ce8cd555cedd5e5f35169a9c7696a61e9
--- /dev/null
+++ b/ui/gfx/image/image_skia.cc
@@ -0,0 +1,57 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/gfx/image/image_skia.h"
+
+#include <limits.h>
+#include <math.h>
+
+#include "base/logging.h"
+#include "base/stl_util.h"
+
+namespace gfx {
+
+ImageSkia::ImageSkia(const SkBitmap* bitmap)
+ : size_(bitmap->width(), bitmap->height()) {
+ DCHECK(bitmap);
+ DCHECK(!bitmap->isNull());
+ bitmaps_.push_back(bitmap);
+}
+
+ImageSkia::ImageSkia(const std::vector<const SkBitmap*>& bitmaps)
+ : bitmaps_(bitmaps) {
+ // Assume that the smallest bitmap represents 1x scale factor.
+ for (size_t i = 0; i < bitmaps_.size(); ++i) {
+ DCHECK(!bitmaps[i]->isNull());
+ gfx::Size bitmap_size(bitmaps_[i]->width(), bitmaps_[i]->height());
+ if (size_.IsEmpty() || bitmap_size.GetArea() < size_.GetArea())
+ size_ = bitmap_size;
+ }
+}
+
+ImageSkia::~ImageSkia() {
+ STLDeleteElements(&bitmaps_);
+}
+
+const SkBitmap* ImageSkia::GetBitmapForScaleFactor(
+ float device_scale_factor) const {
+ // Get the desired bitmap width and height given |device_scale_factor| and
+ // |size_| at 1x density.
+ float desired_width = size_.width() * device_scale_factor;
+ float desired_height = size_.height() * device_scale_factor;
+
+ size_t closest_index;
+ float smallest_diff = FLT_MAX;
Robert Sesek 2012/04/16 16:06:01 std::numeric_limits
+ for (size_t i = 0; i < bitmaps_.size(); ++i) {
+ float diff = fabs(bitmaps_[i]->width() - desired_width) +
Robert Sesek 2012/04/16 16:06:01 std::abs
+ fabs(bitmaps_[i]->height() - desired_height);
+ if (diff < smallest_diff) {
+ closest_index = i;
+ smallest_diff = diff;
+ }
+ }
+ return bitmaps_[closest_index];
+}
+
+} // namespace gfx

Powered by Google App Engine
This is Rietveld 408576698