Home | History | Annotate | Download | only in ios
      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 
     17 class SkImageDecoder_iOS : public SkImageDecoder {
     18 protected:
     19     virtual bool onDecode(SkStream* stream, SkBitmap* bm, Mode);
     20 };
     21 
     22 #define BITMAP_INFO (kCGBitmapByteOrder32Big | kCGImageAlphaPremultipliedLast)
     23 
     24 bool 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     bm->setInfo(SkImageInfo::MakeN32(width, height, kPremul_SkAlphaType), 0);
     33     if (SkImageDecoder::kDecodeBounds_Mode == mode) {
     34         return true;
     35     }
     36 
     37     if (!this->allocPixelRef(bm, NULL)) {
     38         return false;
     39     }
     40 
     41     bm->lockPixels();
     42     bm->eraseColor(0);
     43 
     44     CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB();
     45     CGContextRef cg = CGBitmapContextCreate(bm->getPixels(), width, height,
     46                                             8, bm->rowBytes(), cs, BITMAP_INFO);
     47     CGContextDrawImage(cg, CGRectMake(0, 0, width, height), uimage.CGImage);
     48     CGContextRelease(cg);
     49     CGColorSpaceRelease(cs);
     50 
     51     bm->unlockPixels();
     52     return true;
     53 }
     54 
     55 /////////////////////////////////////////////////////////////////////////
     56 
     57 SkImageDecoder* SkImageDecoder::Factory(SkStreamRewindable* stream) {
     58     return new SkImageDecoder_iOS;
     59 }
     60 
     61 SkMovie* SkMovie::DecodeStream(SkStreamRewindable* stream) {
     62     return NULL;
     63 }
     64 
     65 
     66