epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | /* |
| 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 Canary | fdcfb8b | 2018-06-13 09:42:32 -0400 | [diff] [blame] | 8 | #include "SkEmbossMask.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 9 | |
benjaminwagner | 6c71e0a | 2016-04-07 08:49:31 -0700 | [diff] [blame] | 10 | #include "SkFixed.h" |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 11 | #include "SkMath.h" |
Hal Canary | c640d0d | 2018-06-13 09:59:02 -0400 | [diff] [blame] | 12 | #include "SkTo.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 14 | static inline int nonzero_to_one(int x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | #if 0 |
| 16 | return x != 0; |
| 17 | #else |
| 18 | return ((unsigned)(x | -x)) >> 31; |
| 19 | #endif |
| 20 | } |
| 21 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 22 | static inline int neq_to_one(int x, int max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 23 | #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.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 31 | static inline int neq_to_mask(int x, int max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | #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.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 40 | static inline unsigned div255(unsigned x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | 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.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 47 | void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 48 | 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.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 68 | for (int y = 0; y <= maxy; y++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 69 | int next_row = neq_to_mask(y, maxy) & rowBytes; |
| 70 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 71 | for (int x = 0; x <= maxx; x++) { |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 72 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 75 | SkFixed numer = lx * nx + ly * ny + lz_dot_nz; |
| 76 | int mul = ambient; |
| 77 | int add = 0; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 79 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 85 | // now for the reflection |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 87 | // R = 2 (Light * Normal) Normal - Light |
| 88 | // hilite = R * Eye(0, 0, 1) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 89 | |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 90 | 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.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 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; |
| 100 | for (int i = specular >> 4; i > 0; --i) { |
| 101 | add = div255(add * hilite); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | } |
Ben Wagner | 339b84e | 2017-11-10 16:24:50 -0500 | [diff] [blame] | 105 | multiply[x] = SkToU8(mul); |
| 106 | additive[x] = SkToU8(add); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | } |
| 108 | alpha += rowBytes; |
| 109 | multiply += rowBytes; |
| 110 | additive += rowBytes; |
| 111 | prev_row = rowBytes; |
| 112 | } |
| 113 | } |