blob: ba16e4b45c0e53f8e952a5713ae497d0d22b5d4f [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2008 The Android Open Source Project
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
scroggo@google.comdaaea2d2013-06-14 20:39:48 +00008#include "SkCGUtils.h"
reed@google.com2a7579d2012-11-07 18:30:18 +00009#include "SkColorPriv.h"
reed@android.com0767e472008-12-23 16:06:51 +000010#include "SkImageDecoder.h"
reed@android.comb08eb2b2009-01-06 20:16:26 +000011#include "SkImageEncoder.h"
reed@android.com0767e472008-12-23 16:06:51 +000012#include "SkMovie.h"
13#include "SkStream.h"
halcanary67ec1f82014-06-27 11:36:20 -070014#include "SkStreamPriv.h"
reed@android.com0767e472008-12-23 16:06:51 +000015#include "SkTemplates.h"
scroggo@google.comdaaea2d2013-06-14 20:39:48 +000016#include "SkUnPreMultiply.h"
reed@android.com0767e472008-12-23 16:06:51 +000017
yangsu@google.com900d8772011-06-24 18:56:00 +000018#ifdef SK_BUILD_FOR_MAC
19#include <ApplicationServices/ApplicationServices.h>
20#endif
21
22#ifdef SK_BUILD_FOR_IOS
23#include <CoreGraphics/CoreGraphics.h>
caryclark@google.com35f5ac92012-09-18 15:41:18 +000024#include <ImageIO/ImageIO.h>
caryclark@google.com594dd3c2012-09-24 19:33:57 +000025#include <MobileCoreServices/MobileCoreServices.h>
yangsu@google.com900d8772011-06-24 18:56:00 +000026#endif
yangsu@google.comc134f392011-06-23 22:27:30 +000027
reed@android.com0767e472008-12-23 16:06:51 +000028static void malloc_release_proc(void* info, const void* data, size_t size) {
29 sk_free(info);
30}
31
32static CGDataProviderRef SkStreamToDataProvider(SkStream* stream) {
33 // TODO: use callbacks, so we don't have to load all the data into RAM
scroggo@google.com1a2e71d2013-08-28 19:13:13 +000034 SkAutoMalloc storage;
halcanary67ec1f82014-06-27 11:36:20 -070035 const size_t len = SkCopyStreamToStorage(&storage, stream);
scroggo@google.com1a2e71d2013-08-28 19:13:13 +000036 void* data = storage.detach();
rmistry@google.comd6176b02012-08-23 18:14:13 +000037
reed@android.com0767e472008-12-23 16:06:51 +000038 return CGDataProviderCreateWithData(data, data, len, malloc_release_proc);
39}
40
41static CGImageSourceRef SkStreamToCGImageSource(SkStream* stream) {
42 CGDataProviderRef data = SkStreamToDataProvider(stream);
scroggo3ceef9a2014-10-24 06:55:07 -070043 if (!data) {
44 return NULL;
45 }
reed@android.com0767e472008-12-23 16:06:51 +000046 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(data, 0);
47 CGDataProviderRelease(data);
48 return imageSrc;
49}
50
51class SkImageDecoder_CG : public SkImageDecoder {
52protected:
scroggo2a120802014-10-22 12:07:00 -070053 virtual Result onDecode(SkStream* stream, SkBitmap* bm, Mode);
reed@android.com0767e472008-12-23 16:06:51 +000054};
55
reed935a9f62014-06-30 14:06:00 -070056static void argb_4444_force_opaque(void* row, int count) {
57 uint16_t* row16 = (uint16_t*)row;
58 for (int i = 0; i < count; ++i) {
59 row16[i] |= 0xF000;
60 }
61}
62
63static void argb_8888_force_opaque(void* row, int count) {
64 // can use RGBA or BGRA, they have the same shift for alpha
65 const uint32_t alphaMask = 0xFF << SK_RGBA_A32_SHIFT;
66 uint32_t* row32 = (uint32_t*)row;
67 for (int i = 0; i < count; ++i) {
68 row32[i] |= alphaMask;
69 }
70}
71
72static void alpha_8_force_opaque(void* row, int count) {
73 memset(row, 0xFF, count);
74}
75
76static void force_opaque(SkBitmap* bm) {
77 SkAutoLockPixels alp(*bm);
78 if (!bm->getPixels()) {
79 return;
80 }
81
82 void (*proc)(void*, int);
83 switch (bm->colorType()) {
84 case kARGB_4444_SkColorType:
85 proc = argb_4444_force_opaque;
86 break;
87 case kRGBA_8888_SkColorType:
88 case kBGRA_8888_SkColorType:
89 proc = argb_8888_force_opaque;
90 break;
91 case kAlpha_8_SkColorType:
92 proc = alpha_8_force_opaque;
93 break;
94 default:
95 return;
96 }
97
98 char* row = (char*)bm->getPixels();
99 for (int y = 0; y < bm->height(); ++y) {
100 proc(row, bm->width());
101 row += bm->rowBytes();
102 }
103 bm->setAlphaType(kOpaque_SkAlphaType);
104}
105
reed@android.com0767e472008-12-23 16:06:51 +0000106#define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast)
107
reedddbc4912014-11-11 07:27:25 -0800108class AutoCFDataRelease {
109 CFDataRef fDR;
110public:
111 AutoCFDataRelease(CFDataRef dr) : fDR(dr) {}
112 ~AutoCFDataRelease() { if (fDR) { CFRelease(fDR); } }
reed7c748852014-11-10 08:57:21 -0800113
reedddbc4912014-11-11 07:27:25 -0800114 operator CFDataRef () { return fDR; }
115};
116
117static bool colorspace_is_sRGB(CGColorSpaceRef cs) {
118#ifdef SK_BUILD_FOR_IOS
119 return true; // iOS seems to define itself to always return sRGB <reed>
120#else
121 AutoCFDataRelease data(CGColorSpaceCopyICCProfile(cs));
122 if (data) {
123 // found by inspection -- need a cleaner way to sniff a profile
124 const CFIndex ICC_PROFILE_OFFSET_TO_SRGB_TAG = 52;
125
126 if (CFDataGetLength(data) >= ICC_PROFILE_OFFSET_TO_SRGB_TAG + 4) {
127 return !memcmp(CFDataGetBytePtr(data) + ICC_PROFILE_OFFSET_TO_SRGB_TAG, "sRGB", 4);
128 }
reed7c748852014-11-10 08:57:21 -0800129 }
130 return false;
reedddbc4912014-11-11 07:27:25 -0800131#endif
reed7c748852014-11-10 08:57:21 -0800132}
133
scroggo2a120802014-10-22 12:07:00 -0700134SkImageDecoder::Result SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
reed@android.com0767e472008-12-23 16:06:51 +0000135 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream);
136
137 if (NULL == imageSrc) {
scroggo2a120802014-10-22 12:07:00 -0700138 return kFailure;
reed@android.com0767e472008-12-23 16:06:51 +0000139 }
reed@android.com0767e472008-12-23 16:06:51 +0000140 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141
reed@android.com0767e472008-12-23 16:06:51 +0000142 CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, NULL);
143 if (NULL == image) {
scroggo2a120802014-10-22 12:07:00 -0700144 return kFailure;
reed@android.com0767e472008-12-23 16:06:51 +0000145 }
reed@android.com0767e472008-12-23 16:06:51 +0000146 SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000147
reed@google.com7fa2a652014-01-27 13:42:58 +0000148 const int width = SkToInt(CGImageGetWidth(image));
149 const int height = SkToInt(CGImageGetHeight(image));
reed7c748852014-11-10 08:57:21 -0800150 SkColorProfileType cpType = kLinear_SkColorProfileType;
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000151
reed7c748852014-11-10 08:57:21 -0800152 CGColorSpaceRef cs = CGImageGetColorSpace(image);
153 if (cs) {
154 CGColorSpaceModel m = CGColorSpaceGetModel(cs);
reedddbc4912014-11-11 07:27:25 -0800155 if (kCGColorSpaceModelRGB == m && colorspace_is_sRGB(cs)) {
156 cpType = kSRGB_SkColorProfileType;
reed7c748852014-11-10 08:57:21 -0800157 }
158 }
159
reed871872f2015-06-22 12:48:26 -0700160 SkAlphaType at = kPremul_SkAlphaType;
161 switch (CGImageGetAlphaInfo(image)) {
162 case kCGImageAlphaNone:
163 case kCGImageAlphaNoneSkipLast:
164 case kCGImageAlphaNoneSkipFirst:
165 at = kOpaque_SkAlphaType;
166 break;
167 default:
168 break;
169 }
170
171 bm->setInfo(SkImageInfo::Make(width, height, kN32_SkColorType, at, cpType));
reed@android.com0767e472008-12-23 16:06:51 +0000172 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
scroggo2a120802014-10-22 12:07:00 -0700173 return kSuccess;
reed@android.com0767e472008-12-23 16:06:51 +0000174 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000175
reed@android.com0767e472008-12-23 16:06:51 +0000176 if (!this->allocPixelRef(bm, NULL)) {
scroggo2a120802014-10-22 12:07:00 -0700177 return kFailure;
reed@android.com0767e472008-12-23 16:06:51 +0000178 }
skia.committer@gmail.comede0c5c2014-04-23 03:04:11 +0000179
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000180 SkAutoLockPixels alp(*bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000181
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000182 if (!SkCopyPixelsFromCGImage(bm->info(), bm->rowBytes(), bm->getPixels(), image)) {
scroggo2a120802014-10-22 12:07:00 -0700183 return kFailure;
commit-bot@chromium.org60b5dce2014-04-22 20:24:33 +0000184 }
reed@android.com0767e472008-12-23 16:06:51 +0000185
reed@google.com52e91bf2012-11-07 22:06:55 +0000186 CGImageAlphaInfo info = CGImageGetAlphaInfo(image);
187 switch (info) {
188 case kCGImageAlphaNone:
189 case kCGImageAlphaNoneSkipLast:
190 case kCGImageAlphaNoneSkipFirst:
reed935a9f62014-06-30 14:06:00 -0700191 // We're opaque, but we can't rely on the data always having 0xFF
192 // in the alpha slot (which Skia wants), so we have to ram it in
193 // ourselves.
194 force_opaque(bm);
reed@google.com52e91bf2012-11-07 22:06:55 +0000195 break;
196 default:
197 // we don't know if we're opaque or not, so compute it.
reed@google.com383a6972013-10-21 14:00:07 +0000198 if (SkBitmap::ComputeIsOpaque(*bm)) {
199 bm->setAlphaType(kOpaque_SkAlphaType);
200 }
reed@google.com52e91bf2012-11-07 22:06:55 +0000201 }
scroggo@google.comdaaea2d2013-06-14 20:39:48 +0000202 if (!bm->isOpaque() && this->getRequireUnpremultipliedColors()) {
203 // CGBitmapContext does not support unpremultiplied, so the image has been premultiplied.
204 // Convert to unpremultiplied.
205 for (int i = 0; i < width; ++i) {
206 for (int j = 0; j < height; ++j) {
207 uint32_t* addr = bm->getAddr32(i, j);
commit-bot@chromium.orgb06faac2014-01-15 22:24:58 +0000208 *addr = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*addr);
scroggo@google.comdaaea2d2013-06-14 20:39:48 +0000209 }
210 }
reed@google.com383a6972013-10-21 14:00:07 +0000211 bm->setAlphaType(kUnpremul_SkAlphaType);
scroggo@google.comdaaea2d2013-06-14 20:39:48 +0000212 }
scroggo2a120802014-10-22 12:07:00 -0700213 return kSuccess;
reed@android.com0767e472008-12-23 16:06:51 +0000214}
215
216///////////////////////////////////////////////////////////////////////////////
217
scroggo@google.comb5571b32013-09-25 21:34:24 +0000218extern SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*);
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000219
scroggo@google.comb5571b32013-09-25 21:34:24 +0000220SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) {
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000221 SkImageDecoder* decoder = image_decoder_from_stream(stream);
222 if (NULL == decoder) {
223 // If no image decoder specific to the stream exists, use SkImageDecoder_CG.
224 return SkNEW(SkImageDecoder_CG);
225 } else {
226 return decoder;
227 }
reed@android.com0767e472008-12-23 16:06:51 +0000228}
229
reed@android.com0767e472008-12-23 16:06:51 +0000230/////////////////////////////////////////////////////////////////////////
231
scroggo@google.comb5571b32013-09-25 21:34:24 +0000232SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) {
reed@android.com0767e472008-12-23 16:06:51 +0000233 return NULL;
234}
235
236/////////////////////////////////////////////////////////////////////////
237
reed@android.com0ae6b242008-12-23 16:49:54 +0000238static size_t consumer_put(void* info, const void* buffer, size_t count) {
239 SkWStream* stream = reinterpret_cast<SkWStream*>(info);
240 return stream->write(buffer, count) ? count : 0;
241}
242
243static void consumer_release(void* info) {
244 // we do nothing, since by design we don't "own" the stream (i.e. info)
245}
246
247static CGDataConsumerRef SkStreamToCGDataConsumer(SkWStream* stream) {
248 CGDataConsumerCallbacks procs;
249 procs.putBytes = consumer_put;
250 procs.releaseConsumer = consumer_release;
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
253 return CGDataConsumerCreate(stream, &procs);
254}
255
256static CGImageDestinationRef SkStreamToImageDestination(SkWStream* stream,
257 CFStringRef type) {
258 CGDataConsumerRef consumer = SkStreamToCGDataConsumer(stream);
259 if (NULL == consumer) {
260 return NULL;
261 }
262 SkAutoTCallVProc<const void, CFRelease> arconsumer(consumer);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000263
reed@android.com0ae6b242008-12-23 16:49:54 +0000264 return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, NULL);
265}
266
267class SkImageEncoder_CG : public SkImageEncoder {
268public:
269 SkImageEncoder_CG(Type t) : fType(t) {}
270
271protected:
272 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000273
reed@android.com0ae6b242008-12-23 16:49:54 +0000274private:
275 Type fType;
276};
277
reed@android.com0ae6b242008-12-23 16:49:54 +0000278/* Encode bitmaps via CGImageDestination. We setup a DataConsumer which writes
279 to our SkWStream. Since we don't reference/own the SkWStream, our consumer
280 must only live for the duration of the onEncode() method.
281 */
282bool SkImageEncoder_CG::onEncode(SkWStream* stream, const SkBitmap& bm,
283 int quality) {
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000284 // Used for converting a bitmap to 8888.
285 const SkBitmap* bmPtr = &bm;
286 SkBitmap bitmap8888;
287
reed@android.com0ae6b242008-12-23 16:49:54 +0000288 CFStringRef type;
289 switch (fType) {
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000290 case kICO_Type:
291 type = kUTTypeICO;
292 break;
293 case kBMP_Type:
294 type = kUTTypeBMP;
295 break;
296 case kGIF_Type:
297 type = kUTTypeGIF;
298 break;
reed@android.com0ae6b242008-12-23 16:49:54 +0000299 case kJPEG_Type:
300 type = kUTTypeJPEG;
301 break;
302 case kPNG_Type:
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000303 // PNG encoding an ARGB_4444 bitmap gives the following errors in GM:
304 // <Error>: CGImageDestinationAddImage image could not be converted to destination
305 // format.
306 // <Error>: CGImageDestinationFinalize image destination does not have enough images
307 // So instead we copy to 8888.
commit-bot@chromium.org8a2ad3c2014-02-23 03:59:35 +0000308 if (bm.colorType() == kARGB_4444_SkColorType) {
commit-bot@chromium.org28fcae22014-04-11 17:15:40 +0000309 bm.copyTo(&bitmap8888, kN32_SkColorType);
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000310 bmPtr = &bitmap8888;
311 }
reed@android.com0ae6b242008-12-23 16:49:54 +0000312 type = kUTTypePNG;
313 break;
314 default:
315 return false;
316 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000317
reed@android.com0ae6b242008-12-23 16:49:54 +0000318 CGImageDestinationRef dst = SkStreamToImageDestination(stream, type);
319 if (NULL == dst) {
320 return false;
321 }
322 SkAutoTCallVProc<const void, CFRelease> ardst(dst);
323
scroggo@google.com5a7c6be2012-10-04 21:46:08 +0000324 CGImageRef image = SkCreateCGImageRef(*bmPtr);
reed@android.com0ae6b242008-12-23 16:49:54 +0000325 if (NULL == image) {
326 return false;
327 }
328 SkAutoTCallVProc<CGImage, CGImageRelease> agimage(image);
epoger@google.com0928c4a2012-01-31 15:14:08 +0000329
330 CGImageDestinationAddImage(dst, image, NULL);
331 return CGImageDestinationFinalize(dst);
reed@android.com0ae6b242008-12-23 16:49:54 +0000332}
333
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000334///////////////////////////////////////////////////////////////////////////////
335
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000336static SkImageEncoder* sk_imageencoder_cg_factory(SkImageEncoder::Type t) {
reed@android.com0767e472008-12-23 16:06:51 +0000337 switch (t) {
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000338 case SkImageEncoder::kICO_Type:
339 case SkImageEncoder::kBMP_Type:
340 case SkImageEncoder::kGIF_Type:
341 case SkImageEncoder::kJPEG_Type:
342 case SkImageEncoder::kPNG_Type:
reed@android.com0ae6b242008-12-23 16:49:54 +0000343 break;
reed@android.com0767e472008-12-23 16:06:51 +0000344 default:
345 return NULL;
346 }
reed@android.com0ae6b242008-12-23 16:49:54 +0000347 return SkNEW_ARGS(SkImageEncoder_CG, (t));
reed@android.com0767e472008-12-23 16:06:51 +0000348}
scroggo@google.com4c6adf92013-04-17 21:07:55 +0000349
scroggo@google.comb5571b32013-09-25 21:34:24 +0000350static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_cg_factory);
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000351
caryclark936b7342014-07-11 12:14:51 -0700352#ifdef SK_BUILD_FOR_IOS
353class SkPNGImageEncoder_IOS : public SkImageEncoder_CG {
354public:
355 SkPNGImageEncoder_IOS()
356 : SkImageEncoder_CG(kPNG_Type) {
357 }
358};
359
360DEFINE_ENCODER_CREATOR(PNGImageEncoder_IOS);
361#endif
362
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000363struct FormatConversion {
364 CFStringRef fUTType;
365 SkImageDecoder::Format fFormat;
366};
367
368// Array of the types supported by the decoder.
369static const FormatConversion gFormatConversions[] = {
370 { kUTTypeBMP, SkImageDecoder::kBMP_Format },
371 { kUTTypeGIF, SkImageDecoder::kGIF_Format },
372 { kUTTypeICO, SkImageDecoder::kICO_Format },
373 { kUTTypeJPEG, SkImageDecoder::kJPEG_Format },
374 // Also include JPEG2000
375 { kUTTypeJPEG2000, SkImageDecoder::kJPEG_Format },
376 { kUTTypePNG, SkImageDecoder::kPNG_Format },
377};
378
379static SkImageDecoder::Format UTType_to_Format(const CFStringRef uttype) {
380 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormatConversions); i++) {
381 if (CFStringCompare(uttype, gFormatConversions[i].fUTType, 0) == kCFCompareEqualTo) {
382 return gFormatConversions[i].fFormat;
383 }
384 }
385 return SkImageDecoder::kUnknown_Format;
386}
387
scroggo@google.comb5571b32013-09-25 21:34:24 +0000388static SkImageDecoder::Format get_format_cg(SkStreamRewindable* stream) {
scroggo@google.com39edf4c2013-04-25 17:33:51 +0000389 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream);
390
391 if (NULL == imageSrc) {
392 return SkImageDecoder::kUnknown_Format;
393 }
394
395 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc);
396 const CFStringRef name = CGImageSourceGetType(imageSrc);
397 if (NULL == name) {
398 return SkImageDecoder::kUnknown_Format;
399 }
400 return UTType_to_Format(name);
401}
402
scroggo@google.comb5571b32013-09-25 21:34:24 +0000403static SkImageDecoder_FormatReg gFormatReg(get_format_cg);