Index: src/utils/mac/SkCreateCGImageRef.cpp |
diff --git a/src/utils/mac/SkCreateCGImageRef.cpp b/src/utils/mac/SkCreateCGImageRef.cpp |
index 00166e9ed7488b4976d8c426a90061a7d3b40fd3..f6826da8d3162e11a83b6da9d54ab560f7d2492d 100644 |
--- a/src/utils/mac/SkCreateCGImageRef.cpp |
+++ b/src/utils/mac/SkCreateCGImageRef.cpp |
@@ -231,3 +231,71 @@ bool SkPDFDocumentToBitmap(SkStream* stream, SkBitmap* output) { |
output->swap(bitmap); |
return true; |
} |
+ |
+/////////////////////////////////////////////////////////////////////////////////////////////////// |
+ |
+SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, void* pixels, |
+ CGImageRef image) { |
+ uint32_t cg_bitmap_info = kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast; |
+ // TODO: update cg_bitmap_info based on colortype |
+ switch (info.colorType()) { |
+ case kRGB_565_SkColorType: |
+ break; |
+ case kRGBA_8888_SkColorType: |
+ break; |
+ case kBGRA_8888_SkColorType: |
+ break; |
+ default: |
+ return false; // no other colortypes are supported (afaik) |
+ } |
+ |
+ CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); |
+ CGContextRef cg = CGBitmapContextCreate(pixels, info.width(), info.height(), 8, rowBytes, cs, |
scroggo
2014/04/21 20:18:44
Isn't this 8 specific to 8888? If info.colorType()
reed1
2014/04/21 20:44:41
Yes. that is specfic, and so is the cg_bitmap_info
|
+ cg_bitmap_info); |
+ CFRelease(cs); |
+ if (NULL == cg) { |
+ return false; |
+ } |
+ |
+ // use this blend mode, to avoid having to erase the pixels first, and to avoid CG performing |
+ // any blending (which could introduce errors and be slower). |
+ CGContextSetBlendMode(cg, kCGBlendModeCopy); |
+ |
+ CGContextDrawImage(cg, CGRectMake(0, 0, info.width(), info.height()), image); |
+ CGContextRelease(cg); |
+ return true; |
+} |
+ |
+bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image, SkISize* scaleToFit) { |
+ const int width = scaleToFit ? scaleToFit->width() : SkToInt(CGImageGetWidth(image)); |
+ const int height = scaleToFit ? scaleToFit->height() : SkToInt(CGImageGetHeight(image)); |
+ SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
+ |
+ SkBitmap tmp; |
+ if (!tmp.allocPixels(info)) { |
+ return false; |
+ } |
+ |
+ if (!SkCopyPixelsFromCGImage(tmp.info(), tmp.rowBytes(), tmp.getPixels(), image)) { |
+ return false; |
+ } |
+ |
+ CGImageAlphaInfo cgInfo = CGImageGetAlphaInfo(image); |
scroggo
2014/04/21 20:18:44
I'm not a big fan of this duplicated code, if we c
reed1
2014/04/21 20:44:41
Working on it, but haven't seen how to share yet.
|
+ switch (cgInfo) { |
+ case kCGImageAlphaNone: |
+ case kCGImageAlphaNoneSkipLast: |
+ case kCGImageAlphaNoneSkipFirst: |
+ SkASSERT(SkBitmap::ComputeIsOpaque(tmp)); |
+ tmp.setAlphaType(kOpaque_SkAlphaType); |
+ break; |
+ default: |
+ // we don't know if we're opaque or not, so compute it. |
+ if (SkBitmap::ComputeIsOpaque(tmp)) { |
+ tmp.setAlphaType(kOpaque_SkAlphaType); |
+ } |
+ } |
+ |
+ *dst = tmp; |
+ return true; |
+} |
+ |