mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 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 "SkCanvas.h" |
| 10 | #include "SkGradientShader.h" |
| 11 | #include "SkTableColorFilter.h" |
| 12 | |
| 13 | static void make_bm0(SkBitmap* bm) { |
| 14 | int W = 120; |
| 15 | int H = 120; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame^] | 16 | bm->allocN32Pixels(W, H); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 17 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 18 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 19 | SkCanvas canvas(*bm); |
| 20 | SkPaint paint; |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 21 | SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} }; |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 22 | 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 | } |
| 31 | static void make_bm1(SkBitmap* bm) { |
| 32 | int W = 120; |
| 33 | int H = 120; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame^] | 34 | bm->allocN32Pixels(W, H); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 35 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 36 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 37 | 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 | |
| 53 | static 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 | } |
| 59 | static void make_table1(uint8_t table[]) { |
| 60 | for (int i = 0; i < 256; ++i) { |
| 61 | table[i] = i * i / 255; |
| 62 | } |
| 63 | } |
| 64 | static void make_table2(uint8_t table[]) { |
| 65 | for (int i = 0; i < 256; ++i) { |
| 66 | float fi = i / 255.0f; |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 67 | table[i] = static_cast<uint8_t>(sqrtf(fi) * 255); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | |
| 71 | static SkColorFilter* make_cf0() { |
| 72 | uint8_t table[256]; make_table0(table); |
| 73 | return SkTableColorFilter::Create(table); |
| 74 | } |
| 75 | static SkColorFilter* make_cf1() { |
| 76 | uint8_t table[256]; make_table1(table); |
| 77 | return SkTableColorFilter::Create(table); |
| 78 | } |
| 79 | static SkColorFilter* make_cf2() { |
| 80 | uint8_t table[256]; make_table2(table); |
| 81 | return SkTableColorFilter::Create(table); |
| 82 | } |
| 83 | static 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 | |
| 90 | class TableColorFilterGM : public skiagm::GM { |
| 91 | public: |
| 92 | TableColorFilterGM() {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 93 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 94 | protected: |
| 95 | virtual SkString onShortName() { |
| 96 | return SkString("tablecolorfilter"); |
| 97 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 98 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 99 | virtual SkISize onISize() { |
reed@google.com | 6b2f7ea | 2012-03-14 20:00:42 +0000 | [diff] [blame] | 100 | return SkISize::Make(700, 300); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 101 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 102 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 103 | virtual void onDraw(SkCanvas* canvas) { |
| 104 | canvas->drawColor(0xFFDDDDDD); |
| 105 | canvas->translate(20, 20); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 106 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 107 | SkScalar x = 0, y = 0; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 108 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 109 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 113 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 114 | 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.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 125 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 126 | y += bm.height() * 9 / 8; |
| 127 | } |
| 128 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 129 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 130 | private: |
| 131 | typedef GM INHERITED; |
| 132 | }; |
| 133 | |
| 134 | ////////////////////////////////////////////////////////////////////////////// |
| 135 | |
| 136 | static skiagm::GM* MyFactory(void*) { return new TableColorFilterGM; } |
| 137 | static skiagm::GMRegistry reg(MyFactory); |