1 // Copyright 2014 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.
5 #include "components/favicon_base/favicon_util.h"
9 #include "components/favicon_base/favicon_types.h"
10 #include "components/favicon_base/select_favicon_frames.h"
11 #include "skia/ext/image_operations.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "third_party/skia/include/core/SkCanvas.h"
14 #include "ui/base/layout.h"
15 #include "ui/gfx/codec/png_codec.h"
16 #include "ui/gfx/favicon_size.h"
17 #include "ui/gfx/geometry/size.h"
18 #include "ui/gfx/image/image_png_rep.h"
19 #include "ui/gfx/image/image_skia.h"
21 #if defined(OS_MACOSX) && !defined(OS_IOS)
22 #include "base/mac/mac_util.h"
23 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
25 namespace favicon_base
{
28 // Creates image reps of DIP size |favicon_size| for the subset of
29 // |favicon_scales| for which the image reps can be created without resizing
30 // or decoding the bitmap data.
31 std::vector
<gfx::ImagePNGRep
> SelectFaviconFramesFromPNGsWithoutResizing(
32 const std::vector
<favicon_base::FaviconRawBitmapResult
>& png_data
,
33 const std::vector
<float>& favicon_scales
,
35 std::vector
<gfx::ImagePNGRep
> png_reps
;
39 // A |favicon_size| of 0 indicates that the largest frame is desired.
40 if (favicon_size
== 0) {
42 scoped_refptr
<base::RefCountedMemory
> best_candidate
;
43 for (size_t i
= 0; i
< png_data
.size(); ++i
) {
44 int area
= png_data
[i
].pixel_size
.GetArea();
45 if (area
> maximum_area
) {
47 best_candidate
= png_data
[i
].bitmap_data
;
50 png_reps
.push_back(gfx::ImagePNGRep(best_candidate
, 1.0f
));
54 // Build a map which will be used to determine the scale used to
55 // create a bitmap with given pixel size.
56 std::map
<int, float> desired_pixel_sizes
;
57 for (size_t i
= 0; i
< favicon_scales
.size(); ++i
) {
59 static_cast<int>(std::ceil(favicon_size
* favicon_scales
[i
]));
60 desired_pixel_sizes
[pixel_size
] = favicon_scales
[i
];
63 for (size_t i
= 0; i
< png_data
.size(); ++i
) {
64 if (!png_data
[i
].is_valid())
67 const gfx::Size
& pixel_size
= png_data
[i
].pixel_size
;
68 if (pixel_size
.width() != pixel_size
.height())
71 std::map
<int, float>::iterator it
=
72 desired_pixel_sizes
.find(pixel_size
.width());
73 if (it
== desired_pixel_sizes
.end())
76 png_reps
.push_back(gfx::ImagePNGRep(png_data
[i
].bitmap_data
, it
->second
));
82 // Returns a resampled bitmap of |desired_size| x |desired_size| by resampling
83 // the best bitmap out of |input_bitmaps|.
84 // ResizeBitmapByDownsamplingIfPossible() is similar to SelectFaviconFrames()
85 // but it operates on bitmaps which have already been resampled via
86 // SelectFaviconFrames().
87 SkBitmap
ResizeBitmapByDownsamplingIfPossible(
88 const std::vector
<SkBitmap
>& input_bitmaps
,
90 DCHECK(!input_bitmaps
.empty());
91 DCHECK_NE(0, desired_size
);
94 for (size_t i
= 0; i
< input_bitmaps
.size(); ++i
) {
95 const SkBitmap
& input_bitmap
= input_bitmaps
[i
];
96 if (input_bitmap
.width() == desired_size
&&
97 input_bitmap
.height() == desired_size
) {
99 } else if (best_bitmap
.isNull()) {
100 best_bitmap
= input_bitmap
;
101 } else if (input_bitmap
.width() >= best_bitmap
.width() &&
102 input_bitmap
.height() >= best_bitmap
.height()) {
103 if (best_bitmap
.width() < desired_size
||
104 best_bitmap
.height() < desired_size
) {
105 best_bitmap
= input_bitmap
;
108 if (input_bitmap
.width() >= desired_size
&&
109 input_bitmap
.height() >= desired_size
) {
110 best_bitmap
= input_bitmap
;
115 if (desired_size
% best_bitmap
.width() == 0 &&
116 desired_size
% best_bitmap
.height() == 0) {
117 // Use nearest neighbour resampling if upsampling by an integer. This
118 // makes the result look similar to the result of SelectFaviconFrames().
120 bitmap
.allocN32Pixels(desired_size
, desired_size
);
121 if (!best_bitmap
.isOpaque())
122 bitmap
.eraseARGB(0, 0, 0, 0);
124 SkCanvas
canvas(bitmap
);
125 canvas
.drawBitmapRect(
126 best_bitmap
, SkRect::MakeIWH(desired_size
, desired_size
), NULL
);
129 return skia::ImageOperations::Resize(best_bitmap
,
130 skia::ImageOperations::RESIZE_LANCZOS3
,
137 std::vector
<float> GetFaviconScales() {
138 const float kScale1x
= 1.0f
;
139 std::vector
<ui::ScaleFactor
> resource_scale_factors
=
140 ui::GetSupportedScaleFactors();
142 // TODO(ios): 1.0f should not be necessary on iOS retina devices. However
143 // the sync service only supports syncing 100p favicons. Until sync supports
144 // other scales 100p is needed in the list of scales to retrieve and
145 // store the favicons in both 100p for sync and 200p for display. cr/160503.
146 std::vector
<float> favicon_scales(1, kScale1x
);
147 for (size_t i
= 0; i
< resource_scale_factors
.size(); ++i
) {
148 if (resource_scale_factors
[i
] != ui::SCALE_FACTOR_100P
)
149 favicon_scales
.push_back(
150 ui::GetScaleForScaleFactor(resource_scale_factors
[i
]));
152 return favicon_scales
;
155 void SetFaviconColorSpace(gfx::Image
* image
) {
156 #if defined(OS_MACOSX) && !defined(OS_IOS)
157 image
->SetSourceColorSpace(base::mac::GetSystemColorSpace());
158 #endif // defined(OS_MACOSX) && !defined(OS_IOS)
161 gfx::Image
SelectFaviconFramesFromPNGs(
162 const std::vector
<favicon_base::FaviconRawBitmapResult
>& png_data
,
163 const std::vector
<float>& favicon_scales
,
165 // Create image reps for as many scales as possible without resizing
166 // the bitmap data or decoding it. FaviconHandler stores already resized
167 // favicons into history so no additional resizing should be needed in the
169 // Creating the gfx::Image from |png_data| without resizing or decoding if
170 // possible is important because:
171 // - Sync does a byte-to-byte comparison of gfx::Image::As1xPNGBytes() to
172 // the data it put into the database in order to determine whether any
173 // updates should be pushed to sync.
174 // - The decoding occurs on the UI thread and the decoding can be a
175 // significant performance hit if a user has many bookmarks.
176 // TODO(pkotwicz): Move the decoding off the UI thread.
177 std::vector
<gfx::ImagePNGRep
> png_reps
=
178 SelectFaviconFramesFromPNGsWithoutResizing(
179 png_data
, favicon_scales
, favicon_size
);
181 // SelectFaviconFramesFromPNGsWithoutResizing() should have selected the
182 // largest favicon if |favicon_size| == 0.
183 if (favicon_size
== 0)
184 return gfx::Image(png_reps
);
186 std::vector
<float> favicon_scales_to_generate
= favicon_scales
;
187 for (size_t i
= 0; i
< png_reps
.size(); ++i
) {
188 std::vector
<float>::iterator iter
= std::find(
189 favicon_scales_to_generate
.begin(),
190 favicon_scales_to_generate
.end(),
192 if (iter
!= favicon_scales_to_generate
.end())
193 favicon_scales_to_generate
.erase(iter
);
196 if (favicon_scales_to_generate
.empty())
197 return gfx::Image(png_reps
);
199 std::vector
<SkBitmap
> bitmaps
;
200 for (size_t i
= 0; i
< png_data
.size(); ++i
) {
201 if (!png_data
[i
].is_valid())
205 if (gfx::PNGCodec::Decode(png_data
[i
].bitmap_data
->front(),
206 png_data
[i
].bitmap_data
->size(),
208 bitmaps
.push_back(bitmap
);
215 gfx::ImageSkia resized_image_skia
;
216 for (size_t i
= 0; i
< favicon_scales_to_generate
.size(); ++i
) {
217 float scale
= favicon_scales_to_generate
[i
];
218 int desired_size_in_pixel
=
219 static_cast<int>(std::ceil(favicon_size
* scale
));
221 ResizeBitmapByDownsamplingIfPossible(bitmaps
, desired_size_in_pixel
);
222 resized_image_skia
.AddRepresentation(gfx::ImageSkiaRep(bitmap
, scale
));
225 if (png_reps
.empty())
226 return gfx::Image(resized_image_skia
);
228 std::vector
<gfx::ImageSkiaRep
> resized_image_skia_reps
=
229 resized_image_skia
.image_reps();
230 for (size_t i
= 0; i
< resized_image_skia_reps
.size(); ++i
) {
231 scoped_refptr
<base::RefCountedBytes
> png_bytes(new base::RefCountedBytes());
232 if (gfx::PNGCodec::EncodeBGRASkBitmap(
233 resized_image_skia_reps
[i
].sk_bitmap(),
235 &png_bytes
->data())) {
237 gfx::ImagePNGRep(png_bytes
, resized_image_skia_reps
[i
].scale()));
241 return gfx::Image(png_reps
);
244 } // namespace favicon_base