aboutsummaryrefslogtreecommitdiffhomepage
path: root/gm/filterbitmap.cpp
blob: c858aff76697b8f4dc88bec03a3204b026a45ece (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * 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 "gm.h"
#include "SkGradientShader.h"

#include "SkTypeface.h"
#include "SkImageDecoder.h"

static void setTypeface(SkPaint* paint, const char name[], SkTypeface::Style style) {
    SkSafeUnref(paint->setTypeface(SkTypeface::CreateFromName(name, style)));
}

static void load_bm(SkBitmap* bm) {
//    SkImageDecoder::DecodeFile("/skia/trunk/books.jpg", bm);

    bm->setConfig(SkBitmap::kARGB_8888_Config, 160, 120);
    bm->allocPixels();
    SkCanvas canvas(*bm);
    canvas.drawColor(SK_ColorWHITE);

    SkPaint paint;
    paint.setAntiAlias(true);
    paint.setSubpixelText(true);
    paint.setTextSize(17);

    setTypeface(&paint, "Times", SkTypeface::kNormal);
    canvas.drawText("Hamburgefons", 12, 10, 25, paint);
    setTypeface(&paint, "Times", SkTypeface::kBold);
    canvas.drawText("Hamburgefons", 12, 10, 50, paint);
    setTypeface(&paint, "Times", SkTypeface::kItalic);
    canvas.drawText("Hamburgefons", 12, 10, 75, paint);
    setTypeface(&paint, "Times", SkTypeface::kBoldItalic);
    canvas.drawText("Hamburgefons", 12, 10, 100, paint);
}

static SkSize computeSize(const SkBitmap& bm, const SkMatrix& mat) {
    SkRect bounds = SkRect::MakeWH(SkIntToScalar(bm.width()),
                                   SkIntToScalar(bm.height()));
    mat.mapRect(&bounds);
    return SkSize::Make(bounds.width(), bounds.height());
}

static void draw_col(SkCanvas* canvas, const SkBitmap& bm, const SkMatrix& mat,
                     SkScalar dx) {
    SkPaint paint;

    SkAutoCanvasRestore acr(canvas, true);

    canvas->drawBitmapMatrix(bm, mat, &paint);

    paint.setFilterBitmap(true);
    canvas->translate(dx, 0);
    canvas->drawBitmapMatrix(bm, mat, &paint);

    paint.setFlags(paint.getFlags() | SkPaint::kBicubicFilterBitmap_Flag);
    canvas->translate(dx, 0);
    canvas->drawBitmapMatrix(bm, mat, &paint);
}

class FilterBitmapGM : public skiagm::GM {
    bool fOnce;
    void init() {
        if (fOnce) {
            return;
        }
        fOnce = true;
        load_bm(&fBM);

        SkScalar cx = SkScalarHalf(fBM.width());
        SkScalar cy = SkScalarHalf(fBM.height());
        SkScalar scale = 1.6f;

        fMatrix[0].setScale(scale, scale);
        fMatrix[1].setRotate(30, cx, cy); fMatrix[1].postScale(scale, scale);
    }

public:
    SkBitmap    fBM;
    SkMatrix    fMatrix[2];

    FilterBitmapGM() : fOnce(false) {
        this->setBGColor(0xFFDDDDDD);
    }

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

    virtual SkISize onISize() SK_OVERRIDE {
        return SkISize::Make(920, 480);
    }

    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
        this->init();

        canvas->translate(10, 10);
        for (size_t i = 0; i < SK_ARRAY_COUNT(fMatrix); ++i) {
            SkSize size = computeSize(fBM, fMatrix[i]);
            size.fWidth += 20;
            size.fHeight += 20;

            draw_col(canvas, fBM, fMatrix[i], size.fWidth);
            canvas->translate(0, size.fHeight);
        }
    }

private:
    typedef skiagm::GM INHERITED;
};

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

DEF_GM( return new FilterBitmapGM; )