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

Side by Side Diff: ui/gfx/image/image_family.cc

Issue 12704026: Added new class gfx::ImageFamily. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Use Image::Size instead of Width and Height. Created 7 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2013 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_family.h"
6
7 #include <cmath>
8
9 #include "ui/gfx/image/image.h"
10 #include "ui/gfx/image/image_skia.h"
11
12 namespace gfx {
13
14 class Size;
pkotwicz 2013/03/27 19:04:53 Remove this?
Matt Giuca 2013/03/28 03:54:03 Actually converted to an #include since I'm using
15
16 ImageFamily::ImageFamily() {}
17 ImageFamily::~ImageFamily() {}
18
19 void ImageFamily::Add(const gfx::Image& image)
20 {
Robert Sesek 2013/03/27 17:42:50 Opening braces go on the line of the method defini
Matt Giuca 2013/03/28 03:54:03 Done.
21 const gfx::Size& size = image.Size();
22 int width = size.width();
23 int height = size.height();
pkotwicz 2013/03/27 19:04:53 Nit: I believe gfx::Size::IsEmpty() checks if eith
Matt Giuca 2013/03/28 03:54:03 Done.
24 // If either width or height is 0, both are.
25 if (height == 0 || width == 0) {
26 map_[MapKey(1.0f, 0)] = image;
27 } else {
28 float aspect = static_cast<float>(width) / height;
29 DCHECK(aspect > 0.0f);
Robert Sesek 2013/03/27 17:42:50 DCHECK_GT
Matt Giuca 2013/03/28 03:54:03 Done (and all of the other DCHECKs too, except the
30 map_[MapKey(aspect, width)] = image;
31 }
32 }
33
34 void ImageFamily::Add(const gfx::ImageSkia& image_skia)
35 {
36 gfx::Image image(image_skia);
37 Add(image);
pkotwicz 2013/03/27 19:04:53 Nit, merge the two lines into one
Matt Giuca 2013/03/28 03:54:03 Done.
38 }
39
40 const gfx::Image* ImageFamily::Get(int width, int height) const
Robert Sesek 2013/03/27 17:42:50 This method is rather large. Would it make sense t
Matt Giuca 2013/03/28 03:54:03 Done. I'll discuss in the free-form comments.
41 {
42 if (map_.empty())
43 return NULL;
44
45 // If either width or height is 0, both are.
46 float desired_aspect;
47 if (height == 0 || width == 0) {
48 desired_aspect = 1.0f;
49 height = 0;
50 width = 0;
51 } else {
52 desired_aspect = static_cast<float>(width) / height;
53 }
54 DCHECK(desired_aspect > 0.0f);
Robert Sesek 2013/03/27 17:42:50 _GT
Matt Giuca 2013/03/28 03:54:03 Done.
55
56 // Get iterator to images >= and < the desired aspect ratio and size.
57 std::map<MapKey, gfx::Image>::const_iterator greater_or_equal =
58 map_.lower_bound(MapKey(desired_aspect, width));
59 if (greater_or_equal != map_.end() &&
60 greater_or_equal->first.aspect() == desired_aspect) {
61 // Exact same aspect ratio, and we have found the smallest image of the same
62 // size or greater. This is ideal.
63 return &greater_or_equal->second;
64 }
65
66 float closest_aspect; // Closest aspect ratio to the desired one.
67 if (greater_or_equal != map_.begin()) {
68 std::map<MapKey, gfx::Image>::const_iterator less_than =
69 greater_or_equal;
70 --less_than;
71 if (less_than->first.aspect() == desired_aspect) {
72 // Exact same aspect ratio, and we have found the largest image smaller
73 // than desired.
74 return &less_than->second;
75 }
76
77 float thinner_aspect = less_than->first.aspect();
78 DCHECK(thinner_aspect > 0.0f);
Robert Sesek 2013/03/27 17:42:50 _GT
Matt Giuca 2013/03/28 03:54:03 Done.
79 DCHECK(thinner_aspect < desired_aspect);
Robert Sesek 2013/03/27 17:42:50 _LT
Matt Giuca 2013/03/28 03:54:03 Done.
80 closest_aspect = thinner_aspect;
81 if (greater_or_equal != map_.end()) {
82 float wider_aspect = greater_or_equal->first.aspect();
83 DCHECK(wider_aspect > desired_aspect);
Robert Sesek 2013/03/27 17:42:50 _GT
Matt Giuca 2013/03/28 03:54:03 Done.
84 if ((wider_aspect / desired_aspect) < (desired_aspect / thinner_aspect))
85 closest_aspect = wider_aspect;
86 }
87 } else {
88 // No aspect ratio is less than or equal to desired_aspect.
89 DCHECK(greater_or_equal != map_.end());
90 closest_aspect = greater_or_equal->first.aspect();
91 DCHECK(closest_aspect > desired_aspect);
Robert Sesek 2013/03/27 17:42:50 _GT
Matt Giuca 2013/03/28 03:54:03 Done.
92 }
93
94 int desired_width;
95 if (desired_aspect >= closest_aspect) {
96 desired_width = closest_aspect == 0 ? height : width;
97 } else {
98 desired_width = static_cast<int>(ceilf(height * closest_aspect));
99 }
100
101 // Search again, this time with the aspect ratio we know exists.
102 greater_or_equal = map_.lower_bound(MapKey(closest_aspect, desired_width));
103 if (greater_or_equal != map_.end() &&
104 greater_or_equal->first.aspect() == closest_aspect) {
105 // We have found the smallest image of the same size or greater.
106 return &greater_or_equal->second;
107 }
108
109 DCHECK(greater_or_equal != map_.begin());
110 std::map<MapKey, gfx::Image>::const_iterator less_than = greater_or_equal;
111 --less_than;
112 // This must be true because there must be at least one image with
113 // closest_aspect.
114 DCHECK(less_than->first.aspect() == closest_aspect);
115 // We have found the largest image smaller than desired.
116 return &less_than->second;
117 }
118
119 ImageFamily::const_iterator::const_iterator(
120 const std::map<MapKey, gfx::Image>::const_iterator& other)
121 : map_iterator_(other) {}
122
123 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698