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