blob: c73d0c8406a80147b852c011e5ec273b01fc6a9d [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
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000046void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000047 SkASSERT(mask->fFormat == SkMask::k3D_Format);
48
49 int specular = light.fSpecular;
50 int ambient = light.fAmbient;
51 SkFixed lx = SkScalarToFixed(light.fDirection[0]);
52 SkFixed ly = SkScalarToFixed(light.fDirection[1]);
53 SkFixed lz = SkScalarToFixed(light.fDirection[2]);
54 SkFixed lz_dot_nz = lz * kDelta;
55 int lz_dot8 = lz >> 8;
56
57 size_t planeSize = mask->computeImageSize();
58 uint8_t* alpha = mask->fImage;
59 uint8_t* multiply = (uint8_t*)alpha + planeSize;
60 uint8_t* additive = multiply + planeSize;
61
62 int rowBytes = mask->fRowBytes;
63 int maxy = mask->fBounds.height() - 1;
64 int maxx = mask->fBounds.width() - 1;
65
66 int prev_row = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000067 for (int y = 0; y <= maxy; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000068 int next_row = neq_to_mask(y, maxy) & rowBytes;
69
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000070 for (int x = 0; x <= maxx; x++) {
71 if (alpha[x]) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000072 int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
73 int ny = alpha[x + next_row] - alpha[x - prev_row];
74
75 SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
76 int mul = ambient;
77 int add = 0;
78
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000079 if (numer > 0) { // preflight when numer/denom will be <= 0
reed@android.com8a1c16f2008-12-17 15:59:43 +000080 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
81 SkFixed dot = numer / denom;
82 dot >>= 8; // now dot is 2^8 instead of 2^16
reed@android.com8a1c16f2008-12-17 15:59:43 +000083 mul = SkFastMin32(mul + dot, 255);
84
85 // now for the reflection
86
87 // R = 2 (Light * Normal) Normal - Light
88 // hilite = R * Eye(0, 0, 1)
89
90 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000091 if (hilite > 0) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000092 // pin hilite to 255, since our fast math is also a little sloppy
93 hilite = SkClampMax(hilite, 255);
94
95 // specular is 4.4
96 // would really like to compute the fractional part of this
97 // and then possibly cache a 256 table for a given specular
98 // value in the light, and just pass that in to this function.
99 add = hilite;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000100 for (int i = specular >> 4; i > 0; --i) {
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101 add = div255(add * hilite);
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +0000102 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000103 }
104 }
105 multiply[x] = SkToU8(mul);
106 additive[x] = SkToU8(add);
107
108 // multiply[x] = 0xFF;
109 // additive[x] = 0;
110 // ((uint8_t*)alpha)[x] = alpha[x] * multiply[x] >> 8;
111 }
112 }
113 alpha += rowBytes;
114 multiply += rowBytes;
115 additive += rowBytes;
116 prev_row = rowBytes;
117 }
118}