matrixconvolution.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright 2012 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/SkFont.h"
  12. #include "include/core/SkImageFilter.h"
  13. #include "include/core/SkPaint.h"
  14. #include "include/core/SkPoint.h"
  15. #include "include/core/SkRect.h"
  16. #include "include/core/SkScalar.h"
  17. #include "include/core/SkShader.h"
  18. #include "include/core/SkSize.h"
  19. #include "include/core/SkString.h"
  20. #include "include/core/SkTileMode.h"
  21. #include "include/core/SkTypeface.h"
  22. #include "include/effects/SkGradientShader.h"
  23. #include "include/effects/SkMatrixConvolutionImageFilter.h"
  24. #include "tools/ToolUtils.h"
  25. namespace skiagm {
  26. class MatrixConvolutionGM : public GM {
  27. public:
  28. MatrixConvolutionGM(SkColor colorOne, SkColor colorTwo, const char* nameSuffix)
  29. : fNameSuffix(nameSuffix) {
  30. this->setBGColor(0x00000000);
  31. fColors[0] = colorOne;
  32. fColors[1] = colorTwo;
  33. }
  34. protected:
  35. SkString onShortName() override {
  36. return SkStringPrintf("matrixconvolution%s", fNameSuffix);
  37. }
  38. void makeBitmap() {
  39. // Draw our bitmap in N32, so legacy devices get "premul" values they understand
  40. fBitmap.allocN32Pixels(80, 80);
  41. SkCanvas canvas(fBitmap);
  42. canvas.clear(0x00000000);
  43. SkPaint paint;
  44. paint.setColor(0xFFFFFFFF);
  45. SkPoint pts[2] = { {0, 0},
  46. {0, 80.0f} };
  47. SkScalar pos[2] = { 0, 80.0f };
  48. paint.setShader(SkGradientShader::MakeLinear(
  49. pts, fColors, pos, 2, SkTileMode::kClamp));
  50. SkFont font(ToolUtils::create_portable_typeface(), 180.0f);
  51. canvas.drawString("e", -10.0f, 80.0f, font, paint);
  52. }
  53. SkISize onISize() override {
  54. return SkISize::Make(500, 300);
  55. }
  56. void draw(SkCanvas* canvas, int x, int y, const SkIPoint& kernelOffset,
  57. SkMatrixConvolutionImageFilter::TileMode tileMode, bool convolveAlpha,
  58. const SkImageFilter::CropRect* cropRect = nullptr) {
  59. SkScalar kernel[9] = {
  60. SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
  61. SkIntToScalar( 1), SkIntToScalar(-7), SkIntToScalar( 1),
  62. SkIntToScalar( 1), SkIntToScalar( 1), SkIntToScalar( 1),
  63. };
  64. SkISize kernelSize = SkISize::Make(3, 3);
  65. SkScalar gain = 0.3f, bias = SkIntToScalar(100);
  66. SkPaint paint;
  67. paint.setImageFilter(SkMatrixConvolutionImageFilter::Make(kernelSize,
  68. kernel,
  69. gain,
  70. bias,
  71. kernelOffset,
  72. tileMode,
  73. convolveAlpha,
  74. nullptr,
  75. cropRect));
  76. canvas->save();
  77. canvas->translate(SkIntToScalar(x), SkIntToScalar(y));
  78. const SkRect layerBounds = SkRect::MakeIWH(fBitmap.width(), fBitmap.height());
  79. canvas->clipRect(layerBounds);
  80. // This GM is, in part, intended to display the wrapping behavior of the
  81. // matrix image filter. The only (rational) way to achieve that for repeat mode
  82. // is to create a tight layer.
  83. canvas->saveLayer(layerBounds, &paint);
  84. canvas->drawBitmap(fBitmap, 0, 0, nullptr);
  85. canvas->restore();
  86. canvas->restore();
  87. }
  88. typedef SkMatrixConvolutionImageFilter MCIF;
  89. void onOnceBeforeDraw() override {
  90. this->makeBitmap();
  91. }
  92. void onDraw(SkCanvas* canvas) override {
  93. canvas->clear(SK_ColorBLACK);
  94. SkIPoint kernelOffset = SkIPoint::Make(1, 0);
  95. SkImageFilter::CropRect rect(SkRect::Make(fBitmap.bounds()));
  96. for (int x = 10; x < 310; x += 100) {
  97. this->draw(canvas, x, 10, kernelOffset, MCIF::kClamp_TileMode, true, &rect);
  98. this->draw(canvas, x, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &rect);
  99. this->draw(canvas, x, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &rect);
  100. kernelOffset.fY++;
  101. }
  102. kernelOffset.fY = 1;
  103. SkImageFilter::CropRect smallRect(SkRect::MakeXYWH(10, 5, 60, 60));
  104. this->draw(canvas, 310, 10, kernelOffset, MCIF::kClamp_TileMode, true, &smallRect);
  105. this->draw(canvas, 310, 110, kernelOffset, MCIF::kClampToBlack_TileMode, true, &smallRect);
  106. this->draw(canvas, 310, 210, kernelOffset, MCIF::kRepeat_TileMode, true, &smallRect);
  107. this->draw(canvas, 410, 10, kernelOffset, MCIF::kClamp_TileMode, false, &rect);
  108. this->draw(canvas, 410, 110, kernelOffset, MCIF::kClampToBlack_TileMode, false, &rect);
  109. this->draw(canvas, 410, 210, kernelOffset, MCIF::kRepeat_TileMode, false, &rect);
  110. }
  111. private:
  112. SkBitmap fBitmap;
  113. SkColor fColors[2];
  114. const char* fNameSuffix;
  115. typedef GM INHERITED;
  116. };
  117. //////////////////////////////////////////////////////////////////////////////
  118. DEF_GM(return new MatrixConvolutionGM(0xFFFFFFFF, 0x40404040, "");)
  119. DEF_GM(return new MatrixConvolutionGM(0xFFFF0000, 0xFF00FF00, "_color");)
  120. }