blob: 7079d65aab8352a0c80a9ddc62db3a47922a20b3 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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
reed@android.com8a1c16f2008-12-17 15:59:43 +00008
9#include "SkEmbossMask.h"
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000011#include "SkMath.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000012
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000013static inline int nonzero_to_one(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000014#if 0
15 return x != 0;
16#else
17 return ((unsigned)(x | -x)) >> 31;
18#endif
19}
20
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000021static inline int neq_to_one(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000022#if 0
23 return x != max;
24#else
25 SkASSERT(x >= 0 && x <= max);
26 return ((unsigned)(x - max)) >> 31;
27#endif
28}
29
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000030static inline int neq_to_mask(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000031#if 0
32 return -(x != max);
33#else
34 SkASSERT(x >= 0 && x <= max);
35 return (x - max) >> 31;
36#endif
37}
38
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000039static inline unsigned div255(unsigned x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000040 SkASSERT(x <= (255*255));
41 return x * ((1 << 24) / 255) >> 24;
42}
43
44#define kDelta 32 // small enough to show off angle differences
45
46#include "SkEmbossMask_Table.h"
47
48#if defined(SK_BUILD_FOR_WIN32) && defined(SK_DEBUG)
49
50#include <stdio.h>
51
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000052void SkEmbossMask_BuildTable() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000053 // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table
54
55 FILE* file = ::fopen("SkEmbossMask_Table.h", "w");
56 SkASSERT(file);
57 ::fprintf(file, "#include \"SkTypes.h\"\n\n");
58 ::fprintf(file, "static const U16 gInvSqrtTable[128 * 128] = {\n");
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000059 for (int dx = 0; dx <= 255/2; dx++) {
60 for (int dy = 0; dy <= 255/2; dy++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000061 if ((dy & 15) == 0)
62 ::fprintf(file, "\t");
63
reed@android.com9c970452009-04-03 14:26:10 +000064 uint16_t value = SkToU16((1 << 15) / SkSqrt32(dx * dx + dy * dy + kDelta*kDelta/4));
reed@android.com8a1c16f2008-12-17 15:59:43 +000065
66 ::fprintf(file, "0x%04X", value);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000067 if (dx * 128 + dy < 128*128-1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 ::fprintf(file, ", ");
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000069 }
70 if ((dy & 15) == 15) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 ::fprintf(file, "\n");
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000072 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000073 }
74 }
75 ::fprintf(file, "};\n#define kDeltaUsedToBuildTable\t%d\n", kDelta);
76 ::fclose(file);
77}
78
79#endif
80
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000081void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000082 SkASSERT(kDelta == kDeltaUsedToBuildTable);
83
84 SkASSERT(mask->fFormat == SkMask::k3D_Format);
85
86 int specular = light.fSpecular;
87 int ambient = light.fAmbient;
88 SkFixed lx = SkScalarToFixed(light.fDirection[0]);
89 SkFixed ly = SkScalarToFixed(light.fDirection[1]);
90 SkFixed lz = SkScalarToFixed(light.fDirection[2]);
91 SkFixed lz_dot_nz = lz * kDelta;
92 int lz_dot8 = lz >> 8;
93
94 size_t planeSize = mask->computeImageSize();
95 uint8_t* alpha = mask->fImage;
96 uint8_t* multiply = (uint8_t*)alpha + planeSize;
97 uint8_t* additive = multiply + planeSize;
98
99 int rowBytes = mask->fRowBytes;
100 int maxy = mask->fBounds.height() - 1;
101 int maxx = mask->fBounds.width() - 1;
102
103 int prev_row = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000104 for (int y = 0; y <= maxy; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000105 int next_row = neq_to_mask(y, maxy) & rowBytes;
106
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000107 for (int x = 0; x <= maxx; x++) {
108 if (alpha[x]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000109 int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
110 int ny = alpha[x + next_row] - alpha[x - prev_row];
111
112 SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
113 int mul = ambient;
114 int add = 0;
115
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000116 if (numer > 0) { // preflight when numer/denom will be <= 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000117#if 0
118 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
119 SkFixed dot = numer / denom;
120 dot >>= 8; // now dot is 2^8 instead of 2^16
121#else
122 // can use full numer, but then we need to call SkFixedMul, since
123 // numer is 24 bits, and our table is 12 bits
124
125 // SkFixed dot = SkFixedMul(numer, gTable[]) >> 8
126 SkFixed dot = (unsigned)(numer >> 4) * gInvSqrtTable[(SkAbs32(nx) >> 1 << 7) | (SkAbs32(ny) >> 1)] >> 20;
127#endif
128 mul = SkFastMin32(mul + dot, 255);
129
130 // now for the reflection
131
132 // R = 2 (Light * Normal) Normal - Light
133 // hilite = R * Eye(0, 0, 1)
134
135 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000136 if (hilite > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000137 // pin hilite to 255, since our fast math is also a little sloppy
138 hilite = SkClampMax(hilite, 255);
139
140 // specular is 4.4
141 // would really like to compute the fractional part of this
142 // and then possibly cache a 256 table for a given specular
143 // value in the light, and just pass that in to this function.
144 add = hilite;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000145 for (int i = specular >> 4; i > 0; --i) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000146 add = div255(add * hilite);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000147 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000148 }
149 }
150 multiply[x] = SkToU8(mul);
151 additive[x] = SkToU8(add);
152
153 // multiply[x] = 0xFF;
154 // additive[x] = 0;
155 // ((uint8_t*)alpha)[x] = alpha[x] * multiply[x] >> 8;
156 }
157 }
158 alpha += rowBytes;
159 multiply += rowBytes;
160 additive += rowBytes;
161 prev_row = rowBytes;
162 }
163}