SkEmbossMaskFilter.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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 "include/core/SkColorPriv.h"
  8. #include "include/core/SkString.h"
  9. #include "include/effects/SkBlurMaskFilter.h"
  10. #include "src/core/SkBlurMask.h"
  11. #include "src/core/SkReadBuffer.h"
  12. #include "src/core/SkWriteBuffer.h"
  13. #include "src/effects/SkEmbossMask.h"
  14. #include "src/effects/SkEmbossMaskFilter.h"
  15. static void normalize3(SkScalar dst[3], const SkScalar src[3]) {
  16. SkScalar mag = SkScalarSquare(src[0]) + SkScalarSquare(src[1]) + SkScalarSquare(src[2]);
  17. SkScalar scale = SkScalarInvert(SkScalarSqrt(mag));
  18. for (int i = 0; i < 3; i++) {
  19. dst[i] = src[i] * scale;
  20. }
  21. }
  22. sk_sp<SkMaskFilter> SkEmbossMaskFilter::Make(SkScalar blurSigma, const Light& light) {
  23. if (!SkScalarIsFinite(blurSigma) || blurSigma <= 0) {
  24. return nullptr;
  25. }
  26. Light newLight = light;
  27. normalize3(newLight.fDirection, light.fDirection);
  28. if (!SkScalarsAreFinite(newLight.fDirection, 3)) {
  29. return nullptr;
  30. }
  31. return sk_sp<SkMaskFilter>(new SkEmbossMaskFilter(blurSigma, newLight));
  32. }
  33. #ifdef SK_SUPPORT_LEGACY_EMBOSSMASKFILTER
  34. sk_sp<SkMaskFilter> SkBlurMaskFilter::MakeEmboss(SkScalar blurSigma, const SkScalar direction[3],
  35. SkScalar ambient, SkScalar specular) {
  36. if (direction == nullptr) {
  37. return nullptr;
  38. }
  39. SkEmbossMaskFilter::Light light;
  40. memcpy(light.fDirection, direction, sizeof(light.fDirection));
  41. // ambient should be 0...1 as a scalar
  42. light.fAmbient = SkUnitScalarClampToByte(ambient);
  43. // specular should be 0..15.99 as a scalar
  44. static const SkScalar kSpecularMultiplier = SkIntToScalar(255) / 16;
  45. light.fSpecular = static_cast<U8CPU>(SkScalarPin(specular, 0, 16) * kSpecularMultiplier + 0.5);
  46. return SkEmbossMaskFilter::Make(blurSigma, light);
  47. }
  48. #endif
  49. ///////////////////////////////////////////////////////////////////////////////
  50. SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
  51. : fLight(light), fBlurSigma(blurSigma)
  52. {
  53. SkASSERT(fBlurSigma > 0);
  54. SkASSERT(SkScalarsAreFinite(fLight.fDirection, 3));
  55. }
  56. SkMask::Format SkEmbossMaskFilter::getFormat() const {
  57. return SkMask::k3D_Format;
  58. }
  59. bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src,
  60. const SkMatrix& matrix, SkIPoint* margin) const {
  61. if (src.fFormat != SkMask::kA8_Format) {
  62. return false;
  63. }
  64. SkScalar sigma = matrix.mapRadius(fBlurSigma);
  65. if (!SkBlurMask::BoxBlur(dst, src, sigma, kInner_SkBlurStyle)) {
  66. return false;
  67. }
  68. dst->fFormat = SkMask::k3D_Format;
  69. if (margin) {
  70. margin->set(SkScalarCeilToInt(3*sigma), SkScalarCeilToInt(3*sigma));
  71. }
  72. if (src.fImage == nullptr) {
  73. return true;
  74. }
  75. // create a larger buffer for the other two channels (should force fBlur to do this for us)
  76. {
  77. uint8_t* alphaPlane = dst->fImage;
  78. size_t planeSize = dst->computeImageSize();
  79. if (0 == planeSize) {
  80. return false; // too big to allocate, abort
  81. }
  82. dst->fImage = SkMask::AllocImage(planeSize * 3);
  83. memcpy(dst->fImage, alphaPlane, planeSize);
  84. SkMask::FreeImage(alphaPlane);
  85. }
  86. // run the light direction through the matrix...
  87. Light light = fLight;
  88. matrix.mapVectors((SkVector*)(void*)light.fDirection,
  89. (SkVector*)(void*)fLight.fDirection, 1);
  90. // now restore the length of the XY component
  91. // cast to SkVector so we can call setLength (this double cast silences alias warnings)
  92. SkVector* vec = (SkVector*)(void*)light.fDirection;
  93. vec->setLength(light.fDirection[0],
  94. light.fDirection[1],
  95. SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1]));
  96. SkEmbossMask::Emboss(dst, light);
  97. // restore original alpha
  98. memcpy(dst->fImage, src.fImage, src.computeImageSize());
  99. return true;
  100. }
  101. sk_sp<SkFlattenable> SkEmbossMaskFilter::CreateProc(SkReadBuffer& buffer) {
  102. Light light;
  103. if (buffer.readByteArray(&light, sizeof(Light))) {
  104. light.fPad = 0; // for the font-cache lookup to be clean
  105. const SkScalar sigma = buffer.readScalar();
  106. return Make(sigma, light);
  107. }
  108. return nullptr;
  109. }
  110. void SkEmbossMaskFilter::flatten(SkWriteBuffer& buffer) const {
  111. Light tmpLight = fLight;
  112. tmpLight.fPad = 0; // for the font-cache lookup to be clean
  113. buffer.writeByteArray(&tmpLight, sizeof(tmpLight));
  114. buffer.writeScalar(fBlurSigma);
  115. }