aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/bicubicfilter.cpp
blob: c3f80df1183125a042d7cbf954ec40046fbdd805 (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
/*
 * Copyright 2013 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

#include "gm.h"
#include "SkColor.h"
#include "SkBicubicImageFilter.h"

namespace skiagm {

class BicubicGM : public GM {
public:
    BicubicGM() : fInitialized(false) {
        this->setBGColor(0x00000000);
    }

protected:
    virtual SkString onShortName() {
        return SkString("bicubicfilter");
    }

    void make_checkerboard(int width, int height) {
        SkASSERT(width % 2 == 0);
        SkASSERT(height % 2 == 0);
        fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, width, height);
        fCheckerboard.allocPixels();
        SkAutoLockPixels lock(fCheckerboard);
        for (int y = 0; y < height; y += 2) {
            SkPMColor* s = fCheckerboard.getAddr32(0, y);
            for (int x = 0; x < width; x += 2) {
                *s++ = 0xFFFFFFFF;
                *s++ = 0xFF000000;
            }
            s = fCheckerboard.getAddr32(0, y + 1);
            for (int x = 0; x < width; x += 2) {
                *s++ = 0xFF000000;
                *s++ = 0xFFFFFFFF;
            }
        }
    }

    virtual SkISize onISize() {
        return make_isize(400, 300);
    }

    virtual void onDraw(SkCanvas* canvas) {
        if (!fInitialized) {
            make_checkerboard(4, 4);
            fInitialized = true;
        }
        SkScalar sk32 = SkIntToScalar(32);
        canvas->clear(0x00000000);
        SkPaint bilinearPaint, bicubicPaint;
        SkSize scale = SkSize::Make(sk32, sk32);
        canvas->save();
        canvas->scale(sk32, sk32);
        bilinearPaint.setFilterBitmap(true);
        canvas->drawBitmap(fCheckerboard, 0, 0, &bilinearPaint);
        canvas->restore();
        SkAutoTUnref<SkImageFilter> bicubic(SkBicubicImageFilter::CreateMitchell(scale));
        bicubicPaint.setImageFilter(bicubic);
        SkRect srcBounds;
        fCheckerboard.getBounds(&srcBounds);
        canvas->translate(SkIntToScalar(140), 0);
        canvas->saveLayer(&srcBounds, &bicubicPaint);
        canvas->drawBitmap(fCheckerboard, 0, 0);
        canvas->restore();
    }

private:
    typedef GM INHERITED;
    SkBitmap fCheckerboard;
    bool fInitialized;
};

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

static GM* MyFactory(void*) { return new BicubicGM; }
static GMRegistry reg(MyFactory);

}