blob: 6c9415edc07fb4612798101f4670cc4e43485782 [file] [log] [blame]
reed@android.com0d55f1e2008-12-18 19:26:11 +00001#include "SkCGUtils.h"
reed@android.com758b1292008-12-18 17:54:12 +00002#include "SkBitmap.h"
3
4extern CGImageRef SkCreateCGImageRef(const SkBitmap&);
5
reed@android.com2b26cac2008-12-22 02:33:11 +00006static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) {
reed@android.com758b1292008-12-18 17:54:12 +00007 SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info);
8 delete bitmap;
9}
10
11static SkBitmap* prepareForImageRef(const SkBitmap& bm,
12 size_t* bitsPerComponent,
13 CGBitmapInfo* info) {
14 switch (bm.config()) {
15 case SkBitmap::kARGB_8888_Config:
16 *bitsPerComponent = 8;
17 // try to match our argb ordering in SkColorPriv
18 *info = kCGBitmapByteOrder32Big |
19 kCGImageAlphaPremultipliedLast;
20 break;
reed@android.com0680d6c2008-12-19 19:46:15 +000021 case SkBitmap::kRGB_565_Config:
22 // doesn't see quite right. Are they thinking 1555?
23 *bitsPerComponent = 5;
24 *info = kCGBitmapByteOrder16Little;
25 break;
26 case SkBitmap::kARGB_4444_Config:
27 *bitsPerComponent = 4;
28 *info = kCGBitmapByteOrder16Little | kCGImageAlphaPremultipliedLast;
29 break;
reed@android.com758b1292008-12-18 17:54:12 +000030 default:
31 return NULL;
32 }
33
34 return new SkBitmap(bm);
35}
36
37CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
38 size_t bitsPerComponent;
39 CGBitmapInfo info;
40
41 SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info);
42 if (NULL == bitmap) {
43 return NULL;
44 }
45
46 const int w = bitmap->width();
47 const int h = bitmap->height();
48 const size_t s = bitmap->getSize();
49
reed@android.com758b1292008-12-18 17:54:12 +000050 // our provider "owns" the bitmap*, and will take care of deleting it
reed@android.com2b26cac2008-12-22 02:33:11 +000051 // we initially lock it, so we can access the pixels. The bitmap will be deleted in the release
52 // proc, which will in turn unlock the pixels
53 bitmap->lockPixels();
54 CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s,
55 SkBitmap_ReleaseInfo);
56
reed@android.com758b1292008-12-18 17:54:12 +000057 CGColorSpaceRef space = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
58 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent,
59 bitmap->bytesPerPixel() * 8,
60 bitmap->rowBytes(), space, info, dataRef,
61 NULL, false, kCGRenderingIntentDefault);
62 CGColorSpaceRelease(space);
63 CGDataProviderRelease(dataRef);
64 return ref;
65}
66
67