Home | History | Annotate | Download | only in image
      1 /*
      2  * Copyright 2012 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 #include "SkImage_Base.h"
      9 #include "SkBitmap.h"
     10 #include "SkCanvas.h"
     11 #include "SkData.h"
     12 #include "../images/SkImageDecoder.h"
     13 
     14 class SkImage_Codec : public SkImage_Base {
     15 public:
     16     static SkImage* NewEmpty();
     17 
     18     SkImage_Codec(SkData* encodedData, int width, int height);
     19     virtual ~SkImage_Codec();
     20 
     21     virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
     22 
     23 private:
     24     SkData*     fEncodedData;
     25     SkBitmap    fBitmap;
     26 
     27     typedef SkImage_Base INHERITED;
     28 };
     29 
     30 ///////////////////////////////////////////////////////////////////////////////
     31 
     32 SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(width, height) {
     33     fEncodedData = data;
     34     fEncodedData->ref();
     35 }
     36 
     37 SkImage_Codec::~SkImage_Codec() {
     38     fEncodedData->unref();
     39 }
     40 
     41 void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) {
     42     if (!fBitmap.pixelRef()) {
     43         if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(),
     44                                           &fBitmap)) {
     45             return;
     46         }
     47     }
     48     canvas->drawBitmap(fBitmap, x, y, paint);
     49 }
     50 
     51 ///////////////////////////////////////////////////////////////////////////////
     52 
     53 SkImage* SkImage::NewEncodedData(SkData* data) {
     54     if (NULL == data) {
     55         return NULL;
     56     }
     57 
     58     SkBitmap bitmap;
     59     if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap,
     60                                       SkBitmap::kNo_Config,
     61                                       SkImageDecoder::kDecodeBounds_Mode)) {
     62         return NULL;
     63     }
     64 
     65     return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height()));
     66 }
     67