• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 class SkDoOnce {
16 public:
SkDoOnce()17     SkDoOnce() : fOnce(false) {};
18 
once() const19     bool once() const {
20         if (fOnce) {
21             return false;
22         }
23         fOnce = true;
24         return true;
25     }
26 
27 private:
28     mutable bool fOnce;
29 };
30 
setColorMatrix(SkPaint * paint,const SkColorMatrix & matrix)31 static void setColorMatrix(SkPaint* paint, const SkColorMatrix& matrix) {
32     paint->setColorFilter(SkColorMatrixFilter::Create(matrix))->unref();
33 }
34 
setArray(SkPaint * paint,const SkScalar array[])35 static void setArray(SkPaint* paint, const SkScalar array[]) {
36     paint->setColorFilter(SkColorMatrixFilter::Create(array))->unref();
37 }
38 
39 namespace skiagm {
40 
41 class ColorMatrixGM : public GM {
42     SkDoOnce fOnce;
init()43     void init() {
44         if (fOnce.once()) {
45             fSolidBitmap = this->createSolidBitmap(64, 64);
46             fTransparentBitmap = this->createTransparentBitmap(64, 64);
47         }
48     }
49 
50 public:
ColorMatrixGM()51     ColorMatrixGM() {
52         this->setBGColor(0xFF808080);
53     }
54 
55 protected:
onShortName()56     virtual SkString onShortName() {
57         return SkString("colormatrix");
58     }
59 
onISize()60     virtual SkISize onISize() {
61         return SkISize::Make(WIDTH, HEIGHT);
62     }
63 
createSolidBitmap(int width,int height)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.
createTransparentBitmap(int width,int height)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 
onDraw(SkCanvas * canvas)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             matrix.setIdentity();
107             setColorMatrix(&paint, matrix);
108             canvas->drawBitmap(bmps[i], 0, 0, &paint);
109 
110             matrix.setRotate(SkColorMatrix::kR_Axis, 90);
111             setColorMatrix(&paint, matrix);
112             canvas->drawBitmap(bmps[i], 80, 0, &paint);
113 
114             matrix.setRotate(SkColorMatrix::kG_Axis, 90);
115             setColorMatrix(&paint, matrix);
116             canvas->drawBitmap(bmps[i], 160, 0, &paint);
117 
118             matrix.setRotate(SkColorMatrix::kB_Axis, 90);
119             setColorMatrix(&paint, matrix);
120             canvas->drawBitmap(bmps[i], 240, 0, &paint);
121 
122             matrix.setSaturation(0.0f);
123             setColorMatrix(&paint, matrix);
124             canvas->drawBitmap(bmps[i], 0, 80, &paint);
125 
126             matrix.setSaturation(0.5f);
127             setColorMatrix(&paint, matrix);
128             canvas->drawBitmap(bmps[i], 80, 80, &paint);
129 
130             matrix.setSaturation(1.0f);
131             setColorMatrix(&paint, matrix);
132             canvas->drawBitmap(bmps[i], 160, 80, &paint);
133 
134             matrix.setSaturation(2.0f);
135             setColorMatrix(&paint, matrix);
136             canvas->drawBitmap(bmps[i], 240, 80, &paint);
137 
138             matrix.setRGB2YUV();
139             setColorMatrix(&paint, matrix);
140             canvas->drawBitmap(bmps[i], 0, 160, &paint);
141 
142             matrix.setYUV2RGB();
143             setColorMatrix(&paint, matrix);
144             canvas->drawBitmap(bmps[i], 80, 160, &paint);
145 
146             SkScalar s1 = SK_Scalar1;
147             SkScalar s255 = SkIntToScalar(255);
148             // Move red into alpha, set color to white
149             SkScalar data[20] = {
150                 0,  0, 0, 0, s255,
151                 0,  0, 0, 0, s255,
152                 0,  0, 0, 0, s255,
153                 s1, 0, 0, 0, 0,
154             };
155 
156             setArray(&paint, data);
157             canvas->drawBitmap(bmps[i], 160, 160, &paint);
158 
159             canvas->translate(0, 240);
160         }
161     }
162 
163 private:
164     SkBitmap fSolidBitmap;
165     SkBitmap fTransparentBitmap;
166     typedef GM INHERITED;
167 };
168 
169 //////////////////////////////////////////////////////////////////////////////
170 
MyFactory(void *)171 static GM* MyFactory(void*) { return new ColorMatrixGM; }
172 static GMRegistry reg(MyFactory);
173 
174 }
175