2 * Copyright 2012 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkImageDecoder.h"
9 #include "SkImage_Base.h"
14 class SkImage_Codec : public SkImage_Base {
16 static SkImage* NewEmpty();
18 SkImage_Codec(SkData* encodedData, int width, int height);
19 virtual ~SkImage_Codec();
21 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) const SK_OVERRIDE;
22 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&,
23 const SkPaint*) const SK_OVERRIDE;
29 typedef SkImage_Base INHERITED;
32 ///////////////////////////////////////////////////////////////////////////////
34 SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(width, height) {
39 SkImage_Codec::~SkImage_Codec() {
40 fEncodedData->unref();
43 void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
44 if (!fBitmap.pixelRef()) {
45 // todo: this needs to be thread-safe
46 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap);
47 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), bitmap)) {
51 canvas->drawBitmap(fBitmap, x, y, paint);
54 void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
55 const SkPaint* paint) const {
56 if (!fBitmap.pixelRef()) {
57 // todo: this needs to be thread-safe
58 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap);
59 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), bitmap)) {
63 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
66 ///////////////////////////////////////////////////////////////////////////////
68 SkImage* SkImage::NewEncodedData(SkData* data) {
74 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, kUnknown_SkColorType,
75 SkImageDecoder::kDecodeBounds_Mode)) {
79 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height()));