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