1/*
2 * Copyright 2011 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#include "SkColorMatrixFilter.h"
10#include "SkGradientShader.h"
11
12#define WIDTH 500
13#define HEIGHT 500
14
15class SkDoOnce {
16public:
17    SkDoOnce() : fOnce(false) {};
18
19    bool once() const {
20        if (fOnce) {
21            return false;
22        }
23        fOnce = true;
24        return true;
25    }
26
27private:
28    mutable bool fOnce;
29};
30
31static void setColorMatrix(SkPaint* paint, const SkColorMatrix& matrix) {
32    paint->setColorFilter(SkColorMatrixFilter::Create(matrix))->unref();
33}
34
35static void setArray(SkPaint* paint, const SkScalar array[]) {
36    paint->setColorFilter(SkColorMatrixFilter::Create(array))->unref();
37}
38
39namespace skiagm {
40
41class ColorMatrixGM : public GM {
42    SkDoOnce fOnce;
43    void init() {
44        if (fOnce.once()) {
45            fSolidBitmap = this->createSolidBitmap(64, 64);
46            fTransparentBitmap = this->createTransparentBitmap(64, 64);
47        }
48    }
49
50public:
51    ColorMatrixGM() {
52        this->setBGColor(0xFF808080);
53    }
54
55protected:
56    virtual SkString onShortName() {
57        return SkString("colormatrix");
58    }
59
60    virtual SkISize onISize() {
61        return SkISize::Make(WIDTH, HEIGHT);
62    }
63
64    SkBitmap createSolidBitmap(int width, int height) {
65        SkBitmap bm;
66        bm.allocN32Pixels(width, height);
67        SkCanvas canvas(bm);
68        canvas.clear(0x0);
69        for (int y = 0; y < height; ++y) {
70            for (int x = 0; x < width; ++x) {
71                SkPaint paint;
72                paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
73                canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x),
74                    SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint);
75            }
76        }
77        return bm;
78    }
79
80    // creates a bitmap with shades of transparent gray.
81    SkBitmap createTransparentBitmap(int width, int height) {
82        SkBitmap bm;
83        bm.allocN32Pixels(width, height);
84        SkCanvas canvas(bm);
85        canvas.clear(0x0);
86
87        SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}};
88        SkColor colors[] = {0x00000000, 0xFFFFFFFF};
89        SkPaint paint;
90        paint.setShader(SkGradientShader::CreateLinear(pts, colors, NULL, 2,
91                                                       SkShader::kClamp_TileMode))->unref();
92        canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint);
93        return bm;
94    }
95
96    virtual void onDraw(SkCanvas* canvas) {
97        this->init();
98
99        SkPaint paint;
100        SkColorMatrix matrix;
101
102        paint.setXfermodeMode(SkXfermode::kSrc_Mode);
103        const SkBitmap bmps[] = { fSolidBitmap, fTransparentBitmap };
104
105        for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) {
106
107            matrix.setIdentity();
108            setColorMatrix(&paint, matrix);
109            canvas->drawBitmap(bmps[i], 0, 0, &paint);
110
111            matrix.setRotate(SkColorMatrix::kR_Axis, 90);
112            setColorMatrix(&paint, matrix);
113            canvas->drawBitmap(bmps[i], 80, 0, &paint);
114
115            matrix.setRotate(SkColorMatrix::kG_Axis, 90);
116            setColorMatrix(&paint, matrix);
117            canvas->drawBitmap(bmps[i], 160, 0, &paint);
118
119            matrix.setRotate(SkColorMatrix::kB_Axis, 90);
120            setColorMatrix(&paint, matrix);
121            canvas->drawBitmap(bmps[i], 240, 0, &paint);
122
123            matrix.setSaturation(0.0f);
124            setColorMatrix(&paint, matrix);
125            canvas->drawBitmap(bmps[i], 0, 80, &paint);
126
127            matrix.setSaturation(0.5f);
128            setColorMatrix(&paint, matrix);
129            canvas->drawBitmap(bmps[i], 80, 80, &paint);
130
131            matrix.setSaturation(1.0f);
132            setColorMatrix(&paint, matrix);
133            canvas->drawBitmap(bmps[i], 160, 80, &paint);
134
135            matrix.setSaturation(2.0f);
136            setColorMatrix(&paint, matrix);
137            canvas->drawBitmap(bmps[i], 240, 80, &paint);
138
139            matrix.setRGB2YUV();
140            setColorMatrix(&paint, matrix);
141            canvas->drawBitmap(bmps[i], 0, 160, &paint);
142
143            matrix.setYUV2RGB();
144            setColorMatrix(&paint, matrix);
145            canvas->drawBitmap(bmps[i], 80, 160, &paint);
146
147            SkScalar s1 = SK_Scalar1;
148            SkScalar s255 = SkIntToScalar(255);
149            // Move red into alpha, set color to white
150            SkScalar data[20] = {
151                0,  0, 0, 0, s255,
152                0,  0, 0, 0, s255,
153                0,  0, 0, 0, s255,
154                s1, 0, 0, 0, 0,
155            };
156
157            setArray(&paint, data);
158            canvas->drawBitmap(bmps[i], 160, 160, &paint);
159
160            canvas->translate(0, 240);
161        }
162    }
163
164private:
165    SkBitmap fSolidBitmap;
166    SkBitmap fTransparentBitmap;
167    typedef GM INHERITED;
168};
169
170//////////////////////////////////////////////////////////////////////////////
171
172static GM* MyFactory(void*) { return new ColorMatrixGM; }
173static GMRegistry reg(MyFactory);
174
175}
176