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 "chrome/browser/favicon/select_favicon_frames.h" | |
6 | |
7 #include "skia/ext/image_operations.h" | |
8 #include "ui/gfx/image/image.h" | |
9 #include "ui/gfx/image/image_skia.h" | |
10 #include "third_party/skia/include/core/SkCanvas.h" | |
11 | |
12 namespace { | |
13 | |
14 size_t BiggestCandidate(const std::vector<SkBitmap>& bitmaps) { | |
15 size_t max_index = 0; | |
16 int max_area = bitmaps[0].width() * bitmaps[0].height(); | |
17 for (size_t i = 1; i < bitmaps.size(); ++i) { | |
18 int area = bitmaps[i].width() * bitmaps[i].height(); | |
19 if (area > max_area) { | |
20 max_area = area; | |
21 max_index = i; | |
22 } | |
23 } | |
24 return max_index; | |
25 } | |
26 | |
27 SkBitmap SelectCandidate( | |
28 const std::vector<SkBitmap>& bitmaps, int desired_size) { | |
sky
2012/08/06 15:05:08
wrap destired_size to next line.
| |
29 // Try to find an exact match. | |
30 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
31 if (bitmaps[i].width() == desired_size && | |
32 bitmaps[i].height() == desired_size) { | |
33 return bitmaps[i]; | |
34 } | |
35 } | |
36 | |
37 // If that failed, the following special rules apply: | |
38 // 1. Integer multiples are built using nearest neighbor sampling. | |
39 // TODO(thakis): Implement. | |
40 | |
41 // 2. 24px images are built from 16px images by adding a transparent border. | |
42 if (desired_size == 24 || desired_size == 48) { | |
43 int source_size = desired_size == 24 ? 16 : 32; | |
44 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
45 if (bitmaps[i].width() == source_size && | |
46 bitmaps[i].height() == source_size) { | |
47 SkBitmap bitmap; | |
48 bitmap.setConfig( | |
49 SkBitmap::kARGB_8888_Config, desired_size, desired_size); | |
50 bitmap.allocPixels(); | |
51 bitmap.eraseARGB(0, 0, 0, 0); | |
52 | |
53 { | |
54 SkCanvas canvas(bitmap); | |
55 canvas.drawBitmap(bitmaps[i], | |
56 SkIntToScalar(source_size / 4), | |
57 SkIntToScalar(source_size / 4)); | |
58 } | |
59 | |
60 return bitmap; | |
61 } | |
62 } | |
63 } | |
64 | |
65 // 3. Else, use Lancosz scaling: | |
66 // a) If available, from the next bigger integer multiple variant. | |
67 // TODO(thakis): Implement. | |
68 // b) Else, from the next bigger variant. | |
69 int lancosz_candidate = -1; | |
70 int min_area = INT_MAX; | |
71 for (size_t i = 0; i < bitmaps.size(); ++i) { | |
72 int area = bitmaps[i].width() * bitmaps[i].height(); | |
73 if (bitmaps[i].width() > desired_size && | |
74 bitmaps[i].height() > desired_size && | |
75 (lancosz_candidate == -1 || area < min_area)) { | |
76 lancosz_candidate = i; | |
77 min_area = area; | |
78 } | |
79 } | |
80 // c) Else, from the biggest smaller variant. | |
81 if (lancosz_candidate == -1) | |
82 lancosz_candidate = BiggestCandidate(bitmaps); | |
83 | |
84 return skia::ImageOperations::Resize( | |
85 bitmaps[lancosz_candidate], skia::ImageOperations::RESIZE_LANCZOS3, | |
86 desired_size, desired_size); | |
87 } | |
88 | |
89 } // namespace | |
90 | |
91 gfx::ImageSkia SelectFaviconFrames( | |
92 const std::vector<SkBitmap>& bitmaps, | |
93 const std::vector<ui::ScaleFactor>& scale_factors, | |
94 int desired_size) { | |
95 gfx::ImageSkia multi_image; | |
96 if (bitmaps.empty()) | |
97 return multi_image; | |
98 | |
99 if (desired_size == 0) { | |
100 // Just return the biggest image available. | |
101 size_t max_index = BiggestCandidate(bitmaps); | |
102 multi_image.AddRepresentation( | |
103 gfx::ImageSkiaRep(bitmaps[max_index], ui::SCALE_FACTOR_100P)); | |
104 return multi_image; | |
105 } | |
106 | |
107 for (size_t i = 0; i < scale_factors.size(); ++i) { | |
108 int size = static_cast<int>( | |
109 desired_size * GetScaleFactorScale(scale_factors[i]) + 0.5f); | |
110 multi_image.AddRepresentation(gfx::ImageSkiaRep( | |
111 SelectCandidate(bitmaps, size), scale_factors[i])); | |
112 } | |
113 | |
114 return multi_image; | |
115 } | |
OLD | NEW |