OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 The Android Open Source Project | 2 * Copyright 2014 The Android Open Source Project |
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 "SkResizeImageFilter.h" | 8 #include "SkMatrixImageFilter.h" |
9 #include "SkBitmap.h" | 9 #include "SkBitmap.h" |
10 #include "SkCanvas.h" | 10 #include "SkCanvas.h" |
11 #include "SkDevice.h" | 11 #include "SkDevice.h" |
12 #include "SkColorPriv.h" | 12 #include "SkColorPriv.h" |
13 #include "SkReadBuffer.h" | 13 #include "SkReadBuffer.h" |
14 #include "SkWriteBuffer.h" | 14 #include "SkWriteBuffer.h" |
15 #include "SkMatrix.h" | 15 #include "SkMatrix.h" |
16 #include "SkRect.h" | 16 #include "SkRect.h" |
17 | 17 |
18 SkResizeImageFilter::SkResizeImageFilter(SkScalar sx, SkScalar sy, SkPaint::Filt
erLevel filterLevel, | 18 SkMatrixImageFilter::SkMatrixImageFilter(const SkMatrix& transform, |
| 19 SkPaint::FilterLevel filterLevel, |
19 SkImageFilter* input) | 20 SkImageFilter* input) |
20 : INHERITED(input), | 21 : INHERITED(input), |
21 fSx(sx), | 22 fTransform(transform), |
22 fSy(sy), | |
23 fFilterLevel(filterLevel) { | 23 fFilterLevel(filterLevel) { |
24 } | 24 } |
25 | 25 |
26 SkResizeImageFilter::SkResizeImageFilter(SkReadBuffer& buffer) | 26 SkMatrixImageFilter* SkMatrixImageFilter::Create(const SkMatrix& transform, |
| 27 SkPaint::FilterLevel filterLeve
l, |
| 28 SkImageFilter* input) { |
| 29 return SkNEW_ARGS(SkMatrixImageFilter, (transform, filterLevel, input)); |
| 30 } |
| 31 |
| 32 SkMatrixImageFilter::SkMatrixImageFilter(SkReadBuffer& buffer) |
27 : INHERITED(1, buffer) { | 33 : INHERITED(1, buffer) { |
28 fSx = buffer.readScalar(); | 34 buffer.readMatrix(&fTransform); |
29 fSy = buffer.readScalar(); | |
30 fFilterLevel = static_cast<SkPaint::FilterLevel>(buffer.readInt()); | 35 fFilterLevel = static_cast<SkPaint::FilterLevel>(buffer.readInt()); |
31 } | 36 } |
32 | 37 |
33 void SkResizeImageFilter::flatten(SkWriteBuffer& buffer) const { | 38 void SkMatrixImageFilter::flatten(SkWriteBuffer& buffer) const { |
34 this->INHERITED::flatten(buffer); | 39 this->INHERITED::flatten(buffer); |
35 buffer.writeScalar(fSx); | 40 buffer.writeMatrix(fTransform); |
36 buffer.writeScalar(fSy); | |
37 buffer.writeInt(fFilterLevel); | 41 buffer.writeInt(fFilterLevel); |
38 } | 42 } |
39 | 43 |
40 SkResizeImageFilter::~SkResizeImageFilter() { | 44 SkMatrixImageFilter::~SkMatrixImageFilter() { |
41 } | 45 } |
42 | 46 |
43 bool SkResizeImageFilter::onFilterImage(Proxy* proxy, | 47 bool SkMatrixImageFilter::onFilterImage(Proxy* proxy, |
44 const SkBitmap& source, | 48 const SkBitmap& source, |
45 const Context& ctx, | 49 const Context& ctx, |
46 SkBitmap* result, | 50 SkBitmap* result, |
47 SkIPoint* offset) const { | 51 SkIPoint* offset) const { |
48 SkBitmap src = source; | 52 SkBitmap src = source; |
49 SkIPoint srcOffset = SkIPoint::Make(0, 0); | 53 SkIPoint srcOffset = SkIPoint::Make(0, 0); |
50 if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcO
ffset)) { | 54 if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcO
ffset)) { |
51 return false; | 55 return false; |
52 } | 56 } |
53 | 57 |
54 SkRect dstRect; | 58 SkRect dstRect; |
55 SkIRect srcBounds, dstBounds; | 59 SkIRect srcBounds, dstBounds; |
56 src.getBounds(&srcBounds); | 60 src.getBounds(&srcBounds); |
57 srcBounds.offset(srcOffset); | 61 srcBounds.offset(srcOffset); |
58 SkRect srcRect = SkRect::Make(srcBounds); | 62 SkRect srcRect = SkRect::Make(srcBounds); |
59 SkMatrix matrix; | 63 SkMatrix matrix; |
60 if (!ctx.ctm().invert(&matrix)) { | 64 if (!ctx.ctm().invert(&matrix)) { |
61 return false; | 65 return false; |
62 } | 66 } |
63 matrix.postScale(fSx, fSy); | 67 matrix.postConcat(fTransform); |
64 matrix.postConcat(ctx.ctm()); | 68 matrix.postConcat(ctx.ctm()); |
65 matrix.mapRect(&dstRect, srcRect); | 69 matrix.mapRect(&dstRect, srcRect); |
66 dstRect.roundOut(&dstBounds); | 70 dstRect.roundOut(&dstBounds); |
67 | 71 |
68 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dst
Bounds.height())); | 72 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dst
Bounds.height())); |
69 if (NULL == device.get()) { | 73 if (NULL == device.get()) { |
70 return false; | 74 return false; |
71 } | 75 } |
72 | 76 |
73 SkCanvas canvas(device.get()); | 77 SkCanvas canvas(device.get()); |
74 canvas.scale(fSx, fSy); | 78 canvas.translate(-SkIntToScalar(dstBounds.x()), -SkIntToScalar(dstBounds.y()
)); |
| 79 canvas.concat(matrix); |
75 SkPaint paint; | 80 SkPaint paint; |
76 | 81 |
77 paint.setXfermodeMode(SkXfermode::kSrc_Mode); | 82 paint.setXfermodeMode(SkXfermode::kSrc_Mode); |
78 paint.setFilterLevel(fFilterLevel); | 83 paint.setFilterLevel(fFilterLevel); |
79 canvas.drawBitmap(src, 0, 0, &paint); | 84 canvas.drawBitmap(src, srcRect.x(), srcRect.y(), &paint); |
80 | 85 |
81 *result = device.get()->accessBitmap(false); | 86 *result = device.get()->accessBitmap(false); |
82 offset->fX = dstBounds.fLeft; | 87 offset->fX = dstBounds.fLeft; |
83 offset->fY = dstBounds.fTop; | 88 offset->fY = dstBounds.fTop; |
84 return true; | 89 return true; |
85 } | 90 } |
86 | 91 |
87 void SkResizeImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) cons
t { | 92 void SkMatrixImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) cons
t { |
88 SkRect bounds = src; | 93 SkRect bounds = src; |
89 if (getInput(0)) { | 94 if (getInput(0)) { |
90 getInput(0)->computeFastBounds(src, &bounds); | 95 getInput(0)->computeFastBounds(src, &bounds); |
91 } | 96 } |
92 dst->setXYWH(bounds.x(), bounds.y(), bounds.width() * fSx, bounds.height() *
fSy); | 97 SkMatrix matrix; |
| 98 matrix.setTranslate(-bounds.x(), -bounds.y()); |
| 99 matrix.postConcat(fTransform); |
| 100 matrix.postTranslate(bounds.x(), bounds.y()); |
| 101 matrix.mapRect(dst, bounds); |
93 } | 102 } |
94 | 103 |
95 bool SkResizeImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm
, | 104 bool SkMatrixImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm
, |
96 SkIRect* dst) const { | 105 SkIRect* dst) const { |
| 106 SkMatrix transformInverse; |
| 107 if (!fTransform.invert(&transformInverse)) { |
| 108 return false; |
| 109 } |
97 SkMatrix matrix; | 110 SkMatrix matrix; |
98 if (!ctm.invert(&matrix)) { | 111 if (!ctm.invert(&matrix)) { |
99 return false; | 112 return false; |
100 } | 113 } |
101 matrix.postScale(SkScalarInvert(fSx), SkScalarInvert(fSy)); | 114 matrix.postConcat(transformInverse); |
102 matrix.postConcat(ctm); | 115 matrix.postConcat(ctm); |
103 SkRect floatBounds; | 116 SkRect floatBounds; |
104 matrix.mapRect(&floatBounds, SkRect::Make(src)); | 117 matrix.mapRect(&floatBounds, SkRect::Make(src)); |
105 SkIRect bounds; | 118 SkIRect bounds; |
106 floatBounds.roundOut(&bounds); | 119 floatBounds.roundOut(&bounds); |
107 if (getInput(0) && !getInput(0)->filterBounds(bounds, ctm, &bounds)) { | 120 if (getInput(0) && !getInput(0)->filterBounds(bounds, ctm, &bounds)) { |
108 return false; | 121 return false; |
109 } | 122 } |
110 | 123 |
111 *dst = bounds; | 124 *dst = bounds; |
112 return true; | 125 return true; |
113 } | 126 } |
OLD | NEW |