blob: d4e374139218f99b7807a8f6fc6277ceea6f3d6b [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
Hal Canaryfdcfb8b2018-06-13 09:42:32 -04008#include "SkEmbossMask.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -04009
benjaminwagner6c71e0a2016-04-07 08:49:31 -070010#include "SkFixed.h"
tomhudson@google.com889bd8b2011-09-27 17:38:17 +000011#include "SkMath.h"
Hal Canaryc640d0d2018-06-13 09:59:02 -040012#include "SkTo.h"
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000014static inline int nonzero_to_one(int x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000015#if 0
16 return x != 0;
17#else
18 return ((unsigned)(x | -x)) >> 31;
19#endif
20}
21
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000022static inline int neq_to_one(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000023#if 0
24 return x != max;
25#else
26 SkASSERT(x >= 0 && x <= max);
27 return ((unsigned)(x - max)) >> 31;
28#endif
29}
30
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000031static inline int neq_to_mask(int x, int max) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000032#if 0
33 return -(x != max);
34#else
35 SkASSERT(x >= 0 && x <= max);
36 return (x - max) >> 31;
37#endif
38}
39
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000040static inline unsigned div255(unsigned x) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000041 SkASSERT(x <= (255*255));
42 return x * ((1 << 24) / 255) >> 24;
43}
44
45#define kDelta 32 // small enough to show off angle differences
46
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000047void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 SkASSERT(mask->fFormat == SkMask::k3D_Format);
49
50 int specular = light.fSpecular;
51 int ambient = light.fAmbient;
52 SkFixed lx = SkScalarToFixed(light.fDirection[0]);
53 SkFixed ly = SkScalarToFixed(light.fDirection[1]);
54 SkFixed lz = SkScalarToFixed(light.fDirection[2]);
55 SkFixed lz_dot_nz = lz * kDelta;
56 int lz_dot8 = lz >> 8;
57
58 size_t planeSize = mask->computeImageSize();
59 uint8_t* alpha = mask->fImage;
60 uint8_t* multiply = (uint8_t*)alpha + planeSize;
61 uint8_t* additive = multiply + planeSize;
62
63 int rowBytes = mask->fRowBytes;
64 int maxy = mask->fBounds.height() - 1;
65 int maxx = mask->fBounds.width() - 1;
66
67 int prev_row = 0;
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000068 for (int y = 0; y <= maxy; y++) {
reed@android.com8a1c16f2008-12-17 15:59:43 +000069 int next_row = neq_to_mask(y, maxy) & rowBytes;
70
mike@reedtribe.org3334c3a2011-04-20 11:39:28 +000071 for (int x = 0; x <= maxx; x++) {
Ben Wagner339b84e2017-11-10 16:24:50 -050072 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];
reed@android.com8a1c16f2008-12-17 15:59:43 +000074
Ben Wagner339b84e2017-11-10 16:24:50 -050075 SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
76 int mul = ambient;
77 int add = 0;
reed@android.com8a1c16f2008-12-17 15:59:43 +000078
Ben Wagner339b84e2017-11-10 16:24:50 -050079 if (numer > 0) { // preflight when numer/denom will be <= 0
80 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
83 mul = SkFastMin32(mul + dot, 255);
reed@android.com8a1c16f2008-12-17 15:59:43 +000084
Ben Wagner339b84e2017-11-10 16:24:50 -050085 // now for the reflection
reed@android.com8a1c16f2008-12-17 15:59:43 +000086
Ben Wagner339b84e2017-11-10 16:24:50 -050087 // R = 2 (Light * Normal) Normal - Light
88 // hilite = R * Eye(0, 0, 1)
reed@android.com8a1c16f2008-12-17 15:59:43 +000089
Ben Wagner339b84e2017-11-10 16:24:50 -050090 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
91 if (hilite > 0) {
92 // pin hilite to 255, since our fast math is also a little sloppy
93 hilite = SkClampMax(hilite, 255);
reed@android.com8a1c16f2008-12-17 15:59:43 +000094
Ben Wagner339b84e2017-11-10 16:24:50 -050095 // 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;
100 for (int i = specular >> 4; i > 0; --i) {
101 add = div255(add * hilite);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102 }
103 }
reed@android.com8a1c16f2008-12-17 15:59:43 +0000104 }
Ben Wagner339b84e2017-11-10 16:24:50 -0500105 multiply[x] = SkToU8(mul);
106 additive[x] = SkToU8(add);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000107 }
108 alpha += rowBytes;
109 multiply += rowBytes;
110 additive += rowBytes;
111 prev_row = rowBytes;
112 }
113}