3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
8 #include "SkUnitMappers.h"
9 #include "SkReadBuffer.h"
10 #include "SkWriteBuffer.h"
13 SkDiscreteMapper::SkDiscreteMapper(int segments) {
18 if (segments > 0xFFFF) {
22 fScale = (1 << 30) / (segments - 1);
26 uint16_t SkDiscreteMapper::mapUnit16(uint16_t input) {
27 SkFixed x = input * fSegments >> 16;
29 x += x << 15 >> 31; // map 0x10000 to 0xFFFF
33 SkDiscreteMapper::SkDiscreteMapper(SkReadBuffer& rb)
35 fSegments = rb.readInt();
39 void SkDiscreteMapper::flatten(SkWriteBuffer& wb) const {
40 this->INHERITED::flatten(wb);
42 wb.writeInt(fSegments);
46 ///////////////////////////////////////////////////////////////////////////////
48 uint16_t SkCosineMapper::mapUnit16(uint16_t input)
50 /* we want to call cosine(input * pi/2) treating input as [0...1)
51 however, the straight multitply would overflow 32bits since input is
52 16bits and pi/2 is 17bits, so we shift down our pi const before we mul
54 SkFixed rads = (unsigned)(input * (SK_FixedPI >> 2)) >> 15;
55 SkFixed x = SkFixedCos(rads);
56 x += x << 15 >> 31; // map 0x10000 to 0xFFFF
60 SkCosineMapper::SkCosineMapper(SkReadBuffer& rb)