michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #include "SkImageDecoder.h" michael@0: #include "SkImage_Base.h" michael@0: #include "SkBitmap.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkData.h" michael@0: michael@0: class SkImage_Codec : public SkImage_Base { michael@0: public: michael@0: static SkImage* NewEmpty(); michael@0: michael@0: SkImage_Codec(SkData* encodedData, int width, int height); michael@0: virtual ~SkImage_Codec(); michael@0: michael@0: virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE; michael@0: virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE; michael@0: michael@0: private: michael@0: SkData* fEncodedData; michael@0: SkBitmap fBitmap; michael@0: michael@0: typedef SkImage_Base INHERITED; michael@0: }; michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(width, height) { michael@0: fEncodedData = data; michael@0: fEncodedData->ref(); michael@0: } michael@0: michael@0: SkImage_Codec::~SkImage_Codec() { michael@0: fEncodedData->unref(); michael@0: } michael@0: michael@0: void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) { michael@0: if (!fBitmap.pixelRef()) { michael@0: if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), michael@0: &fBitmap)) { michael@0: return; michael@0: } michael@0: } michael@0: canvas->drawBitmap(fBitmap, x, y, paint); michael@0: } michael@0: michael@0: void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, michael@0: const SkRect& dst, const SkPaint* paint) { michael@0: if (!fBitmap.pixelRef()) { michael@0: if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), michael@0: &fBitmap)) { michael@0: return; michael@0: } michael@0: } michael@0: canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: SkImage* SkImage::NewEncodedData(SkData* data) { michael@0: if (NULL == data) { michael@0: return NULL; michael@0: } michael@0: michael@0: SkBitmap bitmap; michael@0: if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, michael@0: SkBitmap::kNo_Config, michael@0: SkImageDecoder::kDecodeBounds_Mode)) { michael@0: return NULL; michael@0: } michael@0: michael@0: return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height())); michael@0: }