bitmapfilters.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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/SkCanvas.h"
  10. #include "include/core/SkColor.h"
  11. #include "include/core/SkFilterQuality.h"
  12. #include "include/core/SkFont.h"
  13. #include "include/core/SkImageInfo.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkScalar.h"
  16. #include "include/core/SkSize.h"
  17. #include "include/core/SkString.h"
  18. #include "include/core/SkTypeface.h"
  19. #include "include/core/SkTypes.h"
  20. #include "tools/ToolUtils.h"
  21. static void make_bm(SkBitmap* bm) {
  22. const SkColor colors[4] = {
  23. SK_ColorRED, SK_ColorGREEN,
  24. SK_ColorBLUE, SK_ColorWHITE
  25. };
  26. SkPMColor colorsPM[4];
  27. for (size_t i = 0; i < SK_ARRAY_COUNT(colors); ++i) {
  28. colorsPM[i] = SkPreMultiplyColor(colors[i]);
  29. }
  30. bm->allocN32Pixels(2, 2, true);
  31. *bm->getAddr32(0, 0) = colorsPM[0];
  32. *bm->getAddr32(1, 0) = colorsPM[1];
  33. *bm->getAddr32(0, 1) = colorsPM[2];
  34. *bm->getAddr32(1, 1) = colorsPM[3];
  35. }
  36. static SkScalar draw_bm(SkCanvas* canvas, const SkBitmap& bm,
  37. SkScalar x, SkScalar y, SkPaint* paint) {
  38. canvas->drawBitmap(bm, x, y, paint);
  39. return SkIntToScalar(bm.width()) * 5/4;
  40. }
  41. static SkScalar draw_set(SkCanvas* c, const SkBitmap& bm, SkScalar x,
  42. SkPaint* p) {
  43. x += draw_bm(c, bm, x, 0, p);
  44. p->setFilterQuality(kLow_SkFilterQuality);
  45. x += draw_bm(c, bm, x, 0, p);
  46. p->setDither(true);
  47. return x + draw_bm(c, bm, x, 0, p);
  48. }
  49. static SkScalar draw_row(SkCanvas* canvas, const SkBitmap& bm) {
  50. SkAutoCanvasRestore acr(canvas, true);
  51. SkPaint paint;
  52. paint.setAntiAlias(true);
  53. SkScalar x = 0;
  54. const int scale = 32;
  55. SkFont font(ToolUtils::create_portable_typeface());
  56. const char* name = ToolUtils::colortype_name(bm.colorType());
  57. canvas->drawString(name, x, SkIntToScalar(bm.height())*scale*5/8,
  58. font, paint);
  59. canvas->translate(SkIntToScalar(48), 0);
  60. canvas->scale(SkIntToScalar(scale), SkIntToScalar(scale));
  61. x += draw_set(canvas, bm, 0, &paint);
  62. paint.reset();
  63. paint.setAlphaf(0.5f);
  64. draw_set(canvas, bm, x, &paint);
  65. return x * scale / 3;
  66. }
  67. class FilterGM : public skiagm::GM {
  68. void onOnceBeforeDraw() override {
  69. make_bm(&fBM32);
  70. ToolUtils::copy_to(&fBM4444, kARGB_4444_SkColorType, fBM32);
  71. ToolUtils::copy_to(&fBM16, kRGB_565_SkColorType, fBM32);
  72. }
  73. public:
  74. SkBitmap fBM4444, fBM16, fBM32;
  75. FilterGM() {
  76. this->setBGColor(0xFFDDDDDD);
  77. }
  78. protected:
  79. SkString onShortName() override {
  80. return SkString("bitmapfilters");
  81. }
  82. SkISize onISize() override {
  83. return SkISize::Make(540, 250);
  84. }
  85. void onDraw(SkCanvas* canvas) override {
  86. SkScalar x = SkIntToScalar(10);
  87. SkScalar y = SkIntToScalar(10);
  88. canvas->translate(x, y);
  89. y = draw_row(canvas, fBM4444);
  90. canvas->translate(0, y);
  91. y = draw_row(canvas, fBM16);
  92. canvas->translate(0, y);
  93. draw_row(canvas, fBM32);
  94. }
  95. private:
  96. typedef skiagm::GM INHERITED;
  97. };
  98. DEF_GM( return new FilterGM; )
  99. //////////////////////////////////////////////////////////////////////////////
  100. class TestExtractAlphaGM : public skiagm::GM {
  101. void onOnceBeforeDraw() override {
  102. // Make a bitmap with per-pixels alpha (stroked circle)
  103. fBitmap.allocN32Pixels(100, 100);
  104. SkCanvas canvas(fBitmap);
  105. canvas.clear(0);
  106. SkPaint paint;
  107. paint.setAntiAlias(true);
  108. paint.setColor(SK_ColorBLUE);
  109. paint.setStyle(SkPaint::kStroke_Style);
  110. paint.setStrokeWidth(20);
  111. canvas.drawCircle(50, 50, 39, paint);
  112. fBitmap.extractAlpha(&fAlpha);
  113. }
  114. public:
  115. SkBitmap fBitmap, fAlpha;
  116. protected:
  117. SkString onShortName() override {
  118. return SkString("extractalpha");
  119. }
  120. SkISize onISize() override {
  121. return SkISize::Make(540, 330);
  122. }
  123. void onDraw(SkCanvas* canvas) override {
  124. SkPaint paint;
  125. paint.setAntiAlias(true);
  126. paint.setFilterQuality(kLow_SkFilterQuality);
  127. paint.setColor(SK_ColorRED);
  128. canvas->drawBitmap(fBitmap, 10, 10, &paint); // should stay blue (ignore paint's color)
  129. canvas->drawBitmap(fAlpha, 120, 10, &paint); // should draw red
  130. }
  131. private:
  132. typedef skiagm::GM INHERITED;
  133. };
  134. DEF_GM( return new TestExtractAlphaGM; )