reed@android.com | 0d55f1e | 2008-12-18 19:26:11 +0000 | [diff] [blame] | 1 | #include "SkCGUtils.h" |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 2 | #include "SkBitmap.h" |
| 3 | |
| 4 | extern CGImageRef SkCreateCGImageRef(const SkBitmap&); |
| 5 | |
reed@android.com | 2b26cac | 2008-12-22 02:33:11 +0000 | [diff] [blame^] | 6 | static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) { |
reed@android.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 7 | SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info); |
| 8 | delete bitmap; |
| 9 | } |
| 10 | |
| 11 | static 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.com | 0680d6c | 2008-12-19 19:46:15 +0000 | [diff] [blame] | 21 | 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.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 30 | default: |
| 31 | return NULL; |
| 32 | } |
| 33 | |
| 34 | return new SkBitmap(bm); |
| 35 | } |
| 36 | |
| 37 | CGImageRef 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.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 50 | // our provider "owns" the bitmap*, and will take care of deleting it |
reed@android.com | 2b26cac | 2008-12-22 02:33:11 +0000 | [diff] [blame^] | 51 | // 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.com | 758b129 | 2008-12-18 17:54:12 +0000 | [diff] [blame] | 57 | 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 | |