Separate Simple Backend creation from initialization.
[chromium-blink-merge.git] / ui / gfx / image / image_skia_util_ios.mm
blob2d4afed000a94b7ffefd586ca0dc2a89870efcd0
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   gfx::ImageSkiaRep image_skia_rep = ImageSkiaRepOfScaleFactorFromUIImage(
20       image, ui::GetMaxScaleFactor());
21   if (!image_skia_rep.is_null())
22     image_skia.AddRepresentation(image_skia_rep);
23   return image_skia;
26 gfx::ImageSkiaRep ImageSkiaRepOfScaleFactorFromUIImage(
27     UIImage* image,
28     ui::ScaleFactor scale_factor) {
29   if (!image)
30     return gfx::ImageSkiaRep();
32   float scale = ui::GetScaleFactorScale(scale_factor);
33   CGSize size = image.size;
34   CGSize desired_size_for_scale =
35       CGSizeMake(size.width * scale, size.height * scale);
36   SkBitmap bitmap(gfx::CGImageToSkBitmap(image.CGImage,
37                                          desired_size_for_scale,
38                                          false));
39   return gfx::ImageSkiaRep(bitmap, scale_factor);
42 UIImage* UIImageFromImageSkia(const gfx::ImageSkia& image_skia) {
43   return UIImageFromImageSkiaRep(image_skia.GetRepresentation(
44       ui::GetMaxScaleFactor()));
47 UIImage* UIImageFromImageSkiaRep(const gfx::ImageSkiaRep& image_skia_rep) {
48   if (image_skia_rep.is_null())
49     return nil;
51   float scale = ui::GetScaleFactorScale(image_skia_rep.scale_factor());
52   base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space(
53       CGColorSpaceCreateDeviceRGB());
54   return gfx::SkBitmapToUIImageWithColorSpace(image_skia_rep.sk_bitmap(), scale,
55                                               color_space);
58 }  // namespace gfx