1
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #ifndef SkCGUtils_DEFINED
9 #define SkCGUtils_DEFINED
10
11 #include "include/core/SkImage.h"
12 #include "include/core/SkImageInfo.h"
13 #include "include/core/SkPixmap.h"
14 #include "include/core/SkSize.h"
15
16 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
17
18 #ifdef SK_BUILD_FOR_MAC
19 #include <ApplicationServices/ApplicationServices.h>
20 #endif
21
22 #ifdef SK_BUILD_FOR_IOS
23 #include <CoreGraphics/CoreGraphics.h>
24 #endif
25
26 class SkBitmap;
27 class SkColorSpace;
28 class SkData;
29 class SkPixmap;
30 class SkStreamRewindable;
31
32 SK_API CGContextRef SkCreateCGContext(const SkPixmap&);
33
34 /**
35 * Given a CGImage, allocate an SkBitmap and copy the image's pixels into it. If scaleToFit is not
36 * null, use it to determine the size of the bitmap, and scale the image to fill the bitmap.
37 * Otherwise use the image's width/height.
38 *
39 * On failure, return false, and leave bitmap unchanged.
40 */
41 SK_API bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef src);
42
43 SK_API sk_sp<SkImage> SkMakeImageFromCGImage(CGImageRef);
44
45 /**
46 * Given a CGColorSpace, return the closest matching SkColorSpace. If no conversion is possible
47 * or if the input CGColorSpace is nullptr then return nullptr.
48 */
49 SK_API sk_sp<SkColorSpace> SkMakeColorSpaceFromCGColorSpace(CGColorSpaceRef);
50
51 /**
52 * Copy the pixels from src into the memory specified by info/rowBytes/dstPixels. On failure,
53 * return false (e.g. ImageInfo incompatible with src).
54 */
55 SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, void* dstPixels,
56 CGImageRef src);
SkCopyPixelsFromCGImage(const SkPixmap & dst,CGImageRef src)57 static inline bool SkCopyPixelsFromCGImage(const SkPixmap& dst, CGImageRef src) {
58 return SkCopyPixelsFromCGImage(dst.info(), dst.rowBytes(), dst.writable_addr(), src);
59 }
60
61 /**
62 * Create an imageref from the specified bitmap. The color space parameter is ignored.
63 */
64 SK_API CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
65 CGColorSpaceRef space);
66
67 /**
68 * Create an imageref from the specified bitmap.
69 */
70 SK_API CGImageRef SkCreateCGImageRef(const SkBitmap& bm);
71
72 /**
73 * Given an SkColorSpace, create a CGColorSpace. This will return sRGB if the specified
74 * SkColorSpace is nullptr or on failure. This will not retain the specified SkColorSpace.
75 */
76 SK_API CGColorSpaceRef SkCreateCGColorSpace(const SkColorSpace*);
77
78 /**
79 * Given an SkData, create a CGDataProviderRef that refers to the and retains the specified data.
80 */
81 SK_API CGDataProviderRef SkCreateCGDataProvider(sk_sp<SkData>);
82
83 /**
84 * Draw the bitmap into the specified CG context. (x,y) specifies the position of the top-left
85 * corner of the bitmap.
86 */
87 void SkCGDrawBitmap(CGContextRef, const SkBitmap&, float x, float y);
88
89 #endif // defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
90 #endif // SkCGUtils_DEFINED
91