imageblur.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkFont.h"
  11. #include "include/core/SkImageFilter.h"
  12. #include "include/core/SkPaint.h"
  13. #include "include/core/SkScalar.h"
  14. #include "include/core/SkTypeface.h"
  15. #include "include/effects/SkBlurImageFilter.h"
  16. #include "include/utils/SkRandom.h"
  17. #include "tools/ToolUtils.h"
  18. #define WIDTH 500
  19. #define HEIGHT 500
  20. void imageblurgm_draw(SkScalar fSigmaX, SkScalar fSigmaY, SkCanvas* canvas) {
  21. SkPaint paint;
  22. paint.setImageFilter(SkBlurImageFilter::Make(fSigmaX, fSigmaY, nullptr));
  23. canvas->saveLayer(nullptr, &paint);
  24. const char* str = "The quick brown fox jumped over the lazy dog.";
  25. SkRandom rand;
  26. SkPaint textPaint;
  27. SkFont font(ToolUtils::create_portable_typeface());
  28. for (int i = 0; i < 25; ++i) {
  29. int x = rand.nextULessThan(WIDTH);
  30. int y = rand.nextULessThan(HEIGHT);
  31. textPaint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000));
  32. font.setSize(rand.nextRangeScalar(0, 300));
  33. canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, textPaint);
  34. }
  35. canvas->restore();
  36. }
  37. DEF_SIMPLE_GM_BG(imageblur, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
  38. imageblurgm_draw(24.0f, 0.0f, canvas);
  39. }
  40. DEF_SIMPLE_GM_BG(imageblur_large, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
  41. imageblurgm_draw(80.0f, 80.0f, canvas);
  42. }