OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2008 The Android Open Source Project | 2 * Copyright 2008 The Android Open Source Project |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkCGUtils.h" | 8 #include "SkCGUtils.h" |
9 #include "SkColorPriv.h" | 9 #include "SkColorPriv.h" |
10 #include "SkImageDecoder.h" | 10 #include "SkImageDecoder.h" |
(...skipping 23 matching lines...) Expand all Loading... |
34 SkAutoMalloc storage; | 34 SkAutoMalloc storage; |
35 const size_t len = SkCopyStreamToStorage(&storage, stream); | 35 const size_t len = SkCopyStreamToStorage(&storage, stream); |
36 void* data = storage.detach(); | 36 void* data = storage.detach(); |
37 | 37 |
38 return CGDataProviderCreateWithData(data, data, len, malloc_release_proc); | 38 return CGDataProviderCreateWithData(data, data, len, malloc_release_proc); |
39 } | 39 } |
40 | 40 |
41 static CGImageSourceRef SkStreamToCGImageSource(SkStream* stream) { | 41 static CGImageSourceRef SkStreamToCGImageSource(SkStream* stream) { |
42 CGDataProviderRef data = SkStreamToDataProvider(stream); | 42 CGDataProviderRef data = SkStreamToDataProvider(stream); |
43 if (!data) { | 43 if (!data) { |
44 return NULL; | 44 return nullptr; |
45 } | 45 } |
46 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(data, 0); | 46 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(data, 0); |
47 CGDataProviderRelease(data); | 47 CGDataProviderRelease(data); |
48 return imageSrc; | 48 return imageSrc; |
49 } | 49 } |
50 | 50 |
51 class SkImageDecoder_CG : public SkImageDecoder { | 51 class SkImageDecoder_CG : public SkImageDecoder { |
52 protected: | 52 protected: |
53 virtual Result onDecode(SkStream* stream, SkBitmap* bm, Mode); | 53 virtual Result onDecode(SkStream* stream, SkBitmap* bm, Mode); |
54 }; | 54 }; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 return !memcmp(CFDataGetBytePtr(data) + ICC_PROFILE_OFFSET_TO_SRGB_T
AG, "sRGB", 4); | 127 return !memcmp(CFDataGetBytePtr(data) + ICC_PROFILE_OFFSET_TO_SRGB_T
AG, "sRGB", 4); |
128 } | 128 } |
129 } | 129 } |
130 return false; | 130 return false; |
131 #endif | 131 #endif |
132 } | 132 } |
133 | 133 |
134 SkImageDecoder::Result SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* b
m, Mode mode) { | 134 SkImageDecoder::Result SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* b
m, Mode mode) { |
135 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); | 135 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); |
136 | 136 |
137 if (NULL == imageSrc) { | 137 if (nullptr == imageSrc) { |
138 return kFailure; | 138 return kFailure; |
139 } | 139 } |
140 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); | 140 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); |
141 | 141 |
142 CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, NULL); | 142 CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, nullptr); |
143 if (NULL == image) { | 143 if (nullptr == image) { |
144 return kFailure; | 144 return kFailure; |
145 } | 145 } |
146 SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image); | 146 SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image); |
147 | 147 |
148 const int width = SkToInt(CGImageGetWidth(image)); | 148 const int width = SkToInt(CGImageGetWidth(image)); |
149 const int height = SkToInt(CGImageGetHeight(image)); | 149 const int height = SkToInt(CGImageGetHeight(image)); |
150 SkColorProfileType cpType = kLinear_SkColorProfileType; | 150 SkColorProfileType cpType = kLinear_SkColorProfileType; |
151 | 151 |
152 CGColorSpaceRef cs = CGImageGetColorSpace(image); | 152 CGColorSpaceRef cs = CGImageGetColorSpace(image); |
153 if (cs) { | 153 if (cs) { |
(...skipping 12 matching lines...) Expand all Loading... |
166 break; | 166 break; |
167 default: | 167 default: |
168 break; | 168 break; |
169 } | 169 } |
170 | 170 |
171 bm->setInfo(SkImageInfo::Make(width, height, kN32_SkColorType, at, cpType)); | 171 bm->setInfo(SkImageInfo::Make(width, height, kN32_SkColorType, at, cpType)); |
172 if (SkImageDecoder::kDecodeBounds_Mode == mode) { | 172 if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
173 return kSuccess; | 173 return kSuccess; |
174 } | 174 } |
175 | 175 |
176 if (!this->allocPixelRef(bm, NULL)) { | 176 if (!this->allocPixelRef(bm, nullptr)) { |
177 return kFailure; | 177 return kFailure; |
178 } | 178 } |
179 | 179 |
180 SkAutoLockPixels alp(*bm); | 180 SkAutoLockPixels alp(*bm); |
181 | 181 |
182 if (!SkCopyPixelsFromCGImage(bm->info(), bm->rowBytes(), bm->getPixels(), im
age)) { | 182 if (!SkCopyPixelsFromCGImage(bm->info(), bm->rowBytes(), bm->getPixels(), im
age)) { |
183 return kFailure; | 183 return kFailure; |
184 } | 184 } |
185 | 185 |
186 CGImageAlphaInfo info = CGImageGetAlphaInfo(image); | 186 CGImageAlphaInfo info = CGImageGetAlphaInfo(image); |
(...skipping 25 matching lines...) Expand all Loading... |
212 } | 212 } |
213 return kSuccess; | 213 return kSuccess; |
214 } | 214 } |
215 | 215 |
216 /////////////////////////////////////////////////////////////////////////////// | 216 /////////////////////////////////////////////////////////////////////////////// |
217 | 217 |
218 extern SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*); | 218 extern SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*); |
219 | 219 |
220 SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) { | 220 SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) { |
221 SkImageDecoder* decoder = image_decoder_from_stream(stream); | 221 SkImageDecoder* decoder = image_decoder_from_stream(stream); |
222 if (NULL == decoder) { | 222 if (nullptr == decoder) { |
223 // If no image decoder specific to the stream exists, use SkImageDecoder
_CG. | 223 // If no image decoder specific to the stream exists, use SkImageDecoder
_CG. |
224 return new SkImageDecoder_CG; | 224 return new SkImageDecoder_CG; |
225 } else { | 225 } else { |
226 return decoder; | 226 return decoder; |
227 } | 227 } |
228 } | 228 } |
229 | 229 |
230 ///////////////////////////////////////////////////////////////////////// | 230 ///////////////////////////////////////////////////////////////////////// |
231 | 231 |
232 SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) { | 232 SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) { |
233 return NULL; | 233 return nullptr; |
234 } | 234 } |
235 | 235 |
236 ///////////////////////////////////////////////////////////////////////// | 236 ///////////////////////////////////////////////////////////////////////// |
237 | 237 |
238 static size_t consumer_put(void* info, const void* buffer, size_t count) { | 238 static size_t consumer_put(void* info, const void* buffer, size_t count) { |
239 SkWStream* stream = reinterpret_cast<SkWStream*>(info); | 239 SkWStream* stream = reinterpret_cast<SkWStream*>(info); |
240 return stream->write(buffer, count) ? count : 0; | 240 return stream->write(buffer, count) ? count : 0; |
241 } | 241 } |
242 | 242 |
243 static void consumer_release(void* info) { | 243 static void consumer_release(void* info) { |
244 // we do nothing, since by design we don't "own" the stream (i.e. info) | 244 // we do nothing, since by design we don't "own" the stream (i.e. info) |
245 } | 245 } |
246 | 246 |
247 static CGDataConsumerRef SkStreamToCGDataConsumer(SkWStream* stream) { | 247 static CGDataConsumerRef SkStreamToCGDataConsumer(SkWStream* stream) { |
248 CGDataConsumerCallbacks procs; | 248 CGDataConsumerCallbacks procs; |
249 procs.putBytes = consumer_put; | 249 procs.putBytes = consumer_put; |
250 procs.releaseConsumer = consumer_release; | 250 procs.releaseConsumer = consumer_release; |
251 // we don't own/reference the stream, so it our consumer must not live | 251 // we don't own/reference the stream, so it our consumer must not live |
252 // longer that our caller's ownership of the stream | 252 // longer that our caller's ownership of the stream |
253 return CGDataConsumerCreate(stream, &procs); | 253 return CGDataConsumerCreate(stream, &procs); |
254 } | 254 } |
255 | 255 |
256 static CGImageDestinationRef SkStreamToImageDestination(SkWStream* stream, | 256 static CGImageDestinationRef SkStreamToImageDestination(SkWStream* stream, |
257 CFStringRef type) { | 257 CFStringRef type) { |
258 CGDataConsumerRef consumer = SkStreamToCGDataConsumer(stream); | 258 CGDataConsumerRef consumer = SkStreamToCGDataConsumer(stream); |
259 if (NULL == consumer) { | 259 if (nullptr == consumer) { |
260 return NULL; | 260 return nullptr; |
261 } | 261 } |
262 SkAutoTCallVProc<const void, CFRelease> arconsumer(consumer); | 262 SkAutoTCallVProc<const void, CFRelease> arconsumer(consumer); |
263 | 263 |
264 return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, NULL); | 264 return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, nullptr); |
265 } | 265 } |
266 | 266 |
267 class SkImageEncoder_CG : public SkImageEncoder { | 267 class SkImageEncoder_CG : public SkImageEncoder { |
268 public: | 268 public: |
269 SkImageEncoder_CG(Type t) : fType(t) {} | 269 SkImageEncoder_CG(Type t) : fType(t) {} |
270 | 270 |
271 protected: | 271 protected: |
272 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality); | 272 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality); |
273 | 273 |
274 private: | 274 private: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 bm.copyTo(&bitmap8888, kN32_SkColorType); | 309 bm.copyTo(&bitmap8888, kN32_SkColorType); |
310 bmPtr = &bitmap8888; | 310 bmPtr = &bitmap8888; |
311 } | 311 } |
312 type = kUTTypePNG; | 312 type = kUTTypePNG; |
313 break; | 313 break; |
314 default: | 314 default: |
315 return false; | 315 return false; |
316 } | 316 } |
317 | 317 |
318 CGImageDestinationRef dst = SkStreamToImageDestination(stream, type); | 318 CGImageDestinationRef dst = SkStreamToImageDestination(stream, type); |
319 if (NULL == dst) { | 319 if (nullptr == dst) { |
320 return false; | 320 return false; |
321 } | 321 } |
322 SkAutoTCallVProc<const void, CFRelease> ardst(dst); | 322 SkAutoTCallVProc<const void, CFRelease> ardst(dst); |
323 | 323 |
324 CGImageRef image = SkCreateCGImageRef(*bmPtr); | 324 CGImageRef image = SkCreateCGImageRef(*bmPtr); |
325 if (NULL == image) { | 325 if (nullptr == image) { |
326 return false; | 326 return false; |
327 } | 327 } |
328 SkAutoTCallVProc<CGImage, CGImageRelease> agimage(image); | 328 SkAutoTCallVProc<CGImage, CGImageRelease> agimage(image); |
329 | 329 |
330 CGImageDestinationAddImage(dst, image, NULL); | 330 CGImageDestinationAddImage(dst, image, nullptr); |
331 return CGImageDestinationFinalize(dst); | 331 return CGImageDestinationFinalize(dst); |
332 } | 332 } |
333 | 333 |
334 /////////////////////////////////////////////////////////////////////////////// | 334 /////////////////////////////////////////////////////////////////////////////// |
335 | 335 |
336 static SkImageEncoder* sk_imageencoder_cg_factory(SkImageEncoder::Type t) { | 336 static SkImageEncoder* sk_imageencoder_cg_factory(SkImageEncoder::Type t) { |
337 switch (t) { | 337 switch (t) { |
338 case SkImageEncoder::kICO_Type: | 338 case SkImageEncoder::kICO_Type: |
339 case SkImageEncoder::kBMP_Type: | 339 case SkImageEncoder::kBMP_Type: |
340 case SkImageEncoder::kGIF_Type: | 340 case SkImageEncoder::kGIF_Type: |
341 case SkImageEncoder::kJPEG_Type: | 341 case SkImageEncoder::kJPEG_Type: |
342 case SkImageEncoder::kPNG_Type: | 342 case SkImageEncoder::kPNG_Type: |
343 break; | 343 break; |
344 default: | 344 default: |
345 return NULL; | 345 return nullptr; |
346 } | 346 } |
347 return new SkImageEncoder_CG(t); | 347 return new SkImageEncoder_CG(t); |
348 } | 348 } |
349 | 349 |
350 static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_cg_factory); | 350 static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_cg_factory); |
351 | 351 |
352 #ifdef SK_BUILD_FOR_IOS | 352 #ifdef SK_BUILD_FOR_IOS |
353 class SkPNGImageEncoder_IOS : public SkImageEncoder_CG { | 353 class SkPNGImageEncoder_IOS : public SkImageEncoder_CG { |
354 public: | 354 public: |
355 SkPNGImageEncoder_IOS() | 355 SkPNGImageEncoder_IOS() |
(...skipping 25 matching lines...) Expand all Loading... |
381 if (CFStringCompare(uttype, gFormatConversions[i].fUTType, 0) == kCFComp
areEqualTo) { | 381 if (CFStringCompare(uttype, gFormatConversions[i].fUTType, 0) == kCFComp
areEqualTo) { |
382 return gFormatConversions[i].fFormat; | 382 return gFormatConversions[i].fFormat; |
383 } | 383 } |
384 } | 384 } |
385 return SkImageDecoder::kUnknown_Format; | 385 return SkImageDecoder::kUnknown_Format; |
386 } | 386 } |
387 | 387 |
388 static SkImageDecoder::Format get_format_cg(SkStreamRewindable* stream) { | 388 static SkImageDecoder::Format get_format_cg(SkStreamRewindable* stream) { |
389 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); | 389 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); |
390 | 390 |
391 if (NULL == imageSrc) { | 391 if (nullptr == imageSrc) { |
392 return SkImageDecoder::kUnknown_Format; | 392 return SkImageDecoder::kUnknown_Format; |
393 } | 393 } |
394 | 394 |
395 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); | 395 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); |
396 const CFStringRef name = CGImageSourceGetType(imageSrc); | 396 const CFStringRef name = CGImageSourceGetType(imageSrc); |
397 if (NULL == name) { | 397 if (nullptr == name) { |
398 return SkImageDecoder::kUnknown_Format; | 398 return SkImageDecoder::kUnknown_Format; |
399 } | 399 } |
400 return UTType_to_Format(name); | 400 return UTType_to_Format(name); |
401 } | 401 } |
402 | 402 |
403 static SkImageDecoder_FormatReg gFormatReg(get_format_cg); | 403 static SkImageDecoder_FormatReg gFormatReg(get_format_cg); |
OLD | NEW |