imagefilterscropexpand.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2014 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/SkColorFilter.h"
  12. #include "include/core/SkImage.h"
  13. #include "include/core/SkImageFilter.h"
  14. #include "include/core/SkPaint.h"
  15. #include "include/core/SkPoint.h"
  16. #include "include/core/SkPoint3.h"
  17. #include "include/core/SkRect.h"
  18. #include "include/core/SkRefCnt.h"
  19. #include "include/core/SkScalar.h"
  20. #include "include/core/SkShader.h"
  21. #include "include/core/SkSurface.h"
  22. #include "include/core/SkTileMode.h"
  23. #include "include/effects/SkBlurImageFilter.h"
  24. #include "include/effects/SkColorFilterImageFilter.h"
  25. #include "include/effects/SkDisplacementMapEffect.h"
  26. #include "include/effects/SkDropShadowImageFilter.h"
  27. #include "include/effects/SkGradientShader.h"
  28. #include "include/effects/SkImageSource.h"
  29. #include "include/effects/SkLightingImageFilter.h"
  30. #include "include/effects/SkMorphologyImageFilter.h"
  31. #include "include/effects/SkOffsetImageFilter.h"
  32. #include <utility>
  33. namespace {
  34. void make_checkerboard(SkBitmap* bitmap);
  35. sk_sp<SkImage> make_gradient_circle(int width, int height);
  36. void draw(SkCanvas* canvas, const SkBitmap& bitmap, const SkRect& rect,
  37. sk_sp<SkImageFilter> filter);
  38. };
  39. ///////////////////////////////////////////////////////////////////////////////
  40. DEF_SIMPLE_GM(imagefilterscropexpand, canvas, 730, 650) {
  41. SkImageFilter::CropRect cropRect(
  42. SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
  43. SkImageFilter::CropRect::kHasAll_CropEdge);
  44. sk_sp<SkImage> gradientCircle(make_gradient_circle(64, 64));
  45. SkBitmap checkerboard;
  46. make_checkerboard(&checkerboard);
  47. sk_sp<SkImageFilter> gradientCircleSource(SkImageSource::Make(std::move(gradientCircle)));
  48. sk_sp<SkImageFilter> noopCropped(SkOffsetImageFilter::Make(0, 0, nullptr, &cropRect));
  49. // This color matrix saturates the green component but only partly increases the opacity.
  50. // For the opaque checkerboard, the opacity boost doesn't matter but it does impact the
  51. // area outside the checkerboard.
  52. float matrix[20] = { 1, 0, 0, 0, 0,
  53. 0, 1, 0, 0, 1,
  54. 0, 0, 1, 0, 0,
  55. 0, 0, 0, 1, 32.0f/255 };
  56. sk_sp<SkColorFilter> cfAlphaTrans(SkColorFilters::Matrix(matrix));
  57. SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
  58. SkScalar MARGIN = SkIntToScalar(12);
  59. SkPoint3 pointLocation = SkPoint3::Make(0, 0, SkIntToScalar(10));
  60. SkScalar kd = SkIntToScalar(2);
  61. SkScalar surfaceScale = SkIntToScalar(1);
  62. SkIRect bounds;
  63. r.roundOut(&bounds);
  64. SkPaint paint;
  65. canvas->translate(MARGIN, MARGIN);
  66. for (int outset = -15; outset <= 20; outset += 5) {
  67. canvas->save();
  68. SkRect rect = cropRect.rect();
  69. rect.outset(SkIntToScalar(outset),
  70. SkIntToScalar(outset));
  71. SkImageFilter::CropRect bigRect(rect, SkImageFilter::CropRect::kHasAll_CropEdge);
  72. draw(canvas, checkerboard, rect, SkColorFilterImageFilter::Make(cfAlphaTrans,
  73. noopCropped,
  74. &bigRect));
  75. draw(canvas, checkerboard, rect, SkBlurImageFilter::Make(0.3f, 0.3f,
  76. noopCropped,
  77. &bigRect));
  78. draw(canvas, checkerboard, rect, SkBlurImageFilter::Make(8.0f, 8.0f,
  79. noopCropped,
  80. &bigRect));
  81. draw(canvas, checkerboard, rect, SkDilateImageFilter::Make(2, 2,
  82. noopCropped,
  83. &bigRect));
  84. draw(canvas, checkerboard, rect, SkErodeImageFilter::Make(2, 2,
  85. noopCropped,
  86. &bigRect));
  87. draw(canvas, checkerboard, rect,
  88. SkDropShadowImageFilter::Make(
  89. SkIntToScalar(10),
  90. SkIntToScalar(10),
  91. SkIntToScalar(3),
  92. SkIntToScalar(3),
  93. SK_ColorBLUE,
  94. SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
  95. noopCropped,
  96. &bigRect));
  97. draw(canvas, checkerboard, rect,
  98. SkDisplacementMapEffect::Make(SkDisplacementMapEffect::kR_ChannelSelectorType,
  99. SkDisplacementMapEffect::kR_ChannelSelectorType,
  100. SkIntToScalar(12),
  101. gradientCircleSource,
  102. noopCropped,
  103. &bigRect));
  104. draw(canvas, checkerboard, rect,
  105. SkOffsetImageFilter::Make(SkIntToScalar(-8), SkIntToScalar(16),
  106. noopCropped,
  107. &bigRect));
  108. draw(canvas, checkerboard, rect,
  109. SkLightingImageFilter::MakePointLitDiffuse(pointLocation,
  110. SK_ColorWHITE,
  111. surfaceScale,
  112. kd,
  113. noopCropped,
  114. &bigRect));
  115. canvas->restore();
  116. canvas->translate(0, SkIntToScalar(80));
  117. }
  118. }
  119. namespace {
  120. void make_checkerboard(SkBitmap* bitmap) {
  121. bitmap->allocN32Pixels(64, 64);
  122. SkCanvas canvas(*bitmap);
  123. canvas.clear(0xFFFF0000);
  124. SkPaint darkPaint;
  125. darkPaint.setColor(0xFF404040);
  126. SkPaint lightPaint;
  127. lightPaint.setColor(0xFFA0A0A0);
  128. for (int y = 8; y < 48; y += 16) {
  129. for (int x = 8; x < 48; x += 16) {
  130. canvas.save();
  131. canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
  132. canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
  133. canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
  134. canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
  135. canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
  136. canvas.restore();
  137. }
  138. }
  139. }
  140. sk_sp<SkImage> make_gradient_circle(int width, int height) {
  141. SkScalar x = SkIntToScalar(width / 2);
  142. SkScalar y = SkIntToScalar(height / 2);
  143. SkScalar radius = SkMinScalar(x, y) * 0.8f;
  144. auto surface(SkSurface::MakeRasterN32Premul(width, height));
  145. SkCanvas* canvas = surface->getCanvas();
  146. canvas->clear(0x00000000);
  147. SkColor colors[2];
  148. colors[0] = SK_ColorWHITE;
  149. colors[1] = SK_ColorBLACK;
  150. SkPaint paint;
  151. paint.setShader(SkGradientShader::MakeRadial(SkPoint::Make(x, y), radius, colors, nullptr,
  152. 2, SkTileMode::kClamp));
  153. canvas->drawCircle(x, y, radius, paint);
  154. return surface->makeImageSnapshot();
  155. }
  156. void draw(SkCanvas* canvas, const SkBitmap& bitmap, const SkRect& rect,
  157. sk_sp<SkImageFilter> filter) {
  158. SkPaint paint;
  159. paint.setImageFilter(std::move(filter));
  160. canvas->saveLayer(&rect, &paint);
  161. canvas->drawBitmap(bitmap, 0, 0);
  162. canvas->restore();
  163. SkPaint strokePaint;
  164. strokePaint.setColor(0xFFFF0000);
  165. strokePaint.setStyle(SkPaint::kStroke_Style);
  166. canvas->drawRect(rect, strokePaint);
  167. canvas->translate(SkIntToScalar(80), 0);
  168. }
  169. };