1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/image/SkImage_Codec.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,78 @@ 1.4 +/* 1.5 + * Copyright 2012 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#include "SkImageDecoder.h" 1.12 +#include "SkImage_Base.h" 1.13 +#include "SkBitmap.h" 1.14 +#include "SkCanvas.h" 1.15 +#include "SkData.h" 1.16 + 1.17 +class SkImage_Codec : public SkImage_Base { 1.18 +public: 1.19 + static SkImage* NewEmpty(); 1.20 + 1.21 + SkImage_Codec(SkData* encodedData, int width, int height); 1.22 + virtual ~SkImage_Codec(); 1.23 + 1.24 + virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE; 1.25 + virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const SkPaint*) SK_OVERRIDE; 1.26 + 1.27 +private: 1.28 + SkData* fEncodedData; 1.29 + SkBitmap fBitmap; 1.30 + 1.31 + typedef SkImage_Base INHERITED; 1.32 +}; 1.33 + 1.34 +/////////////////////////////////////////////////////////////////////////////// 1.35 + 1.36 +SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(width, height) { 1.37 + fEncodedData = data; 1.38 + fEncodedData->ref(); 1.39 +} 1.40 + 1.41 +SkImage_Codec::~SkImage_Codec() { 1.42 + fEncodedData->unref(); 1.43 +} 1.44 + 1.45 +void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) { 1.46 + if (!fBitmap.pixelRef()) { 1.47 + if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), 1.48 + &fBitmap)) { 1.49 + return; 1.50 + } 1.51 + } 1.52 + canvas->drawBitmap(fBitmap, x, y, paint); 1.53 +} 1.54 + 1.55 +void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, 1.56 + const SkRect& dst, const SkPaint* paint) { 1.57 + if (!fBitmap.pixelRef()) { 1.58 + if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), 1.59 + &fBitmap)) { 1.60 + return; 1.61 + } 1.62 + } 1.63 + canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); 1.64 +} 1.65 + 1.66 +/////////////////////////////////////////////////////////////////////////////// 1.67 + 1.68 +SkImage* SkImage::NewEncodedData(SkData* data) { 1.69 + if (NULL == data) { 1.70 + return NULL; 1.71 + } 1.72 + 1.73 + SkBitmap bitmap; 1.74 + if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, 1.75 + SkBitmap::kNo_Config, 1.76 + SkImageDecoder::kDecodeBounds_Mode)) { 1.77 + return NULL; 1.78 + } 1.79 + 1.80 + return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height())); 1.81 +}