• Home
  • 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 "sk_tool_utils.h"
10 #include "SkColorMatrixFilter.h"
11 #include "SkGradientShader.h"
12 #include "SkImage.h"
13 
14 #define WIDTH 500
15 #define HEIGHT 500
16 
set_color_matrix(SkPaint * paint,const SkColorMatrix & matrix)17 static void set_color_matrix(SkPaint* paint, const SkColorMatrix& matrix) {
18     paint->setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(matrix.fMat));
19 }
20 
set_array(SkPaint * paint,const SkScalar array[])21 static void set_array(SkPaint* paint, const SkScalar array[]) {
22     paint->setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(array));
23 }
24 
25 class ColorMatrixGM : public skiagm::GM {
26 public:
ColorMatrixGM()27     ColorMatrixGM() {
28         this->setBGColor(sk_tool_utils::color_to_565(0xFF808080));
29     }
30 
31 protected:
onShortName()32     SkString onShortName() override {
33         return SkString("colormatrix");
34     }
35 
onISize()36     SkISize onISize() override {
37         return SkISize::Make(WIDTH, HEIGHT);
38     }
39 
onOnceBeforeDraw()40     void onOnceBeforeDraw() override {
41         fSolidImg = CreateSolidBitmap(64, 64);
42         fTransparentImg = CreateTransparentBitmap(64, 64);
43     }
44 
CreateSolidBitmap(int width,int height)45     static sk_sp<SkImage> CreateSolidBitmap(int width, int height) {
46         SkBitmap bm;
47         bm.allocN32Pixels(width, height);
48         SkCanvas canvas(bm);
49         canvas.clear(0x0);
50         for (int y = 0; y < height; ++y) {
51             for (int x = 0; x < width; ++x) {
52                 SkPaint paint;
53                 paint.setColor(SkColorSetARGB(255, x * 255 / width, y * 255 / height, 0));
54                 canvas.drawRect(SkRect::MakeXYWH(SkIntToScalar(x),
55                     SkIntToScalar(y), SK_Scalar1, SK_Scalar1), paint);
56             }
57         }
58         return SkImage::MakeFromBitmap(bm);
59     }
60 
61     // creates a bitmap with shades of transparent gray.
CreateTransparentBitmap(int width,int height)62     static sk_sp<SkImage> CreateTransparentBitmap(int width, int height) {
63         SkBitmap bm;
64         bm.allocN32Pixels(width, height);
65         SkCanvas canvas(bm);
66         canvas.clear(0x0);
67 
68         SkPoint pts[] = {{0, 0}, {SkIntToScalar(width), SkIntToScalar(height)}};
69         SkColor colors[] = {0x00000000, 0xFFFFFFFF};
70         SkPaint paint;
71         paint.setShader(SkGradientShader::MakeLinear(pts, colors, nullptr, 2,
72                                                      SkShader::kClamp_TileMode));
73         canvas.drawRect(SkRect::MakeWH(SkIntToScalar(width), SkIntToScalar(height)), paint);
74         return SkImage::MakeFromBitmap(bm);
75     }
76 
onDraw(SkCanvas * canvas)77     void onDraw(SkCanvas* canvas) override {
78         SkPaint paint;
79         SkColorMatrix matrix;
80 
81         paint.setBlendMode(SkBlendMode::kSrc);
82         const SkImage* bmps[] = { fSolidImg.get(), fTransparentImg.get() };
83 
84         for (size_t i = 0; i < SK_ARRAY_COUNT(bmps); ++i) {
85             matrix.setIdentity();
86             set_color_matrix(&paint, matrix);
87             canvas->drawImage(bmps[i], 0, 0, &paint);
88 
89             matrix.setRotate(SkColorMatrix::kR_Axis, 90);
90             set_color_matrix(&paint, matrix);
91             canvas->drawImage(bmps[i], 80, 0, &paint);
92 
93             matrix.setRotate(SkColorMatrix::kG_Axis, 90);
94             set_color_matrix(&paint, matrix);
95             canvas->drawImage(bmps[i], 160, 0, &paint);
96 
97             matrix.setRotate(SkColorMatrix::kB_Axis, 90);
98             set_color_matrix(&paint, matrix);
99             canvas->drawImage(bmps[i], 240, 0, &paint);
100             ///////////////////////////////////////////////
101             matrix.setSaturation(0.0f);
102             set_color_matrix(&paint, matrix);
103             canvas->drawImage(bmps[i], 0, 80, &paint);
104 
105             matrix.setSaturation(0.5f);
106             set_color_matrix(&paint, matrix);
107             canvas->drawImage(bmps[i], 80, 80, &paint);
108 
109             matrix.setSaturation(1.0f);
110             set_color_matrix(&paint, matrix);
111             canvas->drawImage(bmps[i], 160, 80, &paint);
112 
113             matrix.setSaturation(2.0f);
114             set_color_matrix(&paint, matrix);
115             canvas->drawImage(bmps[i], 240, 80, &paint);
116             ///////////////////////////////////////////////
117             matrix.setRGB2YUV();
118             set_color_matrix(&paint, matrix);
119             canvas->drawImage(bmps[i], 0, 160, &paint);
120 
121             matrix.setYUV2RGB();
122             set_color_matrix(&paint, matrix);
123             canvas->drawImage(bmps[i], 80, 160, &paint);
124 
125             SkScalar s1 = SK_Scalar1;
126             SkScalar s255 = SkIntToScalar(255);
127             // Move red into alpha, set color to white
128             SkScalar data[20] = {
129                 0,  0, 0, 0, s255,
130                 0,  0, 0, 0, s255,
131                 0,  0, 0, 0, s255,
132                 s1, 0, 0, 0, 0,
133             };
134 
135             set_array(&paint, data);
136             canvas->drawImage(bmps[i], 160, 160, &paint);
137             ///////////////////////////////////////////////
138             canvas->translate(0, 240);
139         }
140     }
141 
142 private:
143     sk_sp<SkImage>   fSolidImg;
144     sk_sp<SkImage>   fTransparentImg;
145 
146     typedef skiagm::GM INHERITED;
147 };
148 DEF_GM( return new ColorMatrixGM; )
149