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 "SkBitmap.h" | 8 #include "SkBitmap.h" |
9 #include "SkCanvas.h" | 9 #include "SkCanvas.h" |
10 #include "SkImagePriv.h" | 10 #include "SkImagePriv.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 void SkImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, | 32 void SkImage::draw(SkCanvas* canvas, SkScalar x, SkScalar y, |
33 const SkPaint* paint) { | 33 const SkPaint* paint) { |
34 as_IB(this)->onDraw(canvas, x, y, paint); | 34 as_IB(this)->onDraw(canvas, x, y, paint); |
35 } | 35 } |
36 | 36 |
37 void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst, | 37 void SkImage::draw(SkCanvas* canvas, const SkRect* src, const SkRect& dst, |
38 const SkPaint* paint) { | 38 const SkPaint* paint) { |
39 as_IB(this)->onDrawRectToRect(canvas, src, dst, paint); | 39 as_IB(this)->onDrawRectToRect(canvas, src, dst, paint); |
40 } | 40 } |
41 | 41 |
| 42 const void* SkImage::peekPixels(SkImageInfo* info, size_t* rowBytes) const { |
| 43 SkImageInfo infoStorage; |
| 44 size_t rowBytesStorage; |
| 45 if (NULL == info) { |
| 46 info = &infoStorage; |
| 47 } |
| 48 if (NULL == rowBytes) { |
| 49 rowBytes = &rowBytesStorage; |
| 50 } |
| 51 return as_IB(this)->onPeekPixels(info, rowBytes); |
| 52 } |
| 53 |
| 54 bool SkImage::readPixels(SkBitmap* bitmap, const SkIRect* subset) const { |
| 55 if (NULL == bitmap) { |
| 56 return false; |
| 57 } |
| 58 |
| 59 SkIRect bounds = SkIRect::MakeWH(this->width(), this->height()); |
| 60 |
| 61 // trim against the bitmap, if its already been allocated |
| 62 if (bitmap->pixelRef()) { |
| 63 bounds.fRight = SkMin32(bounds.fRight, bitmap->width()); |
| 64 bounds.fBottom = SkMin32(bounds.fBottom, bitmap->height()); |
| 65 if (bounds.isEmpty()) { |
| 66 return false; |
| 67 } |
| 68 } |
| 69 |
| 70 if (subset && !bounds.intersect(*subset)) { |
| 71 // perhaps we could return true + empty-bitmap? |
| 72 return false; |
| 73 } |
| 74 return as_IB(this)->onReadPixels(bitmap, bounds); |
| 75 } |
| 76 |
42 GrTexture* SkImage::getTexture() { | 77 GrTexture* SkImage::getTexture() { |
43 return as_IB(this)->onGetTexture(); | 78 return as_IB(this)->onGetTexture(); |
44 } | 79 } |
45 | 80 |
46 SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const { | 81 SkData* SkImage::encode(SkImageEncoder::Type type, int quality) const { |
47 SkBitmap bm; | 82 SkBitmap bm; |
48 if (as_IB(this)->getROPixels(&bm)) { | 83 if (as_IB(this)->getROPixels(&bm)) { |
49 return SkImageEncoder::EncodeData(bm, type, quality); | 84 return SkImageEncoder::EncodeData(bm, type, quality); |
50 } | 85 } |
51 return NULL; | 86 return NULL; |
52 } | 87 } |
| 88 |
| 89 /////////////////////////////////////////////////////////////////////////////// |
| 90 |
| 91 static bool raster_canvas_supports(const SkImageInfo& info) { |
| 92 switch (info.fColorType) { |
| 93 case kPMColor_SkColorType: |
| 94 return kUnpremul_SkAlphaType != info.fAlphaType; |
| 95 case kRGB_565_SkColorType: |
| 96 return true; |
| 97 case kAlpha_8_SkColorType: |
| 98 return true; |
| 99 default: |
| 100 break; |
| 101 } |
| 102 return false; |
| 103 } |
| 104 |
| 105 bool SkImage_Base::onReadPixels(SkBitmap* bitmap, const SkIRect& subset) const { |
| 106 SkImageInfo info; |
| 107 |
| 108 if (bitmap->pixelRef()) { |
| 109 if (!bitmap->asImageInfo(&info)) { |
| 110 return false; |
| 111 } |
| 112 if (!raster_canvas_supports(info)) { |
| 113 return false; |
| 114 } |
| 115 } else { |
| 116 SkImageInfo info = SkImageInfo::MakeN32Premul(subset.width(), |
| 117 subset.height()); |
| 118 SkBitmap tmp; |
| 119 if (!tmp.allocPixels(info)) { |
| 120 return false; |
| 121 } |
| 122 *bitmap = tmp; |
| 123 } |
| 124 |
| 125 SkRect srcR, dstR; |
| 126 srcR.set(subset); |
| 127 dstR = srcR; |
| 128 dstR.offset(-dstR.left(), -dstR.top()); |
| 129 |
| 130 SkCanvas canvas(*bitmap); |
| 131 |
| 132 SkPaint paint; |
| 133 paint.setXfermodeMode(SkXfermode::kClear_Mode); |
| 134 canvas.drawRect(dstR, paint); |
| 135 |
| 136 const_cast<SkImage_Base*>(this)->onDrawRectToRect(&canvas, &srcR, dstR, NULL
); |
| 137 return true; |
| 138 } |
| 139 |
OLD | NEW |