| OLD | NEW |
| (Empty) |
| 1 /* | |
| 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 | |
| 14 class SkImage_Codec : public SkImage_Base { | |
| 15 public: | |
| 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 | |
| 25 virtual bool isOpaque() const SK_OVERRIDE; | |
| 26 | |
| 27 private: | |
| 28 bool ensureBitmapDecoded() const; | |
| 29 | |
| 30 SkData* fEncodedData; | |
| 31 SkBitmap fBitmap; | |
| 32 | |
| 33 typedef SkImage_Base INHERITED; | |
| 34 }; | |
| 35 | |
| 36 /////////////////////////////////////////////////////////////////////////////// | |
| 37 | |
| 38 SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(wi
dth, height) { | |
| 39 fEncodedData = data; | |
| 40 fEncodedData->ref(); | |
| 41 } | |
| 42 | |
| 43 SkImage_Codec::~SkImage_Codec() { | |
| 44 fEncodedData->unref(); | |
| 45 } | |
| 46 | |
| 47 bool SkImage_Codec::ensureBitmapDecoded() const { | |
| 48 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->s
ize(), bitmap)) { | |
| 52 return false; | |
| 53 } | |
| 54 } | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPai
nt* paint) const { | |
| 59 if(!this->ensureBitmapDecoded()) { | |
| 60 return; | |
| 61 } | |
| 62 | |
| 63 canvas->drawBitmap(fBitmap, x, y, paint); | |
| 64 } | |
| 65 | |
| 66 void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, const
SkRect& dst, | |
| 67 const SkPaint* paint) const { | |
| 68 if(!this->ensureBitmapDecoded()) { | |
| 69 return; | |
| 70 } | |
| 71 | |
| 72 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); | |
| 73 } | |
| 74 | |
| 75 /////////////////////////////////////////////////////////////////////////////// | |
| 76 | |
| 77 SkImage* 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, kUnk
nown_SkColorType, | |
| 84 SkImageDecoder::kDecodeBounds_Mode)) { | |
| 85 return NULL; | |
| 86 } | |
| 87 | |
| 88 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height())); | |
| 89 } | |
| 90 | |
| 91 | |
| 92 bool SkImage_Codec::isOpaque() const { | |
| 93 return fBitmap.isOpaque(); | |
| 94 } | |
| OLD | NEW |