epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2008 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
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 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 11 | #include "SkImageDecoder.h" |
reed@android.com | b08eb2b | 2009-01-06 20:16:26 +0000 | [diff] [blame] | 12 | #include "SkImageEncoder.h" |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 13 | #include "SkMovie.h" |
| 14 | #include "SkStream.h" |
| 15 | #include "SkTemplates.h" |
reed@android.com | ad789ee | 2011-01-03 19:52:17 +0000 | [diff] [blame] | 16 | #include "SkCGUtils.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 |
| 34 | size_t len = stream->getLength(); |
| 35 | void* data = sk_malloc_throw(len); |
| 36 | stream->read(data, len); |
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); |
| 43 | CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(data, 0); |
| 44 | CGDataProviderRelease(data); |
| 45 | return imageSrc; |
| 46 | } |
| 47 | |
| 48 | class SkImageDecoder_CG : public SkImageDecoder { |
| 49 | protected: |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 50 | virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode); |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | #define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast) |
| 54 | |
reed@android.com | 3f1f06a | 2010-03-03 21:04:12 +0000 | [diff] [blame] | 55 | bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) { |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 56 | CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream); |
| 57 | |
| 58 | if (NULL == imageSrc) { |
| 59 | return false; |
| 60 | } |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 61 | SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 62 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 63 | CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, NULL); |
| 64 | if (NULL == image) { |
| 65 | return false; |
| 66 | } |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 67 | SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 68 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 69 | const int width = CGImageGetWidth(image); |
| 70 | const int height = CGImageGetHeight(image); |
| 71 | bm->setConfig(SkBitmap::kARGB_8888_Config, width, height); |
| 72 | if (SkImageDecoder::kDecodeBounds_Mode == mode) { |
| 73 | return true; |
| 74 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 75 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 76 | if (!this->allocPixelRef(bm, NULL)) { |
| 77 | return false; |
| 78 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 79 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 80 | bm->lockPixels(); |
| 81 | bm->eraseColor(0); |
| 82 | |
reed@google.com | af9d9c2 | 2011-06-15 17:33:29 +0000 | [diff] [blame] | 83 | // use the same colorspace, so we don't change the pixels at all |
| 84 | CGColorSpaceRef cs = CGImageGetColorSpace(image); |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 85 | CGContextRef cg = CGBitmapContextCreate(bm->getPixels(), width, height, |
| 86 | 8, bm->rowBytes(), cs, BITMAP_INFO); |
| 87 | CGContextDrawImage(cg, CGRectMake(0, 0, width, height), image); |
| 88 | CGContextRelease(cg); |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 89 | |
reed@google.com | 2a7579d | 2012-11-07 18:30:18 +0000 | [diff] [blame^] | 90 | // since CGImage won't tell us if it is opaque, we have to compute it. |
| 91 | bm->computeAndSetOpaquePredicate(); |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 92 | bm->unlockPixels(); |
| 93 | return true; |
| 94 | } |
| 95 | |
| 96 | /////////////////////////////////////////////////////////////////////////////// |
| 97 | |
| 98 | SkImageDecoder* SkImageDecoder::Factory(SkStream* stream) { |
| 99 | return SkNEW(SkImageDecoder_CG); |
| 100 | } |
| 101 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 102 | ///////////////////////////////////////////////////////////////////////// |
| 103 | |
| 104 | SkMovie* SkMovie::DecodeStream(SkStream* stream) { |
| 105 | return NULL; |
| 106 | } |
| 107 | |
| 108 | ///////////////////////////////////////////////////////////////////////// |
| 109 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 110 | static size_t consumer_put(void* info, const void* buffer, size_t count) { |
| 111 | SkWStream* stream = reinterpret_cast<SkWStream*>(info); |
| 112 | return stream->write(buffer, count) ? count : 0; |
| 113 | } |
| 114 | |
| 115 | static void consumer_release(void* info) { |
| 116 | // we do nothing, since by design we don't "own" the stream (i.e. info) |
| 117 | } |
| 118 | |
| 119 | static CGDataConsumerRef SkStreamToCGDataConsumer(SkWStream* stream) { |
| 120 | CGDataConsumerCallbacks procs; |
| 121 | procs.putBytes = consumer_put; |
| 122 | procs.releaseConsumer = consumer_release; |
| 123 | // we don't own/reference the stream, so it our consumer must not live |
| 124 | // longer that our caller's ownership of the stream |
| 125 | return CGDataConsumerCreate(stream, &procs); |
| 126 | } |
| 127 | |
| 128 | static CGImageDestinationRef SkStreamToImageDestination(SkWStream* stream, |
| 129 | CFStringRef type) { |
| 130 | CGDataConsumerRef consumer = SkStreamToCGDataConsumer(stream); |
| 131 | if (NULL == consumer) { |
| 132 | return NULL; |
| 133 | } |
| 134 | SkAutoTCallVProc<const void, CFRelease> arconsumer(consumer); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 135 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 136 | return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, NULL); |
| 137 | } |
| 138 | |
| 139 | class SkImageEncoder_CG : public SkImageEncoder { |
| 140 | public: |
| 141 | SkImageEncoder_CG(Type t) : fType(t) {} |
| 142 | |
| 143 | protected: |
| 144 | virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 145 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 146 | private: |
| 147 | Type fType; |
| 148 | }; |
| 149 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 150 | /* Encode bitmaps via CGImageDestination. We setup a DataConsumer which writes |
| 151 | to our SkWStream. Since we don't reference/own the SkWStream, our consumer |
| 152 | must only live for the duration of the onEncode() method. |
| 153 | */ |
| 154 | bool SkImageEncoder_CG::onEncode(SkWStream* stream, const SkBitmap& bm, |
| 155 | int quality) { |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 156 | // Used for converting a bitmap to 8888. |
| 157 | const SkBitmap* bmPtr = &bm; |
| 158 | SkBitmap bitmap8888; |
| 159 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 160 | CFStringRef type; |
| 161 | switch (fType) { |
| 162 | case kJPEG_Type: |
| 163 | type = kUTTypeJPEG; |
| 164 | break; |
| 165 | case kPNG_Type: |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 166 | // PNG encoding an ARGB_4444 bitmap gives the following errors in GM: |
| 167 | // <Error>: CGImageDestinationAddImage image could not be converted to destination |
| 168 | // format. |
| 169 | // <Error>: CGImageDestinationFinalize image destination does not have enough images |
| 170 | // So instead we copy to 8888. |
| 171 | if (bm.getConfig() == SkBitmap::kARGB_4444_Config) { |
| 172 | bm.copyTo(&bitmap8888, SkBitmap::kARGB_8888_Config); |
| 173 | bmPtr = &bitmap8888; |
| 174 | } |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 175 | type = kUTTypePNG; |
| 176 | break; |
| 177 | default: |
| 178 | return false; |
| 179 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 180 | |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 181 | CGImageDestinationRef dst = SkStreamToImageDestination(stream, type); |
| 182 | if (NULL == dst) { |
| 183 | return false; |
| 184 | } |
| 185 | SkAutoTCallVProc<const void, CFRelease> ardst(dst); |
| 186 | |
scroggo@google.com | 5a7c6be | 2012-10-04 21:46:08 +0000 | [diff] [blame] | 187 | CGImageRef image = SkCreateCGImageRef(*bmPtr); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 188 | if (NULL == image) { |
| 189 | return false; |
| 190 | } |
| 191 | SkAutoTCallVProc<CGImage, CGImageRelease> agimage(image); |
epoger@google.com | 0928c4a | 2012-01-31 15:14:08 +0000 | [diff] [blame] | 192 | |
| 193 | CGImageDestinationAddImage(dst, image, NULL); |
| 194 | return CGImageDestinationFinalize(dst); |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 195 | } |
| 196 | |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 197 | SkImageEncoder* SkImageEncoder::Create(Type t) { |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 198 | switch (t) { |
| 199 | case kJPEG_Type: |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 200 | case kPNG_Type: |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 201 | break; |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 202 | default: |
| 203 | return NULL; |
| 204 | } |
reed@android.com | 0ae6b24 | 2008-12-23 16:49:54 +0000 | [diff] [blame] | 205 | return SkNEW_ARGS(SkImageEncoder_CG, (t)); |
reed@android.com | 0767e47 | 2008-12-23 16:06:51 +0000 | [diff] [blame] | 206 | } |
| 207 | |