blob: 742ccd23d67fb3c4c6c62e3666ad188aadaaf7a1 [file] [log] [blame]
reed@android.com8a1c16f2008-12-17 15:59:43 +00001/* 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
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000020static inline int nonzero_to_one(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#if 0
22 return x != 0;
23#else
24 return ((unsigned)(x | -x)) >> 31;
25#endif
26}
27
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000028static inline int neq_to_one(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000029#if 0
30 return x != max;
31#else
32 SkASSERT(x >= 0 && x <= max);
33 return ((unsigned)(x - max)) >> 31;
34#endif
35}
36
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000037static inline int neq_to_mask(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000038#if 0
39 return -(x != max);
40#else
41 SkASSERT(x >= 0 && x <= max);
42 return (x - max) >> 31;
43#endif
44}
45
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000046static inline unsigned div255(unsigned x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 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
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000059void SkEmbossMask_BuildTable() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 // 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");
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000066 for (int dx = 0; dx <= 255/2; dx++) {
67 for (int dy = 0; dy <= 255/2; dy++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 if ((dy & 15) == 0)
69 ::fprintf(file, "\t");
70
reed@android.com9c970452009-04-03 14:26:10 +000071 uint16_t value = SkToU16((1 << 15) / SkSqrt32(dx * dx + dy * dy + kDelta*kDelta/4));
reed@android.com8a1c16f2008-12-17 15:59:43 +000072
73 ::fprintf(file, "0x%04X", value);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000074 if (dx * 128 + dy < 128*128-1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000075 ::fprintf(file, ", ");
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000076 }
77 if ((dy & 15) == 15) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000078 ::fprintf(file, "\n");
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000079 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 }
81 }
82 ::fprintf(file, "};\n#define kDeltaUsedToBuildTable\t%d\n", kDelta);
83 ::fclose(file);
84}
85
86#endif
87
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000088void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000089 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;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000111 for (int y = 0; y <= maxy; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000112 int next_row = neq_to_mask(y, maxy) & rowBytes;
113
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000114 for (int x = 0; x <= maxx; x++) {
115 if (alpha[x]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116 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
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000123 if (numer > 0) { // preflight when numer/denom will be <= 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000124#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;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000143 if (hilite > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000144 // 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;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000152 for (int i = specular >> 4; i > 0; --i) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000153 add = div255(add * hilite);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000154 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000155 }
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