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 "SkSize.h"
12 #include "SkImageInfo.h"
13 #include "SkImage.h"
14
15 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
16
17 #ifdef SK_BUILD_FOR_MAC
18 #include <ApplicationServices/ApplicationServices.h>
19 #endif
20
21 #ifdef SK_BUILD_FOR_IOS
22 #include <CoreGraphics/CoreGraphics.h>
23 #endif
24
25 class SkBitmap;
26 class SkData;
27 class SkPixmap;
28 class SkStreamRewindable;
29
30 SK_API CGContextRef SkCreateCGContext(const SkPixmap&);
31
32 /**
33 * Given a CGImage, allocate an SkBitmap and copy the image's pixels into it. If scaleToFit is not
34 * null, use it to determine the size of the bitmap, and scale the image to fill the bitmap.
35 * Otherwise use the image's width/height.
36 *
37 * On failure, return false, and leave bitmap unchanged.
38 */
39 SK_API bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef src);
40
41 SK_API sk_sp<SkImage> SkMakeImageFromCGImage(CGImageRef);
42
43 /**
44 * Copy the pixels from src into the memory specified by info/rowBytes/dstPixels. On failure,
45 * return false (e.g. ImageInfo incompatible with src).
46 */
47 SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, void* dstPixels,
48 CGImageRef src);
49
50 /**
51 * Create an imageref from the specified bitmap using the specified colorspace.
52 * If space is NULL, then CGColorSpaceCreateDeviceRGB() is used.
53 */
54 SK_API CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
55 CGColorSpaceRef space);
56
57 /**
58 * Create an imageref from the specified bitmap using the colorspace returned
59 * by CGColorSpaceCreateDeviceRGB()
60 */
SkCreateCGImageRef(const SkBitmap & bm)61 static inline CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
62 return SkCreateCGImageRefWithColorspace(bm, NULL);
63 }
64
65 /**
66 * Draw the bitmap into the specified CG context. The bitmap will be converted
67 * to a CGImage using the generic RGB colorspace. (x,y) specifies the position
68 * of the top-left corner of the bitmap. The bitmap is converted using the
69 * colorspace returned by CGColorSpaceCreateDeviceRGB()
70 */
71 void SkCGDrawBitmap(CGContextRef, const SkBitmap&, float x, float y);
72
73 /**
74 * Return a provider that wraps the specified stream.
75 * When the provider is finally deleted, it will delete the stream.
76 */
77 CGDataProviderRef SkCreateDataProviderFromStream(std::unique_ptr<SkStreamRewindable>);
78
79 CGDataProviderRef SkCreateDataProviderFromData(sk_sp<SkData>);
80
81 #endif // defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
82 #endif // SkCGUtils_DEFINED
83