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