2 * Copyright 2013 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkResizeImageFilter.h"
12 #include "SkColorPriv.h"
13 #include "SkReadBuffer.h"
14 #include "SkWriteBuffer.h"
18 SkResizeImageFilter::SkResizeImageFilter(SkScalar sx, SkScalar sy, SkPaint::FilterLevel filterLevel,
23 fFilterLevel(filterLevel) {
26 SkResizeImageFilter::SkResizeImageFilter(SkReadBuffer& buffer)
27 : INHERITED(1, buffer) {
28 fSx = buffer.readScalar();
29 fSy = buffer.readScalar();
30 fFilterLevel = static_cast<SkPaint::FilterLevel>(buffer.readInt());
33 void SkResizeImageFilter::flatten(SkWriteBuffer& buffer) const {
34 this->INHERITED::flatten(buffer);
35 buffer.writeScalar(fSx);
36 buffer.writeScalar(fSy);
37 buffer.writeInt(fFilterLevel);
40 SkResizeImageFilter::~SkResizeImageFilter() {
43 bool SkResizeImageFilter::onFilterImage(Proxy* proxy,
44 const SkBitmap& source,
47 SkIPoint* offset) const {
48 SkBitmap src = source;
49 SkIPoint srcOffset = SkIPoint::Make(0, 0);
50 if (getInput(0) && !getInput(0)->filterImage(proxy, source, ctx, &src, &srcOffset)) {
55 SkIRect srcBounds, dstBounds;
56 src.getBounds(&srcBounds);
57 srcBounds.offset(srcOffset);
58 SkRect srcRect = SkRect::Make(srcBounds);
60 if (!ctx.ctm().invert(&matrix)) {
63 matrix.postScale(fSx, fSy);
64 matrix.postConcat(ctx.ctm());
65 matrix.mapRect(&dstRect, srcRect);
66 dstRect.roundOut(&dstBounds);
68 SkAutoTUnref<SkBaseDevice> device(proxy->createDevice(dstBounds.width(), dstBounds.height()));
69 if (NULL == device.get()) {
73 SkCanvas canvas(device.get());
74 canvas.scale(fSx, fSy);
77 paint.setXfermodeMode(SkXfermode::kSrc_Mode);
78 paint.setFilterLevel(fFilterLevel);
79 canvas.drawBitmap(src, 0, 0, &paint);
81 *result = device.get()->accessBitmap(false);
82 offset->fX = dstBounds.fLeft;
83 offset->fY = dstBounds.fTop;
87 void SkResizeImageFilter::computeFastBounds(const SkRect& src, SkRect* dst) const {
90 getInput(0)->computeFastBounds(src, &bounds);
92 dst->setXYWH(bounds.x(), bounds.y(), bounds.width() * fSx, bounds.height() * fSy);
95 bool SkResizeImageFilter::onFilterBounds(const SkIRect& src, const SkMatrix& ctm,
98 if (!ctm.invert(&matrix)) {
101 matrix.postScale(SkScalarInvert(fSx), SkScalarInvert(fSy));
102 matrix.postConcat(ctm);
104 matrix.mapRect(&floatBounds, SkRect::Make(src));
106 floatBounds.roundOut(&bounds);
107 if (getInput(0) && !getInput(0)->filterBounds(bounds, ctm, &bounds)) {