1 2/* 3 * Copyright 2011 Google Inc. 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#include "SkUnitMappers.h" 9#include "SkFlattenableBuffers.h" 10 11SkDiscreteMapper::SkDiscreteMapper(int segments) { 12 if (segments < 2) { 13 fSegments = 0; 14 fScale = 0; 15 } else { 16 if (segments > 0xFFFF) { 17 segments = 0xFFFF; 18 } 19 fSegments = segments; 20 fScale = SK_Fract1 / (segments - 1); 21 } 22} 23 24uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) { 25 SkFixed x = input * fSegments >> 16; 26 x = x * fScale >> 14; 27 x += x << 15 >> 31; // map 0x10000 to 0xFFFF 28 return SkToU16(x); 29} 30 31SkDiscreteMapper::SkDiscreteMapper(SkFlattenableReadBuffer& rb) 32 : SkUnitMapper(rb) { 33 fSegments = rb.readInt(); 34 fScale = rb.read32(); 35} 36 37void SkDiscreteMapper::flatten(SkFlattenableWriteBuffer& wb) const { 38 this->INHERITED::flatten(wb); 39 40 wb.writeInt(fSegments); 41 wb.write32(fScale); 42} 43 44/////////////////////////////////////////////////////////////////////////////// 45 46uint16_t SkCosineMapper::mapUnit16(uint16_t input) 47{ 48 /* we want to call cosine(input * pi/2) treating input as [0...1) 49 however, the straight multitply would overflow 32bits since input is 50 16bits and pi/2 is 17bits, so we shift down our pi const before we mul 51 */ 52 SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15; 53 SkFixed x = SkFixedCos(rads); 54 x += x << 15 >> 31; // map 0x10000 to 0xFFFF 55 return SkToU16(x); 56} 57 58SkCosineMapper::SkCosineMapper(SkFlattenableReadBuffer& rb) 59 : SkUnitMapper(rb) {} 60