Home | History | Annotate | Download | only in effects
      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             int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)];
     72             int ny = alpha[x + next_row] - alpha[x - prev_row];
     73 
     74             SkFixed numer = lx * nx + ly * ny + lz_dot_nz;
     75             int     mul = ambient;
     76             int     add = 0;
     77 
     78             if (numer > 0) {  // preflight when numer/denom will be <= 0
     79                 int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta);
     80                 SkFixed dot = numer / denom;
     81                 dot >>= 8;  // now dot is 2^8 instead of 2^16
     82                 mul = SkFastMin32(mul + dot, 255);
     83 
     84                 // now for the reflection
     85 
     86                 //  R = 2 (Light * Normal) Normal - Light
     87                 //  hilite = R * Eye(0, 0, 1)
     88 
     89                 int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8;
     90                 if (hilite > 0) {
     91                     // pin hilite to 255, since our fast math is also a little sloppy
     92                     hilite = SkClampMax(hilite, 255);
     93 
     94                     // specular is 4.4
     95                     // would really like to compute the fractional part of this
     96                     // and then possibly cache a 256 table for a given specular
     97                     // value in the light, and just pass that in to this function.
     98                     add = hilite;
     99                     for (int i = specular >> 4; i > 0; --i) {
    100                         add = div255(add * hilite);
    101                     }
    102                 }
    103             }
    104             multiply[x] = SkToU8(mul);
    105             additive[x] = SkToU8(add);
    106         }
    107         alpha += rowBytes;
    108         multiply += rowBytes;
    109         additive += rowBytes;
    110         prev_row = rowBytes;
    111     }
    112 }
    113