Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1308)

Side by Side Diff: src/ports/SkImageGeneratorCG.cpp

Issue 1718273004: Add an SkImageGeneratorCG (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Use CGCopyProperties to perform a deferred decode Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« gyp/ports.gyp ('K') | « src/ports/SkImageGeneratorCG.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
10 #include "SkUnpremultiply.h"
11
12 #ifdef SK_BUILD_FOR_MAC
13 #include <ApplicationServices/ApplicationServices.h>
14 #endif
15
16 #ifdef SK_BUILD_FOR_IOS
17 #include <CoreGraphics/CoreGraphics.h>
18 #include <ImageIO/ImageIO.h>
19 #include <MobileCoreServices/MobileCoreServices.h>
20 #endif
21
22 static CGImageSourceRef data_to_CGImageSrc(SkData* data) {
23 CGDataProviderRef cgData = CGDataProviderCreateWithData(data, data->data(), data->size(),
24 nullptr);
25 if (!cgData) {
26 return nullptr;
27 }
28 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(cgData, 0);
29 CGDataProviderRelease(cgData);
30 return imageSrc;
31 }
32
33 SkImageGenerator* SkImageGeneratorCG::NewFromEncodedCG(SkData* data) {
34 CGImageSourceRef imageSrc = data_to_CGImageSrc(data);
35 if (nullptr == imageSrc) {
36 return nullptr;
37 }
38 SkAutoTCallVProc<const void, CFRelease> autoImageSrc(imageSrc);
39
40 CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSrc, 0, nullptr);
41 CFNumberRef widthRef = (CFNumberRef) (CFDictionaryGetValue(properties,
42 kCGImagePropertyPixelWidth));
43 CFNumberRef heightRef = (CFNumberRef) (CFDictionaryGetValue(properties,
44 kCGImagePropertyPixelHeight));
45 bool hasAlpha = (bool) (CFDictionaryGetValue(properties,
46 kCGImagePropertyHasAlpha));
reed1 2016/03/02 01:45:42 Do we know that hasalpha means its not opaque? Do
msarett 2016/03/04 23:48:33 I tested this experimentally. We can trust hasAlp
47
48 int width, height;
49 CFNumberGetValue(widthRef, kCFNumberIntType, &width);
50 CFNumberGetValue(heightRef, kCFNumberIntType, &height);
51
52 SkAlphaType alphaType = hasAlpha ? kPremul_SkAlphaType : kOpaque_SkAlphaType ;
53 SkImageInfo info = SkImageInfo::Make(width, height, kN32_SkColorType, alphaT ype);
reed1 2016/03/02 01:45:42 It appears that we can query properties to discove
msarett 2016/03/04 23:48:33 Strangely, we can't query for unpremul/premul unti
54 return new SkImageGeneratorCG(info, autoImageSrc.detach(), data);
55 }
56
57 SkImageGeneratorCG::SkImageGeneratorCG(const SkImageInfo& info, const void* imag eSrc, SkData* data)
58 : INHERITED(info)
59 , fImageSrc(imageSrc)
60 , fData(SkRef(data))
61 {}
62
63 SkData* SkImageGeneratorCG::onRefEncodedData(SK_REFENCODEDDATA_CTXPARAM) {
64 return SkRef(fData.get());
65 }
66
67 bool SkImageGeneratorCG::onGetPixels(const SkImageInfo& info, void* pixels, size _t rowBytes,
68 SkPMColor ctable[], int* ctableCount) {
69 CGImageRef image = CGImageSourceCreateImageAtIndex((CGImageSourceRef) fImage Src.get(), 0,
70 nullptr);
71 if (nullptr == image) {
72 return false;
73 }
74 SkAutoTCallVProc<CGImage, CGImageRelease> autoImage(image);
75
76 if (!SkCopyPixelsFromCGImage(info, rowBytes, pixels, image)) {
77 return false;
78 }
79
80 // CG cannot produce unpremultiplied outputs, so if the client requests
81 // kUnpremul, we must unpremultiply.
scroggo 2016/03/02 13:08:14 I think this is the same as the old one, but this
msarett 2016/03/04 23:48:33 Removed this code.
82 if (kUnpremul_SkAlphaType == info.alphaType()) {
83 SkPMColor* dstRow = (SkPMColor*) pixels;
84 for (int y = 0; y < info.height(); y++) {
85 for (int x = 0; x < info.width(); x++) {
86 dstRow[x] = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(ds tRow[x]);
87 }
88 dstRow = SkTAddOffset<SkPMColor>(dstRow, rowBytes);
89 }
90 }
91
92 return true;
93 }
94
95 #endif //defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
OLDNEW
« gyp/ports.gyp ('K') | « src/ports/SkImageGeneratorCG.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698