alpha_image.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright 2017 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/SkBlurTypes.h"
  10. #include "include/core/SkCanvas.h"
  11. #include "include/core/SkColor.h"
  12. #include "include/core/SkColorFilter.h"
  13. #include "include/core/SkImage.h"
  14. #include "include/core/SkImageInfo.h"
  15. #include "include/core/SkMaskFilter.h"
  16. #include "include/core/SkPaint.h"
  17. #include "include/core/SkRefCnt.h"
  18. #include "include/core/SkShader.h"
  19. static SkBitmap make_alpha_image(int w, int h) {
  20. SkBitmap bm;
  21. bm.allocPixels(SkImageInfo::MakeA8(w, h));
  22. bm.eraseARGB(10, 0, 0 , 0);
  23. for (int y = 0; y < bm.height(); ++y) {
  24. for (int x = y; x < bm.width(); ++x) {
  25. *bm.getAddr8(x, y) = 0xFF;
  26. }
  27. }
  28. bm.setImmutable();
  29. return bm;
  30. }
  31. static sk_sp<SkColorFilter> make_color_filter() {
  32. float colorMatrix[20] = {
  33. 1, 0, 0, 0, 0,
  34. 0, 1, 0, 0, 0,
  35. 0, 0, 0.5, 0.5, 0,
  36. 0, 0, 0.5, 0.5, 0}; // mix G and A.
  37. return SkColorFilters::Matrix(colorMatrix);
  38. }
  39. DEF_SIMPLE_GM(alpha_image, canvas, 256, 256) {
  40. auto image = SkImage::MakeFromBitmap(make_alpha_image(96, 96));
  41. SkPaint paint;
  42. paint.setColorFilter(make_color_filter());
  43. paint.setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, 10.0f));
  44. canvas->drawImage(image.get(), 16, 16, &paint);
  45. paint.setColorFilter(nullptr);
  46. paint.setShader(SkShaders::Color(SK_ColorCYAN));
  47. canvas->drawImage(image.get(), 144, 16, &paint);
  48. paint.setColorFilter(make_color_filter());
  49. canvas->drawImage(image.get(), 16, 144, &paint);
  50. paint.setMaskFilter(nullptr);
  51. canvas->drawImage(image.get(), 144, 144, &paint);
  52. }