epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 8 | #include "SkTypes.h" |
| 9 | #if defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |
| 10 | |
msarett | d442d32 | 2016-03-25 11:08:14 -0700 | [diff] [blame] | 11 | #include "SkBitmap.h" |
scroggo@google.com | daaea2d | 2013-06-14 20:39:48 +0000 | [diff] [blame] | 12 | #include "SkCGUtils.h" |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 13 | #include "SkColorPriv.h" |
scroggo | a913275 | 2016-01-19 07:53:39 -0800 | [diff] [blame] | 14 | #include "SkData.h" |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 15 | #include "SkImageEncoder.h" |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 16 | #include "SkStream.h" |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 17 | #include "SkStreamPriv.h" |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 18 | #include "SkTemplates.h" |
scroggo@google.com | daaea2d | 2013-06-14 20:39:48 +0000 | [diff] [blame] | 19 | #include "SkUnPreMultiply.h" |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 20 | |
yangsu@google.com | 900d877 | 2011-06-24 18:56:00 +0000 | [diff] [blame] | 21 | #ifdef SK_BUILD_FOR_MAC |
| 22 | #include <ApplicationServices/ApplicationServices.h> |
| 23 | #endif |
| 24 | |
| 25 | #ifdef SK_BUILD_FOR_IOS |
| 26 | #include <CoreGraphics/CoreGraphics.h> |
caryclark@google.com | 35f5ac9 | 2012-09-18 15:41:18 +0000 | [diff] [blame] | 27 | #include <ImageIO/ImageIO.h> |
caryclark@google.com | 594dd3c | 2012-09-24 19:33:57 +0000 | [diff] [blame] | 28 | #include <MobileCoreServices/MobileCoreServices.h> |
yangsu@google.com | 900d877 | 2011-06-24 18:56:00 +0000 | [diff] [blame] | 29 | #endif |
yangsu@google.com | c134f39 | 2011-06-23 22:27:30 +0000 | [diff] [blame] | 30 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 31 | static size_t consumer_put(void* info, const void* buffer, size_t count) { |
| 32 | SkWStream* stream = reinterpret_cast<SkWStream*>(info); |
| 33 | return stream->write(buffer, count) ? count : 0; |
| 34 | } |
| 35 | |
| 36 | static void consumer_release(void* info) { |
| 37 | // we do nothing, since by design we don't "own" the stream (i.e. info) |
| 38 | } |
| 39 | |
| 40 | static CGDataConsumerRef SkStreamToCGDataConsumer(SkWStream* stream) { |
| 41 | CGDataConsumerCallbacks procs; |
| 42 | procs.putBytes = consumer_put; |
| 43 | procs.releaseConsumer = consumer_release; |
| 44 | // we don't own/reference the stream, so it our consumer must not live |
| 45 | // longer that our caller's ownership of the stream |
| 46 | return CGDataConsumerCreate(stream, &procs); |
| 47 | } |
| 48 | |
| 49 | static CGImageDestinationRef SkStreamToImageDestination(SkWStream* stream, |
| 50 | CFStringRef type) { |
| 51 | CGDataConsumerRef consumer = SkStreamToCGDataConsumer(stream); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 52 | if (nullptr == consumer) { |
| 53 | return nullptr; |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 54 | } |
| 55 | SkAutoTCallVProc<const void, CFRelease> arconsumer(consumer); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 56 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 57 | return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, nullptr); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | class SkImageEncoder_CG : public SkImageEncoder { |
| 61 | public: |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 62 | SkImageEncoder_CG(Type t) : fType(t) {} |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 63 | |
| 64 | protected: |
| 65 | virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 66 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 67 | private: |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 68 | Type fType; |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 71 | /* Encode bitmaps via CGImageDestination. We setup a DataConsumer which writes |
| 72 | to our SkWStream. Since we don't reference/own the SkWStream, our consumer |
| 73 | must only live for the duration of the onEncode() method. |
| 74 | */ |
| 75 | bool SkImageEncoder_CG::onEncode(SkWStream* stream, const SkBitmap& bm, |
| 76 | int quality) { |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 77 | // Used for converting a bitmap to 8888. |
| 78 | const SkBitmap* bmPtr = &bm; |
| 79 | SkBitmap bitmap8888; |
| 80 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 81 | CFStringRef type; |
| 82 | switch (fType) { |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 83 | case kICO_Type: |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 84 | type = kUTTypeICO; |
| 85 | break; |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 86 | case kBMP_Type: |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 87 | type = kUTTypeBMP; |
| 88 | break; |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 89 | case kGIF_Type: |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 90 | type = kUTTypeGIF; |
| 91 | break; |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 92 | case kJPEG_Type: |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 93 | type = kUTTypeJPEG; |
| 94 | break; |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 95 | case kPNG_Type: |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 96 | // PNG encoding an ARGB_4444 bitmap gives the following errors in GM: |
| 97 | // <Error>: CGImageDestinationAddImage image could not be converted to destination |
| 98 | // format. |
| 99 | // <Error>: CGImageDestinationFinalize image destination does not have enough images |
| 100 | // So instead we copy to 8888. |
commit-bot@chromium.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 101 | if (bm.colorType() == kARGB_4444_SkColorType) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 102 | bm.copyTo(&bitmap8888, kN32_SkColorType); |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 103 | bmPtr = &bitmap8888; |
| 104 | } |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 105 | type = kUTTypePNG; |
| 106 | break; |
| 107 | default: |
| 108 | return false; |
| 109 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 110 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 111 | CGImageDestinationRef dst = SkStreamToImageDestination(stream, type); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 112 | if (nullptr == dst) { |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | SkAutoTCallVProc<const void, CFRelease> ardst(dst); |
| 116 | |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 117 | CGImageRef image = SkCreateCGImageRef(*bmPtr); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 118 | if (nullptr == image) { |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 119 | return false; |
| 120 | } |
| 121 | SkAutoTCallVProc<CGImage, CGImageRelease> agimage(image); |
epoger@google.com | 0928c4a | 2012-01-31 15:14:08 +0000 | [diff] [blame] | 122 | |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | CGImageDestinationAddImage(dst, image, nullptr); |
epoger@google.com | 0928c4a | 2012-01-31 15:14:08 +0000 | [diff] [blame] | 124 | return CGImageDestinationFinalize(dst); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 125 | } |
| 126 | |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 127 | /////////////////////////////////////////////////////////////////////////////// |
| 128 | |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 129 | #ifdef SK_USE_CG_ENCODER |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 130 | static SkImageEncoder* sk_imageencoder_cg_factory(SkImageEncoder::Type t) { |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 131 | switch (t) { |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 132 | case SkImageEncoder::kICO_Type: |
| 133 | case SkImageEncoder::kBMP_Type: |
| 134 | case SkImageEncoder::kGIF_Type: |
| 135 | case SkImageEncoder::kJPEG_Type: |
| 136 | case SkImageEncoder::kPNG_Type: |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 137 | break; |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 138 | default: |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 139 | return nullptr; |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 140 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 141 | return new SkImageEncoder_CG(t); |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 142 | } |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 143 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 144 | static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_cg_factory); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 145 | #endif |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 146 | |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 147 | SkImageEncoder* CreateImageEncoder_CG(SkImageEncoder::Type type) { |
Hal Canary | a2b4bdc | 2016-11-22 14:21:38 -0700 | [diff] [blame] | 148 | return new SkImageEncoder_CG(type); |
msarett | 36931c2 | 2016-08-16 15:11:24 -0700 | [diff] [blame] | 149 | } |
msarett | 39b5495 | 2016-03-23 12:26:29 -0700 | [diff] [blame] | 150 | |
mtklein | 1ee7651 | 2015-11-02 10:20:27 -0800 | [diff] [blame] | 151 | #endif//defined(SK_BUILD_FOR_MAC) || defined(SK_BUILD_FOR_IOS) |