blob: e08118656411cecd7ed488f8b4b74975109ad577 [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
9#include "SkEmbossMask.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000011#include "SkMath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000013static inline int nonzero_to_one(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#if 0
15 return x != 0;
16#else
17 return ((unsigned)(x | -x)) >> 31;
18#endif
19}
20
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000021static inline int neq_to_one(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#if 0
23 return x != max;
24#else
25 SkASSERT(x >= 0 && x <= max);
26 return ((unsigned)(x - max)) >> 31;
27#endif
28}
29
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000030static inline int neq_to_mask(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031#if 0
32 return -(x != max);
33#else
34 SkASSERT(x >= 0 && x <= max);
35 return (x - max) >> 31;
36#endif
37}
38
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000039static inline unsigned div255(unsigned x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkASSERT(x <= (255*255));
41 return x * ((1 << 24) / 255) >> 24;
42}
43
44#define kDelta 32 // small enough to show off angle differences
45
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000046void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 SkASSERT(mask->fFormat == SkMask::k3D_Format);
48
49 int specular = light.fSpecular;
50 int ambient = light.fAmbient;
51 SkFixed lx = SkScalarToFixed(light.fDirection[0]);
52 SkFixed ly = SkScalarToFixed(light.fDirection[1]);
53 SkFixed lz = SkScalarToFixed(light.fDirection[2]);
54 SkFixed lz_dot_nz = lz * kDelta;
55 int lz_dot8 = lz >> 8;
56
57 size_t planeSize = mask->computeImageSize();
58 uint8_t* alpha = mask->fImage;
59 uint8_t* multiply = (uint8_t*)alpha + planeSize;
60 uint8_t* additive = multiply + planeSize;
61
62 int rowBytes = mask->fRowBytes;
63 int maxy = mask->fBounds.height() - 1;
64 int maxx = mask->fBounds.width() - 1;
65
66 int prev_row = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000067 for (int y = 0; y <= maxy; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 int next_row = neq_to_mask(y, maxy) & rowBytes;
69
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000070 for (int x = 0; x <= maxx; x++) {
Ben Wagner339b84e2017-11-10 16:24:50 -050071 int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
72 int ny = alpha[x + next_row] - alpha[x - prev_row];
reed@android.com8a1c16f2008-12-17 15:59:43 +000073
Ben Wagner339b84e2017-11-10 16:24:50 -050074 SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
75 int mul = ambient;
76 int add = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000077
Ben Wagner339b84e2017-11-10 16:24:50 -050078 if (numer > 0) { // preflight when numer/denom will be <= 0
79 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
80 SkFixed dot = numer / denom;
81 dot >>= 8; // now dot is 2^8 instead of 2^16
82 mul = SkFastMin32(mul + dot, 255);
reed@android.com8a1c16f2008-12-17 15:59:43 +000083
Ben Wagner339b84e2017-11-10 16:24:50 -050084 // now for the reflection
reed@android.com8a1c16f2008-12-17 15:59:43 +000085
Ben Wagner339b84e2017-11-10 16:24:50 -050086 // R = 2 (Light * Normal) Normal - Light
87 // hilite = R * Eye(0, 0, 1)
reed@android.com8a1c16f2008-12-17 15:59:43 +000088
Ben Wagner339b84e2017-11-10 16:24:50 -050089 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
90 if (hilite > 0) {
91 // pin hilite to 255, since our fast math is also a little sloppy
92 hilite = SkClampMax(hilite, 255);
reed@android.com8a1c16f2008-12-17 15:59:43 +000093
Ben Wagner339b84e2017-11-10 16:24:50 -050094 // specular is 4.4
95 // would really like to compute the fractional part of this
96 // and then possibly cache a 256 table for a given specular
97 // value in the light, and just pass that in to this function.
98 add = hilite;
99 for (int i = specular >> 4; i > 0; --i) {
100 add = div255(add * hilite);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 }
102 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 }
Ben Wagner339b84e2017-11-10 16:24:50 -0500104 multiply[x] = SkToU8(mul);
105 additive[x] = SkToU8(add);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106 }
107 alpha += rowBytes;
108 multiply += rowBytes;
109 additive += rowBytes;
110 prev_row = rowBytes;
111 }
112}