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