OLD | NEW |
1 | 1 |
2 /* | 2 /* |
3 * Copyright 2011 Google Inc. | 3 * Copyright 2011 Google Inc. |
4 * | 4 * |
5 * Use of this source code is governed by a BSD-style license that can be | 5 * Use of this source code is governed by a BSD-style license that can be |
6 * found in the LICENSE file. | 6 * found in the LICENSE file. |
7 */ | 7 */ |
8 #include "SkCGUtils.h" | 8 #include "SkCGUtils.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkColorPriv.h" | 10 #include "SkColorPriv.h" |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 if (NULL == page) { | 214 if (NULL == page) { |
215 return false; | 215 return false; |
216 } | 216 } |
217 | 217 |
218 CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); | 218 CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); |
219 | 219 |
220 int w = (int)CGRectGetWidth(bounds); | 220 int w = (int)CGRectGetWidth(bounds); |
221 int h = (int)CGRectGetHeight(bounds); | 221 int h = (int)CGRectGetHeight(bounds); |
222 | 222 |
223 SkBitmap bitmap; | 223 SkBitmap bitmap; |
224 if (!bitmap.allocPixels(SkImageInfo::MakeN32Premul(w, h))) { | 224 if (!bitmap.tryAllocN32Pixels(w, h)) { |
225 return false; | 225 return false; |
226 } | 226 } |
227 bitmap.eraseColor(SK_ColorWHITE); | 227 bitmap.eraseColor(SK_ColorWHITE); |
228 | 228 |
229 size_t bitsPerComponent; | 229 size_t bitsPerComponent; |
230 CGBitmapInfo info; | 230 CGBitmapInfo info; |
231 getBitmapInfo(bitmap, &bitsPerComponent, &info, NULL); | 231 getBitmapInfo(bitmap, &bitsPerComponent, &info, NULL); |
232 | 232 |
233 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); | 233 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); |
234 CGContextRef ctx = CGBitmapContextCreate(bitmap.getPixels(), w, h, | 234 CGContextRef ctx = CGBitmapContextCreate(bitmap.getPixels(), w, h, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
280 CGContextRelease(cg); | 280 CGContextRelease(cg); |
281 return true; | 281 return true; |
282 } | 282 } |
283 | 283 |
284 bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image, SkISize* scaleTo
Fit) { | 284 bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image, SkISize* scaleTo
Fit) { |
285 const int width = scaleToFit ? scaleToFit->width() : SkToInt(CGImageGetWidth
(image)); | 285 const int width = scaleToFit ? scaleToFit->width() : SkToInt(CGImageGetWidth
(image)); |
286 const int height = scaleToFit ? scaleToFit->height() : SkToInt(CGImageGetHei
ght(image)); | 286 const int height = scaleToFit ? scaleToFit->height() : SkToInt(CGImageGetHei
ght(image)); |
287 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); | 287 SkImageInfo info = SkImageInfo::MakeN32Premul(width, height); |
288 | 288 |
289 SkBitmap tmp; | 289 SkBitmap tmp; |
290 if (!tmp.allocPixels(info)) { | 290 if (!tmp.tryAllocPixels(info)) { |
291 return false; | 291 return false; |
292 } | 292 } |
293 | 293 |
294 if (!SkCopyPixelsFromCGImage(tmp.info(), tmp.rowBytes(), tmp.getPixels(), im
age)) { | 294 if (!SkCopyPixelsFromCGImage(tmp.info(), tmp.rowBytes(), tmp.getPixels(), im
age)) { |
295 return false; | 295 return false; |
296 } | 296 } |
297 | 297 |
298 CGImageAlphaInfo cgInfo = CGImageGetAlphaInfo(image); | 298 CGImageAlphaInfo cgInfo = CGImageGetAlphaInfo(image); |
299 switch (cgInfo) { | 299 switch (cgInfo) { |
300 case kCGImageAlphaNone: | 300 case kCGImageAlphaNone: |
301 case kCGImageAlphaNoneSkipLast: | 301 case kCGImageAlphaNoneSkipLast: |
302 case kCGImageAlphaNoneSkipFirst: | 302 case kCGImageAlphaNoneSkipFirst: |
303 SkASSERT(SkBitmap::ComputeIsOpaque(tmp)); | 303 SkASSERT(SkBitmap::ComputeIsOpaque(tmp)); |
304 tmp.setAlphaType(kOpaque_SkAlphaType); | 304 tmp.setAlphaType(kOpaque_SkAlphaType); |
305 break; | 305 break; |
306 default: | 306 default: |
307 // we don't know if we're opaque or not, so compute it. | 307 // we don't know if we're opaque or not, so compute it. |
308 if (SkBitmap::ComputeIsOpaque(tmp)) { | 308 if (SkBitmap::ComputeIsOpaque(tmp)) { |
309 tmp.setAlphaType(kOpaque_SkAlphaType); | 309 tmp.setAlphaType(kOpaque_SkAlphaType); |
310 } | 310 } |
311 } | 311 } |
312 | 312 |
313 *dst = tmp; | 313 *dst = tmp; |
314 return true; | 314 return true; |
315 } | 315 } |
OLD | NEW |