2 * Copyright 2006 The Android Open Source Project
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
8 #include "SkEmbossMaskFilter.h"
9 #include "SkBlurMaskFilter.h"
10 #include "SkBlurMask.h"
11 #include "SkEmbossMask.h"
12 #include "SkReadBuffer.h"
13 #include "SkWriteBuffer.h"
16 SkEmbossMaskFilter* SkEmbossMaskFilter::Create(SkScalar blurSigma, const Light& light) {
17 return SkNEW_ARGS(SkEmbossMaskFilter, (blurSigma, light));
20 static inline int pin2byte(int n) {
23 } else if (n > 0xFF) {
29 SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3],
30 SkScalar ambient, SkScalar specular,
31 SkScalar blurRadius) {
32 return SkBlurMaskFilter::CreateEmboss(SkBlurMask::ConvertRadiusToSigma(blurRadius),
33 direction, ambient, specular);
36 SkMaskFilter* SkBlurMaskFilter::CreateEmboss(SkScalar blurSigma, const SkScalar direction[3],
37 SkScalar ambient, SkScalar specular) {
38 if (direction == NULL) {
42 // ambient should be 0...1 as a scalar
43 int am = pin2byte(SkScalarToFixed(ambient) >> 8);
45 // specular should be 0..15.99 as a scalar
46 int sp = pin2byte(SkScalarToFixed(specular) >> 12);
48 SkEmbossMaskFilter::Light light;
50 memcpy(light.fDirection, direction, sizeof(light.fDirection));
51 light.fAmbient = SkToU8(am);
52 light.fSpecular = SkToU8(sp);
54 return SkEmbossMaskFilter::Create(blurSigma, light);
57 ///////////////////////////////////////////////////////////////////////////////
59 static void normalize(SkScalar v[3]) {
60 SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare(v[2]);
61 mag = SkScalarSqrt(mag);
63 for (int i = 0; i < 3; i++) {
64 v[i] = SkScalarDiv(v[i], mag);
68 SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
69 : fLight(light), fBlurSigma(blurSigma) {
70 normalize(fLight.fDirection);
73 SkMask::Format SkEmbossMaskFilter::getFormat() const {
74 return SkMask::k3D_Format;
77 bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src,
78 const SkMatrix& matrix, SkIPoint* margin) const {
79 SkScalar sigma = matrix.mapRadius(fBlurSigma);
81 if (!SkBlurMask::BoxBlur(dst, src, sigma, kInner_SkBlurStyle, kLow_SkBlurQuality)) {
85 dst->fFormat = SkMask::k3D_Format;
87 margin->set(SkScalarCeilToInt(3*sigma), SkScalarCeilToInt(3*sigma));
90 if (src.fImage == NULL) {
94 // create a larger buffer for the other two channels (should force fBlur to do this for us)
97 uint8_t* alphaPlane = dst->fImage;
98 size_t planeSize = dst->computeImageSize();
100 return false; // too big to allocate, abort
102 dst->fImage = SkMask::AllocImage(planeSize * 3);
103 memcpy(dst->fImage, alphaPlane, planeSize);
104 SkMask::FreeImage(alphaPlane);
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);
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],
117 SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1]));
119 SkEmbossMask::Emboss(dst, light);
121 // restore original alpha
122 memcpy(dst->fImage, src.fImage, src.computeImageSize());
127 SkEmbossMaskFilter::SkEmbossMaskFilter(SkReadBuffer& buffer)
128 : SkMaskFilter(buffer) {
129 SkASSERT(buffer.getArrayCount() == sizeof(Light));
130 buffer.readByteArray(&fLight, sizeof(Light));
131 SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean
132 fBlurSigma = buffer.readScalar();
135 void SkEmbossMaskFilter::flatten(SkWriteBuffer& buffer) const {
136 this->INHERITED::flatten(buffer);
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);
144 #ifndef SK_IGNORE_TO_STRING
145 void SkEmbossMaskFilter::toString(SkString* str) const {
146 str->append("SkEmbossMaskFilter: (");
148 str->append("direction: (");
149 str->appendScalar(fLight.fDirection[0]);
151 str->appendScalar(fLight.fDirection[1]);
153 str->appendScalar(fLight.fDirection[2]);
156 str->appendf("ambient: %d specular: %d ",
157 fLight.fAmbient, fLight.fSpecular);
159 str->append("blurSigma: ");
160 str->appendScalar(fBlurSigma);