| 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 |
|
| 8 |
#include "src/effects/SkEmbossMask.h" |
| 9 |
|
| 10 |
#include "include/core/SkRect.h" |
| 11 |
#include "include/core/SkScalar.h" |
| 12 |
#include "include/core/SkTypes.h" |
| 13 |
#include "include/private/base/SkFixed.h" |
| 14 |
#include "include/private/base/SkTo.h" |
| 15 |
#include "src/base/SkMathPriv.h" |
| 16 |
#include "src/core/SkMask.h" |
| 17 |
|
| 18 |
#include <algorithm> |
| 19 |
#include <cstddef> |
| 20 |
#include <cstdint> |
| 21 |
|
| 22 |
static inline int nonzero_to_one(int x) { |
| 23 |
#if 0 |
| 24 |
return x != 0; |
| 25 |
#else |
| 26 |
return ((unsigned)(x | -x)) >> 31; |
| 27 |
#endif |
| 28 |
} |
| 29 |
|
| 30 |
static inline int neq_to_one(int x, int max) { |
| 31 |
#if 0 |
| 32 |
return x != max; |
| 33 |
#else |
| 34 |
SkASSERT(x >= 0 && x <= max); |
| 35 |
return ((unsigned)(x - max)) >> 31; |
| 36 |
#endif |
| 37 |
} |
| 38 |
|
| 39 |
static inline int neq_to_mask(int x, int max) { |
| 40 |
#if 0 |
| 41 |
return -(x != max); |
| 42 |
#else |
| 43 |
SkASSERT(x >= 0 && x <= max); |
| 44 |
return (x - max) >> 31; |
| 45 |
#endif |
| 46 |
} |
| 47 |
|
| 48 |
static inline unsigned div255(unsigned x) { |
| 49 |
SkASSERT(x <= (255*255)); |
| 50 |
return x * ((1 << 24) / 255) >> 24; |
| 51 |
} |
| 52 |
|
| 53 |
#define kDelta 32 // small enough to show off angle differences |
| 54 |
|
| 55 |
void SkEmbossMask::Emboss(SkMaskBuilder* mask, const SkEmbossMaskFilter::Light& light) { |
| 56 |
SkASSERT(mask->fFormat == SkMask::k3D_Format); |
| 57 |
|
| 58 |
int specular = light.fSpecular; |
| 59 |
int ambient = light.fAmbient; |
| 60 |
SkFixed lx = SkScalarToFixed(light.fDirection[0]); |
| 61 |
SkFixed ly = SkScalarToFixed(light.fDirection[1]); |
| 62 |
SkFixed lz = SkScalarToFixed(light.fDirection[2]); |
| 63 |
SkFixed lz_dot_nz = lz * kDelta; |
| 64 |
int lz_dot8 = lz >> 8; |
| 65 |
|
| 66 |
size_t planeSize = mask->computeImageSize(); |
| 67 |
uint8_t* alpha = mask->image(); |
| 68 |
uint8_t* multiply = (uint8_t*)alpha + planeSize; |
| 69 |
uint8_t* additive = multiply + planeSize; |
| 70 |
|
| 71 |
int rowBytes = mask->fRowBytes; |
| 72 |
int maxy = mask->fBounds.height() - 1; |
| 73 |
int maxx = mask->fBounds.width() - 1; |
| 74 |
|
| 75 |
int prev_row = 0; |
| 76 |
for (int y = 0; y <= maxy; y++) { |
| 77 |
int next_row = neq_to_mask(y, maxy) & rowBytes; |
| 78 |
|
| 79 |
for (int x = 0; x <= maxx; x++) { |
| 80 |
int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)]; |
| 81 |
int ny = alpha[x + next_row] - alpha[x - prev_row]; |
| 82 |
|
| 83 |
SkFixed numer = lx * nx + ly * ny + lz_dot_nz; |
| 84 |
int mul = ambient; |
| 85 |
int add = 0; |
| 86 |
|
| 87 |
if (numer > 0) { // preflight when numer/denom will be <= 0 |
| 88 |
int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta); |
| 89 |
SkFixed dot = numer / denom; |
| 90 |
dot >>= 8; // now dot is 2^8 instead of 2^16 |
| 91 |
mul = std::min(mul + dot, 255); |
| 92 |
|
| 93 |
// now for the reflection |
| 94 |
|
| 95 |
// R = 2 (Light * Normal) Normal - Light |
| 96 |
// hilite = R * Eye(0, 0, 1) |
| 97 |
|
| 98 |
int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8; |
| 99 |
if (hilite > 0) { |
| 100 |
// pin hilite to 255, since our fast math is also a little sloppy |
| 101 |
hilite = std::min(hilite, 255); |
| 102 |
|
| 103 |
// specular is 4.4 |
| 104 |
// would really like to compute the fractional part of this |
| 105 |
// and then possibly cache a 256 table for a given specular |
| 106 |
// value in the light, and just pass that in to this function. |
| 107 |
add = hilite; |
| 108 |
for (int i = specular >> 4; i > 0; --i) { |
| 109 |
add = div255(add * hilite); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
multiply[x] = SkToU8(mul); |
| 114 |
additive[x] = SkToU8(add); |
| 115 |
} |
| 116 |
alpha += rowBytes; |
| 117 |
multiply += rowBytes; |
| 118 |
additive += rowBytes; |
| 119 |
prev_row = rowBytes; |
| 120 |
} |
| 121 |
} |