blob: ab549ccac8d8552aebb73dcb3d88310b3499d08c [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"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000014#include "SkReadBuffer.h"
15#include "SkWriteBuffer.h"
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +000016#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000017
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000018static inline int pin2byte(int n) {
19 if (n < 0) {
20 n = 0;
21 } else if (n > 0xFF) {
22 n = 0xFF;
23 }
24 return n;
25}
26
reed@android.com8a1c16f2008-12-17 15:59:43 +000027SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3],
28 SkScalar ambient, SkScalar specular,
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000029 SkScalar blurRadius) {
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000030 return SkBlurMaskFilter::CreateEmboss(SkBlurMask::ConvertRadiusToSigma(blurRadius),
31 direction, ambient, specular);
32}
33
34SkMaskFilter* SkBlurMaskFilter::CreateEmboss(SkScalar blurSigma, const SkScalar direction[3],
35 SkScalar ambient, SkScalar specular) {
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000036 if (direction == NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000037 return NULL;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000038 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000039
40 // ambient should be 0...1 as a scalar
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000041 int am = pin2byte(SkScalarToFixed(ambient) >> 8);
reed@android.com8a1c16f2008-12-17 15:59:43 +000042
43 // specular should be 0..15.99 as a scalar
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000044 int sp = pin2byte(SkScalarToFixed(specular) >> 12);
reed@android.com8a1c16f2008-12-17 15:59:43 +000045
46 SkEmbossMaskFilter::Light light;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000047
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 memcpy(light.fDirection, direction, sizeof(light.fDirection));
49 light.fAmbient = SkToU8(am);
50 light.fSpecular = SkToU8(sp);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000051
commit-bot@chromium.org7c9d0f32014-02-21 10:13:32 +000052 return SkEmbossMaskFilter::Create(blurSigma, light);
reed@android.com8a1c16f2008-12-17 15:59:43 +000053}
54
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000055///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000056
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000057static void normalize(SkScalar v[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000058 SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare(v[2]);
59 mag = SkScalarSqrt(mag);
60
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000061 for (int i = 0; i < 3; i++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000062 v[i] = SkScalarDiv(v[i], mag);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000063 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000064}
65
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000066SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
robertphillips@google.comb75233b2013-08-27 16:31:31 +000067 : fLight(light), fBlurSigma(blurSigma) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 normalize(fLight.fDirection);
69}
70
reed@google.com30711b72012-12-18 19:18:39 +000071SkMask::Format SkEmbossMaskFilter::getFormat() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 return SkMask::k3D_Format;
73}
74
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000075bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src,
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000076 const SkMatrix& matrix, SkIPoint* margin) const {
77 SkScalar sigma = matrix.mapRadius(fBlurSigma);
reed@android.com8a1c16f2008-12-17 15:59:43 +000078
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000079 if (!SkBlurMask::BoxBlur(dst, src, sigma, SkBlurMask::kInner_Style,
80 SkBlurMask::kLow_Quality)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 return false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000082 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000083
84 dst->fFormat = SkMask::k3D_Format;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000085 if (margin) {
reed@google.come1ca7052013-12-17 19:22:07 +000086 margin->set(SkScalarCeilToInt(3*sigma), SkScalarCeilToInt(3*sigma));
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000087 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000089 if (src.fImage == NULL) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000090 return true;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000091 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000092
93 // create a larger buffer for the other two channels (should force fBlur to do this for us)
94
95 {
96 uint8_t* alphaPlane = dst->fImage;
97 size_t planeSize = dst->computeImageSize();
reed@android.com543ed932009-04-24 12:43:40 +000098 if (0 == planeSize) {
99 return false; // too big to allocate, abort
100 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 dst->fImage = SkMask::AllocImage(planeSize * 3);
102 memcpy(dst->fImage, alphaPlane, planeSize);
103 SkMask::FreeImage(alphaPlane);
104 }
105
106 // run the light direction through the matrix...
107 Light light = fLight;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000108 matrix.mapVectors((SkVector*)(void*)light.fDirection,
109 (SkVector*)(void*)fLight.fDirection, 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000110
111 // now restore the length of the XY component
112 // cast to SkVector so we can call setLength (this double cast silences alias warnings)
113 SkVector* vec = (SkVector*)(void*)light.fDirection;
114 vec->setLength(light.fDirection[0],
115 light.fDirection[1],
116 SkPoint::Length(fLight.fDirection[0], fLight.fDirection[1]));
117
118 SkEmbossMask::Emboss(dst, light);
119
120 // restore original alpha
121 memcpy(dst->fImage, src.fImage, src.computeImageSize());
122
123 return true;
124}
125
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000126SkEmbossMaskFilter::SkEmbossMaskFilter(SkReadBuffer& buffer)
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000127 : SkMaskFilter(buffer) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000128 SkASSERT(buffer.getArrayCount() == sizeof(Light));
commit-bot@chromium.org02512882013-10-31 18:37:50 +0000129 buffer.readByteArray(&fLight, sizeof(Light));
reed@android.com8a1c16f2008-12-17 15:59:43 +0000130 SkASSERT(fLight.fPad == 0); // for the font-cache lookup to be clean
commit-bot@chromium.orgfed2ab62014-01-23 15:16:05 +0000131 fBlurSigma = buffer.readScalar();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000132}
133
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000134void SkEmbossMaskFilter::flatten(SkWriteBuffer& buffer) const {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000135 this->INHERITED::flatten(buffer);
136
djsollen@google.com54924242012-03-29 15:18:04 +0000137 Light tmpLight = fLight;
138 tmpLight.fPad = 0; // for the font-cache lookup to be clean
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000139 buffer.writeByteArray(&tmpLight, sizeof(tmpLight));
robertphillips@google.com11e05552013-12-03 19:46:58 +0000140 buffer.writeScalar(fBlurSigma);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000141}
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000142
143#ifdef SK_DEVELOPER
144void SkEmbossMaskFilter::toString(SkString* str) const {
145 str->append("SkEmbossMaskFilter: (");
146
147 str->append("direction: (");
148 str->appendScalar(fLight.fDirection[0]);
149 str->append(", ");
150 str->appendScalar(fLight.fDirection[1]);
151 str->append(", ");
152 str->appendScalar(fLight.fDirection[2]);
153 str->append(") ");
154
skia.committer@gmail.com8eaddb02013-03-19 07:15:10 +0000155 str->appendf("ambient: %d specular: %d ",
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000156 fLight.fAmbient, fLight.fSpecular);
157
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000158 str->append("blurSigma: ");
159 str->appendScalar(fBlurSigma);
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000160 str->append(")");
161}
162#endif