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: #include "SkAvoidXfermode.h" michael@0: #include "SkColorPriv.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: #include "SkString.h" michael@0: michael@0: SkAvoidXfermode::SkAvoidXfermode(SkColor opColor, U8CPU tolerance, Mode mode) { michael@0: if (tolerance > 255) { michael@0: tolerance = 255; michael@0: } michael@0: michael@0: fOpColor = opColor; michael@0: fDistMul = (256 << 14) / (tolerance + 1); michael@0: fMode = mode; michael@0: } michael@0: michael@0: SkAvoidXfermode::SkAvoidXfermode(SkReadBuffer& buffer) michael@0: : INHERITED(buffer) { michael@0: fOpColor = buffer.readColor(); michael@0: fDistMul = buffer.readUInt(); michael@0: fMode = (Mode)buffer.readUInt(); michael@0: } michael@0: michael@0: void SkAvoidXfermode::flatten(SkWriteBuffer& buffer) const { michael@0: this->INHERITED::flatten(buffer); michael@0: michael@0: buffer.writeColor(fOpColor); michael@0: buffer.writeUInt(fDistMul); michael@0: buffer.writeUInt(fMode); michael@0: } michael@0: michael@0: // returns 0..31 michael@0: static unsigned color_dist16(uint16_t c, unsigned r, unsigned g, unsigned b) { michael@0: SkASSERT(r <= SK_R16_MASK); michael@0: SkASSERT(g <= SK_G16_MASK); michael@0: SkASSERT(b <= SK_B16_MASK); michael@0: michael@0: unsigned dr = SkAbs32(SkGetPackedR16(c) - r); michael@0: unsigned dg = SkAbs32(SkGetPackedG16(c) - g) >> (SK_G16_BITS - SK_R16_BITS); michael@0: unsigned db = SkAbs32(SkGetPackedB16(c) - b); michael@0: michael@0: return SkMax32(dr, SkMax32(dg, db)); michael@0: } michael@0: michael@0: // returns 0..255 michael@0: static unsigned color_dist32(SkPMColor c, U8CPU r, U8CPU g, U8CPU b) { michael@0: SkASSERT(r <= 0xFF); michael@0: SkASSERT(g <= 0xFF); michael@0: SkASSERT(b <= 0xFF); michael@0: michael@0: unsigned dr = SkAbs32(SkGetPackedR32(c) - r); michael@0: unsigned dg = SkAbs32(SkGetPackedG32(c) - g); michael@0: unsigned db = SkAbs32(SkGetPackedB32(c) - b); michael@0: michael@0: return SkMax32(dr, SkMax32(dg, db)); michael@0: } michael@0: michael@0: static int scale_dist_14(int dist, uint32_t mul, uint32_t sub) { michael@0: int tmp = dist * mul - sub; michael@0: int result = (tmp + (1 << 13)) >> 14; michael@0: michael@0: return result; michael@0: } michael@0: michael@0: static inline unsigned Accurate255To256(unsigned x) { michael@0: return x + (x >> 7); michael@0: } michael@0: michael@0: void SkAvoidXfermode::xfer32(SkPMColor dst[], const SkPMColor src[], int count, michael@0: const SkAlpha aa[]) const { michael@0: unsigned opR = SkColorGetR(fOpColor); michael@0: unsigned opG = SkColorGetG(fOpColor); michael@0: unsigned opB = SkColorGetB(fOpColor); michael@0: uint32_t mul = fDistMul; michael@0: uint32_t sub = (fDistMul - (1 << 14)) << 8; michael@0: michael@0: int MAX, mask; michael@0: michael@0: if (kTargetColor_Mode == fMode) { michael@0: mask = -1; michael@0: MAX = 255; michael@0: } else { michael@0: mask = 0; michael@0: MAX = 0; michael@0: } michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: int d = color_dist32(dst[i], opR, opG, opB); michael@0: // now reverse d if we need to michael@0: d = MAX + (d ^ mask) - mask; michael@0: SkASSERT((unsigned)d <= 255); michael@0: d = Accurate255To256(d); michael@0: michael@0: d = scale_dist_14(d, mul, sub); michael@0: SkASSERT(d <= 256); michael@0: michael@0: if (d > 0) { michael@0: if (NULL != aa) { michael@0: d = SkAlphaMul(d, Accurate255To256(*aa++)); michael@0: if (0 == d) { michael@0: continue; michael@0: } michael@0: } michael@0: dst[i] = SkFourByteInterp256(src[i], dst[i], d); michael@0: } michael@0: } michael@0: } michael@0: michael@0: static inline U16CPU SkBlend3216(SkPMColor src, U16CPU dst, unsigned scale) { michael@0: SkASSERT(scale <= 32); michael@0: scale <<= 3; michael@0: michael@0: return SkPackRGB16( SkAlphaBlend(SkPacked32ToR16(src), SkGetPackedR16(dst), scale), michael@0: SkAlphaBlend(SkPacked32ToG16(src), SkGetPackedG16(dst), scale), michael@0: SkAlphaBlend(SkPacked32ToB16(src), SkGetPackedB16(dst), scale)); michael@0: } michael@0: michael@0: void SkAvoidXfermode::xfer16(uint16_t dst[], const SkPMColor src[], int count, michael@0: const SkAlpha aa[]) const { michael@0: unsigned opR = SkColorGetR(fOpColor) >> (8 - SK_R16_BITS); michael@0: unsigned opG = SkColorGetG(fOpColor) >> (8 - SK_G16_BITS); michael@0: unsigned opB = SkColorGetB(fOpColor) >> (8 - SK_R16_BITS); michael@0: uint32_t mul = fDistMul; michael@0: uint32_t sub = (fDistMul - (1 << 14)) << SK_R16_BITS; michael@0: michael@0: int MAX, mask; michael@0: michael@0: if (kTargetColor_Mode == fMode) { michael@0: mask = -1; michael@0: MAX = 31; michael@0: } else { michael@0: mask = 0; michael@0: MAX = 0; michael@0: } michael@0: michael@0: for (int i = 0; i < count; i++) { michael@0: int d = color_dist16(dst[i], opR, opG, opB); michael@0: // now reverse d if we need to michael@0: d = MAX + (d ^ mask) - mask; michael@0: SkASSERT((unsigned)d <= 31); michael@0: // convert from 0..31 to 0..32 michael@0: d += d >> 4; michael@0: d = scale_dist_14(d, mul, sub); michael@0: SkASSERT(d <= 32); michael@0: michael@0: if (d > 0) { michael@0: if (NULL != aa) { michael@0: d = SkAlphaMul(d, Accurate255To256(*aa++)); michael@0: if (0 == d) { michael@0: continue; michael@0: } michael@0: } michael@0: dst[i] = SkBlend3216(src[i], dst[i], d); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void SkAvoidXfermode::xferA8(SkAlpha dst[], const SkPMColor src[], int count, michael@0: const SkAlpha aa[]) const { michael@0: // override in subclass michael@0: } michael@0: michael@0: #ifndef SK_IGNORE_TO_STRING michael@0: void SkAvoidXfermode::toString(SkString* str) const { michael@0: str->append("SkAvoidXfermode: opColor: "); michael@0: str->appendHex(fOpColor); michael@0: str->appendf("distMul: %d ", fDistMul); michael@0: michael@0: static const char* gModeStrings[] = { "Avoid", "Target" }; michael@0: michael@0: str->appendf("mode: %s", gModeStrings[fMode]); michael@0: } michael@0: #endif