tablecolorfilter.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. #include "gm/gm.h"
  8. #include "include/core/SkBitmap.h"
  9. #include "include/core/SkBlendMode.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkColorFilter.h"
  13. #include "include/core/SkImageFilter.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkPoint.h"
  16. #include "include/core/SkRect.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkScalar.h"
  19. #include "include/core/SkShader.h"
  20. #include "include/core/SkSize.h"
  21. #include "include/core/SkString.h"
  22. #include "include/core/SkTileMode.h"
  23. #include "include/core/SkTypes.h"
  24. #include "include/effects/SkColorFilterImageFilter.h"
  25. #include "include/effects/SkGradientShader.h"
  26. #include "include/effects/SkTableColorFilter.h"
  27. #include <math.h>
  28. #include <utility>
  29. static sk_sp<SkShader> make_shader0(int w, int h) {
  30. SkPoint pts[] = { {0, 0}, {SkIntToScalar(w), SkIntToScalar(h)} };
  31. SkColor colors[] = {
  32. SK_ColorBLACK, SK_ColorGREEN, SK_ColorCYAN,
  33. SK_ColorRED, 0, SK_ColorBLUE, SK_ColorWHITE
  34. };
  35. return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
  36. SkTileMode::kClamp);
  37. }
  38. static void make_bm0(SkBitmap* bm) {
  39. int W = 120;
  40. int H = 120;
  41. bm->allocN32Pixels(W, H);
  42. bm->eraseColor(SK_ColorTRANSPARENT);
  43. SkCanvas canvas(*bm);
  44. SkPaint paint;
  45. paint.setShader(make_shader0(W, H));
  46. canvas.drawPaint(paint);
  47. }
  48. static sk_sp<SkShader> make_shader1(int w, int h) {
  49. SkScalar cx = SkIntToScalar(w)/2;
  50. SkScalar cy = SkIntToScalar(h)/2;
  51. SkColor colors[] = {
  52. SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE,
  53. };
  54. return SkGradientShader::MakeRadial(SkPoint::Make(cx, cy), cx, colors, nullptr,
  55. SK_ARRAY_COUNT(colors), SkTileMode::kClamp);
  56. }
  57. static void make_bm1(SkBitmap* bm) {
  58. int W = 120;
  59. int H = 120;
  60. SkScalar cx = SkIntToScalar(W)/2;
  61. SkScalar cy = SkIntToScalar(H)/2;
  62. bm->allocN32Pixels(W, H);
  63. bm->eraseColor(SK_ColorTRANSPARENT);
  64. SkCanvas canvas(*bm);
  65. SkPaint paint;
  66. paint.setShader(make_shader1(W, H));
  67. paint.setAntiAlias(true);
  68. canvas.drawCircle(cx, cy, cx, paint);
  69. }
  70. static void make_table0(uint8_t table[]) {
  71. for (int i = 0; i < 256; ++i) {
  72. int n = i >> 5;
  73. table[i] = (n << 5) | (n << 2) | (n >> 1);
  74. }
  75. }
  76. static void make_table1(uint8_t table[]) {
  77. for (int i = 0; i < 256; ++i) {
  78. table[i] = i * i / 255;
  79. }
  80. }
  81. static void make_table2(uint8_t table[]) {
  82. for (int i = 0; i < 256; ++i) {
  83. float fi = i / 255.0f;
  84. table[i] = static_cast<uint8_t>(sqrtf(fi) * 255);
  85. }
  86. }
  87. static sk_sp<SkColorFilter> make_null_cf() {
  88. return nullptr;
  89. }
  90. static sk_sp<SkColorFilter> make_cf0() {
  91. uint8_t table[256]; make_table0(table);
  92. return SkTableColorFilter::Make(table);
  93. }
  94. static sk_sp<SkColorFilter> make_cf1() {
  95. uint8_t table[256]; make_table1(table);
  96. return SkTableColorFilter::Make(table);
  97. }
  98. static sk_sp<SkColorFilter> make_cf2() {
  99. uint8_t table[256]; make_table2(table);
  100. return SkTableColorFilter::Make(table);
  101. }
  102. static sk_sp<SkColorFilter> make_cf3() {
  103. uint8_t table0[256]; make_table0(table0);
  104. uint8_t table1[256]; make_table1(table1);
  105. uint8_t table2[256]; make_table2(table2);
  106. return SkTableColorFilter::MakeARGB(nullptr, table0, table1, table2);
  107. }
  108. class TableColorFilterGM : public skiagm::GM {
  109. public:
  110. TableColorFilterGM() {}
  111. protected:
  112. SkString onShortName() override {
  113. return SkString("tablecolorfilter");
  114. }
  115. SkISize onISize() override {
  116. return {700, 1650};
  117. }
  118. void onDraw(SkCanvas* canvas) override {
  119. canvas->drawColor(0xFFDDDDDD);
  120. canvas->translate(20, 20);
  121. static sk_sp<SkColorFilter> (*gColorFilterMakers[])() = {
  122. make_null_cf, make_cf0, make_cf1, make_cf2, make_cf3
  123. };
  124. static void (*gBitmapMakers[])(SkBitmap*) = { make_bm0, make_bm1 };
  125. // This test will be done once for each bitmap with the results stacked vertically.
  126. // For a single bitmap the resulting image will be the following:
  127. // - A first line with the original bitmap, followed by the image drawn once
  128. // with each of the N color filters
  129. // - N lines of the bitmap drawn N times, this will cover all N*N combinations of
  130. // pair of color filters in order to test the collpsing of consecutive table
  131. // color filters.
  132. //
  133. // Here is a graphical representation of the result for 2 bitmaps and 2 filters
  134. // with the number corresponding to the number of filters the bitmap goes through:
  135. //
  136. // --bitmap1
  137. // 011
  138. // 22
  139. // 22
  140. // --bitmap2
  141. // 011
  142. // 22
  143. // 22
  144. SkScalar x = 0, y = 0;
  145. for (size_t bitmapMaker = 0; bitmapMaker < SK_ARRAY_COUNT(gBitmapMakers); ++bitmapMaker) {
  146. SkBitmap bm;
  147. gBitmapMakers[bitmapMaker](&bm);
  148. SkScalar xOffset = SkScalar(bm.width() * 9 / 8);
  149. SkScalar yOffset = SkScalar(bm.height() * 9 / 8);
  150. // Draw the first element of the first line
  151. x = 0;
  152. SkPaint paint;
  153. canvas->drawBitmap(bm, x, y, &paint);
  154. // Draws the rest of the first line for this bitmap
  155. // each draw being at xOffset of the previous one
  156. for (unsigned i = 1; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
  157. x += xOffset;
  158. paint.setColorFilter(gColorFilterMakers[i]());
  159. canvas->drawBitmap(bm, x, y, &paint);
  160. }
  161. paint.setColorFilter(nullptr);
  162. for (unsigned i = 0; i < SK_ARRAY_COUNT(gColorFilterMakers); ++i) {
  163. sk_sp<SkColorFilter> colorFilter1(gColorFilterMakers[i]());
  164. sk_sp<SkImageFilter> imageFilter1(SkColorFilterImageFilter::Make(
  165. std::move(colorFilter1), nullptr));
  166. // Move down to the next line and draw it
  167. // each draw being at xOffset of the previous one
  168. y += yOffset;
  169. x = 0;
  170. for (unsigned j = 1; j < SK_ARRAY_COUNT(gColorFilterMakers); ++j) {
  171. sk_sp<SkColorFilter> colorFilter2(gColorFilterMakers[j]());
  172. sk_sp<SkImageFilter> imageFilter2(SkColorFilterImageFilter::Make(
  173. std::move(colorFilter2), imageFilter1, nullptr));
  174. paint.setImageFilter(std::move(imageFilter2));
  175. canvas->drawBitmap(bm, x, y, &paint);
  176. x += xOffset;
  177. }
  178. }
  179. // Move down one line to the beginning of the block for next bitmap
  180. y += yOffset;
  181. }
  182. }
  183. private:
  184. typedef GM INHERITED;
  185. };
  186. DEF_GM( return new TableColorFilterGM; )
  187. //////////////////////////////////////////////////////////////////////////////
  188. class ComposeColorFilterGM : public skiagm::GM {
  189. enum {
  190. COLOR_COUNT = 3,
  191. MODE_COUNT = 4,
  192. };
  193. const SkColor* fColors;
  194. const SkBlendMode* fModes;
  195. const char* fName;
  196. public:
  197. ComposeColorFilterGM(const SkColor colors[], const SkBlendMode modes[], const char* name)
  198. : fColors(colors), fModes(modes), fName(name) {}
  199. private:
  200. SkString onShortName() override { return SkString(fName); }
  201. SkISize onISize() override { return {790, 790}; }
  202. void onDraw(SkCanvas* canvas) override {
  203. SkBitmap bm;
  204. make_bm1(&bm);
  205. canvas->drawColor(0xFFDDDDDD);
  206. const int MODES = MODE_COUNT * COLOR_COUNT;
  207. sk_sp<SkColorFilter> filters[MODES];
  208. int index = 0;
  209. for (int i = 0; i < MODE_COUNT; ++i) {
  210. for (int j = 0; j < COLOR_COUNT; ++j) {
  211. filters[index++] = SkColorFilters::Blend(fColors[j], fModes[i]);
  212. }
  213. }
  214. SkPaint paint;
  215. paint.setShader(make_shader1(50, 50));
  216. SkRect r = SkRect::MakeWH(50, 50);
  217. const SkScalar spacer = 10;
  218. canvas->translate(spacer, spacer);
  219. canvas->drawRect(r, paint); // orig
  220. for (int i = 0; i < MODES; ++i) {
  221. paint.setColorFilter(filters[i]);
  222. canvas->save();
  223. canvas->translate((i + 1) * (r.width() + spacer), 0);
  224. canvas->drawRect(r, paint);
  225. canvas->restore();
  226. canvas->save();
  227. canvas->translate(0, (i + 1) * (r.width() + spacer));
  228. canvas->drawRect(r, paint);
  229. canvas->restore();
  230. }
  231. canvas->translate(r.width() + spacer, r.width() + spacer);
  232. for (int y = 0; y < MODES; ++y) {
  233. canvas->save();
  234. for (int x = 0; x < MODES; ++x) {
  235. paint.setColorFilter(filters[y]->makeComposed(filters[x]));
  236. canvas->drawRect(r, paint);
  237. canvas->translate(r.width() + spacer, 0);
  238. }
  239. canvas->restore();
  240. canvas->translate(0, r.height() + spacer);
  241. }
  242. }
  243. };
  244. const SkColor gColors0[] = { SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW };
  245. const SkBlendMode gModes0[] = {
  246. SkBlendMode::kOverlay,
  247. SkBlendMode::kDarken,
  248. SkBlendMode::kColorBurn,
  249. SkBlendMode::kExclusion,
  250. };
  251. DEF_GM( return new ComposeColorFilterGM(gColors0, gModes0, "colorcomposefilter_wacky"); )
  252. const SkColor gColors1[] = { 0x80FF0000, 0x8000FF00, 0x800000FF };
  253. const SkBlendMode gModes1[] = {
  254. SkBlendMode::kSrcOver,
  255. SkBlendMode::kXor,
  256. SkBlendMode::kDstOut,
  257. SkBlendMode::kSrcATop,
  258. };
  259. DEF_GM( return new ComposeColorFilterGM(gColors1, gModes1, "colorcomposefilter_alpha"); )