1 /* libs/graphics/effects/SkEmbossMask.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include "SkEmbossMask.h"
19
nonzero_to_one(int x)20 static inline int nonzero_to_one(int x) {
21 #if 0
22 return x != 0;
23 #else
24 return ((unsigned)(x | -x)) >> 31;
25 #endif
26 }
27
neq_to_one(int x,int max)28 static inline int neq_to_one(int x, int max) {
29 #if 0
30 return x != max;
31 #else
32 SkASSERT(x >= 0 && x <= max);
33 return ((unsigned)(x - max)) >> 31;
34 #endif
35 }
36
neq_to_mask(int x,int max)37 static inline int neq_to_mask(int x, int max) {
38 #if 0
39 return -(x != max);
40 #else
41 SkASSERT(x >= 0 && x <= max);
42 return (x - max) >> 31;
43 #endif
44 }
45
div255(unsigned x)46 static inline unsigned div255(unsigned x) {
47 SkASSERT(x <= (255*255));
48 return x * ((1 << 24) / 255) >> 24;
49 }
50
51 #define kDelta 32 // small enough to show off angle differences
52
53 #include "SkEmbossMask_Table.h"
54
55 #if defined(SK_BUILD_FOR_WIN32) && defined(SK_DEBUG)
56
57 #include <stdio.h>
58
SkEmbossMask_BuildTable()59 void SkEmbossMask_BuildTable() {
60 // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table
61
62 FILE* file = ::fopen("SkEmbossMask_Table.h", "w");
63 SkASSERT(file);
64 ::fprintf(file, "#include \"SkTypes.h\"\n\n");
65 ::fprintf(file, "static const U16 gInvSqrtTable[128 * 128] = {\n");
66 for (int dx = 0; dx <= 255/2; dx++) {
67 for (int dy = 0; dy <= 255/2; dy++) {
68 if ((dy & 15) == 0)
69 ::fprintf(file, "\t");
70
71 uint16_t value = SkToU16((1 << 15) / SkSqrt32(dx * dx + dy * dy + kDelta*kDelta/4));
72
73 ::fprintf(file, "0x%04X", value);
74 if (dx * 128 + dy < 128*128-1) {
75 ::fprintf(file, ", ");
76 }
77 if ((dy & 15) == 15) {
78 ::fprintf(file, "\n");
79 }
80 }
81 }
82 ::fprintf(file, "};\n#define kDeltaUsedToBuildTable\t%d\n", kDelta);
83 ::fclose(file);
84 }
85
86 #endif
87
Emboss(SkMask * mask,const SkEmbossMaskFilter::Light & light)88 void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
89 SkASSERT(kDelta == kDeltaUsedToBuildTable);
90
91 SkASSERT(mask->fFormat == SkMask::k3D_Format);
92
93 int specular = light.fSpecular;
94 int ambient = light.fAmbient;
95 SkFixed lx = SkScalarToFixed(light.fDirection[0]);
96 SkFixed ly = SkScalarToFixed(light.fDirection[1]);
97 SkFixed lz = SkScalarToFixed(light.fDirection[2]);
98 SkFixed lz_dot_nz = lz * kDelta;
99 int lz_dot8 = lz >> 8;
100
101 size_t planeSize = mask->computeImageSize();
102 uint8_t* alpha = mask->fImage;
103 uint8_t* multiply = (uint8_t*)alpha + planeSize;
104 uint8_t* additive = multiply + planeSize;
105
106 int rowBytes = mask->fRowBytes;
107 int maxy = mask->fBounds.height() - 1;
108 int maxx = mask->fBounds.width() - 1;
109
110 int prev_row = 0;
111 for (int y = 0; y <= maxy; y++) {
112 int next_row = neq_to_mask(y, maxy) & rowBytes;
113
114 for (int x = 0; x <= maxx; x++) {
115 if (alpha[x]) {
116 int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
117 int ny = alpha[x + next_row] - alpha[x - prev_row];
118
119 SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
120 int mul = ambient;
121 int add = 0;
122
123 if (numer > 0) { // preflight when numer/denom will be <= 0
124 #if 0
125 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
126 SkFixed dot = numer / denom;
127 dot >>= 8; // now dot is 2^8 instead of 2^16
128 #else
129 // can use full numer, but then we need to call SkFixedMul, since
130 // numer is 24 bits, and our table is 12 bits
131
132 // SkFixed dot = SkFixedMul(numer, gTable[]) >> 8
133 SkFixed dot = (unsigned)(numer >> 4) * gInvSqrtTable[(SkAbs32(nx) >> 1 << 7) | (SkAbs32(ny) >> 1)] >> 20;
134 #endif
135 mul = SkFastMin32(mul + dot, 255);
136
137 // now for the reflection
138
139 // R = 2 (Light * Normal) Normal - Light
140 // hilite = R * Eye(0, 0, 1)
141
142 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
143 if (hilite > 0) {
144 // pin hilite to 255, since our fast math is also a little sloppy
145 hilite = SkClampMax(hilite, 255);
146
147 // specular is 4.4
148 // would really like to compute the fractional part of this
149 // and then possibly cache a 256 table for a given specular
150 // value in the light, and just pass that in to this function.
151 add = hilite;
152 for (int i = specular >> 4; i > 0; --i) {
153 add = div255(add * hilite);
154 }
155 }
156 }
157 multiply[x] = SkToU8(mul);
158 additive[x] = SkToU8(add);
159
160 // multiply[x] = 0xFF;
161 // additive[x] = 0;
162 // ((uint8_t*)alpha)[x] = alpha[x] * multiply[x] >> 8;
163 }
164 }
165 alpha += rowBytes;
166 multiply += rowBytes;
167 additive += rowBytes;
168 prev_row = rowBytes;
169 }
170 }
171
172
173