imagefiltersgraph.cpp revision 985fa79141a82ec5e9bb5b01d9090aef76fac234
1/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "gm.h"
9
10#include "SkBitmapSource.h"
11#include "SkBlendImageFilter.h"
12#include "SkBlurImageFilter.h"
13#include "SkColorFilter.h"
14#include "SkColorMatrixFilter.h"
15#include "SkColorFilterImageFilter.h"
16#include "SkMorphologyImageFilter.h"
17
18#include "SkTestImageFilters.h"
19
20///////////////////////////////////////////////////////////////////////////////
21
22class ImageFiltersGraphGM : public skiagm::GM {
23public:
24    ImageFiltersGraphGM() : fInitialized(false) {}
25
26protected:
27    virtual SkString onShortName() {
28        return SkString("imagefiltersgraph");
29    }
30
31    void make_bitmap() {
32        fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 100, 100);
33        fBitmap.allocPixels();
34        SkDevice device(fBitmap);
35        SkCanvas canvas(&device);
36        canvas.clear(0x00000000);
37        SkPaint paint;
38        paint.setAntiAlias(true);
39        paint.setColor(0xFFFFFFFF);
40        paint.setTextSize(SkIntToScalar(96));
41        const char* str = "e";
42        canvas.drawText(str, strlen(str), SkIntToScalar(20), SkIntToScalar(70), paint);
43    }
44
45    virtual SkISize onISize() { return SkISize::Make(200, 100); }
46
47    virtual void onDraw(SkCanvas* canvas) {
48        if (!fInitialized) {
49            this->make_bitmap();
50            fInitialized = true;
51        }
52        canvas->clear(0x00000000);
53        {
54            SkAutoTUnref<SkImageFilter> bitmapSource(new SkBitmapSource(fBitmap));
55            SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(SK_ColorRED,
56                                                         SkXfermode::kSrcIn_Mode));
57            SkAutoTUnref<SkImageFilter> blur(new SkBlurImageFilter(4.0f, 4.0f, bitmapSource));
58            SkAutoTUnref<SkImageFilter> erode(new SkErodeImageFilter(4, 4, blur));
59            SkAutoTUnref<SkImageFilter> color(new SkColorFilterImageFilter(cf, erode));
60            SkAutoTUnref<SkImageFilter> merge(new SkMergeImageFilter(blur, color));
61
62            SkPaint paint;
63            paint.setImageFilter(merge);
64            canvas->drawPaint(paint);
65        }
66        {
67            SkAutoTUnref<SkImageFilter> morph(new SkDilateImageFilter(5, 5));
68
69            SkScalar matrix[20] = { SK_Scalar1, 0, 0, 0, 0,
70                                    0, SK_Scalar1, 0, 0, 0,
71                                    0, 0, SK_Scalar1, 0, 0,
72                                    0, 0, 0, SkFloatToScalar(0.5f), 0 };
73
74            SkAutoTUnref<SkColorFilter> matrixFilter(new SkColorMatrixFilter(matrix));
75            SkAutoTUnref<SkImageFilter> colorMorph(new SkColorFilterImageFilter(matrixFilter, morph));
76            SkAutoTUnref<SkImageFilter> blendColor(new SkBlendImageFilter(SkBlendImageFilter::kNormal_Mode, colorMorph));
77
78            SkPaint paint;
79            paint.setImageFilter(blendColor);
80            canvas->drawBitmap(fBitmap, 100, 0, &paint);
81        }
82    }
83
84private:
85    typedef GM INHERITED;
86    SkBitmap fBitmap;
87    bool fInitialized;
88};
89
90///////////////////////////////////////////////////////////////////////////////
91
92static skiagm::GM* MyFactory(void*) { return new ImageFiltersGraphGM; }
93static skiagm::GMRegistry reg(MyFactory);
94
95
96