1 /*
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 
8 #include "SkEmbossMask.h"
9 
10 #include "SkFixed.h"
11 #include "SkMath.h"
12 #include "SkMathPriv.h"
13 #include "SkTo.h"
14 
nonzero_to_one(int x)15 static inline int nonzero_to_one(int x) {
16 #if 0
17     return x != 0;
18 #else
19     return ((unsigned)(x | -x)) >> 31;
20 #endif
21 }
22 
neq_to_one(int x,int max)23 static inline int neq_to_one(int x, int max) {
24 #if 0
25     return x != max;
26 #else
27     SkASSERT(x >= 0 && x <= max);
28     return ((unsigned)(x - max)) >> 31;
29 #endif
30 }
31 
neq_to_mask(int x,int max)32 static inline int neq_to_mask(int x, int max) {
33 #if 0
34     return -(x != max);
35 #else
36     SkASSERT(x >= 0 && x <= max);
37     return (x - max) >> 31;
38 #endif
39 }
40 
div255(unsigned x)41 static inline unsigned div255(unsigned x) {
42     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 
Emboss(SkMask * mask,const SkEmbossMaskFilter::Light & light)48 void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
49     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;
69     for (int y = 0; y <= maxy; y++) {
70         int next_row = neq_to_mask(y, maxy) & rowBytes;
71 
72         for (int x = 0; x <= maxx; x++) {
73             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];
75 
76             SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
77             int     mul = ambient;
78             int     add = 0;
79 
80             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
84                 mul = SkMin32(mul + dot, 255);
85 
86                 // now for the reflection
87 
88                 //  R = 2 (Light * Normal) Normal - Light
89                 //  hilite = R * Eye(0, 0, 1)
90 
91                 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);
95 
96                     // 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);
103                     }
104                 }
105             }
106             multiply[x] = SkToU8(mul);
107             additive[x] = SkToU8(add);
108         }
109         alpha += rowBytes;
110         multiply += rowBytes;
111         additive += rowBytes;
112         prev_row = rowBytes;
113     }
114 }
115