michael@0: michael@0: /* michael@0: * Copyright 2006 The Android Open Source Project michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: michael@0: #include "SkEmbossMask.h" michael@0: #include "SkMath.h" michael@0: michael@0: static inline int nonzero_to_one(int x) { michael@0: #if 0 michael@0: return x != 0; michael@0: #else michael@0: return ((unsigned)(x | -x)) >> 31; michael@0: #endif michael@0: } michael@0: michael@0: static inline int neq_to_one(int x, int max) { michael@0: #if 0 michael@0: return x != max; michael@0: #else michael@0: SkASSERT(x >= 0 && x <= max); michael@0: return ((unsigned)(x - max)) >> 31; michael@0: #endif michael@0: } michael@0: michael@0: static inline int neq_to_mask(int x, int max) { michael@0: #if 0 michael@0: return -(x != max); michael@0: #else michael@0: SkASSERT(x >= 0 && x <= max); michael@0: return (x - max) >> 31; michael@0: #endif michael@0: } michael@0: michael@0: static inline unsigned div255(unsigned x) { michael@0: SkASSERT(x <= (255*255)); michael@0: return x * ((1 << 24) / 255) >> 24; michael@0: } michael@0: michael@0: #define kDelta 32 // small enough to show off angle differences michael@0: michael@0: #include "SkEmbossMask_Table.h" michael@0: michael@0: #if defined(SK_BUILD_FOR_WIN32) && defined(SK_DEBUG) michael@0: michael@0: #include michael@0: michael@0: void SkEmbossMask_BuildTable() { michael@0: // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table michael@0: michael@0: FILE* file = ::fopen("SkEmbossMask_Table.h", "w"); michael@0: SkASSERT(file); michael@0: ::fprintf(file, "#include \"SkTypes.h\"\n\n"); michael@0: ::fprintf(file, "static const U16 gInvSqrtTable[128 * 128] = {\n"); michael@0: for (int dx = 0; dx <= 255/2; dx++) { michael@0: for (int dy = 0; dy <= 255/2; dy++) { michael@0: if ((dy & 15) == 0) michael@0: ::fprintf(file, "\t"); michael@0: michael@0: uint16_t value = SkToU16((1 << 15) / SkSqrt32(dx * dx + dy * dy + kDelta*kDelta/4)); michael@0: michael@0: ::fprintf(file, "0x%04X", value); michael@0: if (dx * 128 + dy < 128*128-1) { michael@0: ::fprintf(file, ", "); michael@0: } michael@0: if ((dy & 15) == 15) { michael@0: ::fprintf(file, "\n"); michael@0: } michael@0: } michael@0: } michael@0: ::fprintf(file, "};\n#define kDeltaUsedToBuildTable\t%d\n", kDelta); michael@0: ::fclose(file); michael@0: } michael@0: michael@0: #endif michael@0: michael@0: void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) { michael@0: SkASSERT(kDelta == kDeltaUsedToBuildTable); michael@0: michael@0: SkASSERT(mask->fFormat == SkMask::k3D_Format); michael@0: michael@0: int specular = light.fSpecular; michael@0: int ambient = light.fAmbient; michael@0: SkFixed lx = SkScalarToFixed(light.fDirection[0]); michael@0: SkFixed ly = SkScalarToFixed(light.fDirection[1]); michael@0: SkFixed lz = SkScalarToFixed(light.fDirection[2]); michael@0: SkFixed lz_dot_nz = lz * kDelta; michael@0: int lz_dot8 = lz >> 8; michael@0: michael@0: size_t planeSize = mask->computeImageSize(); michael@0: uint8_t* alpha = mask->fImage; michael@0: uint8_t* multiply = (uint8_t*)alpha + planeSize; michael@0: uint8_t* additive = multiply + planeSize; michael@0: michael@0: int rowBytes = mask->fRowBytes; michael@0: int maxy = mask->fBounds.height() - 1; michael@0: int maxx = mask->fBounds.width() - 1; michael@0: michael@0: int prev_row = 0; michael@0: for (int y = 0; y <= maxy; y++) { michael@0: int next_row = neq_to_mask(y, maxy) & rowBytes; michael@0: michael@0: for (int x = 0; x <= maxx; x++) { michael@0: if (alpha[x]) { michael@0: int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)]; michael@0: int ny = alpha[x + next_row] - alpha[x - prev_row]; michael@0: michael@0: SkFixed numer = lx * nx + ly * ny + lz_dot_nz; michael@0: int mul = ambient; michael@0: int add = 0; michael@0: michael@0: if (numer > 0) { // preflight when numer/denom will be <= 0 michael@0: #if 0 michael@0: int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta); michael@0: SkFixed dot = numer / denom; michael@0: dot >>= 8; // now dot is 2^8 instead of 2^16 michael@0: #else michael@0: // can use full numer, but then we need to call SkFixedMul, since michael@0: // numer is 24 bits, and our table is 12 bits michael@0: michael@0: // SkFixed dot = SkFixedMul(numer, gTable[]) >> 8 michael@0: SkFixed dot = (unsigned)(numer >> 4) * gInvSqrtTable[(SkAbs32(nx) >> 1 << 7) | (SkAbs32(ny) >> 1)] >> 20; michael@0: #endif michael@0: mul = SkFastMin32(mul + dot, 255); michael@0: michael@0: // now for the reflection michael@0: michael@0: // R = 2 (Light * Normal) Normal - Light michael@0: // hilite = R * Eye(0, 0, 1) michael@0: michael@0: int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8; michael@0: if (hilite > 0) { michael@0: // pin hilite to 255, since our fast math is also a little sloppy michael@0: hilite = SkClampMax(hilite, 255); michael@0: michael@0: // specular is 4.4 michael@0: // would really like to compute the fractional part of this michael@0: // and then possibly cache a 256 table for a given specular michael@0: // value in the light, and just pass that in to this function. michael@0: add = hilite; michael@0: for (int i = specular >> 4; i > 0; --i) { michael@0: add = div255(add * hilite); michael@0: } michael@0: } michael@0: } michael@0: multiply[x] = SkToU8(mul); michael@0: additive[x] = SkToU8(add); michael@0: michael@0: // multiply[x] = 0xFF; michael@0: // additive[x] = 0; michael@0: // ((uint8_t*)alpha)[x] = alpha[x] * multiply[x] >> 8; michael@0: } michael@0: } michael@0: alpha += rowBytes; michael@0: multiply += rowBytes; michael@0: additive += rowBytes; michael@0: prev_row = rowBytes; michael@0: } michael@0: }