blob: 0b14216a064d9c6d81670f418b25692b58a9c8bf [file] [log] [blame]
halcanaryb2edec22014-08-12 06:53:28 -07001/*
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 "SkImageDecoder.h"
9#include "SkImage_Base.h"
10#include "SkBitmap.h"
11#include "SkCanvas.h"
12#include "SkData.h"
13
14class SkImage_Codec : public SkImage_Base {
15public:
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*) const SK_OVERRIDE;
22 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&,
23 const SkPaint*) const SK_OVERRIDE;
24
piotaixrd2a35222014-08-19 14:29:02 -070025 virtual bool isOpaque() const SK_OVERRIDE;
26
halcanaryb2edec22014-08-12 06:53:28 -070027private:
28 SkData* fEncodedData;
29 SkBitmap fBitmap;
30
31 typedef SkImage_Base INHERITED;
32};
33
34///////////////////////////////////////////////////////////////////////////////
35
36SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(width, height) {
37 fEncodedData = data;
38 fEncodedData->ref();
39}
40
41SkImage_Codec::~SkImage_Codec() {
42 fEncodedData->unref();
43}
44
45void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
46 if (!fBitmap.pixelRef()) {
47 // todo: this needs to be thread-safe
48 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap);
49 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), bitmap)) {
50 return;
51 }
52 }
53 canvas->drawBitmap(fBitmap, x, y, paint);
54}
55
56void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
57 const SkPaint* paint) const {
58 if (!fBitmap.pixelRef()) {
59 // todo: this needs to be thread-safe
60 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap);
61 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), bitmap)) {
62 return;
63 }
64 }
65 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
66}
67
68///////////////////////////////////////////////////////////////////////////////
69
70SkImage* SkImage::NewEncodedData(SkData* data) {
71 if (NULL == data) {
72 return NULL;
73 }
74
75 SkBitmap bitmap;
76 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, kUnknown_SkColorType,
77 SkImageDecoder::kDecodeBounds_Mode)) {
78 return NULL;
79 }
80
81 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height()));
82}
piotaixrd2a35222014-08-19 14:29:02 -070083
84
85bool SkImage_Codec::isOpaque() const {
86 return fBitmap.isOpaque();
87}