blob: 06995c3ec1b1b6fdbad9e914b644ab23330b3906 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com0d55f1e2008-12-18 19:26:11 +00008#ifndef SkCGUtils_DEFINED
9#define SkCGUtils_DEFINED
10
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000011#include "SkSize.h"
12#include "SkImageInfo.h"
Mike Reed463c8482016-12-21 12:01:12 -050013#include "SkImage.h"
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -040014#include "SkPixmap.h"
reed@android.comf2b98d62010-12-20 18:26:13 +000015
halcanary0cbe7ee2015-12-01 09:02:49 -080016#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
17
reed@android.comf2b98d62010-12-20 18:26:13 +000018#ifdef SK_BUILD_FOR_MAC
yangsu@google.comccb74ea2011-06-21 13:09:32 +000019#include <ApplicationServices/ApplicationServices.h>
reed@android.comf2b98d62010-12-20 18:26:13 +000020#endif
reed@android.com0d55f1e2008-12-18 19:26:11 +000021
yangsu@google.comc134f392011-06-23 22:27:30 +000022#ifdef SK_BUILD_FOR_IOS
23#include <CoreGraphics/CoreGraphics.h>
24#endif
25
reed@android.com0d55f1e2008-12-18 19:26:11 +000026class SkBitmap;
reed@google.com12b1f192013-03-25 20:15:40 +000027class SkData;
Mike Reed356f7c22017-01-10 11:58:39 -050028class SkPixmap;
bungemanf93d7112016-09-16 06:24:20 -070029class SkStreamRewindable;
reed@android.com0d55f1e2008-12-18 19:26:11 +000030
Mike Reed356f7c22017-01-10 11:58:39 -050031SK_API CGContextRef SkCreateCGContext(const SkPixmap&);
32
reed@android.com38669c12011-01-03 13:48:50 +000033/**
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000034 * 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 */
Mike Reed463c8482016-12-21 12:01:12 -050040SK_API bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef src);
41
42SK_API sk_sp<SkImage> SkMakeImageFromCGImage(CGImageRef);
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000043
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);
Leon Scroggins III0cbc10f2017-10-30 09:07:53 -040050static inline bool SkCopyPixelsFromCGImage(const SkPixmap& dst, CGImageRef src) {
51 return SkCopyPixelsFromCGImage(dst.info(), dst.rowBytes(), dst.writable_addr(), src);
52}
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +000053
54/**
reed@android.com38669c12011-01-03 13:48:50 +000055 * Create an imageref from the specified bitmap using the specified colorspace.
reed@google.com5a8a1312011-01-05 16:29:02 +000056 * If space is NULL, then CGColorSpaceCreateDeviceRGB() is used.
reed@android.com38669c12011-01-03 13:48:50 +000057 */
thakis@chromium.org368b4192012-02-09 22:09:41 +000058SK_API CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
59 CGColorSpaceRef space);
reed@android.com0d55f1e2008-12-18 19:26:11 +000060
reed@android.com38669c12011-01-03 13:48:50 +000061/**
reed@google.com5a8a1312011-01-05 16:29:02 +000062 * Create an imageref from the specified bitmap using the colorspace returned
63 * by CGColorSpaceCreateDeviceRGB()
reed@android.com38669c12011-01-03 13:48:50 +000064 */
reed@android.com4aaee0a2011-01-04 01:29:52 +000065static inline CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
reed@android.com38669c12011-01-03 13:48:50 +000066 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
reed@google.com5a8a1312011-01-05 16:29:02 +000072 * of the top-left corner of the bitmap. The bitmap is converted using the
73 * colorspace returned by CGColorSpaceCreateDeviceRGB()
reed@android.com38669c12011-01-03 13:48:50 +000074 */
reed@android.comf2b98d62010-12-20 18:26:13 +000075void SkCGDrawBitmap(CGContextRef, const SkBitmap&, float x, float y);
76
scroggoa1193e42015-01-21 12:09:53 -080077/**
bungemanf93d7112016-09-16 06:24:20 -070078 * Return a provider that wraps the specified stream.
scroggoa1193e42015-01-21 12:09:53 -080079 * When the provider is finally deleted, it will delete the stream.
reed@google.com61c22042012-02-24 15:29:00 +000080 */
bungemanf93d7112016-09-16 06:24:20 -070081CGDataProviderRef SkCreateDataProviderFromStream(std::unique_ptr<SkStreamRewindable>);
reed@google.com50b14a22013-03-25 20:21:26 +000082
bungemanf93d7112016-09-16 06:24:20 -070083CGDataProviderRef SkCreateDataProviderFromData(sk_sp<SkData>);
reed@google.com61c22042012-02-24 15:29:00 +000084
halcanary0cbe7ee2015-12-01 09:02:49 -080085#endif // defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
86#endif // SkCGUtils_DEFINED