OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/gfx/image/image_skia.h" | |
6 | |
7 #include <limits.h> | |
8 #include <math.h> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/stl_util.h" | |
12 | |
13 namespace gfx { | |
14 | |
15 ImageSkia::ImageSkia(const SkBitmap* bitmap) | |
16 : size_(bitmap->width(), bitmap->height()) { | |
17 DCHECK(bitmap); | |
18 DCHECK(!bitmap->isNull()); | |
19 bitmaps_.push_back(bitmap); | |
20 } | |
21 | |
22 ImageSkia::ImageSkia(const std::vector<const SkBitmap*>& bitmaps) | |
23 : bitmaps_(bitmaps) { | |
24 // Assume that the smallest bitmap represents 1x scale factor. | |
25 for (size_t i = 0; i < bitmaps_.size(); ++i) { | |
26 DCHECK(!bitmaps[i]->isNull()); | |
27 gfx::Size bitmap_size(bitmaps_[i]->width(), bitmaps_[i]->height()); | |
28 if (size_.IsEmpty() || bitmap_size.GetArea() < size_.GetArea()) | |
29 size_ = bitmap_size; | |
30 } | |
31 } | |
32 | |
33 ImageSkia::~ImageSkia() { | |
34 STLDeleteElements(&bitmaps_); | |
35 } | |
36 | |
37 const SkBitmap* ImageSkia::GetBitmapForScaleFactor( | |
38 float device_scale_factor) const { | |
39 // Get the desired bitmap width and height given |device_scale_factor| and | |
40 // |size_| at 1x density. | |
41 float desired_width = size_.width() * device_scale_factor; | |
42 float desired_height = size_.height() * device_scale_factor; | |
43 | |
44 size_t closest_index; | |
45 float smallest_diff = FLT_MAX; | |
Robert Sesek
2012/04/16 16:06:01
std::numeric_limits
| |
46 for (size_t i = 0; i < bitmaps_.size(); ++i) { | |
47 float diff = fabs(bitmaps_[i]->width() - desired_width) + | |
Robert Sesek
2012/04/16 16:06:01
std::abs
| |
48 fabs(bitmaps_[i]->height() - desired_height); | |
49 if (diff < smallest_diff) { | |
50 closest_index = i; | |
51 smallest_diff = diff; | |
52 } | |
53 } | |
54 return bitmaps_[closest_index]; | |
55 } | |
56 | |
57 } // namespace gfx | |
OLD | NEW |