blob: 2607e255d9a07619a83827f86bc894e1b7d19287 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008#include "SkEmbossMaskFilter.h"
9#include "SkBlurMaskFilter.h"
10#include "SkBlurMask.h"
11#include "SkEmbossMask.h"
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +000012#include "SkReadBuffer.h"
13#include "SkWriteBuffer.h"
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +000014#include "SkString.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000015
reed@google.comdaaafa62014-04-29 15:20:16 +000016SkEmbossMaskFilter* SkEmbossMaskFilter::Create(SkScalar blurSigma, const Light& light) {
halcanary385fe4d2015-08-26 13:07:48 -070017 return new SkEmbossMaskFilter(blurSigma, light);
reed@google.comdaaafa62014-04-29 15:20:16 +000018}
19
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000020static inline int pin2byte(int n) {
21 if (n < 0) {
22 n = 0;
23 } else if (n > 0xFF) {
24 n = 0xFF;
25 }
26 return n;
27}
28
reed@android.com8a1c16f2008-12-17 15:59:43 +000029SkMaskFilter* SkBlurMaskFilter::CreateEmboss(const SkScalar direction[3],
30 SkScalar ambient, SkScalar specular,
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000031 SkScalar blurRadius) {
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000032 return SkBlurMaskFilter::CreateEmboss(SkBlurMask::ConvertRadiusToSigma(blurRadius),
33 direction, ambient, specular);
34}
35
36SkMaskFilter* SkBlurMaskFilter::CreateEmboss(SkScalar blurSigma, const SkScalar direction[3],
37 SkScalar ambient, SkScalar specular) {
halcanary96fcdcc2015-08-27 07:41:13 -070038 if (direction == nullptr) {
39 return nullptr;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000040 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000041
42 // ambient should be 0...1 as a scalar
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000043 int am = pin2byte(SkScalarToFixed(ambient) >> 8);
reed@android.com8a1c16f2008-12-17 15:59:43 +000044
45 // specular should be 0..15.99 as a scalar
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000046 int sp = pin2byte(SkScalarToFixed(specular) >> 12);
reed@android.com8a1c16f2008-12-17 15:59:43 +000047
48 SkEmbossMaskFilter::Light light;
rmistry@google.comfbfcd562012-08-23 18:09:54 +000049
reed@android.com8a1c16f2008-12-17 15:59:43 +000050 memcpy(light.fDirection, direction, sizeof(light.fDirection));
51 light.fAmbient = SkToU8(am);
52 light.fSpecular = SkToU8(sp);
rmistry@google.comfbfcd562012-08-23 18:09:54 +000053
commit-bot@chromium.org7c9d0f32014-02-21 10:13:32 +000054 return SkEmbossMaskFilter::Create(blurSigma, light);
reed@android.com8a1c16f2008-12-17 15:59:43 +000055}
56
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000057///////////////////////////////////////////////////////////////////////////////
reed@android.com8a1c16f2008-12-17 15:59:43 +000058
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000059static void normalize(SkScalar v[3]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 SkScalar mag = SkScalarSquare(v[0]) + SkScalarSquare(v[1]) + SkScalarSquare(v[2]);
61 mag = SkScalarSqrt(mag);
62
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000063 for (int i = 0; i < 3; i++) {
reed80ea19c2015-05-12 10:37:34 -070064 v[i] /= mag;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000065 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000066}
67
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000068SkEmbossMaskFilter::SkEmbossMaskFilter(SkScalar blurSigma, const Light& light)
robertphillips@google.comb75233b2013-08-27 16:31:31 +000069 : fLight(light), fBlurSigma(blurSigma) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 normalize(fLight.fDirection);
71}
72
reed@google.com30711b72012-12-18 19:18:39 +000073SkMask::Format SkEmbossMaskFilter::getFormat() const {
reed@android.com8a1c16f2008-12-17 15:59:43 +000074 return SkMask::k3D_Format;
75}
76
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000077bool SkEmbossMaskFilter::filterMask(SkMask* dst, const SkMask& src,
robertphillips@google.com7ce661d2013-08-27 16:14:03 +000078 const SkMatrix& matrix, SkIPoint* margin) const {
79 SkScalar sigma = matrix.mapRadius(fBlurSigma);
reed@android.com8a1c16f2008-12-17 15:59:43 +000080
commit-bot@chromium.orge3964552014-04-28 16:25:35 +000081 if (!SkBlurMask::BoxBlur(dst, src, sigma, kInner_SkBlurStyle, kLow_SkBlurQuality)) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 return false;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000083 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
85 dst->fFormat = SkMask::k3D_Format;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000086 if (margin) {
reed@google.come1ca7052013-12-17 19:22:07 +000087 margin->set(SkScalarCeilToInt(3*sigma), SkScalarCeilToInt(3*sigma));
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000088 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
halcanary96fcdcc2015-08-27 07:41:13 -070090 if (src.fImage == nullptr) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000091 return true;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000092 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
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();
reed@android.com543ed932009-04-24 12:43:40 +000099 if (0 == planeSize) {
100 return false; // too big to allocate, abort
101 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 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;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000109 matrix.mapVectors((SkVector*)(void*)light.fDirection,
110 (SkVector*)(void*)fLight.fDirection, 1);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000111
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
reed9fa60da2014-08-21 07:59:51 -0700127SkFlattenable* 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 Create(sigma, light);
133 }
halcanary96fcdcc2015-08-27 07:41:13 -0700134 return nullptr;
reed9fa60da2014-08-21 07:59:51 -0700135}
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136
commit-bot@chromium.org8b0e8ac2014-01-30 18:58:24 +0000137void SkEmbossMaskFilter::flatten(SkWriteBuffer& buffer) const {
djsollen@google.com54924242012-03-29 15:18:04 +0000138 Light tmpLight = fLight;
139 tmpLight.fPad = 0; // for the font-cache lookup to be clean
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000140 buffer.writeByteArray(&tmpLight, sizeof(tmpLight));
robertphillips@google.com11e05552013-12-03 19:46:58 +0000141 buffer.writeScalar(fBlurSigma);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000142}
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000143
commit-bot@chromium.org0f10f7b2014-03-13 18:02:17 +0000144#ifndef SK_IGNORE_TO_STRING
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000145void SkEmbossMaskFilter::toString(SkString* str) const {
146 str->append("SkEmbossMaskFilter: (");
147
148 str->append("direction: (");
149 str->appendScalar(fLight.fDirection[0]);
150 str->append(", ");
151 str->appendScalar(fLight.fDirection[1]);
152 str->append(", ");
153 str->appendScalar(fLight.fDirection[2]);
154 str->append(") ");
155
skia.committer@gmail.com8eaddb02013-03-19 07:15:10 +0000156 str->appendf("ambient: %d specular: %d ",
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000157 fLight.fAmbient, fLight.fSpecular);
158
robertphillips@google.com7ce661d2013-08-27 16:14:03 +0000159 str->append("blurSigma: ");
160 str->appendScalar(fBlurSigma);
robertphillips@google.com0bd80fa2013-03-18 17:53:38 +0000161 str->append(")");
162}
163#endif