/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/SkRect.h" |
11 | | #include "include/core/SkScalar.h" |
12 | | #include "include/core/SkTypes.h" |
13 | | #include "include/private/base/SkFixed.h" |
14 | | #include "include/private/base/SkTo.h" |
15 | | #include "src/base/SkMathPriv.h" |
16 | | #include "src/core/SkMask.h" |
17 | | |
18 | | #include <algorithm> |
19 | | #include <cstddef> |
20 | | #include <cstdint> |
21 | | |
22 | 0 | static inline int nonzero_to_one(int x) { |
23 | | #if 0 |
24 | | return x != 0; |
25 | | #else |
26 | 0 | return ((unsigned)(x | -x)) >> 31; |
27 | 0 | #endif |
28 | 0 | } |
29 | | |
30 | 0 | static inline int neq_to_one(int x, int max) { |
31 | | #if 0 |
32 | | return x != max; |
33 | | #else |
34 | 0 | SkASSERT(x >= 0 && x <= max); |
35 | 0 | return ((unsigned)(x - max)) >> 31; |
36 | 0 | #endif |
37 | 0 | } Unexecuted instantiation: SkEmbossMask.cpp:neq_to_one(int, int) Unexecuted instantiation: SkEmbossMask.cpp:neq_to_one(int, int) |
38 | | |
39 | 0 | static inline int neq_to_mask(int x, int max) { |
40 | | #if 0 |
41 | | return -(x != max); |
42 | | #else |
43 | 0 | SkASSERT(x >= 0 && x <= max); |
44 | 0 | return (x - max) >> 31; |
45 | 0 | #endif |
46 | 0 | } Unexecuted instantiation: SkEmbossMask.cpp:neq_to_mask(int, int) Unexecuted instantiation: SkEmbossMask.cpp:neq_to_mask(int, int) |
47 | | |
48 | 0 | static inline unsigned div255(unsigned x) { |
49 | 0 | SkASSERT(x <= (255*255)); |
50 | 0 | return x * ((1 << 24) / 255) >> 24; |
51 | 0 | } Unexecuted instantiation: SkEmbossMask.cpp:div255(unsigned int) Unexecuted instantiation: SkEmbossMask.cpp:div255(unsigned int) |
52 | | |
53 | 0 | #define kDelta 32 // small enough to show off angle differences |
54 | | |
55 | 0 | void SkEmbossMask::Emboss(SkMaskBuilder* mask, const SkEmbossMaskFilter::Light& light) { |
56 | 0 | SkASSERT(mask->fFormat == SkMask::k3D_Format); |
57 | |
|
58 | 0 | int specular = light.fSpecular; |
59 | 0 | int ambient = light.fAmbient; |
60 | 0 | SkFixed lx = SkScalarToFixed(light.fDirection[0]); |
61 | 0 | SkFixed ly = SkScalarToFixed(light.fDirection[1]); |
62 | 0 | SkFixed lz = SkScalarToFixed(light.fDirection[2]); |
63 | 0 | SkFixed lz_dot_nz = lz * kDelta; |
64 | 0 | int lz_dot8 = lz >> 8; |
65 | |
|
66 | 0 | size_t planeSize = mask->computeImageSize(); |
67 | 0 | uint8_t* alpha = mask->image(); |
68 | 0 | uint8_t* multiply = (uint8_t*)alpha + planeSize; |
69 | 0 | uint8_t* additive = multiply + planeSize; |
70 | |
|
71 | 0 | int rowBytes = mask->fRowBytes; |
72 | 0 | int maxy = mask->fBounds.height() - 1; |
73 | 0 | int maxx = mask->fBounds.width() - 1; |
74 | |
|
75 | 0 | int prev_row = 0; |
76 | 0 | for (int y = 0; y <= maxy; y++) { |
77 | 0 | int next_row = neq_to_mask(y, maxy) & rowBytes; |
78 | |
|
79 | 0 | for (int x = 0; x <= maxx; x++) { |
80 | 0 | int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)]; |
81 | 0 | int ny = alpha[x + next_row] - alpha[x - prev_row]; |
82 | |
|
83 | 0 | SkFixed numer = lx * nx + ly * ny + lz_dot_nz; |
84 | 0 | int mul = ambient; |
85 | 0 | int add = 0; |
86 | |
|
87 | 0 | if (numer > 0) { // preflight when numer/denom will be <= 0 |
88 | 0 | int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta); |
89 | 0 | SkFixed dot = numer / denom; |
90 | 0 | dot >>= 8; // now dot is 2^8 instead of 2^16 |
91 | 0 | mul = std::min(mul + dot, 255); |
92 | | |
93 | | // now for the reflection |
94 | | |
95 | | // R = 2 (Light * Normal) Normal - Light |
96 | | // hilite = R * Eye(0, 0, 1) |
97 | |
|
98 | 0 | int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8; |
99 | 0 | if (hilite > 0) { |
100 | | // pin hilite to 255, since our fast math is also a little sloppy |
101 | 0 | hilite = std::min(hilite, 255); |
102 | | |
103 | | // specular is 4.4 |
104 | | // would really like to compute the fractional part of this |
105 | | // and then possibly cache a 256 table for a given specular |
106 | | // value in the light, and just pass that in to this function. |
107 | 0 | add = hilite; |
108 | 0 | for (int i = specular >> 4; i > 0; --i) { |
109 | 0 | add = div255(add * hilite); |
110 | 0 | } |
111 | 0 | } |
112 | 0 | } |
113 | 0 | multiply[x] = SkToU8(mul); |
114 | 0 | additive[x] = SkToU8(add); |
115 | 0 | } |
116 | 0 | alpha += rowBytes; |
117 | 0 | multiply += rowBytes; |
118 | 0 | additive += rowBytes; |
119 | 0 | prev_row = rowBytes; |
120 | 0 | } |
121 | 0 | } Unexecuted instantiation: SkEmbossMask::Emboss(SkMaskBuilder*, SkEmbossMaskFilter::Light const&) Unexecuted instantiation: SkEmbossMask::Emboss(SkMaskBuilder*, SkEmbossMaskFilter::Light const&) |