OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/favicon/select_favicon_frames.h" | 5 #include "chrome/browser/favicon/select_favicon_frames.h" |
6 | 6 |
7 #include "skia/ext/image_operations.h" | 7 #include "skia/ext/image_operations.h" |
8 #include "ui/gfx/image/image.h" | 8 #include "ui/gfx/image/image.h" |
9 #include "ui/gfx/image/image_skia.h" | 9 #include "ui/gfx/image/image_skia.h" |
10 #include "third_party/skia/include/core/SkCanvas.h" | 10 #include "third_party/skia/include/core/SkCanvas.h" |
11 | 11 |
12 namespace { | 12 namespace { |
13 | 13 |
14 size_t BiggestCandidate(const std::vector<SkBitmap>& bitmaps) { | 14 size_t BiggestCandidate(const std::vector<SkBitmap>& bitmaps) { |
15 size_t max_index = 0; | 15 size_t max_index = 0; |
16 int max_area = bitmaps[0].width() * bitmaps[0].height(); | 16 int max_area = bitmaps[0].width() * bitmaps[0].height(); |
17 for (size_t i = 1; i < bitmaps.size(); ++i) { | 17 for (size_t i = 1; i < bitmaps.size(); ++i) { |
18 int area = bitmaps[i].width() * bitmaps[i].height(); | 18 int area = bitmaps[i].width() * bitmaps[i].height(); |
19 if (area > max_area) { | 19 if (area > max_area) { |
20 max_area = area; | 20 max_area = area; |
21 max_index = i; | 21 max_index = i; |
22 } | 22 } |
23 } | 23 } |
24 return max_index; | 24 return max_index; |
25 } | 25 } |
26 | 26 |
| 27 SkBitmap PadWithBorder(SkBitmap contents, int desired_size, int source_size) { |
| 28 SkBitmap bitmap; |
| 29 bitmap.setConfig( |
| 30 SkBitmap::kARGB_8888_Config, desired_size, desired_size); |
| 31 bitmap.allocPixels(); |
| 32 bitmap.eraseARGB(0, 0, 0, 0); |
| 33 |
| 34 { |
| 35 SkCanvas canvas(bitmap); |
| 36 int shift = (desired_size - source_size) / 2; |
| 37 SkRect dest(SkRect::MakeXYWH(shift, shift, source_size, source_size)); |
| 38 canvas.drawBitmapRect(contents, NULL, dest); |
| 39 } |
| 40 |
| 41 return bitmap; |
| 42 } |
| 43 |
27 SkBitmap SelectCandidate(const std::vector<SkBitmap>& bitmaps, | 44 SkBitmap SelectCandidate(const std::vector<SkBitmap>& bitmaps, |
28 int desired_size) { | 45 int desired_size, |
| 46 ui::ScaleFactor scale_factor) { |
| 47 float scale = GetScaleFactorScale(scale_factor); |
| 48 desired_size = static_cast<int>(desired_size * scale + 0.5f); |
| 49 |
29 // Try to find an exact match. | 50 // Try to find an exact match. |
30 for (size_t i = 0; i < bitmaps.size(); ++i) { | 51 for (size_t i = 0; i < bitmaps.size(); ++i) { |
31 if (bitmaps[i].width() == desired_size && | 52 if (bitmaps[i].width() == desired_size && |
32 bitmaps[i].height() == desired_size) { | 53 bitmaps[i].height() == desired_size) { |
33 return bitmaps[i]; | 54 return bitmaps[i]; |
34 } | 55 } |
35 } | 56 } |
36 | 57 |
37 // If that failed, the following special rules apply: | 58 // If that failed, the following special rules apply: |
38 // 1. Integer multiples are built using nearest neighbor sampling. | 59 // 1. 17px-24px images are built from 16px images by adding |
39 // TODO(thakis): Implement. | 60 // a transparent border. |
40 | 61 if (desired_size > 16 * scale && desired_size <= 24 * scale) { |
41 // 2. 24px images are built from 16px images by adding a transparent border. | 62 int source_size = static_cast<int>(16 * scale + 0.5f); |
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) { | 63 for (size_t i = 0; i < bitmaps.size(); ++i) { |
45 if (bitmaps[i].width() == source_size && | 64 if (bitmaps[i].width() == source_size && |
46 bitmaps[i].height() == source_size) { | 65 bitmaps[i].height() == source_size) { |
47 SkBitmap bitmap; | 66 return PadWithBorder(bitmaps[i], desired_size, source_size); |
48 bitmap.setConfig( | 67 } |
49 SkBitmap::kARGB_8888_Config, desired_size, desired_size); | 68 } |
50 bitmap.allocPixels(); | 69 // Try again, with upsizing the base variant. |
51 bitmap.eraseARGB(0, 0, 0, 0); | 70 for (size_t i = 0; i < bitmaps.size(); ++i) { |
52 | 71 if (bitmaps[i].width() * scale == source_size && |
53 { | 72 bitmaps[i].height() * scale == source_size) { |
54 SkCanvas canvas(bitmap); | 73 return PadWithBorder(bitmaps[i], desired_size, source_size); |
55 canvas.drawBitmap(bitmaps[i], | |
56 SkIntToScalar(source_size / 4), | |
57 SkIntToScalar(source_size / 4)); | |
58 } | |
59 | |
60 return bitmap; | |
61 } | 74 } |
62 } | 75 } |
63 } | 76 } |
64 | 77 |
| 78 // 2. Integer multiples are built using nearest neighbor sampling. |
| 79 // TODO(thakis): Implement. |
| 80 |
65 // 3. Else, use Lancosz scaling: | 81 // 3. Else, use Lancosz scaling: |
66 // a) If available, from the next bigger integer multiple variant. | 82 // b) If available, from the next bigger variant. |
67 // TODO(thakis): Implement. | |
68 // b) Else, from the next bigger variant. | |
69 int lancosz_candidate = -1; | 83 int lancosz_candidate = -1; |
70 int min_area = INT_MAX; | 84 int min_area = INT_MAX; |
71 for (size_t i = 0; i < bitmaps.size(); ++i) { | 85 for (size_t i = 0; i < bitmaps.size(); ++i) { |
72 int area = bitmaps[i].width() * bitmaps[i].height(); | 86 int area = bitmaps[i].width() * bitmaps[i].height(); |
73 if (bitmaps[i].width() > desired_size && | 87 if (bitmaps[i].width() > desired_size && |
74 bitmaps[i].height() > desired_size && | 88 bitmaps[i].height() > desired_size && |
75 (lancosz_candidate == -1 || area < min_area)) { | 89 (lancosz_candidate == -1 || area < min_area)) { |
76 lancosz_candidate = i; | 90 lancosz_candidate = i; |
77 min_area = area; | 91 min_area = area; |
78 } | 92 } |
(...skipping 19 matching lines...) Expand all Loading... |
98 | 112 |
99 if (desired_size == 0) { | 113 if (desired_size == 0) { |
100 // Just return the biggest image available. | 114 // Just return the biggest image available. |
101 size_t max_index = BiggestCandidate(bitmaps); | 115 size_t max_index = BiggestCandidate(bitmaps); |
102 multi_image.AddRepresentation( | 116 multi_image.AddRepresentation( |
103 gfx::ImageSkiaRep(bitmaps[max_index], ui::SCALE_FACTOR_100P)); | 117 gfx::ImageSkiaRep(bitmaps[max_index], ui::SCALE_FACTOR_100P)); |
104 return multi_image; | 118 return multi_image; |
105 } | 119 } |
106 | 120 |
107 for (size_t i = 0; i < scale_factors.size(); ++i) { | 121 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( | 122 multi_image.AddRepresentation(gfx::ImageSkiaRep( |
111 SelectCandidate(bitmaps, size), scale_factors[i])); | 123 SelectCandidate(bitmaps, desired_size, scale_factors[i]), |
| 124 scale_factors[i])); |
112 } | 125 } |
113 | 126 |
114 return multi_image; | 127 return multi_image; |
115 } | 128 } |
OLD | NEW |