michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. 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: #include "SkUnitMappers.h" michael@0: #include "SkReadBuffer.h" michael@0: #include "SkWriteBuffer.h" michael@0: michael@0: michael@0: SkDiscreteMapper::SkDiscreteMapper(int segments) { michael@0: if (segments < 2) { michael@0: fSegments = 0; michael@0: fScale = 0; michael@0: } else { michael@0: if (segments > 0xFFFF) { michael@0: segments = 0xFFFF; michael@0: } michael@0: fSegments = segments; michael@0: fScale = (1 << 30) / (segments - 1); michael@0: } michael@0: } michael@0: michael@0: uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) { michael@0: SkFixed x = input * fSegments >> 16; michael@0: x = x * fScale >> 14; michael@0: x += x << 15 >> 31; // map 0x10000 to 0xFFFF michael@0: return SkToU16(x); michael@0: } michael@0: michael@0: SkDiscreteMapper::SkDiscreteMapper(SkReadBuffer& rb) michael@0: : SkUnitMapper(rb) { michael@0: fSegments = rb.readInt(); michael@0: fScale = rb.read32(); michael@0: } michael@0: michael@0: void SkDiscreteMapper::flatten(SkWriteBuffer& wb) const { michael@0: this->INHERITED::flatten(wb); michael@0: michael@0: wb.writeInt(fSegments); michael@0: wb.write32(fScale); michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: uint16_t SkCosineMapper::mapUnit16(uint16_t input) michael@0: { michael@0: /* we want to call cosine(input * pi/2) treating input as [0...1) michael@0: however, the straight multitply would overflow 32bits since input is michael@0: 16bits and pi/2 is 17bits, so we shift down our pi const before we mul michael@0: */ michael@0: SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15; michael@0: SkFixed x = SkFixedCos(rads); michael@0: x += x << 15 >> 31; // map 0x10000 to 0xFFFF michael@0: return SkToU16(x); michael@0: } michael@0: michael@0: SkCosineMapper::SkCosineMapper(SkReadBuffer& rb) michael@0: : SkUnitMapper(rb) {}