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