aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/effects/SkTestImageFilters.cpp
blob: b8b0e187ede4452ff3523e56fed8b445b3279fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "SkTestImageFilters.h"
#include "SkCanvas.h"
#include "SkDevice.h"
#include "SkReadBuffer.h"
#include "SkWriteBuffer.h"

// Simple helper canvas that "takes ownership" of the provided device, so that
// when this canvas goes out of scope, so will its device. Could be replaced
// with the following:
//
//  SkCanvas canvas(device);
//  SkAutoTUnref<SkBaseDevice> aur(device);
//
class OwnDeviceCanvas : public SkCanvas {
public:
    OwnDeviceCanvas(SkBaseDevice* device) : SkCanvas(device) {
        SkSafeUnref(device);
    }
};

///////////////////////////////////////////////////////////////////////////////

bool SkDownSampleImageFilter::onFilterImageDeprecated(Proxy* proxy, const SkBitmap& src,
                                                      const Context&,
                                                      SkBitmap* result, SkIPoint*) const {
    SkScalar scale = fScale;
    if (scale > SK_Scalar1 || scale <= 0) {
        return false;
    }

    int dstW = SkScalarRoundToInt(src.width() * scale);
    int dstH = SkScalarRoundToInt(src.height() * scale);
    if (dstW < 1) {
        dstW = 1;
    }
    if (dstH < 1) {
        dstH = 1;
    }

    SkBitmap tmp;

    // downsample
    {
        SkBaseDevice* dev = proxy->createDevice(dstW, dstH);
        if (nullptr == dev) {
            return false;
        }
        OwnDeviceCanvas canvas(dev);
        SkPaint paint;

        paint.setFilterQuality(kLow_SkFilterQuality);
        canvas.scale(scale, scale);
        canvas.drawBitmap(src, 0, 0, &paint);
        tmp = dev->accessBitmap(false);
    }

    // upscale
    {
        SkBaseDevice* dev = proxy->createDevice(src.width(), src.height());
        if (nullptr == dev) {
            return false;
        }
        OwnDeviceCanvas canvas(dev);

        canvas.drawBitmapRect(tmp, SkRect::MakeIWH(src.width(), src.height()), nullptr);
        *result = dev->accessBitmap(false);
    }
    return true;
}

SkFlattenable* SkDownSampleImageFilter::CreateProc(SkReadBuffer& buffer) {
    SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
    return Create(buffer.readScalar(), common.getInput(0));
}

void SkDownSampleImageFilter::flatten(SkWriteBuffer& buffer) const {
    this->INHERITED::flatten(buffer);
    buffer.writeScalar(fScale);
}

#ifndef SK_IGNORE_TO_STRING
void SkDownSampleImageFilter::toString(SkString* str) const {
    str->appendf("SkDownSampleImageFilter: (");
    str->append(")");
}
#endif