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