Index: src/utils/mac/SkCreateCGImageRef.cpp |
diff --git a/src/utils/mac/SkCreateCGImageRef.cpp b/src/utils/mac/SkCreateCGImageRef.cpp |
index d9cdb86e49f975193fd6cf0cda1ab86820ab9740..7f15ed7090b1a26cba83ab084926498a1193e83c 100644 |
--- a/src/utils/mac/SkCreateCGImageRef.cpp |
+++ b/src/utils/mac/SkCreateCGImageRef.cpp |
@@ -176,6 +176,71 @@ |
} |
} |
+/////////////////////////////////////////////////////////////////////////////// |
+ |
+#include "SkStream.h" |
+ |
+class SkAutoPDFRelease { |
+public: |
+ SkAutoPDFRelease(CGPDFDocumentRef doc) : fDoc(doc) {} |
+ ~SkAutoPDFRelease() { |
+ if (fDoc) { |
+ CGPDFDocumentRelease(fDoc); |
+ } |
+ } |
+private: |
+ CGPDFDocumentRef fDoc; |
+}; |
+#define SkAutoPDFRelease(...) SK_REQUIRE_LOCAL_VAR(SkAutoPDFRelease) |
+ |
+bool SkPDFDocumentToBitmap(SkStream* stream, SkBitmap* output) { |
+ CGDataProviderRef data = SkCreateDataProviderFromStream(stream); |
+ if (nullptr == data) { |
+ return false; |
+ } |
+ |
+ CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(data); |
+ CGDataProviderRelease(data); |
+ if (nullptr == pdf) { |
+ return false; |
+ } |
+ SkAutoPDFRelease releaseMe(pdf); |
+ |
+ CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1); |
+ if (nullptr == page) { |
+ return false; |
+ } |
+ |
+ CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); |
+ |
+ int w = (int)CGRectGetWidth(bounds); |
+ int h = (int)CGRectGetHeight(bounds); |
+ |
+ SkBitmap bitmap; |
+ if (!bitmap.tryAllocN32Pixels(w, h)) { |
+ return false; |
+ } |
+ bitmap.eraseColor(SK_ColorWHITE); |
+ |
+ size_t bitsPerComponent; |
+ CGBitmapInfo info; |
+ getBitmapInfo(bitmap, &bitsPerComponent, &info, nullptr); |
+ |
+ CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); |
+ CGContextRef ctx = CGBitmapContextCreate(bitmap.getPixels(), w, h, |
+ bitsPerComponent, bitmap.rowBytes(), |
+ cs, info); |
+ CGColorSpaceRelease(cs); |
+ |
+ if (ctx) { |
+ CGContextDrawPDFPage(ctx, page); |
+ CGContextRelease(ctx); |
+ } |
+ |
+ output->swap(bitmap); |
+ return true; |
+} |
+ |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, void* pixels, |