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 | |
scroggo@google.com | daaea2d | 2013-06-14 20:39:48 +0000 | [diff] [blame] | 8 | #include "SkCGUtils.h" |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame] | 9 | #include "SkColorPriv.h" |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 10 | #include "SkImageDecoder.h" |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 11 | #include "SkImageEncoder.h" |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 12 | #include "SkMovie.h" |
| 13 | #include "SkStream.h" |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 14 | #include "SkStreamPriv.h" |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 15 | #include "SkTemplates.h" |
scroggo@google.com | daaea2d | 2013-06-14 20:39:48 +0000 | [diff] [blame] | 16 | #include "SkUnPreMultiply.h" |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 17 | |
yangsu@google.com | 900d877 | 2011-06-24 18:56:00 +0000 | [diff] [blame] | 18 | #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.com | 35f5ac9 | 2012-09-18 15:41:18 +0000 | [diff] [blame] | 24 | #include <ImageIO/ImageIO.h> |
caryclark@google.com | 594dd3c | 2012-09-24 19:33:57 +0000 | [diff] [blame] | 25 | #include <MobileCoreServices/MobileCoreServices.h> |
yangsu@google.com | 900d877 | 2011-06-24 18:56:00 +0000 | [diff] [blame] | 26 | #endif |
yangsu@google.com | c134f39 | 2011-06-23 22:27:30 +0000 | [diff] [blame] | 27 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 28 | static void malloc_release_proc(void* info, const void* data, size_t size) { |
| 29 | sk_free(info); |
| 30 | } |
| 31 | |
| 32 | static CGDataProviderRef SkStreamToDataProvider(SkStream* stream) { |
| 33 | // TODO: use callbacks, so we don't have to load all the data into RAM |
scroggo@google.com | 1a2e71d | 2013-08-28 19:13:13 +0000 | [diff] [blame] | 34 | SkAutoMalloc storage; |
halcanary | 67ec1f8 | 2014-06-27 11:36:20 -0700 | [diff] [blame] | 35 | const size_t len = SkCopyStreamToStorage(&storage, stream); |
scroggo@google.com | 1a2e71d | 2013-08-28 19:13:13 +0000 | [diff] [blame] | 36 | void* data = storage.detach(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 37 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 38 | return CGDataProviderCreateWithData(data, data, len, malloc_release_proc); |
| 39 | } |
| 40 | |
| 41 | static CGImageSourceRef SkStreamToCGImageSource(SkStream* stream) { |
| 42 | CGDataProviderRef data = SkStreamToDataProvider(stream); |
scroggo | 3ceef9a | 2014-10-24 06:55:07 -0700 | [diff] [blame] | 43 | if (!data) { |
| 44 | return NULL; |
| 45 | } |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 46 | CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(data, 0); |
| 47 | CGDataProviderRelease(data); |
| 48 | return imageSrc; |
| 49 | } |
| 50 | |
| 51 | class SkImageDecoder_CG : public SkImageDecoder { |
| 52 | protected: |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 53 | virtual Result onDecode(SkStream* stream, SkBitmap* bm, Mode); |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
reed | 935a9f6 | 2014-06-30 14:06:00 -0700 | [diff] [blame] | 56 | static 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 | |
| 63 | static 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 | |
| 72 | static void alpha_8_force_opaque(void* row, int count) { |
| 73 | memset(row, 0xFF, count); |
| 74 | } |
| 75 | |
| 76 | static 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.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 106 | #define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast) |
| 107 | |
reed | ddbc491 | 2014-11-11 07:27:25 -0800 | [diff] [blame] | 108 | class AutoCFDataRelease { |
| 109 | CFDataRef fDR; |
| 110 | public: |
| 111 | AutoCFDataRelease(CFDataRef dr) : fDR(dr) {} |
| 112 | ~AutoCFDataRelease() { if (fDR) { CFRelease(fDR); } } |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 113 | |
reed | ddbc491 | 2014-11-11 07:27:25 -0800 | [diff] [blame] | 114 | operator CFDataRef () { return fDR; } |
| 115 | }; |
| 116 | |
| 117 | static 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 | } |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 129 | } |
| 130 | return false; |
reed | ddbc491 | 2014-11-11 07:27:25 -0800 | [diff] [blame] | 131 | #endif |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 132 | } |
| 133 | |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 134 | SkImageDecoder::Result SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 135 | CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); |
| 136 | |
| 137 | if (NULL == imageSrc) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 138 | return kFailure; |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 139 | } |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 140 | SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 141 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 142 | CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, NULL); |
| 143 | if (NULL == image) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 144 | return kFailure; |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 145 | } |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 146 | SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 147 | |
reed@google.com | 7fa2a65 | 2014-01-27 13:42:58 +0000 | [diff] [blame] | 148 | const int width = SkToInt(CGImageGetWidth(image)); |
| 149 | const int height = SkToInt(CGImageGetHeight(image)); |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 150 | SkColorProfileType cpType = kLinear_SkColorProfileType; |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 151 | |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 152 | CGColorSpaceRef cs = CGImageGetColorSpace(image); |
| 153 | if (cs) { |
| 154 | CGColorSpaceModel m = CGColorSpaceGetModel(cs); |
reed | ddbc491 | 2014-11-11 07:27:25 -0800 | [diff] [blame] | 155 | if (kCGColorSpaceModelRGB == m && colorspace_is_sRGB(cs)) { |
| 156 | cpType = kSRGB_SkColorProfileType; |
reed | 7c74885 | 2014-11-10 08:57:21 -0800 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
reed | 871872f | 2015-06-22 12:48:26 -0700 | [diff] [blame] | 160 | 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.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 172 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 173 | return kSuccess; |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 174 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 175 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 176 | if (!this->allocPixelRef(bm, NULL)) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 177 | return kFailure; |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 178 | } |
skia.committer@gmail.com | ede0c5c | 2014-04-23 03:04:11 +0000 | [diff] [blame] | 179 | |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 180 | SkAutoLockPixels alp(*bm); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 181 | |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 182 | if (!SkCopyPixelsFromCGImage(bm->info(), bm->rowBytes(), bm->getPixels(), image)) { |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 183 | return kFailure; |
commit-bot@chromium.org | 60b5dce | 2014-04-22 20:24:33 +0000 | [diff] [blame] | 184 | } |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 185 | |
reed@google.com | 52e91bf | 2012-11-07 22:06:55 +0000 | [diff] [blame] | 186 | CGImageAlphaInfo info = CGImageGetAlphaInfo(image); |
| 187 | switch (info) { |
| 188 | case kCGImageAlphaNone: |
| 189 | case kCGImageAlphaNoneSkipLast: |
| 190 | case kCGImageAlphaNoneSkipFirst: |
reed | 935a9f6 | 2014-06-30 14:06:00 -0700 | [diff] [blame] | 191 | // 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.com | 52e91bf | 2012-11-07 22:06:55 +0000 | [diff] [blame] | 195 | break; |
| 196 | default: |
| 197 | // we don't know if we're opaque or not, so compute it. |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 198 | if (SkBitmap::ComputeIsOpaque(*bm)) { |
| 199 | bm->setAlphaType(kOpaque_SkAlphaType); |
| 200 | } |
reed@google.com | 52e91bf | 2012-11-07 22:06:55 +0000 | [diff] [blame] | 201 | } |
scroggo@google.com | daaea2d | 2013-06-14 20:39:48 +0000 | [diff] [blame] | 202 | 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.org | b06faac | 2014-01-15 22:24:58 +0000 | [diff] [blame] | 208 | *addr = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*addr); |
scroggo@google.com | daaea2d | 2013-06-14 20:39:48 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
reed@google.com | 383a697 | 2013-10-21 14:00:07 +0000 | [diff] [blame] | 211 | bm->setAlphaType(kUnpremul_SkAlphaType); |
scroggo@google.com | daaea2d | 2013-06-14 20:39:48 +0000 | [diff] [blame] | 212 | } |
scroggo | 2a12080 | 2014-10-22 12:07:00 -0700 | [diff] [blame] | 213 | return kSuccess; |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /////////////////////////////////////////////////////////////////////////////// |
| 217 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 218 | extern SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*); |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 219 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 220 | SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) { |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 221 | 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.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 228 | } |
| 229 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 230 | ///////////////////////////////////////////////////////////////////////// |
| 231 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 232 | SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) { |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 233 | return NULL; |
| 234 | } |
| 235 | |
| 236 | ///////////////////////////////////////////////////////////////////////// |
| 237 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 238 | static 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 | |
| 243 | static void consumer_release(void* info) { |
| 244 | // we do nothing, since by design we don't "own" the stream (i.e. info) |
| 245 | } |
| 246 | |
| 247 | static 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 | |
| 256 | static 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 263 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 264 | return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, NULL); |
| 265 | } |
| 266 | |
| 267 | class SkImageEncoder_CG : public SkImageEncoder { |
| 268 | public: |
| 269 | SkImageEncoder_CG(Type t) : fType(t) {} |
| 270 | |
| 271 | protected: |
| 272 | virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 273 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 274 | private: |
| 275 | Type fType; |
| 276 | }; |
| 277 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 278 | /* 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 | */ |
| 282 | bool SkImageEncoder_CG::onEncode(SkWStream* stream, const SkBitmap& bm, |
| 283 | int quality) { |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 284 | // Used for converting a bitmap to 8888. |
| 285 | const SkBitmap* bmPtr = &bm; |
| 286 | SkBitmap bitmap8888; |
| 287 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 288 | CFStringRef type; |
| 289 | switch (fType) { |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 290 | 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.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 299 | case kJPEG_Type: |
| 300 | type = kUTTypeJPEG; |
| 301 | break; |
| 302 | case kPNG_Type: |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 303 | // 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.org | 8a2ad3c | 2014-02-23 03:59:35 +0000 | [diff] [blame] | 308 | if (bm.colorType() == kARGB_4444_SkColorType) { |
commit-bot@chromium.org | 28fcae2 | 2014-04-11 17:15:40 +0000 | [diff] [blame] | 309 | bm.copyTo(&bitmap8888, kN32_SkColorType); |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 310 | bmPtr = &bitmap8888; |
| 311 | } |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 312 | type = kUTTypePNG; |
| 313 | break; |
| 314 | default: |
| 315 | return false; |
| 316 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 317 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 318 | CGImageDestinationRef dst = SkStreamToImageDestination(stream, type); |
| 319 | if (NULL == dst) { |
| 320 | return false; |
| 321 | } |
| 322 | SkAutoTCallVProc<const void, CFRelease> ardst(dst); |
| 323 | |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 324 | CGImageRef image = SkCreateCGImageRef(*bmPtr); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 325 | if (NULL == image) { |
| 326 | return false; |
| 327 | } |
| 328 | SkAutoTCallVProc<CGImage, CGImageRelease> agimage(image); |
epoger@google.com | 0928c4a | 2012-01-31 15:14:08 +0000 | [diff] [blame] | 329 | |
| 330 | CGImageDestinationAddImage(dst, image, NULL); |
| 331 | return CGImageDestinationFinalize(dst); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 332 | } |
| 333 | |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 334 | /////////////////////////////////////////////////////////////////////////////// |
| 335 | |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 336 | static SkImageEncoder* sk_imageencoder_cg_factory(SkImageEncoder::Type t) { |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 337 | switch (t) { |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 338 | 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.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 343 | break; |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 344 | default: |
| 345 | return NULL; |
| 346 | } |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 347 | return SkNEW_ARGS(SkImageEncoder_CG, (t)); |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 348 | } |
scroggo@google.com | 4c6adf9 | 2013-04-17 21:07:55 +0000 | [diff] [blame] | 349 | |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 350 | static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_cg_factory); |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 351 | |
caryclark | 936b734 | 2014-07-11 12:14:51 -0700 | [diff] [blame] | 352 | #ifdef SK_BUILD_FOR_IOS |
| 353 | class SkPNGImageEncoder_IOS : public SkImageEncoder_CG { |
| 354 | public: |
| 355 | SkPNGImageEncoder_IOS() |
| 356 | : SkImageEncoder_CG(kPNG_Type) { |
| 357 | } |
| 358 | }; |
| 359 | |
| 360 | DEFINE_ENCODER_CREATOR(PNGImageEncoder_IOS); |
| 361 | #endif |
| 362 | |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 363 | struct FormatConversion { |
| 364 | CFStringRef fUTType; |
| 365 | SkImageDecoder::Format fFormat; |
| 366 | }; |
| 367 | |
| 368 | // Array of the types supported by the decoder. |
| 369 | static 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 | |
| 379 | static 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.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 388 | static SkImageDecoder::Format get_format_cg(SkStreamRewindable* stream) { |
scroggo@google.com | 39edf4c | 2013-04-25 17:33:51 +0000 | [diff] [blame] | 389 | 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.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 403 | static SkImageDecoder_FormatReg gFormatReg(get_format_cg); |