epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkAvoidXfermode.h" |
| 9 | #include "SkColorPriv.h" |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 10 | #include "SkReadBuffer.h" |
| 11 | #include "SkWriteBuffer.h" |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 12 | #include "SkString.h" |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 14 | SkAvoidXfermode::SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 15 | if (tolerance > 255) { |
| 16 | tolerance = 255; |
| 17 | } |
| 18 | |
| 19 | fOpColor = opColor; |
| 20 | fDistMul = (256 << 14) / (tolerance + 1); |
| 21 | fMode = mode; |
| 22 | } |
| 23 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 24 | SkAvoidXfermode::SkAvoidXfermode(SkReadBuffer& buffer) |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 25 | : INHERITED(buffer) { |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 26 | fOpColor = buffer.readColor(); |
| 27 | fDistMul = buffer.readUInt(); |
| 28 | fMode = (Mode)buffer.readUInt(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | } |
| 30 | |
commit-bot@chromium.org | 8b0e8ac | 2014-01-30 18:58:24 +0000 | [diff] [blame] | 31 | void SkAvoidXfermode::flatten(SkWriteBuffer& buffer) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 32 | this->INHERITED::flatten(buffer); |
| 33 | |
djsollen@google.com | c73dd5c | 2012-08-07 15:54:32 +0000 | [diff] [blame] | 34 | buffer.writeColor(fOpColor); |
| 35 | buffer.writeUInt(fDistMul); |
| 36 | buffer.writeUInt(fMode); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 37 | } |
| 38 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | // returns 0..31 |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 40 | static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 41 | SkASSERT(r <= SK_R16_MASK); |
| 42 | SkASSERT(g <= SK_G16_MASK); |
| 43 | SkASSERT(b <= SK_B16_MASK); |
| 44 | |
| 45 | unsigned dr = SkAbs32(SkGetPackedR16(c) - r); |
| 46 | unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS); |
| 47 | unsigned db = SkAbs32(SkGetPackedB16(c) - b); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 48 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 49 | return SkMax32(dr, SkMax32(dg, db)); |
| 50 | } |
| 51 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 52 | // returns 0..255 |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 53 | static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 54 | SkASSERT(r <= 0xFF); |
| 55 | SkASSERT(g <= 0xFF); |
| 56 | SkASSERT(b <= 0xFF); |
| 57 | |
| 58 | unsigned dr = SkAbs32(SkGetPackedR32(c) - r); |
| 59 | unsigned dg = SkAbs32(SkGetPackedG32(c) - g); |
| 60 | unsigned db = SkAbs32(SkGetPackedB32(c) - b); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 61 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 62 | return SkMax32(dr, SkMax32(dg, db)); |
| 63 | } |
| 64 | |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 65 | static int scale_dist_14(int dist, uint32_t mul, uint32_t sub) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 66 | int tmp = dist * mul - sub; |
| 67 | int result = (tmp + (1 << 13)) >> 14; |
| 68 | |
| 69 | return result; |
| 70 | } |
| 71 | |
reed@android.com | 370eeeb | 2010-03-12 19:23:27 +0000 | [diff] [blame] | 72 | static inline unsigned Accurate255To256(unsigned x) { |
| 73 | return x + (x >> 7); |
| 74 | } |
| 75 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 76 | void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count, |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 77 | const SkAlpha aa[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 78 | unsigned opR = SkColorGetR(fOpColor); |
| 79 | unsigned opG = SkColorGetG(fOpColor); |
| 80 | unsigned opB = SkColorGetB(fOpColor); |
| 81 | uint32_t mul = fDistMul; |
| 82 | uint32_t sub = (fDistMul - (1 << 14)) << 8; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 83 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 84 | int MAX, mask; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 85 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 86 | if (kTargetColor_Mode == fMode) { |
| 87 | mask = -1; |
| 88 | MAX = 255; |
| 89 | } else { |
| 90 | mask = 0; |
| 91 | MAX = 0; |
| 92 | } |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 93 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 94 | for (int i = 0; i < count; i++) { |
| 95 | int d = color_dist32(dst[i], opR, opG, opB); |
| 96 | // now reverse d if we need to |
| 97 | d = MAX + (d ^ mask) - mask; |
| 98 | SkASSERT((unsigned)d <= 255); |
reed@android.com | 370eeeb | 2010-03-12 19:23:27 +0000 | [diff] [blame] | 99 | d = Accurate255To256(d); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 100 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 101 | d = scale_dist_14(d, mul, sub); |
| 102 | SkASSERT(d <= 256); |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 103 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 104 | if (d > 0) { |
| 105 | if (NULL != aa) { |
reed@android.com | 370eeeb | 2010-03-12 19:23:27 +0000 | [diff] [blame] | 106 | d = SkAlphaMul(d, Accurate255To256(*aa++)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 107 | if (0 == d) { |
| 108 | continue; |
| 109 | } |
| 110 | } |
djsollen@google.com | 8ff1e51 | 2012-03-12 14:39:20 +0000 | [diff] [blame] | 111 | dst[i] = SkFourByteInterp256(src[i], dst[i], d); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 116 | static inline U16CPU SkBlend3216(SkPMColor src, U16CPU dst, unsigned scale) { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 117 | SkASSERT(scale <= 32); |
| 118 | scale <<= 3; |
| 119 | |
| 120 | return SkPackRGB16( SkAlphaBlend(SkPacked32ToR16(src), SkGetPackedR16(dst), scale), |
| 121 | SkAlphaBlend(SkPacked32ToG16(src), SkGetPackedG16(dst), scale), |
| 122 | SkAlphaBlend(SkPacked32ToB16(src), SkGetPackedB16(dst), scale)); |
| 123 | } |
| 124 | |
| 125 | void SkAvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count, |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 126 | const SkAlpha aa[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 127 | unsigned opR = SkColorGetR(fOpColor) >> (8 - SK_R16_BITS); |
| 128 | unsigned opG = SkColorGetG(fOpColor) >> (8 - SK_G16_BITS); |
| 129 | unsigned opB = SkColorGetB(fOpColor) >> (8 - SK_R16_BITS); |
| 130 | uint32_t mul = fDistMul; |
reed@android.com | 0db5a7f | 2009-11-09 16:01:36 +0000 | [diff] [blame] | 131 | uint32_t sub = (fDistMul - (1 << 14)) << SK_R16_BITS; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 132 | |
| 133 | int MAX, mask; |
tomhudson@google.com | 1447c6f | 2011-04-27 14:09:52 +0000 | [diff] [blame] | 134 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 135 | if (kTargetColor_Mode == fMode) { |
| 136 | mask = -1; |
| 137 | MAX = 31; |
| 138 | } else { |
| 139 | mask = 0; |
| 140 | MAX = 0; |
| 141 | } |
| 142 | |
| 143 | for (int i = 0; i < count; i++) { |
| 144 | int d = color_dist16(dst[i], opR, opG, opB); |
| 145 | // now reverse d if we need to |
| 146 | d = MAX + (d ^ mask) - mask; |
| 147 | SkASSERT((unsigned)d <= 31); |
| 148 | // convert from 0..31 to 0..32 |
| 149 | d += d >> 4; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 150 | d = scale_dist_14(d, mul, sub); |
| 151 | SkASSERT(d <= 32); |
| 152 | |
| 153 | if (d > 0) { |
| 154 | if (NULL != aa) { |
reed@android.com | 370eeeb | 2010-03-12 19:23:27 +0000 | [diff] [blame] | 155 | d = SkAlphaMul(d, Accurate255To256(*aa++)); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 156 | if (0 == d) { |
| 157 | continue; |
| 158 | } |
| 159 | } |
| 160 | dst[i] = SkBlend3216(src[i], dst[i], d); |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
reed@google.com | 30da745 | 2012-12-17 19:55:24 +0000 | [diff] [blame] | 165 | void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count, |
| 166 | const SkAlpha aa[]) const { |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 167 | // override in subclass |
| 168 | } |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 169 | |
commit-bot@chromium.org | 0f10f7b | 2014-03-13 18:02:17 +0000 | [diff] [blame] | 170 | #ifndef SK_IGNORE_TO_STRING |
robertphillips@google.com | b83b6b4 | 2013-01-22 14:32:09 +0000 | [diff] [blame] | 171 | void SkAvoidXfermode::toString(SkString* str) const { |
| 172 | str->append("SkAvoidXfermode: opColor: "); |
| 173 | str->appendHex(fOpColor); |
| 174 | str->appendf("distMul: %d ", fDistMul); |
| 175 | |
| 176 | static const char* gModeStrings[] = { "Avoid", "Target" }; |
| 177 | |
| 178 | str->appendf("mode: %s", gModeStrings[fMode]); |
| 179 | } |
| 180 | #endif |