OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkImageDecoder.h" | 8 #include "SkImageDecoder.h" |
9 #include "SkImage_Base.h" | 9 #include "SkImage_Base.h" |
10 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
11 #include "SkCanvas.h" | 11 #include "SkCanvas.h" |
12 #include "SkData.h" | 12 #include "SkData.h" |
13 | 13 |
14 class SkImage_Codec : public SkImage_Base { | 14 class SkImage_Codec : public SkImage_Base { |
15 public: | 15 public: |
16 static SkImage* NewEmpty(); | 16 static SkImage* NewEmpty(); |
17 | 17 |
18 SkImage_Codec(SkData* encodedData, int width, int height); | 18 SkImage_Codec(SkData* encodedData, int width, int height); |
19 virtual ~SkImage_Codec(); | 19 virtual ~SkImage_Codec(); |
20 | 20 |
21 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRI
DE; | 21 virtual void onDraw(SkCanvas*, SkScalar, SkScalar, const SkPaint*) SK_OVERRI
DE; |
| 22 virtual void onDrawRectToRect(SkCanvas*, const SkRect*, const SkRect&, const
SkPaint*) SK_OVERRIDE; |
22 | 23 |
23 private: | 24 private: |
24 SkData* fEncodedData; | 25 SkData* fEncodedData; |
25 SkBitmap fBitmap; | 26 SkBitmap fBitmap; |
26 | 27 |
27 typedef SkImage_Base INHERITED; | 28 typedef SkImage_Base INHERITED; |
28 }; | 29 }; |
29 | 30 |
30 /////////////////////////////////////////////////////////////////////////////// | 31 /////////////////////////////////////////////////////////////////////////////// |
31 | 32 |
32 SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(wi
dth, height) { | 33 SkImage_Codec::SkImage_Codec(SkData* data, int width, int height) : INHERITED(wi
dth, height) { |
33 fEncodedData = data; | 34 fEncodedData = data; |
34 fEncodedData->ref(); | 35 fEncodedData->ref(); |
35 } | 36 } |
36 | 37 |
37 SkImage_Codec::~SkImage_Codec() { | 38 SkImage_Codec::~SkImage_Codec() { |
38 fEncodedData->unref(); | 39 fEncodedData->unref(); |
39 } | 40 } |
40 | 41 |
41 void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPai
nt* paint) { | 42 void SkImage_Codec::onDraw(SkCanvas* canvas, SkScalar x, SkScalar y, const SkPai
nt* paint) { |
42 if (!fBitmap.pixelRef()) { | 43 if (!fBitmap.pixelRef()) { |
43 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), | 44 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), |
44 &fBitmap)) { | 45 &fBitmap)) { |
45 return; | 46 return; |
46 } | 47 } |
47 } | 48 } |
48 canvas->drawBitmap(fBitmap, x, y, paint); | 49 canvas->drawBitmap(fBitmap, x, y, paint); |
49 } | 50 } |
50 | 51 |
| 52 void SkImage_Codec::onDrawRectToRect(SkCanvas* canvas, const SkRect* src, |
| 53 const SkRect& dst, const SkPaint* paint) { |
| 54 if (!fBitmap.pixelRef()) { |
| 55 if (!SkImageDecoder::DecodeMemory(fEncodedData->bytes(), fEncodedData->s
ize(), |
| 56 &fBitmap)) { |
| 57 return; |
| 58 } |
| 59 } |
| 60 canvas->drawBitmapRectToRect(fBitmap, src, dst, paint); |
| 61 } |
| 62 |
51 /////////////////////////////////////////////////////////////////////////////// | 63 /////////////////////////////////////////////////////////////////////////////// |
52 | 64 |
53 SkImage* SkImage::NewEncodedData(SkData* data) { | 65 SkImage* SkImage::NewEncodedData(SkData* data) { |
54 if (NULL == data) { | 66 if (NULL == data) { |
55 return NULL; | 67 return NULL; |
56 } | 68 } |
57 | 69 |
58 SkBitmap bitmap; | 70 SkBitmap bitmap; |
59 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, | 71 if (!SkImageDecoder::DecodeMemory(data->bytes(), data->size(), &bitmap, |
60 SkBitmap::kNo_Config, | 72 SkBitmap::kNo_Config, |
61 SkImageDecoder::kDecodeBounds_Mode)) { | 73 SkImageDecoder::kDecodeBounds_Mode)) { |
62 return NULL; | 74 return NULL; |
63 } | 75 } |
64 | 76 |
65 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height())); | 77 return SkNEW_ARGS(SkImage_Codec, (data, bitmap.width(), bitmap.height())); |
66 } | 78 } |
OLD | NEW |