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