blob: 245ccd69dfa83899dd1468d3587a4863cb76b3c0 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkEmbossMaskFilter.h"
11#include "SkBlurMaskFilter.h"
12#include "SkBlurMask.h"
13#include "SkEmbossMask.h"
14#include "SkBuffer.h"
15
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000016static inline int pin2byte(int n) {
17 if (n < 0) {
18 n = 0;
19 } else if (n > 0xFF) {
20 n = 0xFF;
21 }
22 return n;
23}
24
reed@android.com8a1c16f2008-12-17 15:59:43 +000025SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3],
26 SkScalar ambient, SkScalar specular,
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000027 SkScalar blurRadius) {
28 if (direction == NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 return NULL;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000030 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000031
32 // ambient should be 0...1 as a scalar
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000033 int am = pin2byte(SkScalarToFixed(ambient) >> 8);
reed@android.com8a1c16f2008-12-17 15:59:43 +000034
35 // specular should be 0..15.99 as a scalar
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000036 int sp = pin2byte(SkScalarToFixed(specular) >> 12);
reed@android.com8a1c16f2008-12-17 15:59:43 +000037
38 SkEmbossMaskFilter::Light light;
39
40 memcpy(light.fDirection, direction, sizeof(light.fDirection));
41 light.fAmbient = SkToU8(am);
42 light.fSpecular = SkToU8(sp);
43
44 return SkNEW_ARGS(SkEmbossMaskFilter, (light, blurRadius));
45}
46
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000047///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000048
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000049static void normalize(SkScalar v[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare(v[2]);
51 mag = SkScalarSqrt(mag);
52
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000053 for (int i = 0; i < 3; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000054 v[i] = SkScalarDiv(v[i], mag);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000055 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000056}
57
58SkEmbossMaskFilter::SkEmbossMaskFilter(const Light& light, SkScalar blurRadius)
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000059 : fLight(light), fBlurRadius(blurRadius) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 normalize(fLight.fDirection);
61}
62
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000063SkMask::Format SkEmbossMaskFilter::getFormat() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000064 return SkMask::k3D_Format;
65}
66
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000067bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src,
68 const SkMatrix& matrix, SkIPoint* margin) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 SkScalar radius = matrix.mapRadius(fBlurRadius);
70
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000071 if (!SkBlurMask::Blur(dst, src, radius, SkBlurMask::kInner_Style,
72 SkBlurMask::kLow_Quality)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 return false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000074 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
76 dst->fFormat = SkMask::k3D_Format;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000077 if (margin) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 margin->set(SkScalarCeil(radius), SkScalarCeil(radius));
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000079 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000081 if (src.fImage == NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 return true;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000083 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
85 // create a larger buffer for the other two channels (should force fBlur to do this for us)
86
87 {
88 uint8_t* alphaPlane = dst->fImage;
89 size_t planeSize = dst->computeImageSize();
reed@android.com543ed932009-04-24 12:43:40 +000090 if (0 == planeSize) {
91 return false; // too big to allocate, abort
92 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000093 dst->fImage = SkMask::AllocImage(planeSize * 3);
94 memcpy(dst->fImage, alphaPlane, planeSize);
95 SkMask::FreeImage(alphaPlane);
96 }
97
98 // run the light direction through the matrix...
99 Light light = fLight;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000100 matrix.mapVectors((SkVector*)(void*)light.fDirection,
101 (SkVector*)(void*)fLight.fDirection, 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102
103 // now restore the length of the XY component
104 // cast to SkVector so we can call setLength (this double cast silences alias warnings)
105 SkVector* vec = (SkVector*)(void*)light.fDirection;
106 vec->setLength(light.fDirection[0],
107 light.fDirection[1],
108 SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1]));
109
110 SkEmbossMask::Emboss(dst, light);
111
112 // restore original alpha
113 memcpy(dst->fImage, src.fImage, src.computeImageSize());
114
115 return true;
116}
117
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000118SkEmbossMaskFilter::SkEmbossMaskFilter(SkFlattenableReadBuffer& buffer)
119 : SkMaskFilter(buffer) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000120 buffer.read(&fLight, sizeof(fLight));
121 SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean
122 fBlurRadius = buffer.readScalar();
123}
124
djsollen@google.com54924242012-03-29 15:18:04 +0000125void SkEmbossMaskFilter::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000126 this->INHERITED::flatten(buffer);
127
djsollen@google.com54924242012-03-29 15:18:04 +0000128 Light tmpLight = fLight;
129 tmpLight.fPad = 0; // for the font-cache lookup to be clean
130 buffer.writeMul4(&tmpLight, sizeof(tmpLight));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000131 buffer.writeScalar(fBlurRadius);
132}
133