Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ui / gfx / image / image_skia_util_ios.mm
blob77944eee4c2df1125898815d3275000c3432b2f7
1 // Copyright 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.
5 #include "ui/gfx/image/image_skia_util_ios.h"
7 #include <UIKit/UIKit.h>
9 #include "base/logging.h"
10 #include "base/mac/scoped_cftyperef.h"
11 #include "skia/ext/skia_utils_ios.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/gfx/image/image_skia.h"
15 namespace gfx {
17 gfx::ImageSkia ImageSkiaFromUIImage(UIImage* image) {
18   gfx::ImageSkia image_skia;
19   float max_scale = ImageSkia::GetSupportedScales().back();
20   gfx::ImageSkiaRep image_skia_rep = ImageSkiaRepOfScaleFromUIImage(
21       image, max_scale);
22   if (!image_skia_rep.is_null())
23     image_skia.AddRepresentation(image_skia_rep);
24   return image_skia;
27 gfx::ImageSkiaRep ImageSkiaRepOfScaleFromUIImage(UIImage* image, float scale) {
28   if (!image)
29     return gfx::ImageSkiaRep();
31   CGSize size = image.size;
32   CGSize desired_size_for_scale =
33       CGSizeMake(size.width * scale, size.height * scale);
34   SkBitmap bitmap(gfx::CGImageToSkBitmap(image.CGImage,
35                                          desired_size_for_scale,
36                                          false));
37   return gfx::ImageSkiaRep(bitmap, scale);
40 UIImage* UIImageFromImageSkia(const gfx::ImageSkia& image_skia) {
41   return UIImageFromImageSkiaRep(
42       image_skia.GetRepresentation(ImageSkia::GetSupportedScales().back()));
45 UIImage* UIImageFromImageSkiaRep(const gfx::ImageSkiaRep& image_skia_rep) {
46   if (image_skia_rep.is_null())
47     return nil;
49   float scale = image_skia_rep.scale();
50   base::ScopedCFTypeRef<CGColorSpaceRef> color_space(
51       CGColorSpaceCreateDeviceRGB());
52   return gfx::SkBitmapToUIImageWithColorSpace(image_skia_rep.sk_bitmap(), scale,
53                                               color_space);
56 }  // namespace gfx