blob: 61661de3569f86b4faf479ad43fb17219468d29c [file] [log] [blame]
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +00001/*
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
scroggo@google.com7def5e12013-05-31 14:00:10 +00008#include "SkImageDecoder.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +00009#include "SkImage_Base.h"
10#include "SkBitmap.h"
11#include "SkCanvas.h"
12#include "SkData.h"
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000013
14class SkImage_Codec : public SkImage_Base {
rmistry@google.comfbfcd562012-08-23 18:09:54 +000015public:
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000016 static SkImage* NewEmpty();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000017
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000018 SkImage_Codec(SkData* encodedData, int width, int height);
19 virtual ~SkImage_Codec();
rmistry@google.comfbfcd562012-08-23 18:09:54 +000020
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000021 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRIDE;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000022
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000023private:
24 SkData* fEncodedData;
25 SkBitmap fBitmap;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000026
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000027 typedef SkImage_Base INHERITED;
28};
29
30///////////////////////////////////////////////////////////////////////////////
31
32SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(width, height) {
33 fEncodedData = data;
34 fEncodedData->ref();
35}
36
37SkImage_Codec::~SkImage_Codec() {
38 fEncodedData->unref();
39}
40
41void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPaint* paint) {
42 if (!fBitmap.pixelRef()) {
43 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->size(),
44 &fBitmap)) {
45 return;
46 }
47 }
48 canvas->drawBitmap(fBitmap, x, y, paint);
49}
50
51///////////////////////////////////////////////////////////////////////////////
52
53SkImage* SkImage::NewEncodedData(SkData* data) {
54 if (NULL == data) {
55 return NULL;
56 }
57
58 SkBitmap bitmap;
59 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap,
60 SkBitmap::kNo_Config,
61 SkImageDecoder::kDecodeBounds_Mode)) {
62 return NULL;
63 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +000064
mike@reedtribe.orgd829b5c2012-07-31 03:57:11 +000065 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height()));
66}