blob: 4e00e267f8f100f26a5cbffa3e72a9b7d7965bea [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:
piotaixr7b433f12014-09-17 13:05:14 -070028 bool ensureBitmapDecoded() const;
29
halcanaryb2edec22014-08-12 06:53:28 -070030 SkData* fEncodedData;
31 SkBitmap fBitmap;
32
33 typedef SkImage_Base INHERITED;
34};
35
36///////////////////////////////////////////////////////////////////////////////
37
38SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(width, height) {
39 fEncodedData = data;
40 fEncodedData->ref();
41}
42
43SkImage_Codec::~SkImage_Codec() {
44 fEncodedData->unref();
45}
46
piotaixr7b433f12014-09-17 13:05:14 -070047bool SkImage_Codec::ensureBitmapDecoded() const {
halcanaryb2edec22014-08-12 06:53:28 -070048 if (!fBitmap.pixelRef()) {
49 // todo: this needs to be thread-safe
50 SkBitmap* bitmap = const_cast<SkBitmap*>(&fBitmap);
51 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(), bitmap)) {
piotaixr7b433f12014-09-17 13:05:14 -070052 return false;
halcanaryb2edec22014-08-12 06:53:28 -070053 }
54 }
piotaixr7b433f12014-09-17 13:05:14 -070055 return true;
56}
57
58void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) const {
59 if(!this->ensureBitmapDecoded()) {
60 return;
61 }
62
halcanaryb2edec22014-08-12 06:53:28 -070063 canvas->drawBitmap(fBitmap, x, y, paint);
64}
65
66void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const SkRect& dst,
67 const SkPaint* paint) const {
piotaixr7b433f12014-09-17 13:05:14 -070068 if(!this->ensureBitmapDecoded()) {
69 return;
halcanaryb2edec22014-08-12 06:53:28 -070070 }
piotaixr7b433f12014-09-17 13:05:14 -070071
halcanaryb2edec22014-08-12 06:53:28 -070072 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint);
73}
74
75///////////////////////////////////////////////////////////////////////////////
76
77SkImage* SkImage::NewEncodedData(SkData* data) {
78 if (NULL == data) {
79 return NULL;
80 }
81
82 SkBitmap bitmap;
83 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, kUnknown_SkColorType,
84 SkImageDecoder::kDecodeBounds_Mode)) {
85 return NULL;
86 }
87
88 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height()));
89}
piotaixrd2a35222014-08-19 14:29:02 -070090
91
92bool SkImage_Codec::isOpaque() const {
93 return fBitmap.isOpaque();
94}