• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright 2010 Google Inc.
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
8#import <CoreGraphics/CoreGraphics.h>
9#include <CoreGraphics/CGColorSpace.h>
10#import <UIKit/UIKit.h>
11
12#include "SkImageDecoder.h"
13#include "SkImageEncoder.h"
14#include "SkMovie.h"
15#include "SkStream_NSData.h"
16
17class SkImageDecoder_iOS : public SkImageDecoder {
18protected:
19    virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode);
20};
21
22#define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast)
23
24bool SkImageDecoder_iOS::onDecode(SkStream* stream, SkBitmap* bm, Mode mode) {
25
26    NSData* data = NSData_dataWithStream(stream);
27
28    UIImage* uimage = [UIImage imageWithData:data];
29
30    const int width = uimage.size.width;
31    const int height = uimage.size.height;
32    SkColorType ct = SkBitmapConfigToColorType(SkBitmap::kARGB_8888_Config);
33    bm->setInfo(SkImageInfo::Make(width, height, ct, kPremul_SkAlphaType), 0);
34    if (SkImageDecoder::kDecodeBounds_Mode == mode) {
35        return true;
36    }
37
38    if (!this->allocPixelRef(bm, NULL)) {
39        return false;
40    }
41
42    bm->lockPixels();
43    bm->eraseColor(0);
44
45    CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
46    CGContextRef cg = CGBitmapContextCreate(bm->getPixels(), width, height,
47                                            8, bm->rowBytes(), cs, BITMAP_INFO);
48    CGContextDrawImage(cg, CGRectMake(0, 0, width, height), uimage.CGImage);
49    CGContextRelease(cg);
50    CGColorSpaceRelease(cs);
51
52    bm->unlockPixels();
53    return true;
54}
55
56/////////////////////////////////////////////////////////////////////////
57
58SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) {
59    return new SkImageDecoder_iOS;
60}
61
62SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) {
63    return NULL;
64}
65
66SkImageEncoder* SkImageEncoder::Create(Type t) {
67    return NULL;
68}
69
70