blob: a9b8e4f75193653c42ab902cf7703e26aed55eb5 [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
Hal Canaryfdcfb8b2018-06-13 09:42:32 -04008#include "SkEmbossMask.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000011#include "SkMath.h"
Hal Canary2d404902018-09-06 11:20:23 -040012#include "SkMathPriv.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040013#include "SkTo.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000014
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000015static inline int nonzero_to_one(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000016#if 0
17 return x != 0;
18#else
19 return ((unsigned)(x | -x)) >> 31;
20#endif
21}
22
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000023static inline int neq_to_one(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000024#if 0
25 return x != max;
26#else
27 SkASSERT(x >= 0 && x <= max);
28 return ((unsigned)(x - max)) >> 31;
29#endif
30}
31
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000032static inline int neq_to_mask(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000033#if 0
34 return -(x != max);
35#else
36 SkASSERT(x >= 0 && x <= max);
37 return (x - max) >> 31;
38#endif
39}
40
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000041static inline unsigned div255(unsigned x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000042 SkASSERT(x <= (255*255));
43 return x * ((1 << 24) / 255) >> 24;
44}
45
46#define kDelta 32 // small enough to show off angle differences
47
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000048void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000049 SkASSERT(mask->fFormat == SkMask::k3D_Format);
50
51 int specular = light.fSpecular;
52 int ambient = light.fAmbient;
53 SkFixed lx = SkScalarToFixed(light.fDirection[0]);
54 SkFixed ly = SkScalarToFixed(light.fDirection[1]);
55 SkFixed lz = SkScalarToFixed(light.fDirection[2]);
56 SkFixed lz_dot_nz = lz * kDelta;
57 int lz_dot8 = lz >> 8;
58
59 size_t planeSize = mask->computeImageSize();
60 uint8_t* alpha = mask->fImage;
61 uint8_t* multiply = (uint8_t*)alpha + planeSize;
62 uint8_t* additive = multiply + planeSize;
63
64 int rowBytes = mask->fRowBytes;
65 int maxy = mask->fBounds.height() - 1;
66 int maxx = mask->fBounds.width() - 1;
67
68 int prev_row = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000069 for (int y = 0; y <= maxy; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 int next_row = neq_to_mask(y, maxy) & rowBytes;
71
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000072 for (int x = 0; x <= maxx; x++) {
Ben Wagner339b84e2017-11-10 16:24:50 -050073 int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
74 int ny = alpha[x + next_row] - alpha[x - prev_row];
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
Ben Wagner339b84e2017-11-10 16:24:50 -050076 SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
77 int mul = ambient;
78 int add = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000079
Ben Wagner339b84e2017-11-10 16:24:50 -050080 if (numer > 0) { // preflight when numer/denom will be <= 0
81 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
82 SkFixed dot = numer / denom;
83 dot >>= 8; // now dot is 2^8 instead of 2^16
Nigel Tao70039d82018-07-28 17:12:35 +100084 mul = SkMin32(mul + dot, 255);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
Ben Wagner339b84e2017-11-10 16:24:50 -050086 // now for the reflection
reed@android.com8a1c16f2008-12-17 15:59:43 +000087
Ben Wagner339b84e2017-11-10 16:24:50 -050088 // R = 2 (Light * Normal) Normal - Light
89 // hilite = R * Eye(0, 0, 1)
reed@android.com8a1c16f2008-12-17 15:59:43 +000090
Ben Wagner339b84e2017-11-10 16:24:50 -050091 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
92 if (hilite > 0) {
93 // pin hilite to 255, since our fast math is also a little sloppy
94 hilite = SkClampMax(hilite, 255);
reed@android.com8a1c16f2008-12-17 15:59:43 +000095
Ben Wagner339b84e2017-11-10 16:24:50 -050096 // specular is 4.4
97 // would really like to compute the fractional part of this
98 // and then possibly cache a 256 table for a given specular
99 // value in the light, and just pass that in to this function.
100 add = hilite;
101 for (int i = specular >> 4; i > 0; --i) {
102 add = div255(add * hilite);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 }
104 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 }
Ben Wagner339b84e2017-11-10 16:24:50 -0500106 multiply[x] = SkToU8(mul);
107 additive[x] = SkToU8(add);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 }
109 alpha += rowBytes;
110 multiply += rowBytes;
111 additive += rowBytes;
112 prev_row = rowBytes;
113 }
114}