bdf10810288c3c411f482ccbc656a4fbfc9a83c0
[platform/framework/web/crosswalk.git] / src / xwalk / runtime / browser / image_util.cc
1 // Copyright (c) 2013 Intel Corporation. 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 "xwalk/runtime/browser/image_util.h"
6
7 #include <string>
8
9 #include "base/file_util.h"
10 #include "base/strings/string_util.h"
11 #include "ui/gfx/image/image_skia.h"
12 #include "ui/gfx/size.h"
13
14 #if defined(OS_WIN)
15 #include "ui/gfx/icon_util.h"
16 #endif
17
18 namespace xwalk_utils {
19
20 gfx::Image LoadImageFromFilePath(const base::FilePath& filename) {
21   const base::FilePath::StringType kPNGFormat(FILE_PATH_LITERAL(".png"));
22   const base::FilePath::StringType kICOFormat(FILE_PATH_LITERAL(".ico"));
23
24   if (EndsWith(filename.value(), kPNGFormat, false)) {
25     std::string contents;
26     base::ReadFileToString(filename, &contents);
27     return gfx::Image::CreateFrom1xPNGBytes(
28         reinterpret_cast<const unsigned char*>(contents.data()),
29             contents.size());
30   }
31
32   if (EndsWith(filename.value(), kICOFormat, false)) {
33 #if defined(OS_WIN)
34     HICON icon = static_cast<HICON>(LoadImage(NULL,
35                                     filename.value().c_str(),
36                                     IMAGE_ICON,
37                                     0,
38                                     0,
39                                     LR_LOADTRANSPARENT | LR_LOADFROMFILE));
40     if (icon == NULL)
41       return gfx::Image();
42
43     gfx::Image image;
44     scoped_ptr<SkBitmap> bitmap(IconUtil::CreateSkBitmapFromHICON(icon));
45     if (bitmap.get()) {
46       gfx::ImageSkia image_skia = gfx::ImageSkia::CreateFrom1xBitmap(*bitmap);
47       image = gfx::Image(image_skia);
48     }
49     DestroyIcon(icon);
50
51     return image;
52 #elif defined(USE_AURA) && defined(OS_LINUX)
53     NOTIMPLEMENTED();
54     return gfx::Image();
55 #else
56   NOTREACHED();
57   return gfx::Image();
58 #endif
59   }
60
61   LOG(INFO) << "Only support png and ico file format.";
62   return gfx::Image();
63 }
64
65 }  // namespace xwalk_utils