gfx/skia/trunk/src/ports/SkImageDecoder_CG.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /*
michael@0 2 * Copyright 2008 The Android Open Source Project
michael@0 3 *
michael@0 4 * Use of this source code is governed by a BSD-style license that can be
michael@0 5 * found in the LICENSE file.
michael@0 6 */
michael@0 7
michael@0 8 #include "SkCGUtils.h"
michael@0 9 #include "SkColorPriv.h"
michael@0 10 #include "SkImageDecoder.h"
michael@0 11 #include "SkImageEncoder.h"
michael@0 12 #include "SkMovie.h"
michael@0 13 #include "SkStream.h"
michael@0 14 #include "SkStreamHelpers.h"
michael@0 15 #include "SkTemplates.h"
michael@0 16 #include "SkUnPreMultiply.h"
michael@0 17
michael@0 18 #ifdef SK_BUILD_FOR_MAC
michael@0 19 #include <ApplicationServices/ApplicationServices.h>
michael@0 20 #endif
michael@0 21
michael@0 22 #ifdef SK_BUILD_FOR_IOS
michael@0 23 #include <CoreGraphics/CoreGraphics.h>
michael@0 24 #include <ImageIO/ImageIO.h>
michael@0 25 #include <MobileCoreServices/MobileCoreServices.h>
michael@0 26 #endif
michael@0 27
michael@0 28 static void malloc_release_proc(void* info, const void* data, size_t size) {
michael@0 29 sk_free(info);
michael@0 30 }
michael@0 31
michael@0 32 static CGDataProviderRef SkStreamToDataProvider(SkStream* stream) {
michael@0 33 // TODO: use callbacks, so we don't have to load all the data into RAM
michael@0 34 SkAutoMalloc storage;
michael@0 35 const size_t len = CopyStreamToStorage(&storage, stream);
michael@0 36 void* data = storage.detach();
michael@0 37
michael@0 38 return CGDataProviderCreateWithData(data, data, len, malloc_release_proc);
michael@0 39 }
michael@0 40
michael@0 41 static CGImageSourceRef SkStreamToCGImageSource(SkStream* stream) {
michael@0 42 CGDataProviderRef data = SkStreamToDataProvider(stream);
michael@0 43 CGImageSourceRef imageSrc = CGImageSourceCreateWithDataProvider(data, 0);
michael@0 44 CGDataProviderRelease(data);
michael@0 45 return imageSrc;
michael@0 46 }
michael@0 47
michael@0 48 class SkImageDecoder_CG : public SkImageDecoder {
michael@0 49 protected:
michael@0 50 virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode);
michael@0 51 };
michael@0 52
michael@0 53 #define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast)
michael@0 54
michael@0 55 bool SkImageDecoder_CG::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
michael@0 56 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream);
michael@0 57
michael@0 58 if (NULL == imageSrc) {
michael@0 59 return false;
michael@0 60 }
michael@0 61 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc);
michael@0 62
michael@0 63 CGImageRef image = CGImageSourceCreateImageAtIndex(imageSrc, 0, NULL);
michael@0 64 if (NULL == image) {
michael@0 65 return false;
michael@0 66 }
michael@0 67 SkAutoTCallVProc<CGImage, CGImageRelease> arimage(image);
michael@0 68
michael@0 69 const int width = SkToInt(CGImageGetWidth(image));
michael@0 70 const int height = SkToInt(CGImageGetHeight(image));
michael@0 71 bm->setConfig(SkBitmap::kARGB_8888_Config, width, height);
michael@0 72 if (SkImageDecoder::kDecodeBounds_Mode == mode) {
michael@0 73 return true;
michael@0 74 }
michael@0 75
michael@0 76 if (!this->allocPixelRef(bm, NULL)) {
michael@0 77 return false;
michael@0 78 }
michael@0 79
michael@0 80 bm->lockPixels();
michael@0 81 bm->eraseColor(SK_ColorTRANSPARENT);
michael@0 82
michael@0 83 CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
michael@0 84 CGContextRef cg = CGBitmapContextCreate(bm->getPixels(), width, height, 8, bm->rowBytes(), cs, BITMAP_INFO);
michael@0 85 CFRelease(cs);
michael@0 86
michael@0 87 CGContextDrawImage(cg, CGRectMake(0, 0, width, height), image);
michael@0 88 CGContextRelease(cg);
michael@0 89
michael@0 90 CGImageAlphaInfo info = CGImageGetAlphaInfo(image);
michael@0 91 switch (info) {
michael@0 92 case kCGImageAlphaNone:
michael@0 93 case kCGImageAlphaNoneSkipLast:
michael@0 94 case kCGImageAlphaNoneSkipFirst:
michael@0 95 SkASSERT(SkBitmap::ComputeIsOpaque(*bm));
michael@0 96 bm->setAlphaType(kOpaque_SkAlphaType);
michael@0 97 break;
michael@0 98 default:
michael@0 99 // we don't know if we're opaque or not, so compute it.
michael@0 100 if (SkBitmap::ComputeIsOpaque(*bm)) {
michael@0 101 bm->setAlphaType(kOpaque_SkAlphaType);
michael@0 102 }
michael@0 103 }
michael@0 104 if (!bm->isOpaque() && this->getRequireUnpremultipliedColors()) {
michael@0 105 // CGBitmapContext does not support unpremultiplied, so the image has been premultiplied.
michael@0 106 // Convert to unpremultiplied.
michael@0 107 for (int i = 0; i < width; ++i) {
michael@0 108 for (int j = 0; j < height; ++j) {
michael@0 109 uint32_t* addr = bm->getAddr32(i, j);
michael@0 110 *addr = SkUnPreMultiply::UnPreMultiplyPreservingByteOrder(*addr);
michael@0 111 }
michael@0 112 }
michael@0 113 bm->setAlphaType(kUnpremul_SkAlphaType);
michael@0 114 }
michael@0 115 bm->unlockPixels();
michael@0 116 return true;
michael@0 117 }
michael@0 118
michael@0 119 ///////////////////////////////////////////////////////////////////////////////
michael@0 120
michael@0 121 extern SkImageDecoder* image_decoder_from_stream(SkStreamRewindable*);
michael@0 122
michael@0 123 SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) {
michael@0 124 SkImageDecoder* decoder = image_decoder_from_stream(stream);
michael@0 125 if (NULL == decoder) {
michael@0 126 // If no image decoder specific to the stream exists, use SkImageDecoder_CG.
michael@0 127 return SkNEW(SkImageDecoder_CG);
michael@0 128 } else {
michael@0 129 return decoder;
michael@0 130 }
michael@0 131 }
michael@0 132
michael@0 133 /////////////////////////////////////////////////////////////////////////
michael@0 134
michael@0 135 SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) {
michael@0 136 return NULL;
michael@0 137 }
michael@0 138
michael@0 139 /////////////////////////////////////////////////////////////////////////
michael@0 140
michael@0 141 static size_t consumer_put(void* info, const void* buffer, size_t count) {
michael@0 142 SkWStream* stream = reinterpret_cast<SkWStream*>(info);
michael@0 143 return stream->write(buffer, count) ? count : 0;
michael@0 144 }
michael@0 145
michael@0 146 static void consumer_release(void* info) {
michael@0 147 // we do nothing, since by design we don't "own" the stream (i.e. info)
michael@0 148 }
michael@0 149
michael@0 150 static CGDataConsumerRef SkStreamToCGDataConsumer(SkWStream* stream) {
michael@0 151 CGDataConsumerCallbacks procs;
michael@0 152 procs.putBytes = consumer_put;
michael@0 153 procs.releaseConsumer = consumer_release;
michael@0 154 // we don't own/reference the stream, so it our consumer must not live
michael@0 155 // longer that our caller's ownership of the stream
michael@0 156 return CGDataConsumerCreate(stream, &procs);
michael@0 157 }
michael@0 158
michael@0 159 static CGImageDestinationRef SkStreamToImageDestination(SkWStream* stream,
michael@0 160 CFStringRef type) {
michael@0 161 CGDataConsumerRef consumer = SkStreamToCGDataConsumer(stream);
michael@0 162 if (NULL == consumer) {
michael@0 163 return NULL;
michael@0 164 }
michael@0 165 SkAutoTCallVProc<const void, CFRelease> arconsumer(consumer);
michael@0 166
michael@0 167 return CGImageDestinationCreateWithDataConsumer(consumer, type, 1, NULL);
michael@0 168 }
michael@0 169
michael@0 170 class SkImageEncoder_CG : public SkImageEncoder {
michael@0 171 public:
michael@0 172 SkImageEncoder_CG(Type t) : fType(t) {}
michael@0 173
michael@0 174 protected:
michael@0 175 virtual bool onEncode(SkWStream* stream, const SkBitmap& bm, int quality);
michael@0 176
michael@0 177 private:
michael@0 178 Type fType;
michael@0 179 };
michael@0 180
michael@0 181 /* Encode bitmaps via CGImageDestination. We setup a DataConsumer which writes
michael@0 182 to our SkWStream. Since we don't reference/own the SkWStream, our consumer
michael@0 183 must only live for the duration of the onEncode() method.
michael@0 184 */
michael@0 185 bool SkImageEncoder_CG::onEncode(SkWStream* stream, const SkBitmap& bm,
michael@0 186 int quality) {
michael@0 187 // Used for converting a bitmap to 8888.
michael@0 188 const SkBitmap* bmPtr = &bm;
michael@0 189 SkBitmap bitmap8888;
michael@0 190
michael@0 191 CFStringRef type;
michael@0 192 switch (fType) {
michael@0 193 case kICO_Type:
michael@0 194 type = kUTTypeICO;
michael@0 195 break;
michael@0 196 case kBMP_Type:
michael@0 197 type = kUTTypeBMP;
michael@0 198 break;
michael@0 199 case kGIF_Type:
michael@0 200 type = kUTTypeGIF;
michael@0 201 break;
michael@0 202 case kJPEG_Type:
michael@0 203 type = kUTTypeJPEG;
michael@0 204 break;
michael@0 205 case kPNG_Type:
michael@0 206 // PNG encoding an ARGB_4444 bitmap gives the following errors in GM:
michael@0 207 // <Error>: CGImageDestinationAddImage image could not be converted to destination
michael@0 208 // format.
michael@0 209 // <Error>: CGImageDestinationFinalize image destination does not have enough images
michael@0 210 // So instead we copy to 8888.
michael@0 211 if (bm.colorType() == kARGB_4444_SkColorType) {
michael@0 212 bm.copyTo(&bitmap8888, kPMColor_SkColorType);
michael@0 213 bmPtr = &bitmap8888;
michael@0 214 }
michael@0 215 type = kUTTypePNG;
michael@0 216 break;
michael@0 217 default:
michael@0 218 return false;
michael@0 219 }
michael@0 220
michael@0 221 CGImageDestinationRef dst = SkStreamToImageDestination(stream, type);
michael@0 222 if (NULL == dst) {
michael@0 223 return false;
michael@0 224 }
michael@0 225 SkAutoTCallVProc<const void, CFRelease> ardst(dst);
michael@0 226
michael@0 227 CGImageRef image = SkCreateCGImageRef(*bmPtr);
michael@0 228 if (NULL == image) {
michael@0 229 return false;
michael@0 230 }
michael@0 231 SkAutoTCallVProc<CGImage, CGImageRelease> agimage(image);
michael@0 232
michael@0 233 CGImageDestinationAddImage(dst, image, NULL);
michael@0 234 return CGImageDestinationFinalize(dst);
michael@0 235 }
michael@0 236
michael@0 237 ///////////////////////////////////////////////////////////////////////////////
michael@0 238
michael@0 239 static SkImageEncoder* sk_imageencoder_cg_factory(SkImageEncoder::Type t) {
michael@0 240 switch (t) {
michael@0 241 case SkImageEncoder::kICO_Type:
michael@0 242 case SkImageEncoder::kBMP_Type:
michael@0 243 case SkImageEncoder::kGIF_Type:
michael@0 244 case SkImageEncoder::kJPEG_Type:
michael@0 245 case SkImageEncoder::kPNG_Type:
michael@0 246 break;
michael@0 247 default:
michael@0 248 return NULL;
michael@0 249 }
michael@0 250 return SkNEW_ARGS(SkImageEncoder_CG, (t));
michael@0 251 }
michael@0 252
michael@0 253 static SkImageEncoder_EncodeReg gEReg(sk_imageencoder_cg_factory);
michael@0 254
michael@0 255 struct FormatConversion {
michael@0 256 CFStringRef fUTType;
michael@0 257 SkImageDecoder::Format fFormat;
michael@0 258 };
michael@0 259
michael@0 260 // Array of the types supported by the decoder.
michael@0 261 static const FormatConversion gFormatConversions[] = {
michael@0 262 { kUTTypeBMP, SkImageDecoder::kBMP_Format },
michael@0 263 { kUTTypeGIF, SkImageDecoder::kGIF_Format },
michael@0 264 { kUTTypeICO, SkImageDecoder::kICO_Format },
michael@0 265 { kUTTypeJPEG, SkImageDecoder::kJPEG_Format },
michael@0 266 // Also include JPEG2000
michael@0 267 { kUTTypeJPEG2000, SkImageDecoder::kJPEG_Format },
michael@0 268 { kUTTypePNG, SkImageDecoder::kPNG_Format },
michael@0 269 };
michael@0 270
michael@0 271 static SkImageDecoder::Format UTType_to_Format(const CFStringRef uttype) {
michael@0 272 for (size_t i = 0; i < SK_ARRAY_COUNT(gFormatConversions); i++) {
michael@0 273 if (CFStringCompare(uttype, gFormatConversions[i].fUTType, 0) == kCFCompareEqualTo) {
michael@0 274 return gFormatConversions[i].fFormat;
michael@0 275 }
michael@0 276 }
michael@0 277 return SkImageDecoder::kUnknown_Format;
michael@0 278 }
michael@0 279
michael@0 280 static SkImageDecoder::Format get_format_cg(SkStreamRewindable* stream) {
michael@0 281 CGImageSourceRef imageSrc = SkStreamToCGImageSource(stream);
michael@0 282
michael@0 283 if (NULL == imageSrc) {
michael@0 284 return SkImageDecoder::kUnknown_Format;
michael@0 285 }
michael@0 286
michael@0 287 SkAutoTCallVProc<const void, CFRelease> arsrc(imageSrc);
michael@0 288 const CFStringRef name = CGImageSourceGetType(imageSrc);
michael@0 289 if (NULL == name) {
michael@0 290 return SkImageDecoder::kUnknown_Format;
michael@0 291 }
michael@0 292 return UTType_to_Format(name);
michael@0 293 }
michael@0 294
michael@0 295 static SkImageDecoder_FormatReg gFormatReg(get_format_cg);

mercurial