blob: a49859bf8f0bfeb313ebc98f65705103d21d4c8d [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"
cwallezc12b74d2015-01-26 07:45:53 -080010#include "SkColorFilterImageFilter.h"
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000011#include "SkGradientShader.h"
12#include "SkTableColorFilter.h"
13
14static void make_bm0(SkBitmap* bm) {
15 int W = 120;
16 int H = 120;
reed@google.comeb9a46c2014-01-25 16:46:20 +000017 bm->allocN32Pixels(W, H);
junov@google.comdbfac8a2012-12-06 21:47:40 +000018 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000019
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000020 SkCanvas canvas(*bm);
21 SkPaint paint;
reed5bd055c2015-03-01 19:16:38 -080022 SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} };
reedb675a732015-03-01 18:00:47 -080023 SkColor colors[] = {
reed5bd055c2015-03-01 19:16:38 -080024 SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
25 SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
reedb675a732015-03-01 18:00:47 -080026 };
reed5bd055c2015-03-01 19:16:38 -080027 SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors),
28 SkShader::kClamp_TileMode);
29 paint.setShader(s)->unref();
30 canvas.drawPaint(paint);
reedb675a732015-03-01 18:00:47 -080031}
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000032static void make_bm1(SkBitmap* bm) {
33 int W = 120;
34 int H = 120;
reed@google.comeb9a46c2014-01-25 16:46:20 +000035 bm->allocN32Pixels(W, H);
junov@google.comdbfac8a2012-12-06 21:47:40 +000036 bm->eraseColor(SK_ColorTRANSPARENT);
rmistry@google.comd6176b02012-08-23 18:14:13 +000037
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000038 SkCanvas canvas(*bm);
39 SkPaint paint;
reed5bd055c2015-03-01 19:16:38 -080040 SkScalar cx = SkIntToScalar(W)/2;
41 SkScalar cy = SkIntToScalar(H)/2;
42 SkColor colors[] = {
43 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
44 };
45 SkShader* s = SkGradientShader::CreateRadial(SkPoint::Make(SkIntToScalar(W)/2,
46 SkIntToScalar(H)/2),
47 SkIntToScalar(W)/2, colors, NULL, SK_ARRAY_COUNT(colors),
48 SkShader::kClamp_TileMode);
49 paint.setShader(s)->unref();
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000050 paint.setAntiAlias(true);
51 canvas.drawCircle(cx, cy, cx, paint);
52}
53
54static void make_table0(uint8_t table[]) {
55 for (int i = 0; i < 256; ++i) {
56 int n = i >> 5;
57 table[i] = (n << 5) | (n << 2) | (n >> 1);
58 }
59}
60static void make_table1(uint8_t table[]) {
61 for (int i = 0; i < 256; ++i) {
62 table[i] = i * i / 255;
63 }
64}
65static void make_table2(uint8_t table[]) {
66 for (int i = 0; i < 256; ++i) {
67 float fi = i / 255.0f;
robertphillips@google.com6853e802012-04-16 15:50:18 +000068 table[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000069 }
70}
71
cwallezc12b74d2015-01-26 07:45:53 -080072static SkColorFilter* make_null_cf() {
73 return NULL;
74}
75
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000076static SkColorFilter* make_cf0() {
77 uint8_t table[256]; make_table0(table);
78 return SkTableColorFilter::Create(table);
79}
80static SkColorFilter* make_cf1() {
81 uint8_t table[256]; make_table1(table);
82 return SkTableColorFilter::Create(table);
83}
84static SkColorFilter* make_cf2() {
85 uint8_t table[256]; make_table2(table);
86 return SkTableColorFilter::Create(table);
87}
88static SkColorFilter* make_cf3() {
89 uint8_t table0[256]; make_table0(table0);
90 uint8_t table1[256]; make_table1(table1);
91 uint8_t table2[256]; make_table2(table2);
92 return SkTableColorFilter::CreateARGB(NULL, table0, table1, table2);
93}
94
95class TableColorFilterGM : public skiagm::GM {
96public:
97 TableColorFilterGM() {}
rmistry@google.comd6176b02012-08-23 18:14:13 +000098
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +000099protected:
100 virtual SkString onShortName() {
101 return SkString("tablecolorfilter");
102 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000103
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000104 virtual SkISize onISize() {
cwallezc12b74d2015-01-26 07:45:53 -0800105 return SkISize::Make(700, 1650);
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000106 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000107
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000108 virtual void onDraw(SkCanvas* canvas) {
109 canvas->drawColor(0xFFDDDDDD);
110 canvas->translate(20, 20);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000111
cwallezc12b74d2015-01-26 07:45:53 -0800112
113 static SkColorFilter* (*gColorFilterMakers[])() = { make_null_cf, make_cf0, make_cf1,
114 make_cf2, make_cf3 };
115 static void (*gBitmapMakers[])(SkBitmap*) = { make_bm0, make_bm1 };
116
117 // This test will be done once for each bitmap with the results stacked vertically.
118 // For a single bitmap the resulting image will be the following:
119 // - A first line with the original bitmap, followed by the image drawn once
120 // with each of the N color filters
121 // - N lines of the bitmap drawn N times, this will cover all N*N combinations of
122 // pair of color filters in order to test the collpsing of consecutive table
123 // color filters.
124 //
125 // Here is a graphical representation of the result for 2 bitmaps and 2 filters
126 // with the number corresponding to the number of filters the bitmap goes through:
127 //
128 // --bitmap1
129 // 011
130 // 22
131 // 22
132 // --bitmap2
133 // 011
134 // 22
135 // 22
136
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000137 SkScalar x = 0, y = 0;
cwallezc12b74d2015-01-26 07:45:53 -0800138 for (size_t bitmapMaker = 0; bitmapMaker < SK_ARRAY_COUNT(gBitmapMakers); ++bitmapMaker) {
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000139 SkBitmap bm;
cwallezc12b74d2015-01-26 07:45:53 -0800140 gBitmapMakers[bitmapMaker](&bm);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000141
cwallezc12b74d2015-01-26 07:45:53 -0800142 SkScalar xOffset = SkScalar(bm.width() * 9 / 8);
143 SkScalar yOffset = SkScalar(bm.height() * 9 / 8);
144
145 // Draw the first element of the first line
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000146 x = 0;
cwallezc12b74d2015-01-26 07:45:53 -0800147 SkPaint paint;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000148 canvas->drawBitmap(bm, x, y, &paint);
rmistry@google.comd6176b02012-08-23 18:14:13 +0000149
cwallezc12b74d2015-01-26 07:45:53 -0800150 // Draws the rest of the first line for this bitmap
151 // each draw being at xOffset of the previous one
152 for (unsigned i = 1; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
153 x += xOffset;
154 paint.setColorFilter(gColorFilterMakers[i]())->unref();
155 canvas->drawBitmap(bm, x, y, &paint);
156 }
157
158 paint.setColorFilter(NULL);
159
160 for (unsigned i = 0; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
161 SkAutoTUnref<SkColorFilter> colorFilter1(gColorFilterMakers[i]());
162 SkAutoTUnref<SkImageFilter> imageFilter1(SkColorFilterImageFilter::Create(
163 colorFilter1, NULL, NULL, 0));
164
165 // Move down to the next line and draw it
166 // each draw being at xOffset of the previous one
167 y += yOffset;
168 x = 0;
169 for (unsigned j = 1; j < SK_ARRAY_COUNT(gColorFilterMakers); ++j) {
170 SkAutoTUnref<SkColorFilter> colorFilter2(gColorFilterMakers[j]());
171 SkAutoTUnref<SkImageFilter> imageFilter2(SkColorFilterImageFilter::Create(
172 colorFilter2, imageFilter1, NULL, 0));
173 paint.setImageFilter(imageFilter2);
174 canvas->drawBitmap(bm, x, y, &paint);
175 x += xOffset;
176 }
177 }
178
179 // Move down one line to the beginning of the block for next bitmap
180 y += yOffset;
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000181 }
182 }
rmistry@google.comd6176b02012-08-23 18:14:13 +0000183
mike@reedtribe.orga69b48c2011-12-28 20:31:00 +0000184private:
185 typedef GM INHERITED;
186};
187
188//////////////////////////////////////////////////////////////////////////////
189
reed5bd055c2015-03-01 19:16:38 -0800190static skiagm::GM* MyFactory(void*) { return new TableColorFilterGM; }
191static skiagm::GMRegistry reg(MyFactory);