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; |
| 16 | bm->setConfig(SkBitmap::kARGB_8888_Config, W, H); |
| 17 | bm->allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 18 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 19 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 20 | SkCanvas canvas(*bm); |
| 21 | SkPaint paint; |
bsalomon@google.com | cadbcb8 | 2012-01-06 19:22:11 +0000 | [diff] [blame] | 22 | SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} }; |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 23 | SkColor colors[] = { |
| 24 | SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN, |
| 25 | SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE |
| 26 | }; |
| 27 | SkShader* s = SkGradientShader::CreateLinear(pts, colors, NULL, SK_ARRAY_COUNT(colors), |
| 28 | SkShader::kClamp_TileMode); |
| 29 | paint.setShader(s)->unref(); |
| 30 | canvas.drawPaint(paint); |
| 31 | } |
| 32 | static void make_bm1(SkBitmap* bm) { |
| 33 | int W = 120; |
| 34 | int H = 120; |
| 35 | bm->setConfig(SkBitmap::kARGB_8888_Config, W, H); |
| 36 | bm->allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 37 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 38 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 39 | SkCanvas canvas(*bm); |
| 40 | SkPaint paint; |
| 41 | SkScalar cx = SkIntToScalar(W)/2; |
| 42 | SkScalar cy = SkIntToScalar(H)/2; |
| 43 | SkColor colors[] = { |
| 44 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, |
| 45 | }; |
| 46 | SkShader* s = SkGradientShader::CreateRadial(SkPoint::Make(SkIntToScalar(W)/2, |
| 47 | SkIntToScalar(H)/2), |
| 48 | SkIntToScalar(W)/2, colors, NULL, SK_ARRAY_COUNT(colors), |
| 49 | SkShader::kClamp_TileMode); |
| 50 | paint.setShader(s)->unref(); |
| 51 | paint.setAntiAlias(true); |
| 52 | canvas.drawCircle(cx, cy, cx, paint); |
| 53 | } |
| 54 | |
| 55 | static void make_table0(uint8_t table[]) { |
| 56 | for (int i = 0; i < 256; ++i) { |
| 57 | int n = i >> 5; |
| 58 | table[i] = (n << 5) | (n << 2) | (n >> 1); |
| 59 | } |
| 60 | } |
| 61 | static void make_table1(uint8_t table[]) { |
| 62 | for (int i = 0; i < 256; ++i) { |
| 63 | table[i] = i * i / 255; |
| 64 | } |
| 65 | } |
| 66 | static void make_table2(uint8_t table[]) { |
| 67 | for (int i = 0; i < 256; ++i) { |
| 68 | float fi = i / 255.0f; |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 69 | table[i] = static_cast<uint8_t>(sqrtf(fi) * 255); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 70 | } |
| 71 | } |
| 72 | |
| 73 | static SkColorFilter* make_cf0() { |
| 74 | uint8_t table[256]; make_table0(table); |
| 75 | return SkTableColorFilter::Create(table); |
| 76 | } |
| 77 | static SkColorFilter* make_cf1() { |
| 78 | uint8_t table[256]; make_table1(table); |
| 79 | return SkTableColorFilter::Create(table); |
| 80 | } |
| 81 | static SkColorFilter* make_cf2() { |
| 82 | uint8_t table[256]; make_table2(table); |
| 83 | return SkTableColorFilter::Create(table); |
| 84 | } |
| 85 | static SkColorFilter* make_cf3() { |
| 86 | uint8_t table0[256]; make_table0(table0); |
| 87 | uint8_t table1[256]; make_table1(table1); |
| 88 | uint8_t table2[256]; make_table2(table2); |
| 89 | return SkTableColorFilter::CreateARGB(NULL, table0, table1, table2); |
| 90 | } |
| 91 | |
| 92 | class TableColorFilterGM : public skiagm::GM { |
| 93 | public: |
| 94 | TableColorFilterGM() {} |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 95 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 96 | protected: |
| 97 | virtual SkString onShortName() { |
| 98 | return SkString("tablecolorfilter"); |
| 99 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 100 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 101 | virtual SkISize onISize() { |
reed@google.com | 6b2f7ea | 2012-03-14 20:00:42 +0000 | [diff] [blame] | 102 | return SkISize::Make(700, 300); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 103 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 104 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 105 | virtual void onDraw(SkCanvas* canvas) { |
| 106 | canvas->drawColor(0xFFDDDDDD); |
| 107 | canvas->translate(20, 20); |
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 | SkScalar x = 0, y = 0; |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 110 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 111 | static void (*gMakers[])(SkBitmap*) = { make_bm0, make_bm1 }; |
| 112 | for (size_t maker = 0; maker < SK_ARRAY_COUNT(gMakers); ++maker) { |
| 113 | SkBitmap bm; |
| 114 | gMakers[maker](&bm); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 115 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 116 | SkPaint paint; |
| 117 | x = 0; |
| 118 | canvas->drawBitmap(bm, x, y, &paint); |
| 119 | paint.setColorFilter(make_cf0())->unref(); x += bm.width() * 9 / 8; |
| 120 | canvas->drawBitmap(bm, x, y, &paint); |
| 121 | paint.setColorFilter(make_cf1())->unref(); x += bm.width() * 9 / 8; |
| 122 | canvas->drawBitmap(bm, x, y, &paint); |
| 123 | paint.setColorFilter(make_cf2())->unref(); x += bm.width() * 9 / 8; |
| 124 | canvas->drawBitmap(bm, x, y, &paint); |
| 125 | paint.setColorFilter(make_cf3())->unref(); x += bm.width() * 9 / 8; |
| 126 | canvas->drawBitmap(bm, x, y, &paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 127 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 128 | y += bm.height() * 9 / 8; |
| 129 | } |
| 130 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 131 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 132 | private: |
| 133 | typedef GM INHERITED; |
| 134 | }; |
| 135 | |
| 136 | ////////////////////////////////////////////////////////////////////////////// |
| 137 | |
| 138 | static skiagm::GM* MyFactory(void*) { return new TableColorFilterGM; } |
| 139 | static skiagm::GMRegistry reg(MyFactory); |