OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkImageGeneratorCG.h" |
| 9 #include "SkUnpremultiply.h" |
| 10 |
| 11 #ifdef SK_BUILD_FOR_MAC |
| 12 #include <ApplicationServices/ApplicationServices.h> |
| 13 #endif |
| 14 |
| 15 #ifdef SK_BUILD_FOR_IOS |
| 16 #include <CoreGraphics/CoreGraphics.h> |
| 17 #include <ImageIO/ImageIO.h> |
| 18 #include <MobileCoreServices/MobileCoreServices.h> |
| 19 #endif |
| 20 |
| 21 static CGImageSourceRef data_to_CGImageSrc(SkData* data) { |
| 22 CGDataProviderRef cgData = CGDataProviderCreateWithData(data, data->data(),
data->size(), |
| 23 nullptr); |
| 24 if (!cgData) { |
| 25 return nullptr; |
| 26 } |
| 27 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(cgData, 0); |
| 28 CGDataProviderRelease(cgData); |
| 29 return imageSrc; |
| 30 } |
| 31 |
| 32 SkImageGenerator* SkImageGeneratorCG::NewFromEncodedCG(SkData* data) { |
| 33 CGImageSourceRef imageSrc = data_to_CGImageSrc(data); |
| 34 if (!imageSrc) { |
| 35 return nullptr; |
| 36 } |
| 37 |
| 38 // Make sure we call CFRelease to free the imageSrc. Since CFRelease actual
ly takes |
| 39 // a const void*, we must cast the imageSrc to a const void*. |
| 40 SkAutoTCallVProc<const void, CFRelease> autoImageSrc(imageSrc); |
| 41 |
| 42 CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0,
nullptr); |
| 43 if (!properties) { |
| 44 return nullptr; |
| 45 } |
| 46 |
| 47 CFNumberRef widthRef = (CFNumberRef) (CFDictionaryGetValue(properties, |
| 48 kCGImagePropertyPixelWidth)); |
| 49 CFNumberRef heightRef = (CFNumberRef) (CFDictionaryGetValue(properties, |
| 50 kCGImagePropertyPixelHeight)); |
| 51 if (nullptr == widthRef || nullptr == heightRef) { |
| 52 return nullptr; |
| 53 } |
| 54 bool hasAlpha = (bool) (CFDictionaryGetValue(properties, |
| 55 kCGImagePropertyHasAlpha)); |
| 56 |
| 57 int width, height; |
| 58 if (!CFNumberGetValue(widthRef, kCFNumberIntType, &width) || |
| 59 !CFNumberGetValue(heightRef, kCFNumberIntType, &height)) { |
| 60 return nullptr; |
| 61 } |
| 62 |
| 63 SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType
; |
| 64 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, alphaT
ype); |
| 65 |
| 66 // FIXME: We have the opportunity to extract color space information here, |
| 67 // though I think it makes sense to wait until we understand how |
| 68 // we want to communicate it to the generator. |
| 69 |
| 70 return new SkImageGeneratorCG(info, autoImageSrc.detach(), data); |
| 71 } |
| 72 |
| 73 SkImageGeneratorCG::SkImageGeneratorCG(const SkImageInfo& info, const void* imag
eSrc, SkData* data) |
| 74 : INHERITED(info) |
| 75 , fImageSrc(imageSrc) |
| 76 , fData(SkRef(data)) |
| 77 {} |
| 78 |
| 79 SkData* SkImageGeneratorCG::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) { |
| 80 return SkRef(fData.get()); |
| 81 } |
| 82 |
| 83 bool SkImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size
_t rowBytes, |
| 84 SkPMColor ctable[], int* ctableCount) { |
| 85 if (kN32_SkColorType != info.colorType()) { |
| 86 // FIXME: Support other colorTypes. |
| 87 return false; |
| 88 } |
| 89 |
| 90 switch (info.alphaType()) { |
| 91 case kOpaque_SkAlphaType: |
| 92 if (kOpaque_SkAlphaType != this->getInfo().alphaType()) { |
| 93 return false; |
| 94 } |
| 95 break; |
| 96 case kPremul_SkAlphaType: |
| 97 break; |
| 98 default: |
| 99 return false; |
| 100 } |
| 101 |
| 102 CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImage
Src.get(), 0, |
| 103 nullptr); |
| 104 if (!image) { |
| 105 return false; |
| 106 } |
| 107 SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image); |
| 108 |
| 109 // FIXME: Using this function (as opposed to swizzling ourselves) greatly |
| 110 // restricts the color and alpha types that we support. If we |
| 111 // swizzle ourselves, we can add support for: |
| 112 // kUnpremul_SkAlphaType |
| 113 // 16-bit per component RGBA |
| 114 // kGray_8_SkColorType |
| 115 // kIndex_8_SkColorType |
| 116 // Additionally, it would be interesting to compare the performance |
| 117 // of SkSwizzler with CG's built in swizzler. |
| 118 if (!SkCopyPixelsFromCGImage(info, rowBytes, pixels, image)) { |
| 119 return false; |
| 120 } |
| 121 |
| 122 return true; |
| 123 } |
OLD | NEW |