1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
|
/*
* Copyright 2011 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkTypes.h"
#if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
#include "SkCGUtils.h"
#include "SkBitmap.h"
#include "SkColorPriv.h"
static CGBitmapInfo ComputeCGAlphaInfo_RGBA(SkAlphaType at) {
CGBitmapInfo info = kCGBitmapByteOrder32Big;
switch (at) {
case kUnknown_SkAlphaType:
break;
case kOpaque_SkAlphaType:
info |= kCGImageAlphaNoneSkipLast;
break;
case kPremul_SkAlphaType:
info |= kCGImageAlphaPremultipliedLast;
break;
case kUnpremul_SkAlphaType:
info |= kCGImageAlphaLast;
break;
}
return info;
}
static CGBitmapInfo ComputeCGAlphaInfo_BGRA(SkAlphaType at) {
CGBitmapInfo info = kCGBitmapByteOrder32Little;
switch (at) {
case kUnknown_SkAlphaType:
break;
case kOpaque_SkAlphaType:
info |= kCGImageAlphaNoneSkipFirst;
break;
case kPremul_SkAlphaType:
info |= kCGImageAlphaPremultipliedFirst;
break;
case kUnpremul_SkAlphaType:
info |= kCGImageAlphaFirst;
break;
}
return info;
}
static void SkBitmap_ReleaseInfo(void* info, const void* pixelData, size_t size) {
SkBitmap* bitmap = reinterpret_cast<SkBitmap*>(info);
delete bitmap;
}
static bool getBitmapInfo(const SkBitmap& bm,
size_t* bitsPerComponent,
CGBitmapInfo* info,
bool* upscaleTo32) {
if (upscaleTo32) {
*upscaleTo32 = false;
}
switch (bm.colorType()) {
case kRGB_565_SkColorType:
#if 0
// doesn't see quite right. Are they thinking 1555?
*bitsPerComponent = 5;
*info = kCGBitmapByteOrder16Little | kCGImageAlphaNone;
#else
if (upscaleTo32) {
*upscaleTo32 = true;
}
// now treat like RGBA
*bitsPerComponent = 8;
*info = ComputeCGAlphaInfo_RGBA(kOpaque_SkAlphaType);
#endif
break;
case kRGBA_8888_SkColorType:
*bitsPerComponent = 8;
*info = ComputeCGAlphaInfo_RGBA(bm.alphaType());
break;
case kBGRA_8888_SkColorType:
*bitsPerComponent = 8;
*info = ComputeCGAlphaInfo_BGRA(bm.alphaType());
break;
case kARGB_4444_SkColorType:
*bitsPerComponent = 4;
*info = kCGBitmapByteOrder16Little;
if (bm.isOpaque()) {
*info |= kCGImageAlphaNoneSkipLast;
} else {
*info |= kCGImageAlphaPremultipliedLast;
}
break;
default:
return false;
}
return true;
}
static SkBitmap* prepareForImageRef(const SkBitmap& bm,
size_t* bitsPerComponent,
CGBitmapInfo* info) {
bool upscaleTo32;
if (!getBitmapInfo(bm, bitsPerComponent, info, &upscaleTo32)) {
return nullptr;
}
SkBitmap* copy;
if (upscaleTo32) {
copy = new SkBitmap;
// here we make a ceep copy of the pixels, since CG won't take our
// 565 directly
bm.copyTo(copy, kN32_SkColorType);
} else {
copy = new SkBitmap(bm);
}
return copy;
}
CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm,
CGColorSpaceRef colorSpace) {
size_t bitsPerComponent SK_INIT_TO_AVOID_WARNING;
CGBitmapInfo info SK_INIT_TO_AVOID_WARNING;
SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info);
if (nullptr == bitmap) {
return nullptr;
}
const int w = bitmap->width();
const int h = bitmap->height();
const size_t s = bitmap->getSize();
// our provider "owns" the bitmap*, and will take care of deleting it
// we initially lock it, so we can access the pixels. The bitmap will be deleted in the release
// proc, which will in turn unlock the pixels
bitmap->lockPixels();
CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->getPixels(), s,
SkBitmap_ReleaseInfo);
bool releaseColorSpace = false;
if (nullptr == colorSpace) {
colorSpace = CGColorSpaceCreateDeviceRGB();
releaseColorSpace = true;
}
CGImageRef ref = CGImageCreate(w, h, bitsPerComponent,
bitmap->bytesPerPixel() * 8,
bitmap->rowBytes(), colorSpace, info, dataRef,
nullptr, false, kCGRenderingIntentDefault);
if (releaseColorSpace) {
CGColorSpaceRelease(colorSpace);
}
CGDataProviderRelease(dataRef);
return ref;
}
void SkCGDrawBitmap(CGContextRef cg, const SkBitmap& bm, float x, float y) {
CGImageRef img = SkCreateCGImageRef(bm);
if (img) {
CGRect r = CGRectMake(0, 0, bm.width(), bm.height());
CGContextSaveGState(cg);
CGContextTranslateCTM(cg, x, r.size.height + y);
CGContextScaleCTM(cg, 1, -1);
CGContextDrawImage(cg, r, img);
CGContextRestoreGState(cg);
CGImageRelease(img);
}
}
///////////////////////////////////////////////////////////////////////////////
#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,
CGImageRef image) {
CGBitmapInfo cg_bitmap_info = 0;
size_t bitsPerComponent = 0;
switch (info.colorType()) {
case kRGBA_8888_SkColorType:
bitsPerComponent = 8;
cg_bitmap_info = ComputeCGAlphaInfo_RGBA(info.alphaType());
break;
case kBGRA_8888_SkColorType:
bitsPerComponent = 8;
cg_bitmap_info = ComputeCGAlphaInfo_BGRA(info.alphaType());
break;
default:
return false; // no other colortypes are supported (for now)
}
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
CGContextRef cg = CGBitmapContextCreate(pixels, info.width(), info.height(), bitsPerComponent,
rowBytes, cs, cg_bitmap_info);
CFRelease(cs);
if (nullptr == cg) {
return false;
}
// use this blend mode, to avoid having to erase the pixels first, and to avoid CG performing
// any blending (which could introduce errors and be slower).
CGContextSetBlendMode(cg, kCGBlendModeCopy);
CGContextDrawImage(cg, CGRectMake(0, 0, info.width(), info.height()), image);
CGContextRelease(cg);
return true;
}
bool SkCreateBitmapFromCGImage(SkBitmap* dst, CGImageRef image, SkISize* scaleToFit) {
const int width = scaleToFit ? scaleToFit->width() : SkToInt(CGImageGetWidth(image));
const int height = scaleToFit ? scaleToFit->height() : SkToInt(CGImageGetHeight(image));
SkImageInfo info = SkImageInfo::MakeN32Premul(width, height);
SkBitmap tmp;
if (!tmp.tryAllocPixels(info)) {
return false;
}
if (!SkCopyPixelsFromCGImage(tmp.info(), tmp.rowBytes(), tmp.getPixels(), image)) {
return false;
}
CGImageAlphaInfo cgInfo = CGImageGetAlphaInfo(image);
switch (cgInfo) {
case kCGImageAlphaNone:
case kCGImageAlphaNoneSkipLast:
case kCGImageAlphaNoneSkipFirst:
SkASSERT(SkBitmap::ComputeIsOpaque(tmp));
tmp.setAlphaType(kOpaque_SkAlphaType);
break;
default:
// we don't know if we're opaque or not, so compute it.
if (SkBitmap::ComputeIsOpaque(tmp)) {
tmp.setAlphaType(kOpaque_SkAlphaType);
}
}
*dst = tmp;
return true;
}
#endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS)
|