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" |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 10 | #include "SkColorFilterImageFilter.h" |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 11 | #include "SkGradientShader.h" |
| 12 | #include "SkTableColorFilter.h" |
| 13 | |
| 14 | static void make_bm0(SkBitmap* bm) { |
| 15 | int W = 120; |
| 16 | int H = 120; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 17 | bm->allocN32Pixels(W, H); |
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; |
reed | 5bd055c | 2015-03-01 19:16:38 -0800 | [diff] [blame^] | 22 | SkPoint pts[] = { {0, 0}, {SkIntToScalar(W), SkIntToScalar(H)} }; |
reed | b675a73 | 2015-03-01 18:00:47 -0800 | [diff] [blame] | 23 | SkColor colors[] = { |
reed | 5bd055c | 2015-03-01 19:16:38 -0800 | [diff] [blame^] | 24 | SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN, |
| 25 | SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE |
reed | b675a73 | 2015-03-01 18:00:47 -0800 | [diff] [blame] | 26 | }; |
reed | 5bd055c | 2015-03-01 19:16:38 -0800 | [diff] [blame^] | 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); |
reed | b675a73 | 2015-03-01 18:00:47 -0800 | [diff] [blame] | 31 | } |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 32 | static void make_bm1(SkBitmap* bm) { |
| 33 | int W = 120; |
| 34 | int H = 120; |
reed@google.com | eb9a46c | 2014-01-25 16:46:20 +0000 | [diff] [blame] | 35 | bm->allocN32Pixels(W, H); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 36 | bm->eraseColor(SK_ColorTRANSPARENT); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 37 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 38 | SkCanvas canvas(*bm); |
| 39 | SkPaint paint; |
reed | 5bd055c | 2015-03-01 19:16:38 -0800 | [diff] [blame^] | 40 | 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.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 50 | paint.setAntiAlias(true); |
| 51 | canvas.drawCircle(cx, cy, cx, paint); |
| 52 | } |
| 53 | |
| 54 | static 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 | } |
| 60 | static void make_table1(uint8_t table[]) { |
| 61 | for (int i = 0; i < 256; ++i) { |
| 62 | table[i] = i * i / 255; |
| 63 | } |
| 64 | } |
| 65 | static void make_table2(uint8_t table[]) { |
| 66 | for (int i = 0; i < 256; ++i) { |
| 67 | float fi = i / 255.0f; |
robertphillips@google.com | 6853e80 | 2012-04-16 15:50:18 +0000 | [diff] [blame] | 68 | table[i] = static_cast<uint8_t>(sqrtf(fi) * 255); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 72 | static SkColorFilter* make_null_cf() { |
| 73 | return NULL; |
| 74 | } |
| 75 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 76 | static SkColorFilter* make_cf0() { |
| 77 | uint8_t table[256]; make_table0(table); |
| 78 | return SkTableColorFilter::Create(table); |
| 79 | } |
| 80 | static SkColorFilter* make_cf1() { |
| 81 | uint8_t table[256]; make_table1(table); |
| 82 | return SkTableColorFilter::Create(table); |
| 83 | } |
| 84 | static SkColorFilter* make_cf2() { |
| 85 | uint8_t table[256]; make_table2(table); |
| 86 | return SkTableColorFilter::Create(table); |
| 87 | } |
| 88 | static 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 | |
| 95 | class TableColorFilterGM : public skiagm::GM { |
| 96 | public: |
| 97 | TableColorFilterGM() {} |
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 | protected: |
| 100 | virtual SkString onShortName() { |
| 101 | return SkString("tablecolorfilter"); |
| 102 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 103 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 104 | virtual SkISize onISize() { |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 105 | return SkISize::Make(700, 1650); |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 106 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 107 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 108 | virtual void onDraw(SkCanvas* canvas) { |
| 109 | canvas->drawColor(0xFFDDDDDD); |
| 110 | canvas->translate(20, 20); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 111 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 112 | |
| 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.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 137 | SkScalar x = 0, y = 0; |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 138 | for (size_t bitmapMaker = 0; bitmapMaker < SK_ARRAY_COUNT(gBitmapMakers); ++bitmapMaker) { |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 139 | SkBitmap bm; |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 140 | gBitmapMakers[bitmapMaker](&bm); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 141 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 142 | 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.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 146 | x = 0; |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 147 | SkPaint paint; |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 148 | canvas->drawBitmap(bm, x, y, &paint); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 149 | |
cwallez | c12b74d | 2015-01-26 07:45:53 -0800 | [diff] [blame] | 150 | // 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.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 181 | } |
| 182 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame] | 183 | |
mike@reedtribe.org | a69b48c | 2011-12-28 20:31:00 +0000 | [diff] [blame] | 184 | private: |
| 185 | typedef GM INHERITED; |
| 186 | }; |
| 187 | |
| 188 | ////////////////////////////////////////////////////////////////////////////// |
| 189 | |
reed | 5bd055c | 2015-03-01 19:16:38 -0800 | [diff] [blame^] | 190 | static skiagm::GM* MyFactory(void*) { return new TableColorFilterGM; } |
| 191 | static skiagm::GMRegistry reg(MyFactory); |