add colorspace option to SkBitmap -> CGImageRef utility (patch from nico)



git-svn-id: http://skia.googlecode.com/svn/trunk@666 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/Makefile b/Makefile
index b0cfea5..5829d07 100644
--- a/Makefile
+++ b/Makefile
@@ -86,7 +86,7 @@
 
 	C_INCLUDES += -Iinclude/utils/mac
 #	SRC_LIST += src/ports/SkImageDecoder_CG.cpp
-#	SRC_LIST += src/utils/mac/SkCreateCGImageRef.cpp
+	SRC_LIST += src/utils/mac/SkCreateCGImageRef.cpp
 	SRC_LIST += src/utils/mac/SkEGLContext_mac.cpp
 	SRC_LIST += src/ports/SkFontHost_mac_coretext.cpp
 
diff --git a/include/utils/mac/SkCGUtils.h b/include/utils/mac/SkCGUtils.h
index b1263f4..8a8a1ed 100644
--- a/include/utils/mac/SkCGUtils.h
+++ b/include/utils/mac/SkCGUtils.h
@@ -11,8 +11,25 @@
 
 class SkBitmap;
 
-CGImageRef SkCreateCGImageRef(const SkBitmap&);
+/**
+ *  Create an imageref from the specified bitmap using the specified colorspace.
+ */
+CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
+                                            CGColorSpaceRef space);
 
+/**
+ *  Create an imageref from the specified bitmap using the colorspace
+ *  kCGColorSpaceGenericRGB
+ */
+CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
+    return SkCreateCGImageRefWithColorspace(bm, NULL);
+}
+
+/**
+ *  Draw the bitmap into the specified CG context. The bitmap will be converted
+ *  to a CGImage using the generic RGB colorspace. (x,y) specifies the position
+ *  of the top-left corner of the bitmap.
+ */
 void SkCGDrawBitmap(CGContextRef, const SkBitmap&, float x, float y);
 
 #endif
diff --git a/src/utils/mac/SkCreateCGImageRef.cpp b/src/utils/mac/SkCreateCGImageRef.cpp
index 2169bc0..dea443c 100644
--- a/src/utils/mac/SkCreateCGImageRef.cpp
+++ b/src/utils/mac/SkCreateCGImageRef.cpp
@@ -2,8 +2,6 @@
 #include "SkBitmap.h"
 #include "SkColorPriv.h"
 
-extern CGImageRef SkCreateCGImageRef(const SkBitmap&);
-
 static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) {
     SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info);
     delete bitmap;
@@ -74,9 +72,10 @@
 
 #undef HAS_ARGB_SHIFTS
 
-CGImageRef SkCreateCGImageRef(const SkBitmap& bm) {
-    size_t bitsPerComponent;
-    CGBitmapInfo info;
+CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
+                                            CGColorSpaceRef colorSpace) {
+    size_t bitsPerComponent SK_INIT_TO_AVOID_WARNING;
+    CGBitmapInfo info       SK_INIT_TO_AVOID_WARNING;
 
     SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info);
     if (NULL == bitmap) {
@@ -94,12 +93,20 @@
     CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s,
 															 SkBitmap_ReleaseInfo);
 
-    CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
+    bool releaseColorSpace = false;
+    if (NULL == colorSpace) {
+        colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
+        releaseColorSpace = true;
+    }
+
     CGImageRef ref = CGImageCreate(w, h, bitsPerComponent,
                                    bitmap->bytesPerPixel() * 8,
-                                   bitmap->rowBytes(), space, info, dataRef,
+                                   bitmap->rowBytes(), colorSpace, info, dataRef,
                                    NULL, false, kCGRenderingIntentDefault);
-    CGColorSpaceRelease(space);
+
+    if (releaseColorSpace) {
+        CGColorSpaceRelease(colorSpace);
+    }
     CGDataProviderRelease(dataRef);
     return ref;
 }