/src/skia/src/effects/SkEmbossMask.cpp
Line | Count | Source (jump to first uncovered line) |
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 "src/effects/SkEmbossMask.h" |
9 | | |
10 | | #include "include/core/SkMath.h" |
11 | | #include "include/private/SkFixed.h" |
12 | | #include "include/private/SkTo.h" |
13 | | #include "src/core/SkMathPriv.h" |
14 | | |
15 | 0 | static inline int nonzero_to_one(int x) { |
16 | | #if 0 |
17 | | return x != 0; |
18 | | #else |
19 | 0 | return ((unsigned)(x | -x)) >> 31; |
20 | 0 | #endif |
21 | 0 | } |
22 | | |
23 | 0 | static inline int neq_to_one(int x, int max) { |
24 | | #if 0 |
25 | | return x != max; |
26 | | #else |
27 | 0 | SkASSERT(x >= 0 && x <= max); |
28 | 0 | return ((unsigned)(x - max)) >> 31; |
29 | 0 | #endif |
30 | 0 | } Unexecuted instantiation: SkEmbossMask.cpp:neq_to_one(int, int) Unexecuted instantiation: SkEmbossMask.cpp:neq_to_one(int, int) |
31 | | |
32 | 0 | static inline int neq_to_mask(int x, int max) { |
33 | | #if 0 |
34 | | return -(x != max); |
35 | | #else |
36 | 0 | SkASSERT(x >= 0 && x <= max); |
37 | 0 | return (x - max) >> 31; |
38 | 0 | #endif |
39 | 0 | } Unexecuted instantiation: SkEmbossMask.cpp:neq_to_mask(int, int) Unexecuted instantiation: SkEmbossMask.cpp:neq_to_mask(int, int) |
40 | | |
41 | 0 | static inline unsigned div255(unsigned x) { |
42 | 0 | SkASSERT(x <= (255*255)); |
43 | 0 | return x * ((1 << 24) / 255) >> 24; |
44 | 0 | } Unexecuted instantiation: SkEmbossMask.cpp:div255(unsigned int) Unexecuted instantiation: SkEmbossMask.cpp:div255(unsigned int) |
45 | | |
46 | 0 | #define kDelta 32 // small enough to show off angle differences |
47 | | |
48 | 0 | void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) { |
49 | 0 | SkASSERT(mask->fFormat == SkMask::k3D_Format); |
50 | |
|
51 | 0 | int specular = light.fSpecular; |
52 | 0 | int ambient = light.fAmbient; |
53 | 0 | SkFixed lx = SkScalarToFixed(light.fDirection[0]); |
54 | 0 | SkFixed ly = SkScalarToFixed(light.fDirection[1]); |
55 | 0 | SkFixed lz = SkScalarToFixed(light.fDirection[2]); |
56 | 0 | SkFixed lz_dot_nz = lz * kDelta; |
57 | 0 | int lz_dot8 = lz >> 8; |
58 | |
|
59 | 0 | size_t planeSize = mask->computeImageSize(); |
60 | 0 | uint8_t* alpha = mask->fImage; |
61 | 0 | uint8_t* multiply = (uint8_t*)alpha + planeSize; |
62 | 0 | uint8_t* additive = multiply + planeSize; |
63 | |
|
64 | 0 | int rowBytes = mask->fRowBytes; |
65 | 0 | int maxy = mask->fBounds.height() - 1; |
66 | 0 | int maxx = mask->fBounds.width() - 1; |
67 | |
|
68 | 0 | int prev_row = 0; |
69 | 0 | for (int y = 0; y <= maxy; y++) { |
70 | 0 | int next_row = neq_to_mask(y, maxy) & rowBytes; |
71 | |
|
72 | 0 | for (int x = 0; x <= maxx; x++) { |
73 | 0 | int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)]; |
74 | 0 | int ny = alpha[x + next_row] - alpha[x - prev_row]; |
75 | |
|
76 | 0 | SkFixed numer = lx * nx + ly * ny + lz_dot_nz; |
77 | 0 | int mul = ambient; |
78 | 0 | int add = 0; |
79 | |
|
80 | 0 | if (numer > 0) { // preflight when numer/denom will be <= 0 |
81 | 0 | int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta); |
82 | 0 | SkFixed dot = numer / denom; |
83 | 0 | dot >>= 8; // now dot is 2^8 instead of 2^16 |
84 | 0 | mul = std::min(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 | 0 | int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8; |
92 | 0 | if (hilite > 0) { |
93 | | // pin hilite to 255, since our fast math is also a little sloppy |
94 | 0 | hilite = std::min(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 | 0 | add = hilite; |
101 | 0 | for (int i = specular >> 4; i > 0; --i) { |
102 | 0 | add = div255(add * hilite); |
103 | 0 | } |
104 | 0 | } |
105 | 0 | } |
106 | 0 | multiply[x] = SkToU8(mul); |
107 | 0 | additive[x] = SkToU8(add); |
108 | 0 | } |
109 | 0 | alpha += rowBytes; |
110 | 0 | multiply += rowBytes; |
111 | 0 | additive += rowBytes; |
112 | 0 | prev_row = rowBytes; |
113 | 0 | } |
114 | 0 | } Unexecuted instantiation: SkEmbossMask::Emboss(SkMask*, SkEmbossMaskFilter::Light const&) Unexecuted instantiation: SkEmbossMask::Emboss(SkMask*, SkEmbossMaskFilter::Light const&) |