blob: dffd2befdd308bf1db42e755f3454594660e3ff2 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2006 The Android Open Source Project
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
reed@android.com8a1c16f2008-12-17 15:59:43 +00009
10#include "SkEmbossMask.h"
11
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000012static inline int nonzero_to_one(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000013#if 0
14 return x != 0;
15#else
16 return ((unsigned)(x | -x)) >> 31;
17#endif
18}
19
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000020static inline int neq_to_one(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000021#if 0
22 return x != max;
23#else
24 SkASSERT(x >= 0 && x <= max);
25 return ((unsigned)(x - max)) >> 31;
26#endif
27}
28
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000029static inline int neq_to_mask(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000030#if 0
31 return -(x != max);
32#else
33 SkASSERT(x >= 0 && x <= max);
34 return (x - max) >> 31;
35#endif
36}
37
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000038static inline unsigned div255(unsigned x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 SkASSERT(x <= (255*255));
40 return x * ((1 << 24) / 255) >> 24;
41}
42
43#define kDelta 32 // small enough to show off angle differences
44
45#include "SkEmbossMask_Table.h"
46
47#if defined(SK_BUILD_FOR_WIN32) && defined(SK_DEBUG)
48
49#include <stdio.h>
50
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000051void SkEmbossMask_BuildTable() {
reed@android.com8a1c16f2008-12-17 15:59:43 +000052 // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table
53
54 FILE* file = ::fopen("SkEmbossMask_Table.h", "w");
55 SkASSERT(file);
56 ::fprintf(file, "#include \"SkTypes.h\"\n\n");
57 ::fprintf(file, "static const U16 gInvSqrtTable[128 * 128] = {\n");
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000058 for (int dx = 0; dx <= 255/2; dx++) {
59 for (int dy = 0; dy <= 255/2; dy++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000060 if ((dy & 15) == 0)
61 ::fprintf(file, "\t");
62
reed@android.com9c970452009-04-03 14:26:10 +000063 uint16_t value = SkToU16((1 << 15) / SkSqrt32(dx * dx + dy * dy + kDelta*kDelta/4));
reed@android.com8a1c16f2008-12-17 15:59:43 +000064
65 ::fprintf(file, "0x%04X", value);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000066 if (dx * 128 + dy < 128*128-1) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000067 ::fprintf(file, ", ");
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000068 }
69 if ((dy & 15) == 15) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000070 ::fprintf(file, "\n");
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000071 }
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 }
73 }
74 ::fprintf(file, "};\n#define kDeltaUsedToBuildTable\t%d\n", kDelta);
75 ::fclose(file);
76}
77
78#endif
79
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000080void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000081 SkASSERT(kDelta == kDeltaUsedToBuildTable);
82
83 SkASSERT(mask->fFormat == SkMask::k3D_Format);
84
85 int specular = light.fSpecular;
86 int ambient = light.fAmbient;
87 SkFixed lx = SkScalarToFixed(light.fDirection[0]);
88 SkFixed ly = SkScalarToFixed(light.fDirection[1]);
89 SkFixed lz = SkScalarToFixed(light.fDirection[2]);
90 SkFixed lz_dot_nz = lz * kDelta;
91 int lz_dot8 = lz >> 8;
92
93 size_t planeSize = mask->computeImageSize();
94 uint8_t* alpha = mask->fImage;
95 uint8_t* multiply = (uint8_t*)alpha + planeSize;
96 uint8_t* additive = multiply + planeSize;
97
98 int rowBytes = mask->fRowBytes;
99 int maxy = mask->fBounds.height() - 1;
100 int maxx = mask->fBounds.width() - 1;
101
102 int prev_row = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000103 for (int y = 0; y <= maxy; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 int next_row = neq_to_mask(y, maxy) & rowBytes;
105
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000106 for (int x = 0; x <= maxx; x++) {
107 if (alpha[x]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000108 int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
109 int ny = alpha[x + next_row] - alpha[x - prev_row];
110
111 SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
112 int mul = ambient;
113 int add = 0;
114
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000115 if (numer > 0) { // preflight when numer/denom will be <= 0
reed@android.com8a1c16f2008-12-17 15:59:43 +0000116#if 0
117 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
118 SkFixed dot = numer / denom;
119 dot >>= 8; // now dot is 2^8 instead of 2^16
120#else
121 // can use full numer, but then we need to call SkFixedMul, since
122 // numer is 24 bits, and our table is 12 bits
123
124 // SkFixed dot = SkFixedMul(numer, gTable[]) >> 8
125 SkFixed dot = (unsigned)(numer >> 4) * gInvSqrtTable[(SkAbs32(nx) >> 1 << 7) | (SkAbs32(ny) >> 1)] >> 20;
126#endif
127 mul = SkFastMin32(mul + dot, 255);
128
129 // now for the reflection
130
131 // R = 2 (Light * Normal) Normal - Light
132 // hilite = R * Eye(0, 0, 1)
133
134 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000135 if (hilite > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000136 // pin hilite to 255, since our fast math is also a little sloppy
137 hilite = SkClampMax(hilite, 255);
138
139 // specular is 4.4
140 // would really like to compute the fractional part of this
141 // and then possibly cache a 256 table for a given specular
142 // value in the light, and just pass that in to this function.
143 add = hilite;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000144 for (int i = specular >> 4; i > 0; --i) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000145 add = div255(add * hilite);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000146 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000147 }
148 }
149 multiply[x] = SkToU8(mul);
150 additive[x] = SkToU8(add);
151
152 // multiply[x] = 0xFF;
153 // additive[x] = 0;
154 // ((uint8_t*)alpha)[x] = alpha[x] * multiply[x] >> 8;
155 }
156 }
157 alpha += rowBytes;
158 multiply += rowBytes;
159 additive += rowBytes;
160 prev_row = rowBytes;
161 }
162}
163
164