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