Debian Codegraph @

src/effects/SkEmbossMaskFilter.cpp

raw
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 "src/effects/SkEmbossMaskFilter.h"
9
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkBlurTypes.h"
12 #include "include/core/SkColorSpace.h"
13 #include "include/core/SkImageFilter.h"
14 #include "include/core/SkMatrix.h"
15 #include "include/core/SkPaint.h"
16 #include "include/core/SkPoint.h"
17 #include "include/core/SkPoint3.h"
18 #include "include/core/SkShader.h"
19 #include "include/core/SkTypes.h"
20 #include "include/effects/SkImageFilters.h"
21 #include "include/private/base/SkFloatingPoint.h"
22 #include "src/core/SkBlurMask.h"
23 #include "src/core/SkReadBuffer.h"
24 #include "src/core/SkWriteBuffer.h"
25 #include "src/effects/SkEmbossMask.h"
26
27 #if defined(SK_SUPPORT_LEGACY_EMBOSSMASKFILTER)
28 #include "include/effects/SkBlurMaskFilter.h"
29 #endif
30
31 #include <cstring>
32
33 sk_sp<SkMaskFilter> SkEmbossMaskFilter::Make(SkScalar blurSigma, const Light& light) {
34 if (!SkIsFinite(blurSigma) || blurSigma <= 0) {
35 return nullptr;
36 }
37
38 SkPoint3 lightDir{light.fDirection[0], light.fDirection[1], light.fDirection[2]};
39 if (!lightDir.normalize()) {
40 return nullptr;
41 }
42 Light newLight = light;
43 newLight.fDirection[0] = lightDir.x();
44 newLight.fDirection[1] = lightDir.y();
45 newLight.fDirection[2] = lightDir.z();
46
47 return sk_sp<SkMaskFilter>(new SkEmbossMaskFilter(blurSigma, newLight));
48 }
49
50 #ifdef SK_SUPPORT_LEGACY_EMBOSSMASKFILTER
51 sk_sp<SkMaskFilter> SkBlurMaskFilter::MakeEmboss(SkScalar blurSigma, const SkScalar direction[3],
52 SkScalar ambient, SkScalar specular) {
53 if (direction == nullptr) {
54 return nullptr;
55 }
56
57 SkEmbossMaskFilter::Light light;
58
59 memcpy(light.fDirection, direction, sizeof(light.fDirection));
60 // ambient should be 0...1 as a scalar
61 light.fAmbient = SkUnitScalarClampToByte(ambient);
62 // specular should be 0..15.99 as a scalar
63 static const SkScalar kSpecularMultiplier = SkIntToScalar(255) / 16;
64 light.fSpecular = static_cast<U8CPU>(SkTPin(specular, 0.0f, 16.0f) * kSpecularMultiplier + 0.5);
65
66 return SkEmbossMaskFilter::Make(blurSigma, light);
67 }
68 #endif
69
70 ///////////////////////////////////////////////////////////////////////////////
71
72 SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
73 : fLight(light), fBlurSigma(blurSigma)
74 {
75 SkASSERT(fBlurSigma > 0);
76 SkASSERT(SkIsFinite(fLight.fDirection, 3));
77 }
78
79 SkMask::Format SkEmbossMaskFilter::getFormat() const {
80 return SkMask::k3D_Format;
81 }
82
83 bool SkEmbossMaskFilter::filterMask(SkMaskBuilder* dst, const SkMask& src,
84 const SkMatrix& matrix, SkIPoint* margin) const {
85 if (src.fFormat != SkMask::kA8_Format) {
86 return false;
87 }
88
89 SkScalar sigma = matrix.mapRadius(fBlurSigma);
90
91 if (!SkBlurMask::BoxBlur(dst, src, sigma, kInner_SkBlurStyle)) {
92 return false;
93 }
94
95 dst->format() = SkMask::k3D_Format;
96 if (margin) {
97 margin->set(SkScalarCeilToInt(3*sigma), SkScalarCeilToInt(3*sigma));
98 }
99
100 if (src.fImage == nullptr) {
101 return true;
102 }
103
104 // create a larger buffer for the other two channels (should force fBlur to do this for us)
105
106 {
107 uint8_t* alphaPlane = dst->image();
108 size_t totalSize = dst->computeTotalImageSize();
109 if (totalSize == 0) {
110 return false; // too big to allocate, abort
111 }
112 size_t planeSize = dst->computeImageSize();
113 SkASSERT(planeSize != 0); // if totalSize didn't overflow, this can't either
114 dst->image() = SkMaskBuilder::AllocImage(totalSize);
115 memcpy(dst->image(), alphaPlane, planeSize);
116 SkMaskBuilder::FreeImage(alphaPlane);
117 }
118
119 // run the light direction through the matrix...
120 Light light = fLight;
121 matrix.mapVectors({(SkVector*)(void*)light.fDirection, 1},
122 {(SkVector*)(void*)fLight.fDirection, 1});
123
124 // now restore the length of the XY component
125 // cast to SkVector so we can call setLength (this double cast silences alias warnings)
126 SkVector* vec = (SkVector*)(void*)light.fDirection;
127 vec->setLength(light.fDirection[0],
128 light.fDirection[1],
129 SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1]));
130
131 SkEmbossMask::Emboss(dst, light);
132
133 // restore original alpha
134 memcpy(dst->image(), src.fImage, src.computeImageSize());
135
136 return true;
137 }
138
139 sk_sp<SkFlattenable> SkEmbossMaskFilter::CreateProc(SkReadBuffer& buffer) {
140 Light light;
141 if (buffer.readByteArray(&light, sizeof(Light))) {
142 light.fPad = 0; // for the font-cache lookup to be clean
143 const SkScalar sigma = buffer.readScalar();
144 return Make(sigma, light);
145 }
146 return nullptr;
147 }
148
149 void SkEmbossMaskFilter::flatten(SkWriteBuffer& buffer) const {
150 Light tmpLight = fLight;
151 tmpLight.fPad = 0; // for the font-cache lookup to be clean
152 buffer.writeByteArray(&tmpLight, sizeof(tmpLight));
153 buffer.writeScalar(fBlurSigma);
154 }
155
156 // This image filter uses coverage masks for operations but affects shading properties
157 // of a draw using the paint parameter, and returning true to indicate appliesShading.
158 std::pair<sk_sp<SkImageFilter>, bool> SkEmbossMaskFilter::asImageFilter(
159 const SkMatrix& ctm, const SkPaint& paint) const {
160 // Here the original bitmap we are operating on (nullptr for imageFilters) should be
161 // our coverage mask, as a white RGBA8 image where the alpha corresponds to the coverage.
162 sk_sp<SkImageFilter> coverageBlurred = SkImageFilters::Blur(fBlurSigma, fBlurSigma, nullptr);
163
164 // The paint should have the original shading properties that we want to apply.
165 sk_sp<SkShader> srcShader = SkShaders::Color(paint.getColor4f(), /*cs=*/nullptr);
166 if (paint.getShader()) {
167 srcShader = SkShaders::Blend(SkBlendMode::kDstIn, paint.refShader(), std::move(srcShader));
168 }
169 srcShader = srcShader->makeWithColorFilter(paint.refColorFilter());
170 sk_sp<SkImageFilter> srcColor = SkImageFilters::Shader(
171 std::move(srcShader), paint.isDither() ? SkImageFilters::Dither::kYes
172 : SkImageFilters::Dither::kNo);
173
174 // ka = fLight.fAmbient
175 float ambientf = fLight.fAmbient / 255.f;
176 SkColor4f ambientColor = {ambientf, ambientf, ambientf, 1};
177 sk_sp<SkImageFilter> ambient = SkImageFilters::Shader(SkShaders::Color(ambientColor, nullptr));
178
179 // L = fLight.fDirection
180 SkPoint3 lightDirection = SkPoint3::Make(fLight.fDirection[0],
181 fLight.fDirection[1],
182 fLight.fDirection[2]);
183
184
185 // Amount to scale the alpha by to calculate N, set this way to mimic the legacy
186 // emboss mask filter implementation.
187 // Made negative to match functionality of legacy emboss mask filter which calculates
188 // the normal "into" the monitor, away from the user, whereas all other documentation
189 // points normals towards negative directions (towards user).
190 const float surfaceScale = -255.f/ 32.f;
191
192 // diffuse = kd * dot(L, N)
193 sk_sp<SkImageFilter> diffuseCF = SkImageFilters::DistantLitDiffuse(lightDirection,
194 SK_ColorWHITE,
195 surfaceScale,
196 1,
197 coverageBlurred);
198 // mul = ka + diffuse
199 sk_sp<SkImageFilter> ambientdiffuse = SkImageFilters::Blend(SkBlendMode::kPlus,
200 diffuseCF,
201 ambient);
202 // ambientdiffuseColor = srcColor * mul
203 sk_sp<SkImageFilter> ambientdiffuseBlend = SkImageFilters::Blend(
204 SkBlendMode::kModulate, srcColor, ambientdiffuse);
205
206 // fLight.fSpecular is in a fixed 4.4 format.
207 // This uses the legacy implementation for emboss which calculates the specular
208 // lighting differently than standard specular functions.
209 //
210 // specular = ks * pow((2 * (L * N) - L_z) * L_z), shininess)
211 float shininess = ((fLight.fSpecular >> 4) + 1);
212
213 sk_sp<SkImageFilter> specular = LegacySpecular(lightDirection,
214 SK_ColorWHITE,
215 surfaceScale,
216 1,
217 shininess,
218 coverageBlurred);
219
220 // dstColor = ambientdiffuseColor + specular
221 // = srcColor * (ka + kd * dot(L, N)) + ks * pow((2 * (L * N) - L_z) * L_z), shininess)
222 sk_sp<SkImageFilter> finalFilter = SkImageFilters::Blend(SkBlendMode::kPlus,
223 ambientdiffuseBlend,
224 specular);
225 // Mask by original coverage mask, it remains unchanged.
226 // Return true to indicate applies shading.
227 return {SkImageFilters::Blend(SkBlendMode::kDstIn, finalFilter, nullptr), true};
228 }