epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2006 The Android Open Source Project |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 9 | |
| 10 | #include "SkEmbossMask.h" |
tomhudson@google.com | 889bd8b | 2011-09-27 17:38:17 +0000 | [diff] [blame] | 11 | #include "SkMath.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 12 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 13 | static inline int nonzero_to_one(int x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 14 | #if 0 |
| 15 | return x != 0; |
| 16 | #else |
| 17 | return ((unsigned)(x | -x)) >> 31; |
| 18 | #endif |
| 19 | } |
| 20 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 21 | static inline int neq_to_one(int x, int max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 30 | static inline int neq_to_mask(int x, int max) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 31 | #if 0 |
| 32 | return -(x != max); |
| 33 | #else |
| 34 | SkASSERT(x >= 0 && x <= max); |
| 35 | return (x - max) >> 31; |
| 36 | #endif |
| 37 | } |
| 38 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 39 | static inline unsigned div255(unsigned x) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | #include "SkEmbossMask_Table.h" |
| 47 | |
| 48 | #if defined(SK_BUILD_FOR_WIN32) && defined(SK_DEBUG) |
| 49 | |
| 50 | #include <stdio.h> |
| 51 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 52 | void SkEmbossMask_BuildTable() { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 53 | // build it 0..127 x 0..127, so we use 2^15 - 1 in the numerator for our "fixed" table |
| 54 | |
| 55 | FILE* file = ::fopen("SkEmbossMask_Table.h", "w"); |
| 56 | SkASSERT(file); |
| 57 | ::fprintf(file, "#include \"SkTypes.h\"\n\n"); |
| 58 | ::fprintf(file, "static const U16 gInvSqrtTable[128 * 128] = {\n"); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 59 | for (int dx = 0; dx <= 255/2; dx++) { |
| 60 | for (int dy = 0; dy <= 255/2; dy++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 61 | if ((dy & 15) == 0) |
| 62 | ::fprintf(file, "\t"); |
| 63 | |
reed@android.com | 9c97045 | 2009-04-03 14:26:10 +0000 | [diff] [blame] | 64 | uint16_t value = SkToU16((1 << 15) / SkSqrt32(dx * dx + dy * dy + kDelta*kDelta/4)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | |
| 66 | ::fprintf(file, "0x%04X", value); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 67 | if (dx * 128 + dy < 128*128-1) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 68 | ::fprintf(file, ", "); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 69 | } |
| 70 | if ((dy & 15) == 15) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | ::fprintf(file, "\n"); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 72 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 73 | } |
| 74 | } |
| 75 | ::fprintf(file, "};\n#define kDeltaUsedToBuildTable\t%d\n", kDelta); |
| 76 | ::fclose(file); |
| 77 | } |
| 78 | |
| 79 | #endif |
| 80 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 81 | void SkEmbossMask::Emboss(SkMask* mask, const SkEmbossMaskFilter::Light& light) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | SkASSERT(kDelta == kDeltaUsedToBuildTable); |
| 83 | |
| 84 | SkASSERT(mask->fFormat == SkMask::k3D_Format); |
| 85 | |
| 86 | int specular = light.fSpecular; |
| 87 | int ambient = light.fAmbient; |
| 88 | SkFixed lx = SkScalarToFixed(light.fDirection[0]); |
| 89 | SkFixed ly = SkScalarToFixed(light.fDirection[1]); |
| 90 | SkFixed lz = SkScalarToFixed(light.fDirection[2]); |
| 91 | SkFixed lz_dot_nz = lz * kDelta; |
| 92 | int lz_dot8 = lz >> 8; |
| 93 | |
| 94 | size_t planeSize = mask->computeImageSize(); |
| 95 | uint8_t* alpha = mask->fImage; |
| 96 | uint8_t* multiply = (uint8_t*)alpha + planeSize; |
| 97 | uint8_t* additive = multiply + planeSize; |
| 98 | |
| 99 | int rowBytes = mask->fRowBytes; |
| 100 | int maxy = mask->fBounds.height() - 1; |
| 101 | int maxx = mask->fBounds.width() - 1; |
| 102 | |
| 103 | int prev_row = 0; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 104 | for (int y = 0; y <= maxy; y++) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 105 | int next_row = neq_to_mask(y, maxy) & rowBytes; |
| 106 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 107 | for (int x = 0; x <= maxx; x++) { |
| 108 | if (alpha[x]) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 109 | int nx = alpha[x + neq_to_one(x, maxx)] - alpha[x - nonzero_to_one(x)]; |
| 110 | int ny = alpha[x + next_row] - alpha[x - prev_row]; |
| 111 | |
| 112 | SkFixed numer = lx * nx + ly * ny + lz_dot_nz; |
| 113 | int mul = ambient; |
| 114 | int add = 0; |
| 115 | |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 116 | if (numer > 0) { // preflight when numer/denom will be <= 0 |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | #if 0 |
| 118 | int denom = SkSqrt32(nx * nx + ny * ny + kDelta*kDelta); |
| 119 | SkFixed dot = numer / denom; |
| 120 | dot >>= 8; // now dot is 2^8 instead of 2^16 |
| 121 | #else |
| 122 | // can use full numer, but then we need to call SkFixedMul, since |
| 123 | // numer is 24 bits, and our table is 12 bits |
| 124 | |
| 125 | // SkFixed dot = SkFixedMul(numer, gTable[]) >> 8 |
| 126 | SkFixed dot = (unsigned)(numer >> 4) * gInvSqrtTable[(SkAbs32(nx) >> 1 << 7) | (SkAbs32(ny) >> 1)] >> 20; |
| 127 | #endif |
| 128 | mul = SkFastMin32(mul + dot, 255); |
| 129 | |
| 130 | // now for the reflection |
| 131 | |
| 132 | // R = 2 (Light * Normal) Normal - Light |
| 133 | // hilite = R * Eye(0, 0, 1) |
| 134 | |
| 135 | int hilite = (2 * dot - lz_dot8) * lz_dot8 >> 8; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 136 | if (hilite > 0) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 137 | // pin hilite to 255, since our fast math is also a little sloppy |
| 138 | hilite = SkClampMax(hilite, 255); |
| 139 | |
| 140 | // specular is 4.4 |
| 141 | // would really like to compute the fractional part of this |
| 142 | // and then possibly cache a 256 table for a given specular |
| 143 | // value in the light, and just pass that in to this function. |
| 144 | add = hilite; |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 145 | for (int i = specular >> 4; i > 0; --i) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 146 | add = div255(add * hilite); |
mike@reedtribe.org | 3334c3a | 2011-04-20 11:39:28 +0000 | [diff] [blame] | 147 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | multiply[x] = SkToU8(mul); |
| 151 | additive[x] = SkToU8(add); |
| 152 | |
| 153 | // multiply[x] = 0xFF; |
| 154 | // additive[x] = 0; |
| 155 | // ((uint8_t*)alpha)[x] = alpha[x] * multiply[x] >> 8; |
| 156 | } |
| 157 | } |
| 158 | alpha += rowBytes; |
| 159 | multiply += rowBytes; |
| 160 | additive += rowBytes; |
| 161 | prev_row = rowBytes; |
| 162 | } |
| 163 | } |