imagemagnifier.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/SkImage.h"
  13. #include "include/core/SkImageFilter.h"
  14. #include "include/core/SkImageInfo.h"
  15. #include "include/core/SkPaint.h"
  16. #include "include/core/SkPixelRef.h"
  17. #include "include/core/SkRect.h"
  18. #include "include/core/SkRefCnt.h"
  19. #include "include/core/SkScalar.h"
  20. #include "include/core/SkTypeface.h"
  21. #include "include/effects/SkImageSource.h"
  22. #include "include/effects/SkMagnifierImageFilter.h"
  23. #include "include/utils/SkRandom.h"
  24. #include "tools/ToolUtils.h"
  25. #include <utility>
  26. #define WIDTH 500
  27. #define HEIGHT 500
  28. DEF_SIMPLE_GM_BG(imagemagnifier, canvas, WIDTH, HEIGHT, SK_ColorBLACK) {
  29. SkPaint filterPaint;
  30. filterPaint.setImageFilter(
  31. SkMagnifierImageFilter::Make(
  32. SkRect::MakeXYWH(SkIntToScalar(100), SkIntToScalar(100),
  33. SkIntToScalar(WIDTH / 2),
  34. SkIntToScalar(HEIGHT / 2)),
  35. 100, nullptr));
  36. canvas->saveLayer(nullptr, &filterPaint);
  37. const char* str = "The quick brown fox jumped over the lazy dog.";
  38. SkRandom rand;
  39. SkFont font(ToolUtils::create_portable_typeface());
  40. for (int i = 0; i < 25; ++i) {
  41. int x = rand.nextULessThan(WIDTH);
  42. int y = rand.nextULessThan(HEIGHT);
  43. SkPaint paint;
  44. paint.setColor(ToolUtils::color_to_565(rand.nextBits(24) | 0xFF000000));
  45. font.setSize(rand.nextRangeScalar(0, 300));
  46. canvas->drawString(str, SkIntToScalar(x), SkIntToScalar(y), font, paint);
  47. }
  48. canvas->restore();
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////
  51. #define WIDTH_HEIGHT 256
  52. static sk_sp<SkImage> make_img() {
  53. SkBitmap bitmap;
  54. bitmap.allocN32Pixels(WIDTH_HEIGHT, WIDTH_HEIGHT);
  55. SkCanvas canvas(bitmap);
  56. canvas.clear(0x0);
  57. SkPaint paint;
  58. paint.setColor(SK_ColorBLUE);
  59. for (float pos = 0; pos < WIDTH_HEIGHT; pos += 16) {
  60. canvas.drawLine(0, pos, SkIntToScalar(WIDTH_HEIGHT), pos, paint);
  61. canvas.drawLine(pos, 0, pos, SkIntToScalar(WIDTH_HEIGHT), paint);
  62. }
  63. SkBitmap result;
  64. result.setInfo(SkImageInfo::MakeS32(WIDTH_HEIGHT, WIDTH_HEIGHT, kPremul_SkAlphaType));
  65. result.setPixelRef(sk_ref_sp(bitmap.pixelRef()), 0, 0);
  66. return SkImage::MakeFromBitmap(result);
  67. }
  68. DEF_SIMPLE_GM_BG(imagemagnifier_cropped, canvas, WIDTH_HEIGHT, WIDTH_HEIGHT, SK_ColorBLACK) {
  69. sk_sp<SkImage> image(make_img());
  70. sk_sp<SkImageFilter> imageSource(SkImageSource::Make(std::move(image)));
  71. SkRect srcRect = SkRect::MakeWH(SkIntToScalar(WIDTH_HEIGHT-32),
  72. SkIntToScalar(WIDTH_HEIGHT-32));
  73. srcRect.inset(64.0f, 64.0f);
  74. constexpr SkScalar kInset = 64.0f;
  75. // Crop out a 16 pixel ring around the result
  76. const SkRect rect = SkRect::MakeXYWH(16, 16, WIDTH_HEIGHT-32, WIDTH_HEIGHT-32);
  77. SkImageFilter::CropRect cropRect(rect);
  78. SkPaint filterPaint;
  79. filterPaint.setImageFilter(SkMagnifierImageFilter::Make(srcRect, kInset,
  80. std::move(imageSource),
  81. &cropRect));
  82. canvas->saveLayer(nullptr, &filterPaint);
  83. canvas->restore();
  84. }