| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2011 Google Inc. | 2 * Copyright 2011 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "SkTypes.h" | 8 #include "SkTypes.h" |
| 9 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) | 9 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 10 | 10 |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 CGContextScaleCTM(cg, 1, -1); | 169 CGContextScaleCTM(cg, 1, -1); |
| 170 | 170 |
| 171 CGContextDrawImage(cg, r, img); | 171 CGContextDrawImage(cg, r, img); |
| 172 | 172 |
| 173 CGContextRestoreGState(cg); | 173 CGContextRestoreGState(cg); |
| 174 | 174 |
| 175 CGImageRelease(img); | 175 CGImageRelease(img); |
| 176 } | 176 } |
| 177 } | 177 } |
| 178 | 178 |
| 179 /////////////////////////////////////////////////////////////////////////////// | |
| 180 | |
| 181 #include "SkStream.h" | |
| 182 | |
| 183 class SkAutoPDFRelease { | |
| 184 public: | |
| 185 SkAutoPDFRelease(CGPDFDocumentRef doc) : fDoc(doc) {} | |
| 186 ~SkAutoPDFRelease() { | |
| 187 if (fDoc) { | |
| 188 CGPDFDocumentRelease(fDoc); | |
| 189 } | |
| 190 } | |
| 191 private: | |
| 192 CGPDFDocumentRef fDoc; | |
| 193 }; | |
| 194 #define SkAutoPDFRelease(...) SK_REQUIRE_LOCAL_VAR(SkAutoPDFRelease) | |
| 195 | |
| 196 bool SkPDFDocumentToBitmap(SkStream* stream, SkBitmap* output) { | |
| 197 CGDataProviderRef data = SkCreateDataProviderFromStream(stream); | |
| 198 if (nullptr == data) { | |
| 199 return false; | |
| 200 } | |
| 201 | |
| 202 CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(data); | |
| 203 CGDataProviderRelease(data); | |
| 204 if (nullptr == pdf) { | |
| 205 return false; | |
| 206 } | |
| 207 SkAutoPDFRelease releaseMe(pdf); | |
| 208 | |
| 209 CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1); | |
| 210 if (nullptr == page) { | |
| 211 return false; | |
| 212 } | |
| 213 | |
| 214 CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); | |
| 215 | |
| 216 int w = (int)CGRectGetWidth(bounds); | |
| 217 int h = (int)CGRectGetHeight(bounds); | |
| 218 | |
| 219 SkBitmap bitmap; | |
| 220 if (!bitmap.tryAllocN32Pixels(w, h)) { | |
| 221 return false; | |
| 222 } | |
| 223 bitmap.eraseColor(SK_ColorWHITE); | |
| 224 | |
| 225 size_t bitsPerComponent; | |
| 226 CGBitmapInfo info; | |
| 227 getBitmapInfo(bitmap, &bitsPerComponent, &info, nullptr); | |
| 228 | |
| 229 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); | |
| 230 CGContextRef ctx = CGBitmapContextCreate(bitmap.getPixels(), w, h, | |
| 231 bitsPerComponent, bitmap.rowBytes()
, | |
| 232 cs, info); | |
| 233 CGColorSpaceRelease(cs); | |
| 234 | |
| 235 if (ctx) { | |
| 236 CGContextDrawPDFPage(ctx, page); | |
| 237 CGContextRelease(ctx); | |
| 238 } | |
| 239 | |
| 240 output->swap(bitmap); | |
| 241 return true; | |
| 242 } | |
| 243 | |
| 244 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | 179 ////////////////////////////////////////////////////////////////////////////////
/////////////////// |
| 245 | 180 |
| 246 SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, vo
id* pixels, | 181 SK_API bool SkCopyPixelsFromCGImage(const SkImageInfo& info, size_t rowBytes, vo
id* pixels, |
| 247 CGImageRef image) { | 182 CGImageRef image) { |
| 248 CGBitmapInfo cg_bitmap_info = 0; | 183 CGBitmapInfo cg_bitmap_info = 0; |
| 249 size_t bitsPerComponent = 0; | 184 size_t bitsPerComponent = 0; |
| 250 switch (info.colorType()) { | 185 switch (info.colorType()) { |
| 251 case kRGBA_8888_SkColorType: | 186 case kRGBA_8888_SkColorType: |
| 252 bitsPerComponent = 8; | 187 bitsPerComponent = 8; |
| 253 cg_bitmap_info = ComputeCGAlphaInfo_RGBA(info.alphaType()); | 188 cg_bitmap_info = ComputeCGAlphaInfo_RGBA(info.alphaType()); |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 if (SkBitmap::ComputeIsOpaque(tmp)) { | 239 if (SkBitmap::ComputeIsOpaque(tmp)) { |
| 305 tmp.setAlphaType(kOpaque_SkAlphaType); | 240 tmp.setAlphaType(kOpaque_SkAlphaType); |
| 306 } | 241 } |
| 307 } | 242 } |
| 308 | 243 |
| 309 *dst = tmp; | 244 *dst = tmp; |
| 310 return true; | 245 return true; |
| 311 } | 246 } |
| 312 | 247 |
| 313 #endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) | 248 #endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| OLD | NEW |