colormatrix.cpp revision e5ff3cefe007d092daf9d0bc2b03f9ff87b2c34e
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
11#define WIDTH 500
12#define HEIGHT 500
13
14namespace skiagm {
15
16class ColorMatrixGM : public GM {
17public:
18    ColorMatrixGM() {
19        this->setBGColor(0xFF808080);
20        fBitmap = createBitmap(64, 64);
21    }
22
23protected:
24    virtual SkString onShortName() {
25        return SkString("colormatrix");
26    }
27
28    virtual SkISize onISize() {
29        return make_isize(WIDTH, HEIGHT);
30    }
31
32    SkBitmap createBitmap(int width, int height) {
33        SkBitmap bm;
34        bm.setConfig(SkBitmap::kARGB_8888_Config, width, height);
35        bm.allocPixels();
36        SkCanvas canvas(bm);
37        for (int y = 0; y < height; ++y) {
38            for (int x = 0; x < width; ++x) {
39                SkPaint paint;
40                paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
41                canvas.drawRect(SkRect::MakeXYWH(x, y, 1, 1), paint);
42            }
43        }
44        return bm;
45    }
46    virtual void onDraw(SkCanvas* canvas) {
47
48        SkPaint paint;
49        SkColorMatrix matrix;
50        SkColorMatrixFilter* filter = new SkColorMatrixFilter();
51        paint.setColorFilter(filter)->unref();
52
53        matrix.setIdentity();
54        filter->setMatrix(matrix);
55        canvas->drawBitmap(fBitmap, 0, 0, &paint);
56
57        matrix.setRotate(SkColorMatrix::kR_Axis, 90);
58        filter->setMatrix(matrix);
59        canvas->drawBitmap(fBitmap, 80, 0, &paint);
60
61        matrix.setRotate(SkColorMatrix::kG_Axis, 90);
62        filter->setMatrix(matrix);
63        canvas->drawBitmap(fBitmap, 160, 0, &paint);
64
65        matrix.setRotate(SkColorMatrix::kB_Axis, 90);
66        filter->setMatrix(matrix);
67        canvas->drawBitmap(fBitmap, 240, 0, &paint);
68
69        matrix.setSaturation(SkFloatToScalar(0.0f));
70        filter->setMatrix(matrix);
71        canvas->drawBitmap(fBitmap, 0, 80, &paint);
72
73        matrix.setSaturation(SkFloatToScalar(0.5f));
74        filter->setMatrix(matrix);
75        canvas->drawBitmap(fBitmap, 80, 80, &paint);
76
77        matrix.setSaturation(SkFloatToScalar(1.0f));
78        filter->setMatrix(matrix);
79        canvas->drawBitmap(fBitmap, 160, 80, &paint);
80
81        matrix.setSaturation(SkFloatToScalar(2.0f));
82        filter->setMatrix(matrix);
83        canvas->drawBitmap(fBitmap, 240, 80, &paint);
84
85        matrix.setRGB2YUV();
86        filter->setMatrix(matrix);
87        canvas->drawBitmap(fBitmap, 0, 160, &paint);
88
89        matrix.setYUV2RGB();
90        filter->setMatrix(matrix);
91        canvas->drawBitmap(fBitmap, 80, 160, &paint);
92    }
93
94private:
95    SkBitmap fBitmap;
96    typedef GM INHERITED;
97};
98
99//////////////////////////////////////////////////////////////////////////////
100
101static GM* MyFactory(void*) { return new ColorMatrixGM; }
102static GMRegistry reg(MyFactory);
103
104}
105