1 // Copyright (c) 2011 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/ui/webui/web_ui_util.h"
6
7 #include <vector>
8
9 #include "base/base64.h"
10 #include "ui/base/resource/resource_bundle.h"
11 #include "ui/gfx/codec/png_codec.h"
12
13 namespace web_ui_util {
14
GetImageDataUrl(const SkBitmap & bitmap)15 std::string GetImageDataUrl(const SkBitmap& bitmap) {
16 std::vector<unsigned char> output;
17 gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &output);
18 std::string str_url;
19 str_url.insert(str_url.end(), output.begin(), output.end());
20
21 base::Base64Encode(str_url, &str_url);
22 str_url.insert(0, "data:image/png;base64,");
23 return str_url;
24 }
25
GetImageDataUrlFromResource(int res)26 std::string GetImageDataUrlFromResource(int res) {
27 // Load resource icon and covert to base64 encoded data url
28 RefCountedStaticMemory* icon_data =
29 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(res);
30 if (!icon_data)
31 return std::string();
32 scoped_refptr<RefCountedMemory> raw_icon(icon_data);
33 std::string str_url;
34 str_url.insert(str_url.end(),
35 raw_icon->front(),
36 raw_icon->front() + raw_icon->size());
37 base::Base64Encode(str_url, &str_url);
38 str_url.insert(0, "data:image/png;base64,");
39 return str_url;
40 }
41
42 } // namespace web_ui_util
43