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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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/image/image_family.h" 5 #include "ui/gfx/image/image_family.h"
6 6
7 #include <cmath> 7 #include <cmath>
8 8
9 #include "skia/ext/image_operations.h"
9 #include "ui/gfx/geometry/size.h" 10 #include "ui/gfx/geometry/size.h"
10 #include "ui/gfx/image/image.h" 11 #include "ui/gfx/image/image.h"
11 #include "ui/gfx/image/image_skia.h" 12 #include "ui/gfx/image/image_skia.h"
12 13
13 namespace gfx { 14 namespace gfx {
14 15
15 ImageFamily::const_iterator::const_iterator() {} 16 ImageFamily::const_iterator::const_iterator() {}
16 17
17 ImageFamily::const_iterator::const_iterator(const const_iterator& other) 18 ImageFamily::const_iterator::const_iterator(const const_iterator& other)
18 : map_iterator_(other.map_iterator_) {} 19 : map_iterator_(other.map_iterator_) {}
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 float wider_aspect = greater_or_equal->first.aspect(); 101 float wider_aspect = greater_or_equal->first.aspect();
101 DCHECK_GT(wider_aspect, desired_aspect); 102 DCHECK_GT(wider_aspect, desired_aspect);
102 return wider_aspect; 103 return wider_aspect;
103 } 104 }
104 } 105 }
105 106
106 const gfx::Image* ImageFamily::GetBest(const gfx::Size& size) const { 107 const gfx::Image* ImageFamily::GetBest(const gfx::Size& size) const {
107 return GetBest(size.width(), size.height()); 108 return GetBest(size.width(), size.height());
108 } 109 }
109 110
111 gfx::Image ImageFamily::CreateExact(int width, int height) const {
112 // Resize crashes if width or height is 0, so just return an empty image.
113 if (width == 0 || height == 0)
114 return gfx::Image();
115
116 const gfx::Image* image = GetBest(width, height);
117 if (!image)
118 return gfx::Image();
119
120 if (image->Width() == width && image->Height() == height)
121 return gfx::Image(*image);
122
123 SkBitmap bitmap = image->AsBitmap();
124 SkBitmap resized_bitmap = skia::ImageOperations::Resize(
125 bitmap, skia::ImageOperations::RESIZE_LANCZOS3, width, height);
126 return gfx::Image::CreateFrom1xBitmap(resized_bitmap);
127 }
128
129 gfx::Image ImageFamily::CreateExact(const gfx::Size& size) const {
130 return CreateExact(size.width(), size.height());
131 }
132
110 const gfx::Image* ImageFamily::GetWithExactAspect(float aspect, 133 const gfx::Image* ImageFamily::GetWithExactAspect(float aspect,
111 int width) const { 134 int width) const {
112 // Find the two images of given aspect ratio on either side of |width|. 135 // Find the two images of given aspect ratio on either side of |width|.
113 std::map<MapKey, gfx::Image>::const_iterator greater_or_equal = 136 std::map<MapKey, gfx::Image>::const_iterator greater_or_equal =
114 map_.lower_bound(MapKey(aspect, width)); 137 map_.lower_bound(MapKey(aspect, width));
115 if (greater_or_equal != map_.end() && 138 if (greater_or_equal != map_.end() &&
116 greater_or_equal->first.aspect() == aspect) { 139 greater_or_equal->first.aspect() == aspect) {
117 // We have found the smallest image of the same size or greater. 140 // We have found the smallest image of the same size or greater.
118 return &greater_or_equal->second; 141 return &greater_or_equal->second;
119 } 142 }
120 143
121 DCHECK(greater_or_equal != map_.begin()); 144 DCHECK(greater_or_equal != map_.begin());
122 std::map<MapKey, gfx::Image>::const_iterator less_than = greater_or_equal; 145 std::map<MapKey, gfx::Image>::const_iterator less_than = greater_or_equal;
123 --less_than; 146 --less_than;
124 // This must be true because there must be at least one image with |aspect|. 147 // This must be true because there must be at least one image with |aspect|.
125 DCHECK_EQ(less_than->first.aspect(), aspect); 148 DCHECK_EQ(less_than->first.aspect(), aspect);
126 // We have found the largest image smaller than desired. 149 // We have found the largest image smaller than desired.
127 return &less_than->second; 150 return &less_than->second;
128 } 151 }
129 152
130 } // namespace gfx 153 } // namespace gfx
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698