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 9 #include "SkEmbossMask.h" 10 #include "SkFixed.h" 11 #include "SkMath.h" 12 13 static inline int nonzero_to_one(int x) { 14 #if 0 15 return x != 0; 16 #else 17 return ((unsigned)(x | -x)) >> 31; 18 #endif 19 } 20 21 static inline int neq_to_one(int x, int max) { 22 #if 0 23 return x != max; 24 #else 25 SkASSERT(x >= 0 && x <= max); 26 return ((unsigned)(x - max)) >> 31; 27 #endif 28 } 29 30 static inline int neq_to_mask(int x, int max) { 31 #if 0 32 return -(x != max); 33 #else 34 SkASSERT(x >= 0 && x <= max); 35 return (x - max) >> 31; 36 #endif 37 } 38 39 static inline unsigned div255(unsigned x) { 40 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 46 void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) { 47 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; 67 for (int y = 0; y <= maxy; y++) { 68 int next_row = neq_to_mask(y, maxy) & rowBytes; 69 70 for (int x = 0; x <= maxx; x++) { 71 if (alpha[x]) { 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]; 74 75 SkFixed numer = lx * nx + ly * ny + lz_dot_nz; 76 int mul = ambient; 77 int add = 0; 78 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); 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; 91 if (hilite > 0) { 92 // 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; 100 for (int i = specular >> 4; i > 0; --i) { 101 add = div255(add * hilite); 102 } 103 } 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 } 119