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 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 return false; | 94 return false; |
95 } | 95 } |
96 return true; | 96 return true; |
97 } | 97 } |
98 | 98 |
99 static SkBitmap* prepareForImageRef(const SkBitmap& bm, | 99 static SkBitmap* prepareForImageRef(const SkBitmap& bm, |
100 size_t* bitsPerComponent, | 100 size_t* bitsPerComponent, |
101 CGBitmapInfo* info) { | 101 CGBitmapInfo* info) { |
102 bool upscaleTo32; | 102 bool upscaleTo32; |
103 if (!getBitmapInfo(bm, bitsPerComponent, info, &upscaleTo32)) { | 103 if (!getBitmapInfo(bm, bitsPerComponent, info, &upscaleTo32)) { |
104 return NULL; | 104 return nullptr; |
105 } | 105 } |
106 | 106 |
107 SkBitmap* copy; | 107 SkBitmap* copy; |
108 if (upscaleTo32) { | 108 if (upscaleTo32) { |
109 copy = new SkBitmap; | 109 copy = new SkBitmap; |
110 // here we make a ceep copy of the pixels, since CG won't take our | 110 // here we make a ceep copy of the pixels, since CG won't take our |
111 // 565 directly | 111 // 565 directly |
112 bm.copyTo(copy, kN32_SkColorType); | 112 bm.copyTo(copy, kN32_SkColorType); |
113 } else { | 113 } else { |
114 copy = new SkBitmap(bm); | 114 copy = new SkBitmap(bm); |
115 } | 115 } |
116 return copy; | 116 return copy; |
117 } | 117 } |
118 | 118 |
119 CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm, | 119 CGImageRef SkCreateCGImageRefWithColorspace(const SkBitmap& bm, |
120 CGColorSpaceRef colorSpace) { | 120 CGColorSpaceRef colorSpace) { |
121 size_t bitsPerComponent SK_INIT_TO_AVOID_WARNING; | 121 size_t bitsPerComponent SK_INIT_TO_AVOID_WARNING; |
122 CGBitmapInfo info SK_INIT_TO_AVOID_WARNING; | 122 CGBitmapInfo info SK_INIT_TO_AVOID_WARNING; |
123 | 123 |
124 SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info); | 124 SkBitmap* bitmap = prepareForImageRef(bm, &bitsPerComponent, &info); |
125 if (NULL == bitmap) { | 125 if (nullptr == bitmap) { |
126 return NULL; | 126 return nullptr; |
127 } | 127 } |
128 | 128 |
129 const int w = bitmap->width(); | 129 const int w = bitmap->width(); |
130 const int h = bitmap->height(); | 130 const int h = bitmap->height(); |
131 const size_t s = bitmap->getSize(); | 131 const size_t s = bitmap->getSize(); |
132 | 132 |
133 // our provider "owns" the bitmap*, and will take care of deleting it | 133 // our provider "owns" the bitmap*, and will take care of deleting it |
134 // we initially lock it, so we can access the pixels. The bitmap will be del
eted in the release | 134 // we initially lock it, so we can access the pixels. The bitmap will be del
eted in the release |
135 // proc, which will in turn unlock the pixels | 135 // proc, which will in turn unlock the pixels |
136 bitmap->lockPixels(); | 136 bitmap->lockPixels(); |
137 CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->get
Pixels(), s, | 137 CGDataProviderRef dataRef = CGDataProviderCreateWithData(bitmap, bitmap->get
Pixels(), s, |
138 SkBitmap_ReleaseInf
o); | 138 SkBitmap_ReleaseInf
o); |
139 | 139 |
140 bool releaseColorSpace = false; | 140 bool releaseColorSpace = false; |
141 if (NULL == colorSpace) { | 141 if (nullptr == colorSpace) { |
142 colorSpace = CGColorSpaceCreateDeviceRGB(); | 142 colorSpace = CGColorSpaceCreateDeviceRGB(); |
143 releaseColorSpace = true; | 143 releaseColorSpace = true; |
144 } | 144 } |
145 | 145 |
146 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent, | 146 CGImageRef ref = CGImageCreate(w, h, bitsPerComponent, |
147 bitmap->bytesPerPixel() * 8, | 147 bitmap->bytesPerPixel() * 8, |
148 bitmap->rowBytes(), colorSpace, info, dataRef
, | 148 bitmap->rowBytes(), colorSpace, info, dataRef
, |
149 NULL, false, kCGRenderingIntentDefault); | 149 nullptr, false, kCGRenderingIntentDefault); |
150 | 150 |
151 if (releaseColorSpace) { | 151 if (releaseColorSpace) { |
152 CGColorSpaceRelease(colorSpace); | 152 CGColorSpaceRelease(colorSpace); |
153 } | 153 } |
154 CGDataProviderRelease(dataRef); | 154 CGDataProviderRelease(dataRef); |
155 return ref; | 155 return ref; |
156 } | 156 } |
157 | 157 |
158 void SkCGDrawBitmap(CGContextRef cg, const SkBitmap& bm, float x, float y) { | 158 void SkCGDrawBitmap(CGContextRef cg, const SkBitmap& bm, float x, float y) { |
159 CGImageRef img = SkCreateCGImageRef(bm); | 159 CGImageRef img = SkCreateCGImageRef(bm); |
(...skipping 25 matching lines...) Expand all Loading... |
185 CGPDFDocumentRelease(fDoc); | 185 CGPDFDocumentRelease(fDoc); |
186 } | 186 } |
187 } | 187 } |
188 private: | 188 private: |
189 CGPDFDocumentRef fDoc; | 189 CGPDFDocumentRef fDoc; |
190 }; | 190 }; |
191 #define SkAutoPDFRelease(...) SK_REQUIRE_LOCAL_VAR(SkAutoPDFRelease) | 191 #define SkAutoPDFRelease(...) SK_REQUIRE_LOCAL_VAR(SkAutoPDFRelease) |
192 | 192 |
193 bool SkPDFDocumentToBitmap(SkStream* stream, SkBitmap* output) { | 193 bool SkPDFDocumentToBitmap(SkStream* stream, SkBitmap* output) { |
194 CGDataProviderRef data = SkCreateDataProviderFromStream(stream); | 194 CGDataProviderRef data = SkCreateDataProviderFromStream(stream); |
195 if (NULL == data) { | 195 if (nullptr == data) { |
196 return false; | 196 return false; |
197 } | 197 } |
198 | 198 |
199 CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(data); | 199 CGPDFDocumentRef pdf = CGPDFDocumentCreateWithProvider(data); |
200 CGDataProviderRelease(data); | 200 CGDataProviderRelease(data); |
201 if (NULL == pdf) { | 201 if (nullptr == pdf) { |
202 return false; | 202 return false; |
203 } | 203 } |
204 SkAutoPDFRelease releaseMe(pdf); | 204 SkAutoPDFRelease releaseMe(pdf); |
205 | 205 |
206 CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1); | 206 CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 1); |
207 if (NULL == page) { | 207 if (nullptr == page) { |
208 return false; | 208 return false; |
209 } | 209 } |
210 | 210 |
211 CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); | 211 CGRect bounds = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); |
212 | 212 |
213 int w = (int)CGRectGetWidth(bounds); | 213 int w = (int)CGRectGetWidth(bounds); |
214 int h = (int)CGRectGetHeight(bounds); | 214 int h = (int)CGRectGetHeight(bounds); |
215 | 215 |
216 SkBitmap bitmap; | 216 SkBitmap bitmap; |
217 if (!bitmap.tryAllocN32Pixels(w, h)) { | 217 if (!bitmap.tryAllocN32Pixels(w, h)) { |
218 return false; | 218 return false; |
219 } | 219 } |
220 bitmap.eraseColor(SK_ColorWHITE); | 220 bitmap.eraseColor(SK_ColorWHITE); |
221 | 221 |
222 size_t bitsPerComponent; | 222 size_t bitsPerComponent; |
223 CGBitmapInfo info; | 223 CGBitmapInfo info; |
224 getBitmapInfo(bitmap, &bitsPerComponent, &info, NULL); | 224 getBitmapInfo(bitmap, &bitsPerComponent, &info, nullptr); |
225 | 225 |
226 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); | 226 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); |
227 CGContextRef ctx = CGBitmapContextCreate(bitmap.getPixels(), w, h, | 227 CGContextRef ctx = CGBitmapContextCreate(bitmap.getPixels(), w, h, |
228 bitsPerComponent, bitmap.rowBytes()
, | 228 bitsPerComponent, bitmap.rowBytes()
, |
229 cs, info); | 229 cs, info); |
230 CGColorSpaceRelease(cs); | 230 CGColorSpaceRelease(cs); |
231 | 231 |
232 if (ctx) { | 232 if (ctx) { |
233 CGContextDrawPDFPage(ctx, page); | 233 CGContextDrawPDFPage(ctx, page); |
234 CGContextRelease(ctx); | 234 CGContextRelease(ctx); |
(...skipping 19 matching lines...) Expand all Loading... |
254 cg_bitmap_info = ComputeCGAlphaInfo_BGRA(info.alphaType()); | 254 cg_bitmap_info = ComputeCGAlphaInfo_BGRA(info.alphaType()); |
255 break; | 255 break; |
256 default: | 256 default: |
257 return false; // no other colortypes are supported (for now) | 257 return false; // no other colortypes are supported (for now) |
258 } | 258 } |
259 | 259 |
260 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); | 260 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB(); |
261 CGContextRef cg = CGBitmapContextCreate(pixels, info.width(), info.height(),
bitsPerComponent, | 261 CGContextRef cg = CGBitmapContextCreate(pixels, info.width(), info.height(),
bitsPerComponent, |
262 rowBytes, cs, cg_bitmap_info); | 262 rowBytes, cs, cg_bitmap_info); |
263 CFRelease(cs); | 263 CFRelease(cs); |
264 if (NULL == cg) { | 264 if (nullptr == cg) { |
265 return false; | 265 return false; |
266 } | 266 } |
267 | 267 |
268 // use this blend mode, to avoid having to erase the pixels first, and to av
oid CG performing | 268 // use this blend mode, to avoid having to erase the pixels first, and to av
oid CG performing |
269 // any blending (which could introduce errors and be slower). | 269 // any blending (which could introduce errors and be slower). |
270 CGContextSetBlendMode(cg, kCGBlendModeCopy); | 270 CGContextSetBlendMode(cg, kCGBlendModeCopy); |
271 | 271 |
272 CGContextDrawImage(cg, CGRectMake(0, 0, info.width(), info.height()), image)
; | 272 CGContextDrawImage(cg, CGRectMake(0, 0, info.width(), info.height()), image)
; |
273 CGContextRelease(cg); | 273 CGContextRelease(cg); |
274 return true; | 274 return true; |
(...skipping 24 matching lines...) Expand all Loading... |
299 default: | 299 default: |
300 // we don't know if we're opaque or not, so compute it. | 300 // we don't know if we're opaque or not, so compute it. |
301 if (SkBitmap::ComputeIsOpaque(tmp)) { | 301 if (SkBitmap::ComputeIsOpaque(tmp)) { |
302 tmp.setAlphaType(kOpaque_SkAlphaType); | 302 tmp.setAlphaType(kOpaque_SkAlphaType); |
303 } | 303 } |
304 } | 304 } |
305 | 305 |
306 *dst = tmp; | 306 *dst = tmp; |
307 return true; | 307 return true; |
308 } | 308 } |
OLD | NEW |