blob: f806caad6ae850e68f2a521c7b2dca0bc4730ffb [file] [log] [blame]
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +00001/*
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 "SkCanvas.h"
10#include "SkGradientShader.h"
11#include "SkTableColorFilter.h"
12
13static void make_bm0(SkBitmap* bm) {
14 int W = 120;
15 int H = 120;
reed@google.comeb9a46c2014-01-25 16:46:20 +000016 bm->allocN32Pixels(W, H);
junov@google.comdbfac8a2012-12-06 21:47:40 +000017 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000018
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000019 SkCanvas canvas(*bm);
20 SkPaint paint;
bsalomon@google.comcadbcb82012-01-06 19:22:11 +000021 SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} };
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000022 SkColor colors[] = {
23 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
24 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
25 };
26 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
27 SkShader::kClamp_TileMode);
28 paint.setShader(s)->unref();
29 canvas.drawPaint(paint);
30}
31static void make_bm1(SkBitmap* bm) {
32 int W = 120;
33 int H = 120;
reed@google.comeb9a46c2014-01-25 16:46:20 +000034 bm->allocN32Pixels(W, H);
junov@google.comdbfac8a2012-12-06 21:47:40 +000035 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000036
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000037 SkCanvas canvas(*bm);
38 SkPaint paint;
39 SkScalar cx = SkIntToScalar(W)/2;
40 SkScalar cy = SkIntToScalar(H)/2;
41 SkColor colors[] = {
42 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
43 };
44 SkShader* s = SkGradientShader::CreateRadial(SkPoint::Make(SkIntToScalar(W)/2,
45 SkIntToScalar(H)/2),
46 SkIntToScalar(W)/2, colors, NULL, SK_ARRAY_COUNT(colors),
47 SkShader::kClamp_TileMode);
48 paint.setShader(s)->unref();
49 paint.setAntiAlias(true);
50 canvas.drawCircle(cx, cy, cx, paint);
51}
52
53static void make_table0(uint8_t table[]) {
54 for (int i = 0; i < 256; ++i) {
55 int n = i >> 5;
56 table[i] = (n << 5) | (n << 2) | (n >> 1);
57 }
58}
59static void make_table1(uint8_t table[]) {
60 for (int i = 0; i < 256; ++i) {
61 table[i] = i * i / 255;
62 }
63}
64static void make_table2(uint8_t table[]) {
65 for (int i = 0; i < 256; ++i) {
66 float fi = i / 255.0f;
robertphillips@google.com6853e802012-04-16 15:50:18 +000067 table[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000068 }
69}
70
71static SkColorFilter* make_cf0() {
72 uint8_t table[256]; make_table0(table);
73 return SkTableColorFilter::Create(table);
74}
75static SkColorFilter* make_cf1() {
76 uint8_t table[256]; make_table1(table);
77 return SkTableColorFilter::Create(table);
78}
79static SkColorFilter* make_cf2() {
80 uint8_t table[256]; make_table2(table);
81 return SkTableColorFilter::Create(table);
82}
83static SkColorFilter* make_cf3() {
84 uint8_t table0[256]; make_table0(table0);
85 uint8_t table1[256]; make_table1(table1);
86 uint8_t table2[256]; make_table2(table2);
87 return SkTableColorFilter::CreateARGB(NULL, table0, table1, table2);
88}
89
90class TableColorFilterGM : public skiagm::GM {
91public:
92 TableColorFilterGM() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000093
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000094protected:
95 virtual SkString onShortName() {
96 return SkString("tablecolorfilter");
97 }
rmistry@google.comd6176b02012-08-23 18:14:13 +000098
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000099 virtual SkISize onISize() {
reed@google.com6b2f7ea2012-03-14 20:00:42 +0000100 return SkISize::Make(700, 300);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000101 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000102
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000103 virtual void onDraw(SkCanvas* canvas) {
104 canvas->drawColor(0xFFDDDDDD);
105 canvas->translate(20, 20);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000106
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000107 SkScalar x = 0, y = 0;
rmistry@google.comd6176b02012-08-23 18:14:13 +0000108
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000109 static void (*gMakers[])(SkBitmap*) = { make_bm0, make_bm1 };
110 for (size_t maker = 0; maker < SK_ARRAY_COUNT(gMakers); ++maker) {
111 SkBitmap bm;
112 gMakers[maker](&bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000113
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000114 SkPaint paint;
115 x = 0;
116 canvas->drawBitmap(bm, x, y, &paint);
117 paint.setColorFilter(make_cf0())->unref(); x += bm.width() * 9 / 8;
118 canvas->drawBitmap(bm, x, y, &paint);
119 paint.setColorFilter(make_cf1())->unref(); x += bm.width() * 9 / 8;
120 canvas->drawBitmap(bm, x, y, &paint);
121 paint.setColorFilter(make_cf2())->unref(); x += bm.width() * 9 / 8;
122 canvas->drawBitmap(bm, x, y, &paint);
123 paint.setColorFilter(make_cf3())->unref(); x += bm.width() * 9 / 8;
124 canvas->drawBitmap(bm, x, y, &paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000125
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000126 y += bm.height() * 9 / 8;
127 }
128 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000129
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000130private:
131 typedef GM INHERITED;
132};
133
134//////////////////////////////////////////////////////////////////////////////
135
136static skiagm::GM* MyFactory(void*) { return new TableColorFilterGM; }
137static skiagm::GMRegistry reg(MyFactory);